Skip to content

Commit 387f765

Browse files
committed
Catch Malformed BearerTokenError Descriptions
Fixes gh-7549
1 parent 0ac5f54 commit 387f765

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtReactiveAuthenticationManager.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@
4040
* @since 5.1
4141
*/
4242
public final class JwtReactiveAuthenticationManager implements ReactiveAuthenticationManager {
43+
private final ReactiveJwtDecoder jwtDecoder;
44+
4345
private Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter
4446
= new ReactiveJwtAuthenticationConverterAdapter(new JwtAuthenticationConverter());
4547

46-
private final ReactiveJwtDecoder jwtDecoder;
48+
private static final OAuth2Error DEFAULT_INVALID_TOKEN =
49+
invalidToken("An error occurred while attempting to decode the Jwt: Invalid token");
4750

4851
public JwtReactiveAuthenticationManager(ReactiveJwtDecoder jwtDecoder) {
4952
Assert.notNull(jwtDecoder, "jwtDecoder cannot be null");
@@ -80,10 +83,15 @@ private OAuth2AuthenticationException onError(JwtException e) {
8083
}
8184

8285
private static OAuth2Error invalidToken(String message) {
83-
return new BearerTokenError(
84-
BearerTokenErrorCodes.INVALID_TOKEN,
85-
HttpStatus.UNAUTHORIZED,
86-
message,
87-
"https://tools.ietf.org/html/rfc6750#section-3.1");
86+
try {
87+
return new BearerTokenError(
88+
BearerTokenErrorCodes.INVALID_TOKEN,
89+
HttpStatus.UNAUTHORIZED,
90+
message,
91+
"https://tools.ietf.org/html/rfc6750#section-3.1");
92+
} catch (IllegalArgumentException malformed) {
93+
// some third-party library error messages are not suitable for RFC 6750's error message charset
94+
return DEFAULT_INVALID_TOKEN;
95+
}
8896
}
8997
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtReactiveAuthenticationManagerTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ public void authenticateWhenJwtExceptionThenOAuth2AuthenticationException() {
8888
.isInstanceOf(OAuth2AuthenticationException.class);
8989
}
9090

91+
// gh-7549
92+
@Test
93+
public void authenticateWhenDecoderThrowsIncompatibleErrorMessageThenWrapsWithGenericOne() {
94+
BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");
95+
when(this.jwtDecoder.decode(token.getToken())).thenThrow(new JwtException("with \"invalid\" chars"));
96+
97+
assertThatCode(() -> this.manager.authenticate(token).block())
98+
.isInstanceOf(OAuth2AuthenticationException.class)
99+
.hasFieldOrPropertyWithValue(
100+
"error.description",
101+
"An error occurred while attempting to decode the Jwt: Invalid token");
102+
}
103+
91104
@Test
92105
public void authenticateWhenNotJwtExceptionThenPropagates() {
93106
BearerTokenAuthenticationToken token = new BearerTokenAuthenticationToken("token-1");

0 commit comments

Comments
 (0)