1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.validate;
21
22 import java.util.List;
23
24 import org.apache.ws.security.WSSecurityException;
25 import org.apache.ws.security.handler.RequestData;
26 import org.apache.ws.security.saml.SAMLKeyInfo;
27 import org.apache.ws.security.saml.ext.AssertionWrapper;
28 import org.apache.ws.security.saml.ext.OpenSAMLUtil;
29 import org.joda.time.DateTime;
30 import org.opensaml.common.SAMLVersion;
31 import org.opensaml.xml.validation.ValidationException;
32 import org.opensaml.xml.validation.ValidatorSuite;
33
34
35
36
37
38
39
40
41 public class SamlAssertionValidator extends SignatureTrustValidator {
42
43 private static final org.apache.commons.logging.Log LOG =
44 org.apache.commons.logging.LogFactory.getLog(SamlAssertionValidator.class);
45
46
47
48
49
50 private int futureTTL = 60;
51
52
53
54
55
56 private boolean validateSignatureAgainstProfile = true;
57
58
59
60
61
62 public void setFutureTTL(int newFutureTTL) {
63 futureTTL = newFutureTTL;
64 }
65
66
67
68
69
70
71
72
73
74 public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
75 if (credential == null || credential.getAssertion() == null) {
76 throw new WSSecurityException(WSSecurityException.FAILURE, "noCredential");
77 }
78 AssertionWrapper assertion = credential.getAssertion();
79
80
81 String confirmMethod = null;
82 List<String> methods = assertion.getConfirmationMethods();
83 if (methods != null && methods.size() > 0) {
84 confirmMethod = methods.get(0);
85 }
86 if (OpenSAMLUtil.isMethodHolderOfKey(confirmMethod)) {
87 if (assertion.getSubjectKeyInfo() == null) {
88 LOG.debug("There is no Subject KeyInfo to match the holder-of-key subject conf method");
89 throw new WSSecurityException(WSSecurityException.FAILURE, "noKeyInSAMLToken");
90 }
91
92 if (!assertion.isSigned()) {
93 LOG.debug("A holder-of-key assertion must be signed");
94 throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity");
95 }
96 }
97
98
99 checkConditions(assertion);
100
101
102 validateAssertion(assertion);
103
104
105 if (assertion.isSigned()) {
106 verifySignedAssertion(assertion, data);
107 }
108 return credential;
109 }
110
111
112
113
114
115
116
117
118
119 protected Credential verifySignedAssertion(
120 AssertionWrapper assertion,
121 RequestData data
122 ) throws WSSecurityException {
123 Credential trustCredential = new Credential();
124 SAMLKeyInfo samlKeyInfo = assertion.getSignatureKeyInfo();
125 trustCredential.setPublicKey(samlKeyInfo.getPublicKey());
126 trustCredential.setCertificates(samlKeyInfo.getCerts());
127 return super.validate(trustCredential, data);
128 }
129
130
131
132
133 protected void checkConditions(AssertionWrapper assertion) throws WSSecurityException {
134 DateTime validFrom = null;
135 DateTime validTill = null;
136 if (assertion.getSamlVersion().equals(SAMLVersion.VERSION_20)
137 && assertion.getSaml2().getConditions() != null) {
138 validFrom = assertion.getSaml2().getConditions().getNotBefore();
139 validTill = assertion.getSaml2().getConditions().getNotOnOrAfter();
140 } else if (assertion.getSamlVersion().equals(SAMLVersion.VERSION_11)
141 && assertion.getSaml1().getConditions() != null) {
142 validFrom = assertion.getSaml1().getConditions().getNotBefore();
143 validTill = assertion.getSaml1().getConditions().getNotOnOrAfter();
144 }
145
146 if (validFrom != null) {
147 DateTime currentTime = new DateTime();
148 currentTime = currentTime.plusSeconds(futureTTL);
149 if (validFrom.isAfter(currentTime)) {
150 LOG.debug("SAML Token condition (Not Before) not met");
151 throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity");
152 }
153 }
154
155 if (validTill != null && validTill.isBeforeNow()) {
156 LOG.debug("SAML Token condition (Not On Or After) not met");
157 throw new WSSecurityException(WSSecurityException.FAILURE, "invalidSAMLsecurity");
158 }
159 }
160
161
162
163
164 protected void validateAssertion(AssertionWrapper assertion) throws WSSecurityException {
165 if (validateSignatureAgainstProfile) {
166 assertion.validateSignatureAgainstProfile();
167 }
168
169 if (assertion.getSaml1() != null) {
170 ValidatorSuite schemaValidators =
171 org.opensaml.Configuration.getValidatorSuite("saml1-schema-validator");
172 ValidatorSuite specValidators =
173 org.opensaml.Configuration.getValidatorSuite("saml1-spec-validator");
174 try {
175 schemaValidators.validate(assertion.getSaml1());
176 specValidators.validate(assertion.getSaml1());
177 } catch (ValidationException e) {
178 LOG.debug("Saml Validation error: " + e.getMessage(), e);
179 throw new WSSecurityException(
180 WSSecurityException.FAILURE, "invalidSAMLsecurity", null, e
181 );
182 }
183 } else if (assertion.getSaml2() != null) {
184 ValidatorSuite schemaValidators =
185 org.opensaml.Configuration.getValidatorSuite("saml2-core-schema-validator");
186 ValidatorSuite specValidators =
187 org.opensaml.Configuration.getValidatorSuite("saml2-core-spec-validator");
188 try {
189 schemaValidators.validate(assertion.getSaml2());
190 specValidators.validate(assertion.getSaml2());
191 } catch (ValidationException e) {
192 LOG.debug("Saml Validation error: " + e.getMessage(), e);
193 throw new WSSecurityException(
194 WSSecurityException.FAILURE, "invalidSAMLsecurity", null, e
195 );
196 }
197 }
198 }
199
200
201
202
203
204 public boolean isValidateSignatureAgainstProfile() {
205 return validateSignatureAgainstProfile;
206 }
207
208
209
210
211
212 public void setValidateSignatureAgainstProfile(boolean validateSignatureAgainstProfile) {
213 this.validateSignatureAgainstProfile = validateSignatureAgainstProfile;
214 }
215
216 }