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