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.WSEncryptionPart;
23 import org.apache.wss4j.common.util.XMLUtils;
24 import org.apache.wss4j.dom.WSConstants;
25 import org.apache.wss4j.dom.WSDocInfo;
26 import org.apache.wss4j.dom.WsuIdAllocator;
27 import org.apache.wss4j.dom.callback.CallbackLookup;
28 import org.apache.wss4j.dom.callback.DOMCallbackLookup;
29 import org.apache.wss4j.dom.engine.WSSConfig;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32
33 import javax.security.auth.callback.CallbackHandler;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38
39
40
41
42
43 public class WSSecBase {
44 protected String user;
45 protected String password;
46 protected int keyIdentifierType = WSConstants.ISSUER_SERIAL;
47 protected CallbackLookup callbackLookup;
48 protected CallbackHandler attachmentCallbackHandler;
49 protected boolean storeBytesInAttachment;
50 protected boolean expandXopInclude;
51 protected boolean addWSUNamespace;
52
53 private WsuIdAllocator idAllocator;
54 private final List<WSEncryptionPart> parts = new ArrayList<>();
55 private final WSSecHeader securityHeader;
56 private final Document doc;
57 private WSDocInfo wsDocInfo;
58
59 public WSSecBase(WSSecHeader securityHeader) {
60 this.securityHeader = securityHeader;
61 if (securityHeader != null && securityHeader.getSecurityHeaderElement() != null) {
62 doc = securityHeader.getSecurityHeaderElement().getOwnerDocument();
63 } else {
64 doc = null;
65 }
66
67
68 addWSUNamespace = securityHeader != null && securityHeader.getWsuPrefix() != null
69 && !WSConstants.WSU_PREFIX.equals(securityHeader.getWsuPrefix());
70 }
71
72 public WSSecBase(Document doc) {
73 this.doc = doc;
74 securityHeader = new WSSecHeader(doc);
75 }
76
77 protected Document getDocument() {
78 return doc;
79 }
80
81 public WSSecHeader getSecurityHeader() {
82 return securityHeader;
83 }
84
85
86
87
88 public void setCallbackLookup(CallbackLookup callbackLookup) {
89 this.callbackLookup = callbackLookup;
90 }
91
92
93
94
95 public List<WSEncryptionPart> getParts() {
96 return parts;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public void setKeyIdentifierType(int keyIdType) {
119 keyIdentifierType = keyIdType;
120 }
121
122
123
124
125
126
127
128
129
130
131
132 public int getKeyIdentifierType() {
133 return keyIdentifierType;
134 }
135
136 public void setAttachmentCallbackHandler(CallbackHandler attachmentCallbackHandler) {
137 this.attachmentCallbackHandler = attachmentCallbackHandler;
138 }
139
140 public void setStoreBytesInAttachment(boolean storeBytesInAttachment) {
141 this.storeBytesInAttachment = storeBytesInAttachment;
142 }
143
144
145
146
147
148
149
150
151
152
153
154 protected String setBodyID(Document doc) throws Exception {
155 if (callbackLookup == null) {
156 callbackLookup = new DOMCallbackLookup(doc);
157 }
158 Element bodyElement = callbackLookup.getSOAPBody();
159 if (bodyElement == null) {
160 throw new Exception("SOAP Body Element node not found");
161 }
162 return setWsuId(bodyElement);
163 }
164
165 protected String setWsuId(Element bodyElement) {
166 String id = bodyElement.getAttributeNS(WSConstants.WSU_NS, "Id");
167
168 String newAttrNs = WSConstants.WSU_NS;
169 String newAttrPrefix = WSConstants.WSU_PREFIX;
170
171 if (id == null || id.length() == 0) {
172 if (WSConstants.ENC_NS.equals(bodyElement.getNamespaceURI())
173 && (WSConstants.ENC_DATA_LN.equals(bodyElement.getLocalName())
174 || WSConstants.ENC_KEY_LN.equals(bodyElement.getLocalName()))
175 ) {
176
177
178 id = bodyElement.getAttributeNS(null, "Id");
179 newAttrPrefix = WSConstants.ENC_PREFIX;
180 newAttrNs = WSConstants.ENC_NS;
181 } else if (WSConstants.SAML_NS.equals(bodyElement.getNamespaceURI())
182 && "Assertion".equals(bodyElement.getLocalName())) {
183 id = bodyElement.getAttributeNS(null, "AssertionID");
184 } else if (WSConstants.SAML2_NS.equals(bodyElement.getNamespaceURI())
185 && "Assertion".equals(bodyElement.getLocalName())) {
186 id = bodyElement.getAttributeNS(null, "ID");
187 } else if (WSConstants.SIG_NS.equals(bodyElement.getNamespaceURI())
188 && "KeyInfo".equals(bodyElement.getLocalName())) {
189 id = bodyElement.getAttributeNS(null, "Id");
190 }
191 }
192
193 if (id == null || id.length() == 0) {
194 id = getIdAllocator().createId("id-", bodyElement);
195 String prefix = XMLUtils.setNamespace(bodyElement, newAttrNs, newAttrPrefix);
196 bodyElement.setAttributeNS(newAttrNs, prefix + ":Id", id);
197 }
198 return id;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public void setUserInfo(String user, String password) {
214 this.user = user;
215 this.password = password;
216 }
217
218 public WsuIdAllocator getIdAllocator() {
219 if (idAllocator != null) {
220 return idAllocator;
221 }
222 return WSSConfig.DEFAULT_ID_ALLOCATOR;
223 }
224
225 public void setIdAllocator(WsuIdAllocator idAllocator) {
226 this.idAllocator = idAllocator;
227 }
228
229 public boolean isExpandXopInclude() {
230 return expandXopInclude;
231 }
232
233 public void setExpandXopInclude(boolean expandXopInclude) {
234 this.expandXopInclude = expandXopInclude;
235 }
236
237 public WSDocInfo getWsDocInfo() {
238 return wsDocInfo;
239 }
240
241 public void setWsDocInfo(WSDocInfo wsDocInfo) {
242 this.wsDocInfo = wsDocInfo;
243 }
244
245 public void clean() {
246 user = null;
247 password = null;
248 }
249 }