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.crypto.Crypto;
23  import org.apache.wss4j.common.ext.WSSecurityException;
24  import org.apache.wss4j.common.derivedKey.ConversationConstants;
25  import org.apache.wss4j.dom.engine.WSSConfig;
26  import org.apache.wss4j.dom.message.token.SecurityContextToken;
27  import org.apache.wss4j.dom.util.WSSecurityUtil;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  
31  /**
32   * Builder class to add a <code>wsc:SecurityContextToken</code> into the
33   * <code>wsse:Security</code>
34   */
35  public class WSSecSecurityContextToken {
36  
37      /**
38       * The <code>wsc:SecurityContextToken</code> to be added to the
39       * <code>wsse:SecurityHeader</code>
40       */
41      private SecurityContextToken sct;
42  
43      /**
44       * The <code>wsu:Id</code> of the <code>wsc:SecurityContextToken</code>
45       */
46      private String sctId;
47  
48      /**
49       * The <code>wsc:Identifier</code> of the
50       * <code>wsc:SecurityContextToken</code>
51       */
52      private String identifier;
53  
54      private int wscVersion = ConversationConstants.DEFAULT_VERSION;
55      private WSSConfig wssConfig;
56      private final WSSecHeader securityHeader;
57      private final Document doc;
58  
59      public WSSecSecurityContextToken(WSSecHeader securityHeader, WSSConfig config) {
60          this.securityHeader = securityHeader;
61          if (securityHeader != null && securityHeader.getSecurityHeaderElement() != null) {
62              doc = securityHeader.getSecurityHeaderElement().getOwnerDocument();
63          } else {
64              doc = null;
65          }
66          wssConfig = config;
67      }
68  
69      public WSSecSecurityContextToken(Document doc, WSSConfig config) {
70          this.securityHeader = null;
71          this.doc = doc;
72          wssConfig = config;
73      }
74  
75      public void prepare(Crypto crypto) throws WSSecurityException {
76  
77          if (sct == null) {
78              if (identifier != null) {
79                  sct = new SecurityContextToken(wscVersion, doc, identifier);
80              } else {
81                  sct = new SecurityContextToken(wscVersion, doc);
82                  identifier = sct.getIdentifier();
83              }
84          }
85  
86          // The wsu:Id of the wsc:SecurityContextToken
87          if (sctId == null) {
88              sctId = getWsConfig().getIdAllocator().createId("sctId-", sct);
89          }
90          sct.setID(sctId);
91      }
92  
93      public void prependSCTElementToHeader()
94          throws WSSecurityException {
95          Element secHeaderElement = securityHeader.getSecurityHeaderElement();
96          WSSecurityUtil.prependChildElement(secHeaderElement, sct.getElement());
97      }
98  
99      /**
100      * @return Returns the sct.
101      */
102     public SecurityContextToken getSct() {
103         return sct;
104     }
105 
106     /**
107      * @param sct The sct to set.
108      */
109     public void setSct(SecurityContextToken sct) {
110         this.sct = sct;
111     }
112 
113     /**
114      * @return Returns the identifier.
115      */
116     public String getIdentifier() {
117         return identifier;
118     }
119 
120     /**
121      * @param identifier The identifier to set.
122      */
123     public void setIdentifier(String identifier) {
124         this.identifier = identifier;
125     }
126 
127     /**
128      * @return Returns the sctId.
129      */
130     public String getSctId() {
131         if (sct != null) {
132             return sct.getID();
133         }
134         return sctId;
135     }
136 
137     /**
138      * @param sctId The sctId to set.
139      */
140     public void setSctId(String sctId) {
141         this.sctId = sctId;
142     }
143 
144     /**
145      * @param wscVersion The wscVersion to set.
146      */
147     public void setWscVersion(int wscVersion) {
148         this.wscVersion = wscVersion;
149     }
150 
151     private WSSConfig getWsConfig() {
152         if (wssConfig == null) {
153             wssConfig = WSSConfig.getNewInstance();
154         }
155         return wssConfig;
156     }
157 }