Search in sources :

Example 1 with DefaultAccessTokenRequest

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

the class AccessTokenProviderChainTests method testSunnyDayClientCredentialsWithTokenServicesSave.

@Test
public void testSunnyDayClientCredentialsWithTokenServicesSave() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    chain.setClientTokenServices(clientTokenServices);
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    resource = new ClientCredentialsResourceDetails();
    resource.setId("resource");
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
    Mockito.verify(clientTokenServices).saveAccessToken(resource, null, token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) Test(org.junit.Test)

Example 2 with DefaultAccessTokenRequest

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

the class AuthorizationCodeAccessTokenProviderTests method testGetAccessToken.

@Test
public void testGetAccessToken() throws Exception {
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    request.setPreservedState(new Object());
    resource.setAccessTokenUri("http://localhost/oauth/token");
    assertEquals("FOO", provider.obtainAccessToken(resource, request).getValue());
}
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) Test(org.junit.Test)

Example 3 with DefaultAccessTokenRequest

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

the class AuthorizationCodeAccessTokenProviderTests method testGetAccessTokenRequest.

@Test
public void testGetAccessTokenRequest() throws Exception {
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    request.setStateKey("bar");
    request.setPreservedState(new Object());
    resource.setAccessTokenUri("http://localhost/oauth/token");
    resource.setPreEstablishedRedirectUri("http://anywhere.com");
    assertEquals("FOO", provider.obtainAccessToken(resource, request).getValue());
    // System.err.println(params);
    assertEquals("authorization_code", params.getFirst("grant_type"));
    assertEquals("foo", params.getFirst("code"));
    assertEquals("http://anywhere.com", params.getFirst("redirect_uri"));
    // State is not set in token request
    assertEquals(null, params.getFirst("state"));
}
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) Test(org.junit.Test)

Example 4 with DefaultAccessTokenRequest

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

the class AuthorizationCodeAccessTokenProviderTests method testRedirectToAuthorizationEndpoint.

@Test
public void testRedirectToAuthorizationEndpoint() throws Exception {
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setCurrentUri("/come/back/soon");
    resource.setUserAuthorizationUri("http://localhost/oauth/authorize");
    try {
        provider.obtainAccessToken(resource, request);
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
        assertEquals("http://localhost/oauth/authorize", e.getRedirectUri());
        assertEquals("/come/back/soon", e.getStateToPreserve());
    }
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 5 with DefaultAccessTokenRequest

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

the class AuthorizationCodeAccessTokenProviderWithConversionTests method testGetAccessTokenFromJson.

@Test
public void testGetAccessTokenFromJson() throws Exception {
    final OAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    requestFactory = new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(new ObjectMapper().writeValueAsString(token));
        }
    };
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    resource.setAccessTokenUri("http://localhost/oauth/token");
    request.setPreservedState(new Object());
    setUpRestTemplate();
    assertEquals(token, provider.obtainAccessToken(resource, request));
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) IOException(java.io.IOException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HttpMethod(org.springframework.http.HttpMethod) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)33 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)22 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)18 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)17 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)16 IOException (java.io.IOException)4 URI (java.net.URI)4 Date (java.util.Date)4 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)3 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)3 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)3 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 HttpHeaders (org.springframework.http.HttpHeaders)2 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)2 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)2 ClientCredentialsResourceDetails (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails)2