Skip to content

Commit aefd2d4

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. Also see gh-10337 Closes gh-10337
1 parent 775bf91 commit aefd2d4

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

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

+2-2
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.
@@ -54,7 +54,7 @@ public JwtClaimValidator(String claim, Predicate<T> test) {
5454
Assert.notNull(test, "test can not be null");
5555
this.claim = claim;
5656
this.test = test;
57-
this.error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST, "The " + this.claim + " claim is not valid",
57+
this.error = new OAuth2Error(OAuth2ErrorCodes.INVALID_TOKEN, "The " + this.claim + " claim is not valid",
5858
"https://tools.ietf.org/html/rfc6750#section-3.1");
5959
}
6060

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

+7-1
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.
@@ -16,10 +16,14 @@
1616

1717
package org.springframework.security.oauth2.jwt;
1818

19+
import java.util.Collection;
20+
import java.util.Objects;
1921
import java.util.function.Predicate;
2022

2123
import org.junit.Test;
2224

25+
import org.springframework.security.oauth2.core.OAuth2Error;
26+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2327
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
2428

2529
import static org.assertj.core.api.Assertions.assertThat;
@@ -45,7 +49,9 @@ public void validateWhenClaimPassesTheTestThenReturnsSuccess() {
4549
@Test
4650
public void validateWhenClaimFailsTheTestThenReturnsFailure() {
4751
Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.ISS, "http://abc").build();
52+
Collection<OAuth2Error> details = this.validator.validate(jwt).getErrors();
4853
assertThat(this.validator.validate(jwt).getErrors().isEmpty()).isFalse();
54+
assertThat(details).allMatch((error) -> Objects.equals(error.getErrorCode(), OAuth2ErrorCodes.INVALID_TOKEN));
4955
}
5056

5157
@Test

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

+4-1
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.
@@ -23,6 +23,7 @@
2323
import java.util.Collection;
2424
import java.util.Collections;
2525
import java.util.Map;
26+
import java.util.Objects;
2627
import java.util.stream.Collectors;
2728

2829
import org.junit.Test;
@@ -64,6 +65,7 @@ public void validateWhenJwtIsExpiredThenErrorMessageIndicatesExpirationTime() {
6465
.collect(Collectors.toList());
6566
// @formatter:on
6667
assertThat(messages).contains("Jwt expired at " + oneHourAgo);
68+
assertThat(details).allMatch((error) -> Objects.equals(error.getErrorCode(), OAuth2ErrorCodes.INVALID_TOKEN));
6769
}
6870

6971
@Test
@@ -78,6 +80,7 @@ public void validateWhenJwtIsTooEarlyThenErrorMessageIndicatesNotBeforeTime() {
7880
.collect(Collectors.toList());
7981
// @formatter:on
8082
assertThat(messages).contains("Jwt used before " + oneHourFromNow);
83+
assertThat(details).allMatch((error) -> Objects.equals(error.getErrorCode(), OAuth2ErrorCodes.INVALID_TOKEN));
8184
}
8285

8386
@Test

0 commit comments

Comments
 (0)