Search in sources :

Example 6 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project spring-security-oauth by spring-projects.

the class ClientCredentialsProviderTests method testInvalidCredentials.

@Test
@OAuth2ContextConfiguration(resource = InvalidClientCredentials.class, initialize = false)
public void testInvalidCredentials() throws Exception {
    context.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider() {

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    responseHeaders = response.getHeaders();
                    responseStatus = response.getStatusCode();
                }
            };
        }
    });
    try {
        context.getAccessToken();
        fail("Expected ResourceAccessException");
    } catch (Exception e) {
    // ignore
    }
    // System.err.println(responseHeaders);
    String header = responseHeaders.getFirst("WWW-Authenticate");
    assertTrue("Wrong header: " + header, header.contains("Basic realm"));
    assertEquals(HttpStatus.UNAUTHORIZED, responseStatus);
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) IOException(java.io.IOException) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) IOException(java.io.IOException) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 7 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project spring-security-oauth by spring-projects.

the class OAuth2ContextSetup method createRestTemplate.

private OAuth2RestTemplate createRestTemplate(OAuth2ProtectedResourceDetails resource, AccessTokenRequest request) {
    OAuth2ClientContext context = new DefaultOAuth2ClientContext(request);
    OAuth2RestTemplate client = new OAuth2RestTemplate(resource, context);
    setupConnectionFactory(client);
    client.setErrorHandler(new DefaultResponseErrorHandler() {

        // Pass errors through in response entity for status code analysis
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
        }
    });
    if (accessTokenProvider != null) {
        client.setAccessTokenProvider(accessTokenProvider);
    }
    return client;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) OAuth2ClientContext(org.springframework.security.oauth2.client.OAuth2ClientContext) IOException(java.io.IOException) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 8 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project spring-security-oauth by spring-projects.

the class OAuth2ErrorHandlerTests method testHandleGeneric403ErrorWithBody.

@Test
public // See https://github.com/spring-projects/spring-security-oauth/issues/387
void testHandleGeneric403ErrorWithBody() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    ClientHttpResponse response = new TestClientHttpResponse(headers, 403, new ByteArrayInputStream("{}".getBytes()));
    handler = new OAuth2ErrorHandler(new DefaultResponseErrorHandler(), resource);
    expected.expect(HttpClientErrorException.class);
    handler.handleError(response);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 9 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project spring-security-oauth by spring-projects.

the class AbstractResourceOwnerPasswordProviderTests method createAccessTokenProvider.

@Override
protected ResourceOwnerPasswordAccessTokenProvider createAccessTokenProvider() {
    ResourceOwnerPasswordAccessTokenProvider accessTokenProvider = new ResourceOwnerPasswordAccessTokenProvider() {

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            final ResponseErrorHandler errorHandler = super.getResponseErrorHandler();
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    errorHandler.handleError(response);
                }
            };
        }

        @Override
        protected ResponseExtractor<OAuth2AccessToken> getResponseExtractor() {
            final ResponseExtractor<OAuth2AccessToken> extractor = super.getResponseExtractor();
            return new ResponseExtractor<OAuth2AccessToken>() {

                public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    return extractor.extractData(response);
                }
            };
        }
    };
    return accessTokenProvider;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) ResponseExtractor(org.springframework.web.client.ResponseExtractor) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ResourceOwnerPasswordAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)

Example 10 with DefaultResponseErrorHandler

use of org.springframework.web.client.DefaultResponseErrorHandler in project spring-security-oauth by spring-projects.

the class AbstractClientCredentialsProviderTests method createAccessTokenProvider.

@Override
protected AccessTokenProvider createAccessTokenProvider() {
    return new ClientCredentialsAccessTokenProvider() {

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            final ResponseErrorHandler delegate = super.getResponseErrorHandler();
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    responseHeaders = response.getHeaders();
                    responseStatus = response.getStatusCode();
                    delegate.handleError(response);
                }
            };
        }
    };
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Aggregations

ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)13 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)13 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)10 IOException (java.io.IOException)8 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 ResponseExtractor (org.springframework.web.client.ResponseExtractor)6 BeforeOAuth2Context (org.springframework.security.oauth2.client.test.BeforeOAuth2Context)5 Test (org.junit.Test)4 ClientCredentialsAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider)4 ResponseEntity (org.springframework.http.ResponseEntity)3 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)3 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)3 ResourceOwnerPasswordAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)3 ResourceAccessException (org.springframework.web.client.ResourceAccessException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URI (java.net.URI)1 Builder (org.apache.http.client.config.RequestConfig.Builder)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 HttpContext (org.apache.http.protocol.HttpContext)1 HttpHeaders (org.springframework.http.HttpHeaders)1