Search in sources :

Example 26 with AccessTokenRequest

use of org.springframework.security.oauth2.client.token.AccessTokenRequest in project spring-security-oauth by spring-projects.

the class AuthorizationCodeProviderTests method testWrongRedirectUri.

@Test
@OAuth2ContextConfiguration(resource = MyLessTrustedClient.class, initialize = false)
public void testWrongRedirectUri() throws Exception {
    approveAccessTokenGrant("http://anywhere", true);
    AccessTokenRequest request = context.getAccessTokenRequest();
    // The redirect is stored in the preserved state...
    context.getOAuth2ClientContext().setPreservedState(request.getStateKey(), "http://nowhere");
    // Finally everything is in place for the grant to happen...
    try {
        assertNotNull(context.getAccessToken());
        fail("Expected RedirectMismatchException");
    } catch (RedirectMismatchException e) {
    // expected
    }
    assertEquals(HttpStatus.BAD_REQUEST, tokenEndpointResponse.getStatusCode());
}
Also used : RedirectMismatchException(org.springframework.security.oauth2.common.exceptions.RedirectMismatchException) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 27 with AccessTokenRequest

use of org.springframework.security.oauth2.client.token.AccessTokenRequest in project spring-security-oauth by spring-projects.

the class AuthorizationCodeProviderTests method testSuccessfulFlowWithRegisteredRedirect.

@Test
@OAuth2ContextConfiguration(resource = MyClientWithRegisteredRedirect.class, initialize = false)
public void testSuccessfulFlowWithRegisteredRedirect() throws Exception {
    // Once the request is ready and approved, we can continue with the access token
    approveAccessTokenGrant(null, true);
    // Finally everything is in place for the grant to happen...
    assertNotNull(context.getAccessToken());
    AccessTokenRequest request = context.getAccessTokenRequest();
    assertNotNull(request.getAuthorizationCode());
    assertEquals(HttpStatus.OK, serverRunning.getStatusCode("/sparklr2/photos?format=json"));
}
Also used : AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 28 with AccessTokenRequest

use of org.springframework.security.oauth2.client.token.AccessTokenRequest 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 29 with AccessTokenRequest

use of org.springframework.security.oauth2.client.token.AccessTokenRequest in project spring-security-oauth by spring-projects.

the class OAuth2ContextSetup method starting.

@Override
public void starting(FrameworkMethod method) {
    if (resource != null) {
        logger.info("Starting OAuth2 context for: " + resource);
        AccessTokenRequest request = new DefaultAccessTokenRequest();
        request.setAll(parameters);
        client = createRestTemplate(resource, request);
        if (initializeAccessToken) {
            this.accessToken = null;
            this.accessToken = getAccessToken();
        }
        savedClient = clientHolder.getRestTemplate();
        clientHolder.setRestTemplate(client);
    }
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)

Example 30 with AccessTokenRequest

use of org.springframework.security.oauth2.client.token.AccessTokenRequest in project spring-security-oauth by spring-projects.

the class OAuth2ClientConfiguration method accessTokenRequest.

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
protected AccessTokenRequest accessTokenRequest(@Value("#{request.parameterMap}") Map<String, String[]> parameters, @Value("#{request.getAttribute('currentUri')}") String currentUri) {
    DefaultAccessTokenRequest request = new DefaultAccessTokenRequest(parameters);
    request.setCurrentUri(currentUri);
    return request;
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Scope(org.springframework.context.annotation.Scope) Bean(org.springframework.context.annotation.Bean)

Aggregations

Test (org.junit.Test)38 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)31 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)20 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)18 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)18 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)9 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)8 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)6 IOException (java.io.IOException)5 URI (java.net.URI)5 Date (java.util.Date)5 HttpMethod (org.springframework.http.HttpMethod)4 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)4 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)4 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)4 HttpHeaders (org.springframework.http.HttpHeaders)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)3 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)3 UserApprovalRequiredException (org.springframework.security.oauth2.client.resource.UserApprovalRequiredException)3