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  
20  package org.apache.wss4j.dom.validate;
21  
22  import javax.security.auth.Subject;
23  import javax.security.auth.callback.CallbackHandler;
24  import javax.security.auth.login.LoginContext;
25  import javax.security.auth.login.LoginException;
26  
27  import org.apache.wss4j.dom.WSConstants;
28  import org.apache.wss4j.common.NamePasswordCallbackHandler;
29  import org.apache.wss4j.common.ext.WSSecurityException;
30  import org.apache.wss4j.dom.handler.RequestData;
31  import org.apache.wss4j.dom.message.token.UsernameToken;
32  
33  
34  /**
35   * This class validates a processed UsernameToken, extracted from the Credential passed to
36   * the validate method.
37   * Username/password validation is delegated to JAAS LoginContext.
38   */
39  public class JAASUsernameTokenValidator implements Validator {
40  
41      private static final org.slf4j.Logger LOG =
42          org.slf4j.LoggerFactory.getLogger(JAASUsernameTokenValidator.class);
43  
44      private String contextName;
45  
46      public void setContextName(String name) {
47          contextName = name;
48      }
49  
50      public String getContextName() {
51          return contextName;
52      }
53  
54      /**
55       * Validate the credential argument. It must contain a non-null UsernameToken. A
56       * CallbackHandler implementation is also required to be set.
57       * Validator
58       * If the password type is either digest or plaintext, it extracts a password from the
59       * CallbackHandler and then compares the passwords appropriately.
60       *
61       * If the password is null it queries a hook to allow the user to validate UsernameTokens
62       * of this type.
63       *
64       * @param credential the Credential to be validated
65       * @param data the RequestData associated with the request
66       * @throws WSSecurityException on a failed validation
67       */
68      public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
69          if (credential == null || credential.getUsernametoken() == null) {
70              throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "noCredential");
71          }
72  
73          String user = null;
74          String password = null;
75  
76          UsernameToken usernameToken = credential.getUsernametoken();
77  
78          user = usernameToken.getName();
79          String pwType = usernameToken.getPasswordType();
80          LOG.debug("UsernameToken user {}", usernameToken.getName());
81          LOG.debug("UsernameToken password type {}", pwType);
82  
83          if (usernameToken.isHashed()) {
84              LOG.warn("Authentication failed as hashed username token not supported");
85              throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
86          }
87  
88          password = usernameToken.getPassword();
89  
90          if (!WSConstants.PASSWORD_TEXT.equals(pwType)) {
91              LOG.warn("Password type " + pwType + " not supported");
92              throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
93          }
94  
95          if (!(user != null && user.length() > 0 && password != null && password.length() > 0)) {
96              LOG.warn("User or password empty");
97              throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
98          }
99  
100         try {
101             CallbackHandler handler = getCallbackHandler(user, password);
102             LoginContext ctx = new LoginContext(getContextName(), handler);
103             ctx.login();
104             Subject subject = ctx.getSubject();
105             credential.setSubject(subject);
106 
107         } catch (LoginException ex) {
108             LOG.info("Authentication failed", ex);
109             throw new WSSecurityException(
110                 WSSecurityException.ErrorCode.FAILED_AUTHENTICATION, ex
111             );
112         }
113 
114         return credential;
115 
116     }
117 
118     protected CallbackHandler getCallbackHandler(String name, String password) {
119         return new NamePasswordCallbackHandler(name, password);
120     }
121 
122 }