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.message.token;
21  
22  import org.apache.ws.security.WSConstants;
23  import org.apache.ws.security.WSSConfig;
24  import org.apache.ws.security.WSSecurityException;
25  import org.apache.ws.security.conversation.ConversationConstants;
26  import org.apache.ws.security.conversation.ConversationException;
27  import org.apache.ws.security.util.DOM2Writer;
28  import org.apache.ws.security.util.UUIDGenerator;
29  import org.apache.ws.security.util.WSSecurityUtil;
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.Node;
33  import org.w3c.dom.Text;
34  
35  import javax.xml.namespace.QName;
36  
37  /**
38   * @author Ruchith Fernando
39   * @version 1.0
40   */
41  public class SecurityContextToken {
42  
43      /**
44       * Security context token element
45       */
46      protected Element element = null;
47  
48      /**
49       * Identifier element
50       */
51      protected Element elementIdentifier = null;
52      
53      private WSSConfig wssConfig = WSSConfig.getNewInstance();
54      
55      private String tokenType = WSConstants.WSC_SCT;
56      
57      /**
58       * Constructor to create the SCT
59       *
60       * @param doc
61       */
62      public SecurityContextToken(Document doc) throws ConversationException {
63          this(ConversationConstants.DEFAULT_VERSION, doc);
64      }
65  
66      /**
67       * Constructor to create the SCT with a given uuid
68       *
69       * @param doc
70       */
71      public SecurityContextToken(Document doc, String uuid) throws ConversationException {
72          this(ConversationConstants.DEFAULT_VERSION, doc, uuid);
73      }
74  
75      /**
76       * Constructor to create the SCT
77       *
78       * @param doc
79       */
80      public SecurityContextToken(int version, Document doc) throws ConversationException {
81  
82          String ns = ConversationConstants.getWSCNs(version);
83          
84          element = 
85              doc.createElementNS(ns, "wsc:" + ConversationConstants.SECURITY_CONTEXT_TOKEN_LN);
86  
87          WSSecurityUtil.setNamespace(element, ns, ConversationConstants.WSC_PREFIX);
88  
89          elementIdentifier = 
90              doc.createElementNS(ns, "wsc:" + ConversationConstants.IDENTIFIER_LN);
91  
92          element.appendChild(elementIdentifier);
93  
94          String uuid = "uuid:" + UUIDGenerator.getUUID();
95          
96          elementIdentifier.appendChild(doc.createTextNode(uuid));
97          
98          setID(wssConfig.getIdAllocator().createSecureId("sctId-", element));
99      }
100 
101     /**
102      * Constructor to create the SCT with a given uuid
103      *
104      * @param doc
105      */
106     public SecurityContextToken(int version, Document doc, String uuid) throws ConversationException {
107 
108         String ns = ConversationConstants.getWSCNs(version);
109         
110         element = 
111             doc.createElementNS(ns, "wsc:" + ConversationConstants.SECURITY_CONTEXT_TOKEN_LN);
112 
113         WSSecurityUtil.setNamespace(element, ns, ConversationConstants.WSC_PREFIX);
114 
115         elementIdentifier = 
116             doc.createElementNS(ns, "wsc:" + ConversationConstants.IDENTIFIER_LN);
117 
118         element.appendChild(elementIdentifier);
119 
120         elementIdentifier.appendChild(doc.createTextNode(uuid));
121         
122         if (version == ConversationConstants.VERSION_05_02) {
123             tokenType = WSConstants.WSC_SCT;
124         } else {
125             tokenType = WSConstants.WSC_SCT_05_12;
126         }
127     }
128 
129     
130     /**
131      * This is used to create a SecurityContextToken using a DOM Element
132      *
133      * @param elem The DOM element: The security context token
134      * @throws WSSecurityException If the element passed in in not a security context token
135      */
136     public SecurityContextToken(Element elem) throws WSSecurityException {
137         element = elem;
138         QName el = new QName(element.getNamespaceURI(), element.getLocalName());
139 
140         // If the element is not a security context token, throw an exception
141         if (el.equals(ConversationConstants.SECURITY_CTX_TOKEN_QNAME_05_02)) {
142             tokenType = WSConstants.WSC_SCT;
143         } else if (el.equals(ConversationConstants.SECURITY_CTX_TOKEN_QNAME_05_12)) {
144             tokenType = WSConstants.WSC_SCT_05_12;
145         } else {
146             throw new WSSecurityException(WSSecurityException.INVALID_SECURITY_TOKEN);
147         }
148 
149         elementIdentifier = 
150             WSSecurityUtil.getDirectChildElement(
151                 element, 
152                 ConversationConstants.IDENTIFIER_LN,
153                 el.getNamespaceURI()
154             );
155     }
156     
157     /**
158      * Add the WSU Namespace to this SCT. The namespace is not added by default for
159      * efficiency purposes.
160      */
161     public void addWSUNamespace() {
162         WSSecurityUtil.setNamespace(element, WSConstants.WSU_NS, WSConstants.WSU_PREFIX);
163     }
164 
165     /**
166      * Set the identifier.
167      */
168     public void setIdentifier(String uuid) {
169         Text node = getFirstNode(elementIdentifier);
170         node.setData(uuid);
171     }
172 
173     /**
174      * Get the identifier.
175      *
176      * @return the data from the identifier element.
177      */
178     public String getIdentifier() {
179         if (elementIdentifier != null) {
180             return getFirstNode(elementIdentifier).getData();
181         }
182         return null;
183     }
184     
185     /**
186      * Get the WS-Trust tokenType String associated with this token
187      */
188     public String getTokenType() {
189         return tokenType;
190     }
191 
192     public void setElement(Element elem) {
193         element.appendChild(elem);
194     }
195 
196     /**
197      * Returns the first text node of an element.
198      *
199      * @param e the element to get the node from
200      * @return the first text node or <code>null</code> if node
201      *         is null or is not a text node
202      */
203     private Text getFirstNode(Element e) {
204         Node node = e.getFirstChild();
205         return (node != null && Node.TEXT_NODE == node.getNodeType()) ? (Text) node : null;
206     }
207 
208     /**
209      * Returns the dom element of this <code>SecurityContextToken</code> object.
210      *
211      * @return the <code>wsse:SecurityContextToken</code> element
212      */
213     public Element getElement() {
214         return element;
215     }
216 
217     /**
218      * Returns the string representation of the token.
219      *
220      * @return a XML string representation
221      */
222     public String toString() {
223         return DOM2Writer.nodeToString((Node)element);
224     }
225 
226     /**
227      * Gets the id.
228      *
229      * @return the value of the <code>wsu:Id</code> attribute of this
230      *         SecurityContextToken
231      */
232     public String getID() {
233         return element.getAttributeNS(WSConstants.WSU_NS, "Id");
234     }
235 
236     /**
237      * Set the id of this security context token.
238      *
239      * @param id the value for the <code>wsu:Id</code> attribute of this
240      *           SecurityContextToken
241      */
242     public void setID(String id) {
243         element.setAttributeNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":Id", id);
244     }
245     
246     @Override
247     public int hashCode() {
248         int result = 17;
249         String identifier = getIdentifier();
250         if (identifier != null) {
251             result = 31 * result + identifier.hashCode();
252         }
253         return result;
254     }
255     
256     @Override
257     public boolean equals(Object object) {
258         if (!(object instanceof SecurityContextToken)) {
259             return false;
260         }
261         SecurityContextToken securityToken = (SecurityContextToken)object;
262         if (!compare(getIdentifier(), securityToken.getIdentifier())) {
263             return false;
264         }
265         return true;
266     }
267     
268     private boolean compare(String item1, String item2) {
269         if (item1 == null && item2 != null) { 
270             return false;
271         } else if (item1 != null && !item1.equals(item2)) {
272             return false;
273         }
274         return true;
275     }
276 
277 }