Search in sources :

Example 1 with ClientCredentialsAccessTokenProvider

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

the class AbstractClientCredentialsProviderTests 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) {
    // System.err.println(responseHeaders);
    // ignore
    }
    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 2 with ClientCredentialsAccessTokenProvider

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

the class ClientCredentialsGrantTests method testConnectDirectlyToResourceServer.

@Test
public void testConnectDirectlyToResourceServer() throws Exception {
    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
    resource.setAccessTokenUri(serverRunning.getUrl("/sparklr2/oauth/token"));
    resource.setClientId("my-client-with-registered-redirect");
    resource.setId("sparklr");
    resource.setScope(Arrays.asList("trust"));
    ClientCredentialsAccessTokenProvider provider = new ClientCredentialsAccessTokenProvider();
    OAuth2AccessToken accessToken = provider.obtainAccessToken(resource, new DefaultAccessTokenRequest());
    OAuth2RestTemplate template = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext(accessToken));
    String result = template.getForObject(serverRunning.getUrl("/sparklr2/photos/trusted/message"), String.class);
    assertEquals("Hello, Trusted Client", result);
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 3 with ClientCredentialsAccessTokenProvider

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider in project perry by ca-cwds.

the class NoStateUserInfoRestTemplateCustomizerTest method testCustomize.

@Test
public void testCustomize() throws Exception {
    NoStateUserInfoRestTemplateCustomizer noStateUserInfoRestTemplateCustomizer = new NoStateUserInfoRestTemplateCustomizer();
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(new AuthorizationCodeResourceDetails());
    noStateUserInfoRestTemplateCustomizer.customize(oAuth2RestTemplate);
    AccessTokenProviderChain accessTokenProvider = getFieldValue(oAuth2RestTemplate, "accessTokenProvider", AccessTokenProviderChain.class);
    assert accessTokenProvider != null;
    List list = getFieldValue(accessTokenProvider, "chain", List.class);
    assert list.size() == 4;
    assert list.stream().anyMatch(o -> o instanceof ImplicitAccessTokenProvider);
    assert list.stream().anyMatch(o -> o instanceof ResourceOwnerPasswordAccessTokenProvider);
    assert list.stream().anyMatch(o -> o instanceof ClientCredentialsAccessTokenProvider);
    assert list.stream().anyMatch(o -> o instanceof NoStateAuthorizationCodeTokenProvider);
    NoStateAuthorizationCodeTokenProvider noStateAuthorizationCodeTokenProvider = (NoStateAuthorizationCodeTokenProvider) list.stream().filter(o -> o instanceof NoStateAuthorizationCodeTokenProvider).findFirst().get();
    assert !getFieldValue(noStateAuthorizationCodeTokenProvider, "stateMandatory", Boolean.class);
    List interceptors = getFieldValue(noStateAuthorizationCodeTokenProvider, "interceptors", List.class);
    assert interceptors.size() == 1;
    assert interceptors.stream().anyMatch(o -> o instanceof DebugRestTemplateInterceptor);
}
Also used : AccessTokenProvider(org.springframework.security.oauth2.client.token.AccessTokenProvider) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) List(java.util.List) ImplicitAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider) DebugRestTemplateInterceptor(gov.ca.cwds.config.logging.DebugRestTemplateInterceptor) ResourceOwnerPasswordAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider) Test(org.junit.Test) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) Field(java.lang.reflect.Field) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) AccessTokenProviderChain(org.springframework.security.oauth2.client.token.AccessTokenProviderChain) ImplicitAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider) AccessTokenProviderChain(org.springframework.security.oauth2.client.token.AccessTokenProviderChain) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) List(java.util.List) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) ResourceOwnerPasswordAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider) DebugRestTemplateInterceptor(gov.ca.cwds.config.logging.DebugRestTemplateInterceptor) Test(org.junit.Test)

Example 4 with ClientCredentialsAccessTokenProvider

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider in project vorto by eclipse.

the class EidpUtils method accessTokenProvider.

public static AccessTokenProvider accessTokenProvider() {
    AuthorizationCodeAccessTokenProvider authorizationCodeAccessTokenProvider = new AuthorizationCodeAccessTokenProvider();
    ImplicitAccessTokenProvider implicitAccessTokenProvider = new ImplicitAccessTokenProvider();
    return new AccessTokenProviderChain(Arrays.<AccessTokenProvider>asList(authorizationCodeAccessTokenProvider, implicitAccessTokenProvider, new ResourceOwnerPasswordAccessTokenProvider(), new ClientCredentialsAccessTokenProvider()));
}
Also used : AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) ImplicitAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider) AccessTokenProviderChain(org.springframework.security.oauth2.client.token.AccessTokenProviderChain) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) ResourceOwnerPasswordAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)

Example 5 with ClientCredentialsAccessTokenProvider

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider 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)

Aggregations

ClientCredentialsAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider)9 Test (org.junit.Test)5 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)4 AccessTokenProviderChain (org.springframework.security.oauth2.client.token.AccessTokenProviderChain)4 ImplicitAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider)4 ResourceOwnerPasswordAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)4 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)4 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)4 IOException (java.io.IOException)3 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)3 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)3 DebugRestTemplateInterceptor (gov.ca.cwds.config.logging.DebugRestTemplateInterceptor)2 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)2 AccessTokenProvider (org.springframework.security.oauth2.client.token.AccessTokenProvider)2 Field (java.lang.reflect.Field)1 List (java.util.List)1 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)1 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)1 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)1 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)1