1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.wss4j.stax.impl.securityToken;
20
21 import java.security.Principal;
22 import java.security.cert.X509Certificate;
23
24 import javax.security.auth.Subject;
25
26 import org.apache.wss4j.common.ext.WSSecurityException;
27 import org.apache.wss4j.stax.securityToken.HttpsSecurityToken;
28 import org.apache.wss4j.stax.securityToken.WSSecurityTokenConstants;
29 import org.apache.xml.security.exceptions.XMLSecurityException;
30 import org.apache.xml.security.stax.impl.securityToken.AbstractInboundSecurityToken;
31 import org.apache.xml.security.stax.impl.util.IDGenerator;
32
33 public class HttpsSecurityTokenImpl extends AbstractInboundSecurityToken implements HttpsSecurityToken {
34
35 private String username;
36 private final AuthenticationType authenticationType;
37 private Principal principal;
38
39 private enum AuthenticationType {
40 httpsClientAuthentication,
41 httpBasicAuthentication,
42 httpDigestAuthentication,
43 noAuthentication
44 }
45
46 public HttpsSecurityTokenImpl() {
47 super(null, IDGenerator.generateID(null), WSSecurityTokenConstants.KeyIdentifier_NoKeyInfo, true);
48 this.authenticationType = AuthenticationType.noAuthentication;
49 }
50
51 public HttpsSecurityTokenImpl(X509Certificate x509Certificate) {
52 super(null, IDGenerator.generateID(null), WSSecurityTokenConstants.KeyIdentifier_NoKeyInfo, true);
53 setX509Certificates(new X509Certificate[]{x509Certificate});
54 this.authenticationType = AuthenticationType.httpsClientAuthentication;
55 }
56
57 public HttpsSecurityTokenImpl(boolean basicAuthentication, String username) {
58 super(null, IDGenerator.generateID(null), WSSecurityTokenConstants.KeyIdentifier_NoKeyInfo, true);
59 if (basicAuthentication) {
60 this.authenticationType = AuthenticationType.httpBasicAuthentication;
61 } else {
62 this.authenticationType = AuthenticationType.httpDigestAuthentication;
63 }
64 this.username = username;
65 }
66
67 @Override
68 public WSSecurityTokenConstants.TokenType getTokenType() {
69 return WSSecurityTokenConstants.HTTPS_TOKEN;
70 }
71
72
73 public String getUsername() {
74 return username;
75 }
76
77 public AuthenticationType getAuthenticationType() {
78 return authenticationType;
79 }
80
81 @Override
82 public Subject getSubject() throws WSSecurityException {
83 return null;
84 }
85
86 @Override
87 public Principal getPrincipal() throws WSSecurityException {
88 if (this.principal == null) {
89 try {
90 X509Certificate[] certs = getX509Certificates();
91 if (certs != null && certs.length > 0) {
92 return this.principal = certs[0].getSubjectX500Principal();
93 }
94
95 } catch (XMLSecurityException e) {
96 throw new WSSecurityException(WSSecurityException.ErrorCode.INVALID_SECURITY_TOKEN, e);
97 }
98 }
99 return this.principal;
100 }
101 }