1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.message.token;
21
22 import org.apache.ws.security.WSSecurityException;
23 import org.apache.ws.security.WSSecurityEngine;
24 import org.apache.ws.security.WSSConfig;
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
33
34
35
36
37
38 public class WCFUsernameTokenTest extends org.junit.Assert {
39 private static final org.apache.commons.logging.Log LOG =
40 org.apache.commons.logging.LogFactory.getLog(WCFUsernameTokenTest.class);
41 private static final String SOAPUTMSG =
42 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
43 + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
44 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
45 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
46 + "<SOAP-ENV:Header>"
47 + "<wsse:Security SOAP-ENV:mustUnderstand=\"1\" "
48 + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
49 + "<wsse:UsernameToken wsu:Id=\"UsernameToken-29477163\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
50 + "<wsse:Username>wernerd</wsse:Username>"
51 + "<wsse:Password "
52 + "wsse:Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">verySecret</wsse:Password>"
53 + "</wsse:UsernameToken></wsse:Security></SOAP-ENV:Header>"
54 + "<SOAP-ENV:Body>"
55 + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">"
56 + "<value xmlns=\"\">15</value>" + "</add>"
57 + "</SOAP-ENV:Body>\r\n \r\n" + "</SOAP-ENV:Envelope>";
58
59 private WSSecurityEngine secEngine = new WSSecurityEngine();
60 private CallbackHandler callbackHandler = new UsernamePasswordCallbackHandler();
61
62 public WCFUsernameTokenTest() {
63 WSSConfig config = WSSConfig.getNewInstance();
64 config.setWsiBSPCompliant(false);
65 secEngine.setWssConfig(config);
66 }
67
68
69
70
71
72 @org.junit.Test
73 public void testNamespaceQualifiedTypeRejected() throws Exception {
74 Document doc = SOAPUtil.toSOAPPart(SOAPUTMSG);
75
76 if (LOG.isDebugEnabled()) {
77 LOG.debug("Message with UserNameToken PW Digest:");
78 String outputString =
79 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
80 LOG.debug(outputString);
81 }
82 try {
83 verify(doc);
84 fail("Failure expected on a bad password type");
85 } catch (WSSecurityException ex) {
86
87 }
88 }
89
90
91
92
93
94
95 @org.junit.Test
96 public void testNamespaceQualifiedTypeAccepted() throws Exception {
97 Document doc = SOAPUtil.toSOAPPart(SOAPUTMSG);
98
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("Message with UserNameToken PW Digest:");
101 String outputString =
102 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
103 LOG.debug(outputString);
104 }
105 WSSConfig wssConfig = secEngine.getWssConfig();
106 wssConfig.setAllowNamespaceQualifiedPasswordTypes(true);
107 secEngine.setWssConfig(wssConfig);
108 verify(doc);
109 }
110
111
112
113
114
115
116
117
118 private void verify(Document doc) throws Exception {
119 LOG.info("Before verifying UsernameToken....");
120 secEngine.processSecurityHeader(doc, null, callbackHandler, null);
121 LOG.info("After verifying UsernameToken....");
122 }
123
124 }