/** * Constructs an {@code OAuth2AuthenticationException} using the provided parameters. * * @param error the {@link OAuth2Error OAuth 2.0 Error} */ public OAuth2AuthenticationException(OAuth2Error error) { this(error, error.getDescription()); }
@Override public String toString() { return "[" + this.getErrorCode() + "] " + (this.getDescription() != null ? this.getDescription() : ""); } }
@Override public Map<String, String> convert(OAuth2Error oauth2Error) { Map<String, String> parameters = new HashMap<>(); parameters.put(OAuth2ParameterNames.ERROR, oauth2Error.getErrorCode()); if (StringUtils.hasText(oauth2Error.getDescription())) { parameters.put(OAuth2ParameterNames.ERROR_DESCRIPTION, oauth2Error.getDescription()); } if (StringUtils.hasText(oauth2Error.getUri())) { parameters.put(OAuth2ParameterNames.ERROR_URI, oauth2Error.getUri()); } return parameters; } }
private Map<String, String> createParameters(AuthenticationException authException) { Map<String, String> parameters = new LinkedHashMap<>(); if (this.realmName != null) { parameters.put("realm", this.realmName); } if (authException instanceof OAuth2AuthenticationException) { OAuth2Error error = ((OAuth2AuthenticationException) authException).getError(); parameters.put("error", error.getErrorCode()); if (StringUtils.hasText(error.getDescription())) { parameters.put("error_description", error.getDescription()); } if (StringUtils.hasText(error.getUri())) { parameters.put("error_uri", error.getUri()); } if (error instanceof BearerTokenError) { BearerTokenError bearerTokenError = (BearerTokenError) error; if (StringUtils.hasText(bearerTokenError.getScope())) { parameters.put("scope", bearerTokenError.getScope()); } } } return parameters; }
private Jwt validateJwt(Jwt jwt){ OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt); if (result.hasErrors()) { String description = result.getErrors().iterator().next().getDescription(); throw new JwtValidationException( String.format(DECODING_ERROR_MESSAGE_TEMPLATE, description), result.getErrors()); } return jwt; } }
private Jwt validateJwt(Jwt jwt) { OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt); if ( result.hasErrors() ) { String message = result.getErrors().iterator().next().getDescription(); throw new JwtValidationException(message, result.getErrors()); } return jwt; }
if (StringUtils.hasText(error.getDescription())) { parameters.put("error_description", error.getDescription());
@Test public void constructorWhenAllParametersProvidedAndValidThenCreated() { OAuth2Error error = new OAuth2Error(ERROR_CODE, ERROR_DESCRIPTION, ERROR_URI); assertThat(error.getErrorCode()).isEqualTo(ERROR_CODE); assertThat(error.getDescription()).isEqualTo(ERROR_DESCRIPTION); assertThat(error.getUri()).isEqualTo(ERROR_URI); } }
@Test public void readInternalWhenErrorResponseThenReadOAuth2Error() throws Exception { String errorResponse = "{\n" + " \"error\": \"unauthorized_client\",\n" + " \"error_description\": \"The client is not authorized\",\n" + " \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n" + "}\n"; MockClientHttpResponse response = new MockClientHttpResponse( errorResponse.getBytes(), HttpStatus.BAD_REQUEST); OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response); assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client"); assertThat(oauth2Error.getDescription()).isEqualTo("The client is not authorized"); assertThat(oauth2Error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6749#section-5.2"); }
userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri()); errorDetails.append(", Error Code: ").append(oauth2Error.getErrorCode()); if (oauth2Error.getDescription() != null) { errorDetails.append(", Error Description: ").append(oauth2Error.getDescription());
@Test public void buildErrorResponseWhenAllAttributesProvidedThenAllAttributesAreSet() { OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.error(ERROR_CODE) .errorDescription(ERROR_DESCRIPTION) .errorUri(ERROR_URI) .redirectUri(REDIRECT_URI) .state(STATE) .build(); assertThat(authorizationResponse.getError().getErrorCode()).isEqualTo(ERROR_CODE); assertThat(authorizationResponse.getError().getDescription()).isEqualTo(ERROR_DESCRIPTION); assertThat(authorizationResponse.getError().getUri()).isEqualTo(ERROR_URI); assertThat(authorizationResponse.getRedirectUri()).isEqualTo(REDIRECT_URI); assertThat(authorizationResponse.getState()).isEqualTo(STATE); }
.fromUriString(authorizationResponse.getRedirectUri()) .queryParam(OAuth2ParameterNames.ERROR, error.getErrorCode()); if (!StringUtils.isEmpty(error.getDescription())) { uriBuilder.queryParam(OAuth2ParameterNames.ERROR_DESCRIPTION, error.getDescription());
/** * Decode and validate the * <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer Token</a>. * * @param authentication the authentication request object. * * @return A successful authentication * @throws AuthenticationException if authentication failed for some reason */ @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { BearerTokenAuthenticationToken bearer = (BearerTokenAuthenticationToken) authentication; Jwt jwt; try { jwt = this.jwtDecoder.decode(bearer.getToken()); } catch (JwtException failed) { OAuth2Error invalidToken = invalidToken(failed.getMessage()); throw new OAuth2AuthenticationException(invalidToken, invalidToken.getDescription(), failed); } AbstractAuthenticationToken token = this.jwtAuthenticationConverter.convert(jwt); token.setDetails(bearer.getDetails()); return token; }
/** * Constructs an {@code OAuth2AuthenticationException} using the provided parameters. * * @param error the {@link OAuth2Error OAuth 2.0 Error} */ public OAuth2AuthenticationException(OAuth2Error error) { this(error, error.getDescription()); }
@Override public String toString() { return "[" + this.getErrorCode() + "] " + (this.getDescription() != null ? this.getDescription() : ""); } }
@Override public String toString() { return "[" + this.getErrorCode() + "] " + (this.getDescription() != null ? this.getDescription() : ""); } }
@Override public Map<String, String> convert(OAuth2Error oauth2Error) { Map<String, String> parameters = new HashMap<>(); parameters.put(OAuth2ParameterNames.ERROR, oauth2Error.getErrorCode()); if (StringUtils.hasText(oauth2Error.getDescription())) { parameters.put(OAuth2ParameterNames.ERROR_DESCRIPTION, oauth2Error.getDescription()); } if (StringUtils.hasText(oauth2Error.getUri())) { parameters.put(OAuth2ParameterNames.ERROR_URI, oauth2Error.getUri()); } return parameters; } }
@Override public Map<String, String> convert(OAuth2Error oauth2Error) { Map<String, String> parameters = new HashMap<>(); parameters.put(OAuth2ParameterNames.ERROR, oauth2Error.getErrorCode()); if (StringUtils.hasText(oauth2Error.getDescription())) { parameters.put(OAuth2ParameterNames.ERROR_DESCRIPTION, oauth2Error.getDescription()); } if (StringUtils.hasText(oauth2Error.getUri())) { parameters.put(OAuth2ParameterNames.ERROR_URI, oauth2Error.getUri()); } return parameters; } }
private Jwt validateJwt(Jwt jwt){ OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt); if (result.hasErrors()) { String description = result.getErrors().iterator().next().getDescription(); throw new JwtValidationException( String.format(DECODING_ERROR_MESSAGE_TEMPLATE, description), result.getErrors()); } return jwt; }
private Jwt validateJwt(Jwt jwt) { OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt); if ( result.hasErrors() ) { String message = result.getErrors().iterator().next().getDescription(); throw new JwtValidationException(message, result.getErrors()); } return jwt; }