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.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          * it is possible and legal that we do not have a signature
47          * crypto here - thus ignore the exception. This is usually
48          * the case for the SAML option "sender vouches". In this case
49          * no user crypto is required.
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           * required to add support for the 
86           * signatureParts parameter.
87           * If not set WSSecSignatureSAML
88           * defaults to only sign the body.
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 }