Skip to content

Commit 606bf6b

Browse files
jason076jzheaux
authored andcommitted
Fix JwtClaimValidator wrong error code
Previously JwtClaimValidator returned the invalid_request error on claim validation failure. But validators have to return invalid_token errors on failure according to: https://datatracker.ietf.org/doc/html/rfc6750#section-3.1. Closes gh-10337
1 parent 5a47e17 commit 606bf6b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -49,7 +49,7 @@ public JwtClaimValidator(String claim, Predicate<T> test) {
4949
Assert.notNull(test, "test can not be null");
5050
this.claim = claim;
5151
this.test = test;
52-
this.error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST,
52+
this.error = new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN,
5353
"The " + this.claim + " claim is not valid",
5454
"https://tools.ietf.org/html/rfc6750#section-3.1");
5555
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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,7 +15,14 @@
1515
*/
1616
package org.springframework.security.oauth2.jwt;
1717

18+
import java.util.Collection;
19+
import java.util.Objects;
20+
import java.util.function.Predicate;
21+
1822
import org.junit.Test;
23+
24+
import org.springframework.security.oauth2.core.OAuth2Error;
25+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
1926
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
2027

2128
import java.util.function.Predicate;
@@ -44,9 +51,10 @@ public void validateWhenClaimPassesTheTestThenReturnsSuccess() {
4451

4552
@Test
4653
public void validateWhenClaimFailsTheTestThenReturnsFailure() {
47-
Jwt jwt = jwt().claim(ISS, "http://abc").build();
48-
assertThat(validator.validate(jwt).getErrors().isEmpty())
49-
.isFalse();
54+
Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.ISS, "http://abc").build();
55+
Collection<OAuth2Error> details = this.validator.validate(jwt).getErrors();
56+
assertThat(this.validator.validate(jwt).getErrors().isEmpty()).isFalse();
57+
assertThat(details).allMatch((error) -> Objects.equals(error.getErrorCode(), OAuth2ErrorCodes.INVALID_TOKEN));
5058
}
5159

5260
@Test

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

Lines changed: 4 additions & 1 deletion
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-2021 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.
@@ -22,6 +22,7 @@
2222
import java.util.Collection;
2323
import java.util.Collections;
2424
import java.util.Map;
25+
import java.util.Objects;
2526
import java.util.stream.Collectors;
2627

2728
import org.junit.Test;
@@ -60,6 +61,7 @@ public void validateWhenJwtIsExpiredThenErrorMessageIndicatesExpirationTime() {
6061
Collection<String> messages = details.stream().map(OAuth2Error::getDescription).collect(Collectors.toList());
6162

6263
assertThat(messages).contains("Jwt expired at " + oneHourAgo);
64+
assertThat(details).allMatch((error) -> Objects.equals(error.getErrorCode(), OAuth2ErrorCodes.INVALID_TOKEN));
6365
}
6466

6567
@Test
@@ -74,6 +76,7 @@ public void validateWhenJwtIsTooEarlyThenErrorMessageIndicatesNotBeforeTime() {
7476
Collection<String> messages = details.stream().map(OAuth2Error::getDescription).collect(Collectors.toList());
7577

7678
assertThat(messages).contains("Jwt used before " + oneHourFromNow);
79+
assertThat(details).allMatch((error) -> Objects.equals(error.getErrorCode(), OAuth2ErrorCodes.INVALID_TOKEN));
7780
}
7881

7982
@Test

0 commit comments

Comments
 (0)