View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      //todo username from principal?
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 }