Search in sources :

Example 6 with AccessTokenRequest

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

the class OAuth2RestTemplateTests method testTokenIsResetIfInvalid.

@Test
public void testTokenIsResetIfInvalid() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("TEST");
    token.setExpiration(new Date(System.currentTimeMillis() - 1000));
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    restTemplate.setAccessTokenProvider(new StubAccessTokenProvider() {

        @Override
        public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest parameters) throws UserRedirectRequiredException, AccessDeniedException {
            throw new UserRedirectRequiredException("http://foo.com", Collections.<String, String>emptyMap());
        }
    });
    try {
        OAuth2AccessToken newToken = restTemplate.getAccessToken();
        assertNotNull(newToken);
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
    // planned
    }
    // context token should be reset as it clearly is invalid at this point
    assertNull(restTemplate.getOAuth2ClientContext().getAccessToken());
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) 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) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Date(java.util.Date) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) Test(org.junit.Test)

Example 7 with AccessTokenRequest

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

the class AccessTokenProviderChainTests method testAnonymousUser.

@Test(expected = InsufficientAuthenticationException.class)
public void testAnonymousUser() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("foo", "bar", user.getAuthorities()));
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Test(org.junit.Test)

Example 8 with AccessTokenRequest

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

the class AccessTokenProviderChainTests method testSunnyDayWithExpiredTokenAndTokenServices.

@Test
public void testSunnyDayWithExpiredTokenAndTokenServices() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    chain.setClientTokenServices(clientTokenServices);
    accessToken.setExpiration(new Date(System.currentTimeMillis() - 1000));
    when(clientTokenServices.getAccessToken(resource, user)).thenReturn(accessToken);
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    SecurityContextHolder.getContext().setAuthentication(user);
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
    Mockito.verify(clientTokenServices).removeAccessToken(resource, user);
    Mockito.verify(clientTokenServices).saveAccessToken(resource, user, 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 9 with AccessTokenRequest

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

the class AccessTokenProviderChainTests method testSunnyDayWithTokenServicesSave.

@Test
public void testSunnyDayWithTokenServicesSave() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    chain.setClientTokenServices(clientTokenServices);
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    SecurityContextHolder.getContext().setAuthentication(user);
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
    Mockito.verify(clientTokenServices).saveAccessToken(resource, user, token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Test(org.junit.Test)

Example 10 with AccessTokenRequest

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

the class AccessTokenProviderChainTests method testSunnyDayWIthExpiredTokenAndExpiredRefreshToken.

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

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