use of org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails in project spring-boot by spring-projects.
the class UserInfoTokenServicesRefreshTokenTests method withRestTemplateChangesState.
@Test
public void withRestTemplateChangesState() {
OAuth2ProtectedResourceDetails resource = new AuthorizationCodeResourceDetails();
OAuth2ClientContext context = new DefaultOAuth2ClientContext();
context.setAccessToken(new DefaultOAuth2AccessToken("FOO"));
this.services.setRestTemplate(new OAuth2RestTemplate(resource, context));
assertThat(this.services.loadAuthentication("BAR").getName()).isEqualTo("me");
assertThat(context.getAccessToken().getValue()).isEqualTo("BAR");
}
use of org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails in project spring-security-oauth by spring-projects.
the class JdbcClientTokenServicesTests method testSaveAndRemoveToken.
@Test
public void testSaveAndRemoveToken() throws Exception {
OAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("FOO");
Authentication authentication = new UsernamePasswordAuthenticationToken("marissa", "koala");
AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
resource.setClientId("client");
resource.setScope(Arrays.asList("foo", "bar"));
tokenStore.saveAccessToken(resource, authentication, accessToken);
tokenStore.removeAccessToken(resource, authentication);
// System.err.println(new JdbcTemplate(db).queryForList("select * from oauth_client_token"));
OAuth2AccessToken result = tokenStore.getAccessToken(resource, authentication);
assertNull(result);
}
use of org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails 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);
}
use of org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails 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());
}
use of org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails in project spring-security-oauth by spring-projects.
the class AuthorizationCodeProviderTests method testInsufficientScopeInResourceRequest.
@Test
@OAuth2ContextConfiguration(resource = MyClientWithRegisteredRedirect.class, initialize = false)
public void testInsufficientScopeInResourceRequest() throws Exception {
AuthorizationCodeResourceDetails resource = (AuthorizationCodeResourceDetails) context.getResource();
resource.setScope(Arrays.asList("trust"));
approveAccessTokenGrant("http://anywhere?key=value", true);
assertNotNull(context.getAccessToken());
try {
serverRunning.getForString("/sparklr2/photos?format=json");
fail("Should have thrown exception");
} catch (InsufficientScopeException ex) {
// ignore / all good
}
}
Aggregations