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.action;
21
22 import javax.security.auth.callback.CallbackHandler;
23
24 import org.apache.ws.security.WSPasswordCallback;
25 import org.apache.ws.security.WSSecurityException;
26 import org.apache.ws.security.components.crypto.Crypto;
27 import org.apache.ws.security.handler.RequestData;
28 import org.apache.ws.security.handler.WSHandler;
29 import org.apache.ws.security.handler.WSHandlerConstants;
30 import org.apache.ws.security.saml.SAMLIssuer;
31 import org.apache.ws.security.saml.SAMLIssuerFactory;
32 import org.apache.ws.security.saml.WSSecSignatureSAML;
33 import org.apache.ws.security.saml.ext.AssertionWrapper;
34
35 import org.w3c.dom.Document;
36
37 public class SAMLTokenSignedAction implements Action {
38
39 private static org.apache.commons.logging.Log log =
40 org.apache.commons.logging.LogFactory.getLog(SAMLTokenSignedAction.class);
41
42 public void execute(WSHandler handler, int actionToDo, Document doc, RequestData reqData)
43 throws WSSecurityException {
44 Crypto crypto = null;
45
46
47
48
49
50
51 try {
52 crypto = handler.loadSignatureCrypto(reqData);
53 } catch (Exception ex) {
54 if (log.isDebugEnabled()) {
55 log.debug(ex.getMessage(), ex);
56 }
57 }
58
59 SAMLIssuer saml = loadSamlIssuer(handler, reqData);
60
61 AssertionWrapper assertion = saml.newAssertion();
62 if (assertion == null) {
63 throw new WSSecurityException("WSHandler: Signed SAML: no SAML token received");
64 }
65
66 WSSecSignatureSAML wsSign = new WSSecSignatureSAML(reqData.getWssConfig());
67
68 CallbackHandler callbackHandler =
69 handler.getPasswordCallbackHandler(reqData);
70 WSPasswordCallback passwordCallback =
71 handler.getPasswordCB(reqData.getUsername(), actionToDo, callbackHandler, reqData);
72 wsSign.setUserInfo(reqData.getUsername(), passwordCallback.getPassword());
73
74 if (reqData.getSigKeyId() != 0) {
75 wsSign.setKeyIdentifierType(reqData.getSigKeyId());
76 }
77 if (reqData.getSigAlgorithm() != null) {
78 wsSign.setSignatureAlgorithm(reqData.getSigAlgorithm());
79 }
80 if (reqData.getSigDigestAlgorithm() != null) {
81 wsSign.setDigestAlgo(reqData.getSigDigestAlgorithm());
82 }
83
84
85
86
87
88
89
90 if (reqData.getSignatureParts().size() > 0) {
91 wsSign.setParts(reqData.getSignatureParts());
92 }
93
94 try {
95 wsSign.build(
96 doc,
97 crypto,
98 assertion,
99 saml.getIssuerCrypto(),
100 saml.getIssuerKeyName(),
101 saml.getIssuerKeyPassword(),
102 reqData.getSecHeader());
103 reqData.getSignatureValues().add(wsSign.getSignatureValue());
104 } catch (WSSecurityException e) {
105 throw new WSSecurityException("Error when signing the SAML token: ", e);
106 }
107 }
108
109 protected SAMLIssuer loadSamlIssuer(
110 WSHandler handler,
111 RequestData reqData
112 ) throws WSSecurityException {
113 String samlPropFile =
114 handler.getString(WSHandlerConstants.SAML_PROP_FILE, reqData.getMsgContext());
115 SAMLIssuer samlIssuer = SAMLIssuerFactory.getInstance(samlPropFile);
116 CallbackHandler callbackHandler =
117 handler.getCallbackHandler(
118 WSHandlerConstants.SAML_CALLBACK_CLASS,
119 WSHandlerConstants.SAML_CALLBACK_REF,
120 reqData
121 );
122 if (callbackHandler != null) {
123 samlIssuer.setCallbackHandler(callbackHandler);
124 }
125 return samlIssuer;
126 }
127
128 }