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.handler;
21  
22  import org.apache.ws.security.WSPasswordCallback;
23  import org.apache.ws.security.WSConstants;
24  import org.apache.ws.security.WSSConfig;
25  import org.apache.ws.security.common.CustomHandler;
26  import org.apache.ws.security.common.SOAPUtil;
27  import org.apache.ws.security.common.UsernamePasswordCallbackHandler;
28  import org.w3c.dom.Document;
29  
30  import javax.security.auth.callback.CallbackHandler;
31  
32  /**
33   * WS-Security Test Case for the getPassword method in WSHandler.
34   * <p/>
35   */
36  public class WSHandlerGetPasswordTest extends org.junit.Assert {
37      private static final org.apache.commons.logging.Log LOG = 
38          org.apache.commons.logging.LogFactory.getLog(WSHandlerGetPasswordTest.class);
39      private CallbackHandler callbackHandler = new UsernamePasswordCallbackHandler();
40  
41      /**
42       * A unit test for WSHandler.getPassword(...), where the password is obtained 
43       * from the Message Context.
44       */
45      @org.junit.Test
46      public void
47      testGetPasswordRequestContextUnit() throws Exception {
48          
49          final WSSConfig cfg = WSSConfig.getNewInstance();
50          final RequestData reqData = new RequestData();
51          reqData.setWssConfig(cfg);
52          java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
53          messageContext.put("password", "securityPassword");
54          reqData.setMsgContext(messageContext);
55          
56          WSHandler handler = new CustomHandler();
57          CallbackHandler callbackHandler = 
58              handler.getCallbackHandler("SomeCallbackTag", "SomeCallbackRef", reqData);
59          WSPasswordCallback callback = 
60              handler.getPasswordCB("alice", WSConstants.UT, callbackHandler, reqData);
61          assertTrue("alice".equals(callback.getIdentifier()));
62          assertTrue("securityPassword".equals(callback.getPassword()));
63          assertTrue(WSPasswordCallback.USERNAME_TOKEN == callback.getUsage());
64      }
65      
66      /**
67       * A WSHandler test for WSHandler.getPassword(...), where the password is obtained 
68       * from the Message Context.
69       */
70      @org.junit.Test
71      public void
72      testGetPasswordRequestContext() throws Exception {
73          
74          final WSSConfig cfg = WSSConfig.getNewInstance();
75          final RequestData reqData = new RequestData();
76          reqData.setWssConfig(cfg);
77          reqData.setUsername("alice");
78          reqData.setPwType(WSConstants.PASSWORD_TEXT);
79          java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
80          messageContext.put("password", "securityPassword");
81          reqData.setMsgContext(messageContext);
82          
83          final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
84          actions.add(Integer.valueOf(WSConstants.UT));
85          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
86          CustomHandler handler = new CustomHandler();
87          handler.send(
88              WSConstants.UT, 
89              doc, 
90              reqData, 
91              actions,
92              true
93          );
94          
95          String outputString = 
96              org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
97          if (LOG.isDebugEnabled()) {
98              LOG.debug(outputString);
99          }
100         assertTrue(outputString.indexOf("alice") != -1);
101         assertTrue(outputString.indexOf("securityPassword") != -1);
102     }
103     
104     /**
105      * A test for WSHandler.getPassword(...), where the password is obtained from a 
106      * Callback Handler, which is placed on the Message Context using a reference.
107      */
108     @org.junit.Test
109     public void
110     testGetPasswordCallbackHandlerRef() throws Exception {
111         
112         final WSSConfig cfg = WSSConfig.getNewInstance();
113         final RequestData reqData = new RequestData();
114         reqData.setWssConfig(cfg);
115         reqData.setUsername("alice");
116         reqData.setPwType(WSConstants.PASSWORD_TEXT);
117         java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
118         messageContext.put(
119             WSHandlerConstants.PW_CALLBACK_REF, 
120             callbackHandler
121         );
122         reqData.setMsgContext(messageContext);
123         
124         final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
125         actions.add(Integer.valueOf(WSConstants.UT));
126         Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
127         CustomHandler handler = new CustomHandler();
128         handler.send(
129             WSConstants.UT, 
130             doc, 
131             reqData, 
132             actions,
133             true
134         );
135         
136         String outputString = 
137             org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
138         if (LOG.isDebugEnabled()) {
139             LOG.debug(outputString);
140         }
141         assertTrue(outputString.indexOf("alice") != -1);
142         assertTrue(outputString.indexOf("securityPassword") != -1);
143     }
144     
145 }