1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41 public class SecurityContextToken {
42
43
44
45
46 protected Element element = null;
47
48
49
50
51 protected Element elementIdentifier = null;
52
53 private WSSConfig wssConfig = WSSConfig.getNewInstance();
54
55 private String tokenType = WSConstants.WSC_SCT;
56
57
58
59
60
61
62 public SecurityContextToken(Document doc) throws ConversationException {
63 this(ConversationConstants.DEFAULT_VERSION, doc);
64 }
65
66
67
68
69
70
71 public SecurityContextToken(Document doc, String uuid) throws ConversationException {
72 this(ConversationConstants.DEFAULT_VERSION, doc, uuid);
73 }
74
75
76
77
78
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
103
104
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
132
133
134
135
136 public SecurityContextToken(Element elem) throws WSSecurityException {
137 element = elem;
138 QName el = new QName(element.getNamespaceURI(), element.getLocalName());
139
140
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
159
160
161 public void addWSUNamespace() {
162 WSSecurityUtil.setNamespace(element, WSConstants.WSU_NS, WSConstants.WSU_PREFIX);
163 }
164
165
166
167
168 public void setIdentifier(String uuid) {
169 Text node = getFirstNode(elementIdentifier);
170 node.setData(uuid);
171 }
172
173
174
175
176
177
178 public String getIdentifier() {
179 if (elementIdentifier != null) {
180 return getFirstNode(elementIdentifier).getData();
181 }
182 return null;
183 }
184
185
186
187
188 public String getTokenType() {
189 return tokenType;
190 }
191
192 public void setElement(Element elem) {
193 element.appendChild(elem);
194 }
195
196
197
198
199
200
201
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
210
211
212
213 public Element getElement() {
214 return element;
215 }
216
217
218
219
220
221
222 public String toString() {
223 return DOM2Writer.nodeToString((Node)element);
224 }
225
226
227
228
229
230
231
232 public String getID() {
233 return element.getAttributeNS(WSConstants.WSU_NS, "Id");
234 }
235
236
237
238
239
240
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 }