Skip to content

Commit c70b65c

Browse files
committed
Favor URL.toExternalForm
Converts URLs to Strings before comparing them. Uses toString(), which delegates to toExternalForm(). Fixes: gh-6073
1 parent a32d19e commit c70b65c

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtIssuerValidator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class JwtIssuerValidator implements OAuth2TokenValidator<Jwt> {
3737
"This iss claim is not equal to the configured issuer",
3838
"https://tools.ietf.org/html/rfc6750#section-3.1");
3939

40-
private final URL issuer;
40+
private final String issuer;
4141

4242
/**
4343
* Constructs a {@link JwtIssuerValidator} using the provided parameters
@@ -48,7 +48,7 @@ public JwtIssuerValidator(String issuer) {
4848
Assert.notNull(issuer, "issuer cannot be null");
4949

5050
try {
51-
this.issuer = new URL(issuer);
51+
this.issuer = new URL(issuer).toString();
5252
} catch (MalformedURLException ex) {
5353
throw new IllegalArgumentException(
5454
"Invalid Issuer URL " + issuer + " : " + ex.getMessage(),
@@ -63,7 +63,8 @@ public JwtIssuerValidator(String issuer) {
6363
public OAuth2TokenValidatorResult validate(Jwt token) {
6464
Assert.notNull(token, "token cannot be null");
6565

66-
if (this.issuer.equals(token.getIssuer())) {
66+
String tokenIssuer = token.getClaimAsString(JwtClaimNames.ISS);
67+
if (this.issuer.equals(tokenIssuer)) {
6768
return OAuth2TokenValidatorResult.success();
6869
} else {
6970
return OAuth2TokenValidatorResult.failure(INVALID_ISSUER);

oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/JwtIssuerValidatorTests.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323

2424
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
2525
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
26-
import org.springframework.security.oauth2.jwt.Jwt;
27-
import org.springframework.security.oauth2.jwt.JwtClaimNames;
28-
import org.springframework.security.oauth2.jwt.JwtIssuerValidator;
2926

3027
import static org.assertj.core.api.Assertions.assertThat;
3128
import static org.assertj.core.api.Assertions.assertThatCode;
@@ -72,6 +69,19 @@ public void validateWhenIssuerMismatchesThenReturnsError() {
7269
assertThat(result.getErrors()).isNotEmpty();
7370
}
7471

72+
@Test
73+
public void validateWhenJwtHasNoIssuerThenReturnsError() {
74+
Jwt jwt = new Jwt(
75+
MOCK_TOKEN,
76+
MOCK_ISSUED_AT,
77+
MOCK_EXPIRES_AT,
78+
MOCK_HEADERS,
79+
Collections.singletonMap(JwtClaimNames.AUD, "https://aud"));
80+
81+
OAuth2TokenValidatorResult result = this.validator.validate(jwt);
82+
assertThat(result.getErrors()).isNotEmpty();
83+
}
84+
7585
@Test
7686
public void validateWhenJwtIsNullThenThrowsIllegalArgumentException() {
7787
assertThatCode(() -> this.validator.validate(null))

0 commit comments

Comments
 (0)