1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.wss4j.policy.builders;
20
21 import org.apache.neethi.Assertion;
22 import org.apache.neethi.AssertionBuilderFactory;
23 import org.apache.neethi.Policy;
24 import org.apache.neethi.builders.AssertionBuilder;
25 import org.apache.wss4j.policy.SP11Constants;
26 import org.apache.wss4j.policy.SP13Constants;
27 import org.apache.wss4j.policy.SPConstants;
28 import org.apache.wss4j.policy.SPUtils;
29 import org.apache.wss4j.policy.model.UsernameToken;
30 import org.w3c.dom.Element;
31
32 import javax.xml.namespace.QName;
33
34 public class UsernameTokenBuilder implements AssertionBuilder<Element> {
35
36 @Override
37 public Assertion build(Element element, AssertionBuilderFactory factory) throws IllegalArgumentException {
38
39 final SPConstants.SPVersion spVersion = SPConstants.SPVersion.getSPVersion(element.getNamespaceURI());
40 final String includeTokenValue = SPUtils.getAttribute(element, spVersion.getSPConstants().getIncludeToken());
41 final Element issuer = SPUtils.getFirstChildElement(element, spVersion.getSPConstants().getIssuer());
42 if (spVersion == SPConstants.SPVersion.SP11 && issuer != null) {
43 throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
44 }
45 final String issuerName = SPUtils.getFirstChildElementText(element, spVersion.getSPConstants().getIssuerName());
46 if (spVersion == SPConstants.SPVersion.SP11 && issuerName != null) {
47 throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
48 }
49 final Element claims = SPUtils.getFirstChildElement(element, spVersion.getSPConstants().getClaims());
50 final Element nestedPolicyElement = SPUtils.getFirstPolicyChildElement(element);
51
52 Policy nestedPolicy = null;
53 if (nestedPolicyElement == null) {
54 nestedPolicy = new Policy();
55 } else {
56 nestedPolicy = factory.getPolicyEngine().getPolicy(nestedPolicyElement);
57 }
58
59 UsernameToken usernameToken = new UsernameToken(
60 spVersion,
61 spVersion.getSPConstants().getInclusionFromAttributeValue(includeTokenValue),
62 issuer,
63 issuerName,
64 claims,
65 nestedPolicy
66 );
67 usernameToken.setOptional(SPUtils.isOptional(element));
68 usernameToken.setIgnorable(SPUtils.isIgnorable(element));
69 return usernameToken;
70 }
71
72 @Override
73 public QName[] getKnownElements() {
74 return new QName[]{SP13Constants.USERNAME_TOKEN, SP11Constants.USERNAME_TOKEN};
75 }
76 }