use of org.springframework.security.oauth2.server.resource.BearerTokenError in project spring-security by spring-projects.
the class ServerBearerTokenAuthenticationConverterTests method resolveWhenValidHeaderIsEmptyStringThenTokenIsResolved.
// gh-7011
@Test
public void resolveWhenValidHeaderIsEmptyStringThenTokenIsResolved() {
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/").header(HttpHeaders.AUTHORIZATION, "Bearer ");
// @formatter:off
assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request)).satisfies((ex) -> {
BearerTokenError error = (BearerTokenError) ex.getError();
assertThat(error.getErrorCode()).isEqualTo(BearerTokenErrorCodes.INVALID_TOKEN);
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
assertThat(error.getHttpStatus()).isEqualTo(HttpStatus.UNAUTHORIZED);
});
// @formatter:on
}
use of org.springframework.security.oauth2.server.resource.BearerTokenError in project midpoint by Evolveum.
the class OidcResourceServerModuleAuthentication method getRealmFroHeader.
public String getRealmFroHeader(AuthenticationException authException) {
Map<String, String> parameters = new LinkedHashMap<>();
if (authException instanceof OAuth2AuthenticationException) {
OAuth2Error error = ((OAuth2AuthenticationException) authException).getError();
parameters.put("error", error.getErrorCode());
if (org.springframework.util.StringUtils.hasText(error.getDescription())) {
parameters.put("error_description", error.getDescription());
}
if (org.springframework.util.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());
}
}
}
StringBuilder wwwAuthenticate = new StringBuilder(super.getRealmFroHeader(authException));
if (!parameters.isEmpty()) {
parameters.forEach((key, value) -> {
wwwAuthenticate.append(", ");
wwwAuthenticate.append(key).append("=\"").append(value).append("\"");
});
}
return wwwAuthenticate.toString();
}
use of org.springframework.security.oauth2.server.resource.BearerTokenError in project dhis2-core by dhis2.
the class CrudControllerAdvice method handleOAuth2AuthenticationException.
@ExceptionHandler(OAuth2AuthenticationException.class)
@ResponseBody
public WebMessage handleOAuth2AuthenticationException(OAuth2AuthenticationException ex) {
OAuth2Error error = ex.getError();
if (error instanceof BearerTokenError) {
BearerTokenError bearerTokenError = (BearerTokenError) error;
HttpStatus status = ((BearerTokenError) error).getHttpStatus();
return createWebMessage(bearerTokenError.getErrorCode(), bearerTokenError.getDescription(), Status.ERROR, status);
}
return unauthorized(ex.getMessage());
}
use of org.springframework.security.oauth2.server.resource.BearerTokenError in project spring-security by spring-projects.
the class BearerTokenAuthenticationEntryPointTests method commenceWhenInvalidRequestErrorThenStatus400AndHeaderWithError.
@Test
public void commenceWhenInvalidRequestErrorThenStatus400AndHeaderWithError() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
BearerTokenError error = new BearerTokenError(BearerTokenErrorCodes.INVALID_REQUEST, HttpStatus.BAD_REQUEST, null, null);
this.authenticationEntryPoint.commence(request, response, new OAuth2AuthenticationException(error));
assertThat(response.getStatus()).isEqualTo(400);
assertThat(response.getHeader("WWW-Authenticate")).isEqualTo("Bearer error=\"invalid_request\"");
}
use of org.springframework.security.oauth2.server.resource.BearerTokenError in project spring-security by spring-projects.
the class BearerTokenAuthenticationEntryPointTests method commenceWhenInsufficientScopeErrorThenStatus403AndHeaderWithErrorAndScope.
@Test
public void commenceWhenInsufficientScopeErrorThenStatus403AndHeaderWithErrorAndScope() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
BearerTokenError error = new BearerTokenError(BearerTokenErrorCodes.INSUFFICIENT_SCOPE, HttpStatus.FORBIDDEN, null, null, "test.read test.write");
this.authenticationEntryPoint.commence(request, response, new OAuth2AuthenticationException(error));
assertThat(response.getStatus()).isEqualTo(403);
assertThat(response.getHeader("WWW-Authenticate")).isEqualTo("Bearer error=\"insufficient_scope\", scope=\"test.read test.write\"");
}
Aggregations