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.message;
21  
22  import org.apache.wss4j.common.saml.SamlAssertionWrapper;
23  import org.apache.wss4j.common.ext.WSSecurityException;
24  import org.apache.wss4j.dom.util.WSSecurityUtil;
25  
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  /**
30   * Builds a WS SAML Assertion and inserts it into the SOAP Envelope. Refer to
31   * the WS specification, SAML Token profile
32   */
33  public class WSSecSAMLToken extends WSSecBase {
34  
35      private static final org.slf4j.Logger LOG =
36          org.slf4j.LoggerFactory.getLogger(WSSecSAMLToken.class);
37  
38      private SamlAssertionWrapper saml;
39  
40      private Element samlElement;
41  
42      public WSSecSAMLToken(WSSecHeader securityHeader) {
43          super(securityHeader);
44      }
45  
46      public WSSecSAMLToken(Document doc) {
47          super(doc);
48      }
49  
50      /**
51       * Creates a SAML token.
52       *
53       * The method prepares and initializes a WSSec UsernameToken structure after
54       * the relevant information was set. A Before calling
55       * <code>prepare()</code> all parameters such as user, password,
56       * passwordType etc. must be set. A complete <code>UsernameToken</code> is
57       * constructed.
58       */
59      public void prepare(SamlAssertionWrapper samlAssertion) {
60          saml = samlAssertion;
61      }
62  
63      /**
64       * Prepends the SAML Assertion to the elements already in the
65       * Security header.
66       *
67       * The method can be called any time after <code>prepare()</code>.
68       * This allows to insert the SAML assertion at any position in the
69       * Security header.
70       *
71       */
72      public void prependToHeader() {
73          try {
74              Element element = getElement();
75              if (element != null) {
76                  Element securityHeaderElement = getSecurityHeader().getSecurityHeaderElement();
77                  WSSecurityUtil.prependChildElement(securityHeaderElement, element);
78              }
79          } catch (WSSecurityException ex) {
80              throw new RuntimeException(ex.toString(), ex);
81          }
82      }
83  
84      public Element getElement() throws WSSecurityException {
85          if (samlElement != null) {
86              return samlElement;
87          }
88          if (saml == null) {
89              return null;
90          }
91          samlElement = saml.toDOM(getDocument());
92          return samlElement;
93      }
94  
95      /**
96       * Get the id generated during <code>prepare()</code>.
97       *
98       * Returns the the value of wsu:Id attribute of this Timestamp.
99       *
100      * @return Return the wsu:Id of this token or null if <code>prepareToken()</code>
101      * was not called before.
102      */
103     public String getId() {
104         if (saml == null) {
105             return null;
106         }
107         return saml.getId();
108     }
109 
110     /**
111      * Adds a new <code>SAMLAssertion</code> to a soap envelope.
112      * <p/>
113      * A complete <code>SAMLAssertion</code> is added to the
114      * <code>wsse:Security</code> header.
115      *
116      * @param samlAssertion TODO
117      * @return Document with UsernameToken added
118      */
119     public Document build(SamlAssertionWrapper samlAssertion) {
120         LOG.debug("Begin add SAMLAssertion token...");
121 
122         prepare(samlAssertion);
123         prependToHeader();
124 
125         return getDocument();
126     }
127 }