Skip to content

Commit 401e237

Browse files
vpavicjzheaux
authored andcommitted
Introduce JwtAudienceValidator
Signed-off-by: Vedran Pavic <[email protected]>
1 parent ab52fd8 commit 401e237

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2025 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.oauth2.jwt;
18+
19+
import java.util.Collection;
20+
21+
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
22+
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* Validates that the "aud" claim in a {@link Jwt} matches a configured value.
27+
*
28+
* @author Vedran Pavic
29+
* @since 6.5
30+
*/
31+
public final class JwtAudienceValidator implements OAuth2TokenValidator<Jwt> {
32+
33+
private final JwtClaimValidator<Collection<String>> validator;
34+
35+
/**
36+
* Constructs a {@link JwtAudienceValidator} using the provided parameters
37+
* @param audience - The audience that each {@link Jwt} should have.
38+
*/
39+
public JwtAudienceValidator(String audience) {
40+
Assert.notNull(audience, "audience cannot be null");
41+
this.validator = new JwtClaimValidator<>(JwtClaimNames.AUD,
42+
(claimValue) -> (claimValue != null) && claimValue.contains(audience));
43+
}
44+
45+
@Override
46+
public OAuth2TokenValidatorResult validate(Jwt token) {
47+
Assert.notNull(token, "token cannot be null");
48+
return this.validator.validate(token);
49+
}
50+
51+
}

Diff for: oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtValidators.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -182,8 +182,7 @@ public AtJwtBuilder issuer(String issuer) {
182182
* @return the {@link AtJwtBuilder} for further configuration
183183
*/
184184
public AtJwtBuilder audience(String audience) {
185-
return validators((v) -> v.put(JwtClaimNames.AUD,
186-
require(JwtClaimNames.AUD).satisfies((jwt) -> jwt.getAudience().contains(audience))));
185+
return validators((v) -> v.put(JwtClaimNames.AUD, new JwtAudienceValidator(audience)));
187186
}
188187

189188
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2025 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.oauth2.jwt;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link JwtAudienceValidator}.
29+
*
30+
* @author Vedran Pavic
31+
*/
32+
class JwtAudienceValidatorTests {
33+
34+
private final JwtAudienceValidator validator = new JwtAudienceValidator("audience");
35+
36+
@Test
37+
void givenJwtWithMatchingAudienceThenShouldValidate() {
38+
Jwt jwt = TestJwts.jwt().audience(List.of("audience")).build();
39+
OAuth2TokenValidatorResult result = this.validator.validate(jwt);
40+
assertThat(result).isEqualTo(OAuth2TokenValidatorResult.success());
41+
}
42+
43+
@Test
44+
void givenJwtWithoutMatchingAudienceThenShouldValidate() {
45+
Jwt jwt = TestJwts.jwt().audience(List.of("other")).build();
46+
OAuth2TokenValidatorResult result = this.validator.validate(jwt);
47+
assertThat(result.hasErrors()).isTrue();
48+
}
49+
50+
@Test
51+
void givenJwtWithoutAudienceThenShouldValidate() {
52+
Jwt jwt = TestJwts.jwt().audience(null).build();
53+
OAuth2TokenValidatorResult result = this.validator.validate(jwt);
54+
assertThat(result.hasErrors()).isTrue();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)