Search in sources :

Example 11 with DefaultAccessTokenRequest

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

the class AccessTokenProviderChainTests method testSunnyDayWithExpiredToken.

@Test
public void testSunnyDayWithExpiredToken() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    accessToken.setExpiration(new Date(System.currentTimeMillis() - 1000));
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setExistingToken(accessToken);
    SecurityContextHolder.getContext().setAuthentication(user);
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Date(java.util.Date) Test(org.junit.Test)

Example 12 with DefaultAccessTokenRequest

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

the class AccessTokenProviderChainTests method testRefreshAccessTokenTwicePreserveRefreshToken.

// gh-712
@Test
public void testRefreshAccessTokenTwicePreserveRefreshToken() throws Exception {
    DefaultOAuth2AccessToken accessToken = getExpiredToken();
    DefaultOAuth2AccessToken expectedRefreshedAccessToken = new DefaultOAuth2AccessToken("refreshed-access-token");
    expectedRefreshedAccessToken.setExpiration(accessToken.getExpiration());
    AccessTokenProviderChain chain = getTokenProvider(accessToken, expectedRefreshedAccessToken);
    SecurityContextHolder.getContext().setAuthentication(user);
    // Obtain a new Access Token
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    OAuth2AccessToken tokenResult = chain.obtainAccessToken(resource, request);
    assertEquals(accessToken, tokenResult);
    // Obtain the 1st Refreshed Access Token
    Calendar tokenExpiry = Calendar.getInstance();
    tokenExpiry.setTime(tokenResult.getExpiration());
    tokenExpiry.add(Calendar.MINUTE, -1);
    // Expire
    DefaultOAuth2AccessToken.class.cast(tokenResult).setExpiration(tokenExpiry.getTime());
    request = new DefaultAccessTokenRequest();
    request.setExistingToken(tokenResult);
    tokenResult = chain.obtainAccessToken(resource, request);
    assertEquals(expectedRefreshedAccessToken, tokenResult);
    // Obtain the 2nd Refreshed Access Token
    tokenExpiry.setTime(tokenResult.getExpiration());
    tokenExpiry.add(Calendar.MINUTE, -1);
    // Expire
    DefaultOAuth2AccessToken.class.cast(tokenResult).setExpiration(tokenExpiry.getTime());
    request = new DefaultAccessTokenRequest();
    request.setExistingToken(tokenResult);
    tokenResult = chain.obtainAccessToken(resource, request);
    assertEquals(expectedRefreshedAccessToken, tokenResult);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Calendar(java.util.Calendar) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 13 with DefaultAccessTokenRequest

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

the class AccessTokenProviderChainTests method testRequiresAuthenticationButRedirected.

@Test(expected = UserRedirectRequiredException.class)
public void testRequiresAuthenticationButRedirected() throws Exception {
    final AccessTokenRequest request = new DefaultAccessTokenRequest();
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider() {

        @Override
        public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest parameters) throws UserRedirectRequiredException, AccessDeniedException {
            throw new UserRedirectRequiredException("redirect test", request.toSingleValueMap());
        }
    }));
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) Test(org.junit.Test)

Example 14 with DefaultAccessTokenRequest

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

the class AccessTokenProviderChainTests method testMissingSecurityContext.

@Test
public void testMissingSecurityContext() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
// If there is no authentication to store it with a token is still acquired if
// possible
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Test(org.junit.Test)

Example 15 with DefaultAccessTokenRequest

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

the class AccessTokenProviderChainTests method testRefreshAccessTokenReplacingNullValue.

@Test
public void testRefreshAccessTokenReplacingNullValue() throws Exception {
    DefaultOAuth2AccessToken accessToken = getExpiredToken();
    DefaultOAuth2AccessToken refreshedAccessToken = new DefaultOAuth2AccessToken("refreshed-access-token");
    AccessTokenProviderChain chain = getTokenProvider(accessToken, refreshedAccessToken);
    SecurityContextHolder.getContext().setAuthentication(user);
    // Obtain a new Access Token
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    OAuth2AccessToken newAccessToken = chain.refreshAccessToken(resource, accessToken.getRefreshToken(), request);
    // gh-712
    assertEquals(newAccessToken.getRefreshToken(), accessToken.getRefreshToken());
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) 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