Skip to content

Commit 431cd60

Browse files
zeeshanadnanjzheaux
authored andcommitted
Add JwtClaimValidator
Fixes gh-7860
1 parent e09edf6 commit 431cd60

File tree

3 files changed

+142
-17
lines changed

3 files changed

+142
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
package org.springframework.security.oauth2.jwt;
17+
18+
import org.springframework.security.oauth2.core.OAuth2Error;
19+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
20+
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
21+
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
22+
import org.springframework.util.Assert;
23+
24+
import java.util.function.Predicate;
25+
26+
/**
27+
* Validates a claim in a {@link Jwt} against a provided {@link java.util.function.Predicate}
28+
*
29+
* @author Zeeshan Adnan
30+
* @since 5.3
31+
*/
32+
public final class JwtClaimValidator<T> implements OAuth2TokenValidator<Jwt> {
33+
34+
private final String claim;
35+
private final Predicate<T> test;
36+
private final OAuth2Error error;
37+
38+
/**
39+
* Constructs a {@link JwtClaimValidator} using the provided parameters
40+
*
41+
* @param claim - is the name of the claim in {@link Jwt} to validate.
42+
* @param test - is the predicate function for the claim to test against.
43+
*/
44+
public JwtClaimValidator(String claim, Predicate<T> test) {
45+
Assert.notNull(claim, "claim can not be null");
46+
Assert.notNull(test, "test can not be null");
47+
this.claim = claim;
48+
this.test = test;
49+
this.error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST,
50+
"The " + this.claim + " claim is not valid",
51+
"https://tools.ietf.org/html/rfc6750#section-3.1");
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
@Override
58+
public OAuth2TokenValidatorResult validate(Jwt token) {
59+
Assert.notNull(token, "token cannot be null");
60+
T claimValue = token.getClaim(this.claim);
61+
if (test.test(claimValue)) {
62+
return OAuth2TokenValidatorResult.success();
63+
} else {
64+
return OAuth2TokenValidatorResult.failure(error);
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,26 +15,21 @@
1515
*/
1616
package org.springframework.security.oauth2.jwt;
1717

18-
import org.springframework.security.oauth2.core.OAuth2Error;
19-
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2018
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
2119
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
2220
import org.springframework.util.Assert;
2321

22+
import static org.springframework.security.oauth2.jwt.JwtClaimNames.ISS;
23+
2424
/**
2525
* Validates the "iss" claim in a {@link Jwt}, that is matches a configured value
2626
*
2727
* @author Josh Cummings
2828
* @since 5.1
2929
*/
3030
public final class JwtIssuerValidator implements OAuth2TokenValidator<Jwt> {
31-
private static OAuth2Error INVALID_ISSUER =
32-
new OAuth2Error(
33-
OAuth2ErrorCodes.INVALID_REQUEST,
34-
"This iss claim is not equal to the configured issuer",
35-
"https://tools.ietf.org/html/rfc6750#section-3.1");
3631

37-
private final String issuer;
32+
private final JwtClaimValidator<String> validator;
3833

3934
/**
4035
* Constructs a {@link JwtIssuerValidator} using the provided parameters
@@ -43,7 +38,7 @@ public final class JwtIssuerValidator implements OAuth2TokenValidator<Jwt> {
4338
*/
4439
public JwtIssuerValidator(String issuer) {
4540
Assert.notNull(issuer, "issuer cannot be null");
46-
this.issuer = issuer;
41+
this.validator = new JwtClaimValidator(ISS, issuer::equals);
4742
}
4843

4944
/**
@@ -52,12 +47,6 @@ public JwtIssuerValidator(String issuer) {
5247
@Override
5348
public OAuth2TokenValidatorResult validate(Jwt token) {
5449
Assert.notNull(token, "token cannot be null");
55-
56-
String tokenIssuer = token.getClaimAsString(JwtClaimNames.ISS);
57-
if (this.issuer.equals(tokenIssuer)) {
58-
return OAuth2TokenValidatorResult.success();
59-
} else {
60-
return OAuth2TokenValidatorResult.failure(INVALID_ISSUER);
61-
}
50+
return this.validator.validate(token);
6251
}
6352
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
package org.springframework.security.oauth2.jwt;
17+
18+
import org.junit.Test;
19+
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
20+
21+
import java.util.function.Predicate;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
25+
import static org.springframework.security.oauth2.jwt.JwtClaimNames.ISS;
26+
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
27+
28+
/**
29+
* Tests for {@link JwtClaimValidator}.
30+
*
31+
* @author Zeeshan Adnan
32+
*/
33+
public class JwtClaimValidatorTests {
34+
35+
private static final Predicate<String> test = claim -> claim.equals("http://test");
36+
private final JwtClaimValidator<String> validator = new JwtClaimValidator<>(ISS, test);
37+
38+
@Test
39+
public void validateWhenClaimPassesTheTestThenReturnsSuccess() {
40+
Jwt jwt = jwt().claim(ISS, "http://test").build();
41+
assertThat(validator.validate(jwt))
42+
.isEqualTo(OAuth2TokenValidatorResult.success());
43+
}
44+
45+
@Test
46+
public void validateWhenClaimFailsTheTestThenReturnsFailure() {
47+
Jwt jwt = jwt().claim(ISS, "http://abc").build();
48+
assertThat(validator.validate(jwt).getErrors().isEmpty())
49+
.isFalse();
50+
}
51+
52+
@Test
53+
public void validateWhenClaimIsNullThenThrowsIllegalArgumentException() {
54+
assertThatThrownBy(() -> new JwtClaimValidator<String>(null, test))
55+
.isInstanceOf(IllegalArgumentException.class);
56+
}
57+
58+
@Test
59+
public void validateWhenTestIsNullThenThrowsIllegalArgumentException(){
60+
assertThatThrownBy(() -> new JwtClaimValidator<>(ISS, null))
61+
.isInstanceOf(IllegalArgumentException.class);
62+
}
63+
64+
@Test
65+
public void validateWhenJwtIsNullThenThrowsIllegalArgumentException() {
66+
assertThatThrownBy(() -> validator.validate(null))
67+
.isInstanceOf(IllegalArgumentException.class);
68+
}
69+
}

0 commit comments

Comments
 (0)