1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.wss4j.policy.model;
20
21 import org.apache.neethi.Constants;
22 import org.apache.neethi.Policy;
23 import org.apache.neethi.PolicyComponent;
24 import org.apache.neethi.PolicyContainingAssertion;
25 import org.apache.wss4j.policy.SPConstants;
26 import org.apache.wss4j.policy.SPUtils;
27 import org.w3c.dom.Element;
28
29 import javax.xml.stream.XMLStreamException;
30 import javax.xml.stream.XMLStreamWriter;
31 import java.util.EnumSet;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 public abstract class AbstractToken extends AbstractSecurityAssertion implements PolicyContainingAssertion {
36
37 public enum DerivedKeys {
38 RequireDerivedKeys,
39 RequireExplicitDerivedKeys,
40 RequireImpliedDerivedKeys;
41
42 private static final Map<String, DerivedKeys> LOOKUP = new HashMap<>();
43
44 static {
45 for (DerivedKeys u : EnumSet.allOf(DerivedKeys.class)) {
46 LOOKUP.put(u.name(), u);
47 }
48 }
49
50 public static DerivedKeys lookUp(String name) {
51 return LOOKUP.get(name);
52 }
53 }
54
55 private SPConstants.IncludeTokenType includeTokenType;
56 private Element issuer;
57 private Element claims;
58 private String issuerName;
59 private DerivedKeys derivedKeys;
60 private Policy nestedPolicy;
61 private AbstractSecurityAssertion parentAssertion;
62
63 protected AbstractToken(SPConstants.SPVersion version, SPConstants.IncludeTokenType includeTokenType,
64 Element issuer, String issuerName, Element claims, Policy nestedPolicy) {
65 super(version);
66 this.nestedPolicy = nestedPolicy;
67 this.includeTokenType = includeTokenType;
68 this.issuer = issuer;
69 this.issuerName = issuerName;
70 this.claims = claims;
71 }
72
73 @Override
74 public Policy getPolicy() {
75 return nestedPolicy;
76 }
77
78 @Override
79 public PolicyComponent normalize() {
80 return super.normalize(getPolicy());
81 }
82
83 @Override
84 public boolean equals(Object object) {
85 if (object == this) {
86 return true;
87 }
88 if (!(object instanceof AbstractToken)) {
89 return false;
90 }
91
92 AbstractToken that = (AbstractToken)object;
93 if (includeTokenType != that.includeTokenType
94 || derivedKeys != that.derivedKeys) {
95 return false;
96 }
97 if (issuerName != null && !issuerName.equals(that.issuerName)
98 || issuerName == null && that.issuerName != null) {
99 return false;
100 }
101
102 if (issuer == null && that.issuer != null
103 || issuer != null && that.issuer == null) {
104 return false;
105 }
106
107 if (issuer != null
108 && !DOM2Writer.nodeToString(issuer).equals(DOM2Writer.nodeToString(that.issuer))) {
109 return false;
110 }
111
112 if (claims == null && that.claims != null
113 || claims != null && that.claims == null) {
114 return false;
115 }
116
117 if (claims != null
118 && !DOM2Writer.nodeToString(claims).equals(DOM2Writer.nodeToString(that.claims))) {
119 return false;
120 }
121
122 return super.equals(object);
123 }
124
125 @Override
126 public int hashCode() {
127 int result = 17;
128 if (includeTokenType != null) {
129 result = 31 * result + includeTokenType.hashCode();
130 }
131 if (derivedKeys != null) {
132 result = 31 * result + derivedKeys.hashCode();
133 }
134 if (issuerName != null) {
135 result = 31 * result + issuerName.hashCode();
136 }
137
138 if (issuer != null) {
139 result = 31 * result + DOM2Writer.nodeToString(issuer).hashCode();
140 }
141 if (claims != null) {
142 result = 31 * result + DOM2Writer.nodeToString(claims).hashCode();
143 }
144
145 return 31 * result + super.hashCode();
146 }
147
148 public SPConstants.IncludeTokenType getIncludeTokenType() {
149 return includeTokenType;
150 }
151
152 protected void setIncludeTokenType(SPConstants.IncludeTokenType includeTokenType) {
153 this.includeTokenType = includeTokenType;
154 }
155
156 public Element getIssuer() {
157 return issuer;
158 }
159
160 protected void setIssuer(Element issuer) {
161 this.issuer = issuer;
162 }
163
164 public String getIssuerName() {
165 return issuerName;
166 }
167
168 protected void setIssuerName(String issuerName) {
169 this.issuerName = issuerName;
170 }
171
172 public Element getClaims() {
173 return claims;
174 }
175
176 protected void setClaims(Element claims) {
177 this.claims = claims;
178 }
179
180 public DerivedKeys getDerivedKeys() {
181 return derivedKeys;
182 }
183
184 protected void setDerivedKeys(DerivedKeys derivedKeys) {
185 this.derivedKeys = derivedKeys;
186 }
187
188 public AbstractSecurityAssertion getParentAssertion() {
189 return parentAssertion;
190 }
191
192 public void setParentAssertion(AbstractSecurityAssertion parentAssertion) {
193 this.parentAssertion = parentAssertion;
194 }
195
196 @Override
197 public void serialize(XMLStreamWriter writer) throws XMLStreamException {
198 writer.writeStartElement(getName().getPrefix(), getName().getLocalPart(), getName().getNamespaceURI());
199 writer.writeNamespace(getName().getPrefix(), getName().getNamespaceURI());
200 if (getIncludeTokenType() != null) {
201 writer.writeAttribute(
202 getVersion().getSPConstants().getIncludeToken().getPrefix(),
203 getVersion().getSPConstants().getIncludeToken().getNamespaceURI(),
204 getVersion().getSPConstants().getIncludeToken().getLocalPart(),
205 getVersion().getSPConstants().getAttributeValueFromInclusion(getIncludeTokenType())
206 );
207 }
208 if (!isNormalized() && isOptional()) {
209 writer.writeAttribute(Constants.ATTR_WSP,
210 writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
211 Constants.ATTR_OPTIONAL, "true");
212 }
213 if (isIgnorable()) {
214 writer.writeAttribute(Constants.ATTR_WSP,
215 writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
216 Constants.ATTR_IGNORABLE, "true");
217 }
218 if (getIssuer() != null) {
219 SPUtils.serialize(getIssuer(), writer);
220 }
221 if (getIssuerName() != null) {
222 writer.writeStartElement(
223 getVersion().getSPConstants().getIssuerName().getPrefix(),
224 getVersion().getSPConstants().getIssuerName().getLocalPart(),
225 getVersion().getSPConstants().getIssuerName().getNamespaceURI()
226 );
227 writer.writeCharacters(getIssuerName());
228 writer.writeEndElement();
229 }
230 if (getClaims() != null) {
231 SPUtils.serialize(getClaims(), writer);
232 }
233 getPolicy().serialize(writer);
234 writer.writeEndElement();
235 }
236 }