Skip to content

Commit 7b3dda1

Browse files
committed
Generalize SAML 2.0 Assertion Validation Support
Closes gh-8970
1 parent 1069e91 commit 7b3dda1

File tree

5 files changed

+456
-133
lines changed

5 files changed

+456
-133
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.core;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collection;
22+
import java.util.Collections;
23+
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* A result emitted from a SAML 2.0 Response validation attempt
28+
*
29+
* @author Josh Cummings
30+
* @since 5.4
31+
*/
32+
public final class Saml2ResponseValidatorResult {
33+
static final Saml2ResponseValidatorResult NO_ERRORS = new Saml2ResponseValidatorResult(Collections.emptyList());
34+
35+
private final Collection<Saml2Error> errors;
36+
37+
private Saml2ResponseValidatorResult(Collection<Saml2Error> errors) {
38+
Assert.notNull(errors, "errors cannot be null");
39+
this.errors = new ArrayList<>(errors);
40+
}
41+
42+
/**
43+
* Say whether this result indicates success
44+
*
45+
* @return whether this result has errors
46+
*/
47+
public boolean hasErrors() {
48+
return !this.errors.isEmpty();
49+
}
50+
51+
/**
52+
* Return error details regarding the validation attempt
53+
*
54+
* @return the collection of results in this result, if any; returns an empty list otherwise
55+
*/
56+
public Collection<Saml2Error> getErrors() {
57+
return Collections.unmodifiableCollection(this.errors);
58+
}
59+
60+
/**
61+
* Return a new {@link Saml2ResponseValidatorResult} that contains
62+
* both the given {@link Saml2Error} and the errors from the result
63+
*
64+
* @param error the {@link Saml2Error} to append
65+
* @return a new {@link Saml2ResponseValidatorResult} for further reporting
66+
*/
67+
public Saml2ResponseValidatorResult concat(Saml2Error error) {
68+
Assert.notNull(error, "error cannot be null");
69+
Collection<Saml2Error> errors = new ArrayList<>(this.errors);
70+
errors.add(error);
71+
return failure(errors);
72+
}
73+
74+
/**
75+
* Return a new {@link Saml2ResponseValidatorResult} that contains
76+
* the errors from the given {@link Saml2ResponseValidatorResult} as well
77+
* as this result.
78+
*
79+
* @param result the {@link Saml2ResponseValidatorResult} to merge with this one
80+
* @return a new {@link Saml2ResponseValidatorResult} for further reporting
81+
*/
82+
public Saml2ResponseValidatorResult concat(Saml2ResponseValidatorResult result) {
83+
Assert.notNull(result, "result cannot be null");
84+
Collection<Saml2Error> errors = new ArrayList<>(this.errors);
85+
errors.addAll(result.getErrors());
86+
return failure(errors);
87+
}
88+
89+
/**
90+
* Construct a successful {@link Saml2ResponseValidatorResult}
91+
*
92+
* @return an {@link Saml2ResponseValidatorResult} with no errors
93+
*/
94+
public static Saml2ResponseValidatorResult success() {
95+
return NO_ERRORS;
96+
}
97+
98+
/**
99+
* Construct a failure {@link Saml2ResponseValidatorResult} with the provided detail
100+
*
101+
* @param errors the list of errors
102+
* @return an {@link Saml2ResponseValidatorResult} with the errors specified
103+
*/
104+
public static Saml2ResponseValidatorResult failure(Saml2Error... errors) {
105+
return failure(Arrays.asList(errors));
106+
}
107+
108+
/**
109+
* Construct a failure {@link Saml2ResponseValidatorResult} with the provided detail
110+
*
111+
* @param errors the list of errors
112+
* @return an {@link Saml2ResponseValidatorResult} with the errors specified
113+
*/
114+
public static Saml2ResponseValidatorResult failure(Collection<Saml2Error> errors) {
115+
if (errors.isEmpty()) {
116+
return NO_ERRORS;
117+
}
118+
119+
return new Saml2ResponseValidatorResult(errors);
120+
}
121+
}

0 commit comments

Comments
 (0)