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.action;
21  
22  import javax.security.auth.callback.CallbackHandler;
23  
24  import org.apache.ws.security.WSPasswordCallback;
25  import org.apache.ws.security.WSSecurityException;
26  import org.apache.ws.security.handler.RequestData;
27  import org.apache.ws.security.handler.WSHandler;
28  import org.apache.ws.security.message.WSSecUsernameToken;
29  import org.w3c.dom.Document;
30  
31  public class UsernameTokenAction implements Action {
32      
33      public void execute(WSHandler handler, int actionToDo, Document doc, RequestData reqData)
34          throws WSSecurityException {
35          String username = reqData.getUsername();
36          String password = null;
37          if (reqData.getPwType() != null) {
38              CallbackHandler callbackHandler = 
39                  handler.getPasswordCallbackHandler(reqData);
40              WSPasswordCallback passwordCallback = 
41                  handler.getPasswordCB(reqData.getUsername(), actionToDo, callbackHandler, reqData);
42              username = passwordCallback.getIdentifier();
43              password = passwordCallback.getPassword();
44          }
45  
46          WSSecUsernameToken builder = new WSSecUsernameToken(reqData.getWssConfig());
47          builder.setPasswordType(reqData.getPwType());
48          builder.setPasswordsAreEncoded(reqData.getWssConfig().getPasswordsAreEncoded());
49          builder.setUserInfo(username, password);
50  
51          if (reqData.getUtElements() != null && reqData.getUtElements().length > 0) {
52              for (int j = 0; j < reqData.getUtElements().length; j++) {
53                  String utElement = reqData.getUtElements()[j].trim();
54                  if (utElement.equals("Nonce")) {
55                      builder.addNonce();
56                  }
57                  if (utElement.equals("Created")) {
58                      builder.addCreated();
59                  }
60                  reqData.getUtElements()[j] = null;
61              }
62          }
63          builder.build(doc, reqData.getSecHeader());        
64      }
65  }