Search in sources :

Example 6 with BeforeOAuth2Context

use of org.springframework.security.oauth2.client.test.BeforeOAuth2Context in project spring-security-oauth by spring-projects.

the class AbstractEmptyAuthorizationCodeProviderTests method setupAccessTokenProvider.

@BeforeOAuth2Context
public void setupAccessTokenProvider() {
    accessTokenProvider = new AuthorizationCodeAccessTokenProvider() {

        private ResponseExtractor<OAuth2AccessToken> extractor = super.getResponseExtractor();

        private ResponseExtractor<ResponseEntity<Void>> authExtractor = super.getAuthorizationResponseExtractor();

        private ResponseErrorHandler errorHandler = super.getResponseErrorHandler();

        @Override
        protected ResponseErrorHandler 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() {
            return new ResponseExtractor<OAuth2AccessToken>() {

                public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException {
                    try {
                        response.getHeaders();
                        response.getStatusCode();
                        tokenEndpointResponse = response;
                        return extractor.extractData(response);
                    } catch (ResourceAccessException e) {
                        return null;
                    }
                }
            };
        }

        @Override
        protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
            return new ResponseExtractor<ResponseEntity<Void>>() {

                public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    return authExtractor.extractData(response);
                }
            };
        }
    };
    context.setAccessTokenProvider(getAccessTokenProvider());
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ResponseExtractor(org.springframework.web.client.ResponseExtractor) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) ResponseEntity(org.springframework.http.ResponseEntity) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) BeforeOAuth2Context(org.springframework.security.oauth2.client.test.BeforeOAuth2Context)

Example 7 with BeforeOAuth2Context

use of org.springframework.security.oauth2.client.test.BeforeOAuth2Context in project spring-security-oauth by spring-projects.

the class AbstractResourceOwnerPasswordProviderTests method setupAccessTokenProvider.

@BeforeOAuth2Context
public void setupAccessTokenProvider() {
    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);
                }
            };
        }
    };
    accessTokenProvider.setRequestFactory(context.getRestTemplate().getRequestFactory());
    context.setAccessTokenProvider(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) BeforeOAuth2Context(org.springframework.security.oauth2.client.test.BeforeOAuth2Context)

Example 8 with BeforeOAuth2Context

use of org.springframework.security.oauth2.client.test.BeforeOAuth2Context in project spring-security-oauth by spring-projects.

the class AbstractIntegrationTests method fixPaths.

@BeforeOAuth2Context
public void fixPaths() {
    String prefix = server.getServletPrefix();
    http.setPort(port);
    http.setPrefix(prefix);
    BaseOAuth2ProtectedResourceDetails resource = (BaseOAuth2ProtectedResourceDetails) context.getResource();
    resource.setAccessTokenUri(http.getUrl(tokenPath()));
    if (resource instanceof AbstractRedirectResourceDetails) {
        ((AbstractRedirectResourceDetails) resource).setUserAuthorizationUri(http.getUrl(authorizePath()));
    }
    if (resource instanceof ImplicitResourceDetails) {
        resource.setAccessTokenUri(http.getUrl(authorizePath()));
    }
    if (resource instanceof ResourceOwnerPasswordResourceDetails) {
        ((ResourceOwnerPasswordResourceDetails) resource).setUsername(security.getUser().getName());
        ((ResourceOwnerPasswordResourceDetails) resource).setPassword(security.getUser().getPassword());
    }
}
Also used : AbstractRedirectResourceDetails(org.springframework.security.oauth2.client.token.grant.redirect.AbstractRedirectResourceDetails) ResourceOwnerPasswordResourceDetails(org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails) ImplicitResourceDetails(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) BeforeOAuth2Context(org.springframework.security.oauth2.client.test.BeforeOAuth2Context)

Aggregations

BeforeOAuth2Context (org.springframework.security.oauth2.client.test.BeforeOAuth2Context)7 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)5 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)5 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)5 ResponseExtractor (org.springframework.web.client.ResponseExtractor)5 IOException (java.io.IOException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)3 ResourceOwnerPasswordAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)2 ResourceAccessException (org.springframework.web.client.ResourceAccessException)2 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1 RunBefores (org.junit.internal.runners.statements.RunBefores)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 Statement (org.junit.runners.model.Statement)1 TestClass (org.junit.runners.model.TestClass)1 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)1 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)1 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)1 AccessTokenProvider (org.springframework.security.oauth2.client.token.AccessTokenProvider)1