|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2002-2018 the original author or authors.
|
|
|
+ * Copyright 2002-2020 the original author or authors.
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -18,20 +18,16 @@ package org.springframework.security.oauth2.server.resource.authentication;
|
|
|
import java.util.Collection;
|
|
|
|
|
|
import org.springframework.core.convert.converter.Converter;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
-import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
-import org.springframework.security.oauth2.core.OAuth2Error;
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
import org.springframework.security.oauth2.jwt.JwtDecoder;
|
|
|
import org.springframework.security.oauth2.jwt.JwtException;
|
|
|
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
|
|
-import org.springframework.security.oauth2.server.resource.BearerTokenError;
|
|
|
-import org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes;
|
|
|
+import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
/**
|
|
@@ -63,9 +59,6 @@ public final class JwtAuthenticationProvider implements AuthenticationProvider {
|
|
|
|
|
|
private Converter<Jwt, ? extends AbstractAuthenticationToken> jwtAuthenticationConverter = new JwtAuthenticationConverter();
|
|
|
|
|
|
- private static final OAuth2Error DEFAULT_INVALID_TOKEN =
|
|
|
- invalidToken("An error occurred while attempting to decode the Jwt: Invalid token");
|
|
|
-
|
|
|
public JwtAuthenticationProvider(JwtDecoder jwtDecoder) {
|
|
|
Assert.notNull(jwtDecoder, "jwtDecoder cannot be null");
|
|
|
this.jwtDecoder = jwtDecoder;
|
|
@@ -88,8 +81,7 @@ public final class JwtAuthenticationProvider implements AuthenticationProvider {
|
|
|
try {
|
|
|
jwt = this.jwtDecoder.decode(bearer.getToken());
|
|
|
} catch (JwtException failed) {
|
|
|
- OAuth2Error invalidToken = invalidToken(failed.getMessage());
|
|
|
- throw new OAuth2AuthenticationException(invalidToken, invalidToken.getDescription(), failed);
|
|
|
+ throw new InvalidBearerTokenException(failed.getMessage(), failed);
|
|
|
}
|
|
|
|
|
|
AbstractAuthenticationToken token = this.jwtAuthenticationConverter.convert(jwt);
|
|
@@ -112,17 +104,4 @@ public final class JwtAuthenticationProvider implements AuthenticationProvider {
|
|
|
Assert.notNull(jwtAuthenticationConverter, "jwtAuthenticationConverter cannot be null");
|
|
|
this.jwtAuthenticationConverter = jwtAuthenticationConverter;
|
|
|
}
|
|
|
-
|
|
|
- private static OAuth2Error invalidToken(String message) {
|
|
|
- try {
|
|
|
- return new BearerTokenError(
|
|
|
- BearerTokenErrorCodes.INVALID_TOKEN,
|
|
|
- HttpStatus.UNAUTHORIZED,
|
|
|
- message,
|
|
|
- "https://tools.ietf.org/html/rfc6750#section-3.1");
|
|
|
- } catch (IllegalArgumentException malformed) {
|
|
|
- // some third-party library error messages are not suitable for RFC 6750's error message charset
|
|
|
- return DEFAULT_INVALID_TOKEN;
|
|
|
- }
|
|
|
- }
|
|
|
}
|