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;
21
22 import org.apache.ws.security.WSSecurityException;
23 import org.apache.ws.security.components.crypto.Crypto;
24 import org.apache.ws.security.conversation.ConversationConstants;
25 import org.apache.ws.security.conversation.ConversationException;
26 import org.apache.ws.security.message.token.SecurityContextToken;
27 import org.apache.ws.security.util.WSSecurityUtil;
28 import org.w3c.dom.Document;
29
30 /**
31 * Builder class to add a <code>wsc:SecurityContextToken</code> into the
32 * <code>wsse:Security</code>
33 *
34 * @author Ruchith Fernando (ruchith.fernando@gmail.com)
35 */
36 public class WSSecSecurityContextToken {
37
38 /**
39 * The <code>wsc:SecurityContextToken</code> to be added to the
40 * <code>wsse:SecurityHeader</code>
41 */
42 private SecurityContextToken sct;
43
44 /**
45 * The <code>wsu:Id</code> of the <code>wsc:SecurityContextToken</code>
46 */
47 private String sctId;
48
49 /**
50 * The <code>wsc:Identifier</code> of the
51 * <code>wsc:SecurityContextToken</code>
52 */
53 private String identifier;
54
55 /**
56 * The symmetric secret associated with the SecurityContextToken
57 */
58 protected byte[] secret;
59
60 private int wscVersion = ConversationConstants.DEFAULT_VERSION;
61
62 public void prepare(Document doc, Crypto crypto)
63 throws WSSecurityException, ConversationException {
64
65 if (sct == null) {
66 if (identifier != null) {
67 sct = new SecurityContextToken(wscVersion, doc, identifier);
68 } else {
69 sct = new SecurityContextToken(wscVersion, doc);
70 identifier = sct.getIdentifier();
71 }
72 }
73
74 // The wsu:Id of the wsc:SecurityContextToken
75 if (sctId != null) {
76 sct.setID(sctId);
77 }
78 }
79
80 public void prependSCTElementToHeader(Document doc, WSSecHeader secHeader)
81 throws WSSecurityException {
82 WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), sct.getElement());
83 }
84
85 /**
86 * @return Returns the sct.
87 */
88 public SecurityContextToken getSct() {
89 return sct;
90 }
91
92 /**
93 * @param sct The sct to set.
94 */
95 public void setSct(SecurityContextToken sct) {
96 this.sct = sct;
97 }
98
99 /**
100 * @return Returns the ephemeralKey.
101 */
102 public byte[] getSecret() {
103 return secret;
104 }
105
106 /**
107 * @param ephemeralKey The ephemeralKey to set.
108 */
109 protected void setSecret(byte[] ephemeralKey) {
110 secret = ephemeralKey;
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 }