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.handler;
21
22 import java.io.IOException;
23 import java.util.List;
24
25 import javax.security.auth.callback.Callback;
26 import javax.security.auth.callback.CallbackHandler;
27 import javax.security.auth.callback.UnsupportedCallbackException;
28
29 import org.apache.ws.security.WSConstants;
30 import org.apache.ws.security.WSPasswordCallback;
31 import org.apache.ws.security.WSSConfig;
32 import org.apache.ws.security.WSSecurityEngine;
33 import org.apache.ws.security.WSSecurityEngineResult;
34 import org.apache.ws.security.common.CustomHandler;
35 import org.apache.ws.security.common.SOAPUtil;
36 import org.apache.ws.security.components.crypto.CryptoFactory;
37 import org.w3c.dom.Document;
38
39
40
41
42
43
44 public class SignatureUTAliasTest extends org.junit.Assert implements CallbackHandler {
45 private static final org.apache.commons.logging.Log LOG =
46 org.apache.commons.logging.LogFactory.getLog(SignatureUTAliasTest.class);
47 private WSSecurityEngine secEngine = new WSSecurityEngine();
48
49
50
51
52
53
54 @org.junit.Test
55 public void
56 testUsernameTokenSignatureHandler() throws Exception {
57 final WSSConfig cfg = WSSConfig.getNewInstance();
58 final RequestData reqData = new RequestData();
59 reqData.setWssConfig(cfg);
60 reqData.setUsername("alice");
61 reqData.setPwType(WSConstants.PASSWORD_TEXT);
62 java.util.Map<String, Object> messageContext = new java.util.TreeMap<String, Object>();
63 messageContext.put(
64 WSHandlerConstants.PW_CALLBACK_REF,
65 this
66 );
67 messageContext.put(WSHandlerConstants.SIGNATURE_USER, "wss40");
68 messageContext.put(WSHandlerConstants.SIG_PROP_FILE, "wss40.properties");
69 messageContext.put(
70 WSHandlerConstants.SIGNATURE_PARTS,
71 "{}{" + WSConstants.WSSE_NS + "}" + "UsernameToken"
72 );
73 messageContext.put(WSHandlerConstants.SIG_KEY_ID, "DirectReference");
74 reqData.setMsgContext(messageContext);
75
76 final java.util.List<Integer> actions = new java.util.ArrayList<Integer>();
77 actions.add(Integer.valueOf(WSConstants.UT));
78 actions.add(Integer.valueOf(WSConstants.SIGN));
79 final Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
80 CustomHandler handler = new CustomHandler();
81 handler.send(
82 WSConstants.UT | WSConstants.SIGN,
83 doc,
84 reqData,
85 actions,
86 true
87 );
88
89 if (LOG.isDebugEnabled()) {
90 LOG.debug("After Signing....");
91 String outputString =
92 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
93 LOG.debug(outputString);
94 }
95
96 verify(doc);
97
98 }
99
100
101
102
103
104
105
106
107
108 private List<WSSecurityEngineResult> verify(Document doc) throws Exception {
109 List<WSSecurityEngineResult> results =
110 secEngine.processSecurityHeader(
111 doc, null, this, CryptoFactory.getInstance("wss40CA.properties")
112 );
113 if (LOG.isDebugEnabled()) {
114 LOG.debug("Verfied and decrypted message:");
115 String outputString =
116 org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
117 LOG.debug(outputString);
118 }
119 return results;
120 }
121
122
123 public void handle(Callback[] callbacks)
124 throws IOException, UnsupportedCallbackException {
125 for (int i = 0; i < callbacks.length; i++) {
126 if (callbacks[i] instanceof WSPasswordCallback) {
127 WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
128 if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN
129 && "alice".equals(pc.getIdentifier())) {
130 pc.setPassword("verySecret");
131 } else if (pc.getUsage() == WSPasswordCallback.SIGNATURE
132 && "wss40".equals(pc.getIdentifier())) {
133 pc.setPassword("security");
134 } else {
135 throw new IOException("Authentication failed");
136 }
137 } else {
138 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
139 }
140 }
141 }
142
143
144 }