Search in sources :

Example 1 with TokenEnhancer

use of org.springframework.security.oauth2.provider.token.TokenEnhancer in project spring-security-oauth by spring-projects.

the class AbstractDefaultTokenServicesTests method testRefreshTokenRequestHasRefreshFlag.

@Test
public void testRefreshTokenRequestHasRefreshFlag() throws Exception {
    ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = (ExpiringOAuth2RefreshToken) getTokenServices().createAccessToken(createAuthentication()).getRefreshToken();
    TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", Collections.singleton("read"), null);
    final AtomicBoolean called = new AtomicBoolean(false);
    getTokenServices().setTokenEnhancer(new TokenEnhancer() {

        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            assertTrue(authentication.getOAuth2Request().isRefresh());
            called.set(true);
            return accessToken;
        }
    });
    getTokenServices().refreshAccessToken(expectedExpiringRefreshToken.getValue(), tokenRequest);
    assertTrue(called.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) Test(org.junit.Test)

Example 2 with TokenEnhancer

use of org.springframework.security.oauth2.provider.token.TokenEnhancer in project spring-security-oauth by spring-projects.

the class TokenServicesWithTokenEnhancerTests method additionalInfoPreservedWhenTokenDecoded.

@Test
public void additionalInfoPreservedWhenTokenDecoded() {
    TokenEnhancer info = new TokenEnhancer() {

        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
            result.getAdditionalInformation().put("foo", "bar");
            return result;
        }
    };
    enhancer.setTokenEnhancers(Arrays.<TokenEnhancer>asList(info, jwtTokenEnhancer));
    OAuth2AccessToken token = tokenServices.createAccessToken(authentication);
    assertEquals("bar", token.getAdditionalInformation().get("foo"));
    assertEquals("bar", tokenServices.readAccessToken(token.getValue()).getAdditionalInformation().get("foo"));
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 3 with TokenEnhancer

use of org.springframework.security.oauth2.provider.token.TokenEnhancer in project spring-security-oauth by spring-projects.

the class TokenServicesWithTokenEnhancerTests method init.

@Before
public void init() throws Exception {
    tokenServices = new DefaultTokenServices();
    tokenServices.setClientDetailsService(new InMemoryClientDetailsServiceBuilder().withClient("client").authorizedGrantTypes(new String[] { "authorization_code", "refresh_token" }).scopes("read").secret("secret").and().build());
    enhancer.setTokenEnhancers(Arrays.<TokenEnhancer>asList(jwtTokenEnhancer));
    jwtTokenEnhancer.afterPropertiesSet();
    tokenServices.setTokenStore(new JwtTokenStore(jwtTokenEnhancer));
    tokenServices.setTokenEnhancer(enhancer);
}
Also used : JwtTokenStore(org.springframework.security.oauth2.provider.token.store.JwtTokenStore) InMemoryClientDetailsServiceBuilder(org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder) Before(org.junit.Before)

Example 4 with TokenEnhancer

use of org.springframework.security.oauth2.provider.token.TokenEnhancer in project spring-security-oauth by spring-projects.

the class AbstractPersistentDefaultTokenServicesTests method testTokenEnhancerUpdatesStoredTokens.

@Test
public void testTokenEnhancerUpdatesStoredTokens() throws Exception {
    final ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken("testToken", new Date(System.currentTimeMillis() + 100000));
    getTokenServices().setTokenEnhancer(new TokenEnhancer() {

        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
            result.setRefreshToken(refreshToken);
            return result;
        }
    });
    OAuth2Authentication authentication = createAuthentication();
    OAuth2AccessToken original = getTokenServices().createAccessToken(authentication);
    assertTrue(original.getRefreshToken().equals(refreshToken));
    OAuth2AccessToken result = getTokenStore().getAccessToken(authentication);
    assertEquals(original, result);
    assertEquals(refreshToken, result.getRefreshToken());
    assertEquals(refreshToken, getTokenStore().readRefreshToken(refreshToken.getValue()));
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) Date(java.util.Date) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) Test(org.junit.Test)

Example 5 with TokenEnhancer

use of org.springframework.security.oauth2.provider.token.TokenEnhancer in project paascloud-master by paascloud.

the class PcAuthorizationServerConfig method configure.

/**
 * Configure.
 *
 * @param endpoints the endpoints
 *
 * @throws Exception the exception
 */
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.tokenStore(tokenStore).authenticationManager(authenticationManager).userDetailsService(userDetailsService);
    if (jwtAccessTokenConverter != null && jwtTokenEnhancer != null) {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> enhancers = new ArrayList<>();
        enhancers.add(jwtTokenEnhancer);
        enhancers.add(jwtAccessTokenConverter);
        enhancerChain.setTokenEnhancers(enhancers);
        endpoints.tokenEnhancer(enhancerChain).accessTokenConverter(jwtAccessTokenConverter);
    }
}
Also used : TokenEnhancer(org.springframework.security.oauth2.provider.token.TokenEnhancer) TokenEnhancerChain(org.springframework.security.oauth2.provider.token.TokenEnhancerChain) ArrayList(java.util.ArrayList)

Aggregations

Test (org.junit.Test)4 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 Bean (org.springframework.context.annotation.Bean)3 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)3 TokenEnhancerChain (org.springframework.security.oauth2.provider.token.TokenEnhancerChain)3 Primary (org.springframework.context.annotation.Primary)2 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)2 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)2 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)2 DefaultTokenServices (org.springframework.security.oauth2.provider.token.DefaultTokenServices)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Before (org.junit.Before)1 CustomTokenServices (org.motechproject.mots.security.token.CustomTokenServices)1 InMemoryClientDetailsServiceBuilder (org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder)1 TokenEnhancer (org.springframework.security.oauth2.provider.token.TokenEnhancer)1 JwtAccessTokenConverter (org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter)1 JwtTokenStore (org.springframework.security.oauth2.provider.token.store.JwtTokenStore)1