Search in sources :

Example 11 with BearerTokenError

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
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) BearerTokenError(org.springframework.security.oauth2.server.resource.BearerTokenError) Test(org.junit.jupiter.api.Test)

Example 12 with BearerTokenError

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();
}
Also used : OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) BearerTokenError(org.springframework.security.oauth2.server.resource.BearerTokenError) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with BearerTokenError

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());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) BearerTokenError(org.springframework.security.oauth2.server.resource.BearerTokenError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with BearerTokenError

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\"");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BearerTokenError(org.springframework.security.oauth2.server.resource.BearerTokenError) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 15 with BearerTokenError

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\"");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BearerTokenError(org.springframework.security.oauth2.server.resource.BearerTokenError) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

BearerTokenError (org.springframework.security.oauth2.server.resource.BearerTokenError)24 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)20 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)6 LinkedHashMap (java.util.LinkedHashMap)3 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)3 Matcher (java.util.regex.Matcher)2 HttpStatus (org.springframework.http.HttpStatus)2 BearerTokenAuthenticationToken (org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken)2 BearerTokenError (com.nimbusds.oauth2.sdk.token.BearerTokenError)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1