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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations