Search in sources :

Example 1 with AuthorizationCodeResourceDetails

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");
}
Also used : DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) OAuth2ClientContext(org.springframework.security.oauth2.client.OAuth2ClientContext) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with AuthorizationCodeResourceDetails

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);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 3 with AuthorizationCodeResourceDetails

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);
}
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 4 with AuthorizationCodeResourceDetails

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

Example 5 with AuthorizationCodeResourceDetails

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
    }
}
Also used : InsufficientScopeException(org.springframework.security.oauth2.common.exceptions.InsufficientScopeException) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Aggregations

AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)16 Test (org.junit.Test)10 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)8 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)5 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 Bean (org.springframework.context.annotation.Bean)3 UserApprovalRequiredException (org.springframework.security.oauth2.client.resource.UserApprovalRequiredException)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)2 OAuth2ClientContext (org.springframework.security.oauth2.client.OAuth2ClientContext)2 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)2 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)2 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)2 InsufficientScopeException (org.springframework.security.oauth2.common.exceptions.InsufficientScopeException)2 URI (java.net.URI)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1