introduce api for handling token validation failed exception

This commit is contained in:
Konstantin Schaper
2020-10-08 09:58:51 +02:00
committed by René Pfeuffer
parent 5887c5c268
commit f2a53644b6
6 changed files with 102 additions and 8 deletions

View File

@@ -89,7 +89,7 @@ public final class JwtAccessTokenResolver implements AccessTokenResolver {
if (!validator.validate(accessToken)) {
String msg = createValidationFailedMessage(validator, accessToken);
LOG.debug(msg);
throw new AuthenticationException(msg);
throw new TokenValidationFailedException(validator, accessToken);
}
}

View File

@@ -55,16 +55,16 @@ public class ScmAtLeastOneSuccessfulStrategy extends AbstractAuthenticationStrat
}
@Override
public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException {
public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) {
final List<Throwable> throwables = threadLocal.get();
threadLocal.remove();
if (isAuthenticationSuccessful(aggregate)) {
return aggregate;
}
Optional<TokenExpiredException> tokenExpiredException = findTokenExpiredException(throwables);
Optional<? extends AuthenticationException> specializedException = findSpecializedException(throwables);
if (tokenExpiredException.isPresent()) {
throw tokenExpiredException.get();
if (specializedException.isPresent()) {
throw specializedException.get();
} else {
throw createAuthenticationException(token);
}
@@ -82,6 +82,18 @@ public class ScmAtLeastOneSuccessfulStrategy extends AbstractAuthenticationStrat
return throwables.stream().filter(t -> t instanceof TokenExpiredException).findFirst().map(t -> (TokenExpiredException) t);
}
private static Optional<AuthenticationException> findTokenValidationFailedException(List<Throwable> throwables) {
return throwables.stream().filter(t -> t instanceof TokenValidationFailedException).findFirst().map(t -> (TokenValidationFailedException) t);
}
private static Optional<? extends AuthenticationException> findSpecializedException(List<Throwable> throwables) {
Optional<TokenExpiredException> tokenExpiredException = findTokenExpiredException(throwables);
if (tokenExpiredException.isPresent()) {
return tokenExpiredException;
}
return findTokenValidationFailedException(throwables);
}
private static AuthenticationException createAuthenticationException(AuthenticationToken token) {
return new AuthenticationException("Authentication token of type [" + token.getClass() + "] " +
"could not be authenticated by any configured realms. Please ensure that at least one realm can " +