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.ws.security.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.ws.security.NamePasswordCallbackHandler;
28  import org.apache.ws.security.WSConstants;
29  import org.apache.ws.security.WSSecurityException;
30  import org.apache.ws.security.handler.RequestData;
31  import org.apache.ws.security.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 org.apache.commons.logging.Log log = 
42          org.apache.commons.logging.LogFactory.getLog(JAASUsernameTokenValidator.class);
43      
44      private String contextName = null;
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.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          if (log.isDebugEnabled()) {
81              log.debug("UsernameToken user " + usernameToken.getName());
82              log.debug("UsernameToken password type " + pwType);
83          }
84          
85          if (usernameToken.isHashed()) {
86              log.warn("Authentication failed as hashed username token not supported");
87              throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION);
88          }
89          
90          password = usernameToken.getPassword();
91          
92          if (!WSConstants.PASSWORD_TEXT.equals(pwType)) {
93              log.warn("Password type " + pwType + " not supported");
94              throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION);        	
95          }
96          
97          if (!(user != null && user.length() > 0 && password != null && password.length() > 0)) {
98              log.warn("User or password empty");
99              throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION);
100         }
101         
102         try {
103             CallbackHandler handler = getCallbackHandler(user, password);  
104             LoginContext ctx = new LoginContext(getContextName(), handler);  
105             ctx.login();
106             Subject subject = ctx.getSubject();
107             credential.setSubject(subject);
108 
109         } catch (LoginException ex) {
110             log.info("Authentication failed", ex);
111             throw new WSSecurityException(
112                 WSSecurityException.FAILED_AUTHENTICATION, null, null, ex
113             );
114         }
115         
116         return credential;
117         
118     }
119 
120     protected CallbackHandler getCallbackHandler(String name, String password) {
121         return new NamePasswordCallbackHandler(name, password);
122     }
123    
124 }