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.common;
21  
22  import java.io.IOException;
23  
24  import javax.security.auth.callback.Callback;
25  import javax.security.auth.callback.UnsupportedCallbackException;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  
28  import org.apache.wss4j.common.saml.SamlAssertionWrapper;
29  import org.apache.wss4j.common.saml.SAMLCallback;
30  import org.apache.wss4j.common.saml.SAMLUtil;
31  import org.apache.wss4j.common.saml.builder.SAML1Constants;
32  import org.w3c.dom.Element;
33  
34  /**
35   * A Callback Handler implementation for a SAML 1.1 assertion. Rather than create a set of beans
36   * that SamlAssertionWrapper will use to create a SAML Assertion, it sets a DOM Element directly on
37   * the SAMLCallback object.
38   */
39  public class SAMLElementCallbackHandler extends AbstractSAMLCallbackHandler {
40  
41      public SAMLElementCallbackHandler() {
42          subjectName = "uid=joe,ou=people,ou=saml-demo,o=example.com";
43          subjectQualifier = "www.example.com";
44          confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES;
45      }
46  
47      public void handle(Callback[] callbacks)
48          throws IOException, UnsupportedCallbackException {
49          for (Callback callback : callbacks) {
50              if (callback instanceof SAMLCallback) {
51                  SAMLCallback samlCallback = (SAMLCallback) callback;
52                  Element assertionElement;
53                  try {
54                      assertionElement = getSAMLAssertion();
55                  } catch (Exception e) {
56                      throw new IOException(e.getMessage());
57                  }
58                  samlCallback.setAssertionElement(assertionElement);
59  
60              } else {
61                  throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
62              }
63          }
64      }
65  
66      /**
67       * Mock up a SAML Assertion by using another SAMLCallbackHandler
68       * @throws Exception
69       */
70      private Element getSAMLAssertion() throws Exception {
71          SAML1CallbackHandler callbackHandler = new SAML1CallbackHandler();
72          callbackHandler.setIssuer(issuer);
73          SAMLCallback samlCallback = new SAMLCallback();
74          SAMLUtil.doSAMLCallback(callbackHandler, samlCallback);
75  
76          SamlAssertionWrapper samlAssertionWrapper = new SamlAssertionWrapper(samlCallback);
77  
78          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
79          return samlAssertionWrapper.toDOM(factory.newDocumentBuilder().newDocument());
80      }
81  
82  }