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.WSConstants;
23  import org.apache.ws.security.WSSConfig;
24  import org.apache.ws.security.common.CustomHandler;
25  import org.apache.ws.security.common.SOAPUtil;
26  import org.apache.ws.security.common.UsernamePasswordCallbackHandler;
27  import org.w3c.dom.Document;
28  
29  import javax.security.auth.callback.CallbackHandler;
30  
31  /**
32   * Test case for WSS-245 - "WSHandlerConstants.PW_CALLBACK_REF isn't correctly searched for"
33   *
34   * https://issues.apache.org/jira/browse/WSS-245
35   */
36  public class CallbackRefTest extends org.junit.Assert {
37      private static final org.apache.commons.logging.Log LOG = 
38          org.apache.commons.logging.LogFactory.getLog(CallbackRefTest.class);
39      private CallbackHandler callbackHandler = new UsernamePasswordCallbackHandler();
40      
41      /**
42       * A test for WSHandler.getPassword(...), where the password is obtained from a 
43       * Callback Handler, which is placed on the Message Context using a reference.
44       */
45      @org.junit.Test
46      public void
47      testMessageContextRef() throws Exception {
48          
49          final WSSConfig cfg = WSSConfig.getNewInstance();
50          final RequestData reqData = new RequestData();
51          reqData.setWssConfig(cfg);
52          reqData.setUsername("alice");
53          reqData.setPwType(WSConstants.PASSWORD_TEXT);
54          java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
55          messageContext.put(
56              WSHandlerConstants.PW_CALLBACK_REF, 
57              callbackHandler
58          );
59          reqData.setMsgContext(messageContext);
60          
61          final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
62          actions.add(Integer.valueOf(WSConstants.UT));
63          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
64          CustomHandler handler = new CustomHandler();
65          handler.send(
66              WSConstants.UT, 
67              doc, 
68              reqData, 
69              actions,
70              true
71          );
72          
73          String outputString = 
74              org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
75          if (LOG.isDebugEnabled()) {
76              LOG.debug(outputString);
77          }
78          assertTrue(outputString.indexOf("alice") != -1);
79          assertTrue(outputString.indexOf("securityPassword") != -1);
80      }
81      
82      /**
83       * A test for WSHandler.getPassword(...) where the password is obtained from a 
84       * Callback Handler, which is obtained from the handler options using a ref.
85       */
86      @org.junit.Test
87      public void
88      testHandlerOptionRef() throws Exception {
89          
90          final WSSConfig cfg = WSSConfig.getNewInstance();
91          final RequestData reqData = new RequestData();
92          reqData.setWssConfig(cfg);
93          reqData.setUsername("alice");
94          reqData.setPwType(WSConstants.PASSWORD_TEXT);
95          reqData.setMsgContext(new java.util.TreeMap<String, String>());
96          
97          final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
98          actions.add(Integer.valueOf(WSConstants.UT));
99          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
100         CustomHandler handler = new CustomHandler();
101         handler.setOption(WSHandlerConstants.PW_CALLBACK_REF, callbackHandler);
102         handler.send(
103             WSConstants.UT, 
104             doc, 
105             reqData, 
106             actions,
107             true
108         );
109         
110         String outputString = 
111             org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
112         if (LOG.isDebugEnabled()) {
113             LOG.debug(outputString);
114         }
115         assertTrue(outputString.indexOf("alice") != -1);
116         assertTrue(outputString.indexOf("securityPassword") != -1);
117     }
118     
119 }