1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35 public class WSSecSecurityContextToken {
36
37
38
39
40
41 private SecurityContextToken sct;
42
43
44
45
46 private String sctId;
47
48
49
50
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
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
101
102 public SecurityContextToken getSct() {
103 return sct;
104 }
105
106
107
108
109 public void setSct(SecurityContextToken sct) {
110 this.sct = sct;
111 }
112
113
114
115
116 public String getIdentifier() {
117 return identifier;
118 }
119
120
121
122
123 public void setIdentifier(String identifier) {
124 this.identifier = identifier;
125 }
126
127
128
129
130 public String getSctId() {
131 if (sct != null) {
132 return sct.getID();
133 }
134 return sctId;
135 }
136
137
138
139
140 public void setSctId(String sctId) {
141 this.sctId = sctId;
142 }
143
144
145
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 }