Search in sources :

Example 11 with DefaultOAuth2RefreshToken

use of org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken in project spring-security-oauth by spring-projects.

the class DefaultTokenServices method createRefreshToken.

private OAuth2RefreshToken createRefreshToken(OAuth2Authentication authentication) {
    if (!isSupportRefreshToken(authentication.getOAuth2Request())) {
        return null;
    }
    int validitySeconds = getRefreshTokenValiditySeconds(authentication.getOAuth2Request());
    String value = UUID.randomUUID().toString();
    if (validitySeconds > 0) {
        return new DefaultExpiringOAuth2RefreshToken(value, new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));
    }
    return new DefaultOAuth2RefreshToken(value);
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) Date(java.util.Date)

Example 12 with DefaultOAuth2RefreshToken

use of org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken in project spring-security-oauth by spring-projects.

the class JaxbOAuth2AccessTokenMessageConverterTests method before.

@Before
public void before() throws Exception {
    converter = new JaxbOAuth2AccessTokenMessageConverter();
    accessToken = new DefaultOAuth2AccessToken("SlAV32hkKG");
    accessToken.setExpiration(expiration);
    accessToken.setRefreshToken(new DefaultOAuth2RefreshToken("8xLOxBtZp8"));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Before(org.junit.Before)

Example 13 with DefaultOAuth2RefreshToken

use of org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken in project spring-security-oauth by spring-projects.

the class AccessTokenProviderChainTests method testRefreshAccessTokenKeepingOldValue.

@Test
public void testRefreshAccessTokenKeepingOldValue() throws Exception {
    DefaultOAuth2AccessToken accessToken = getExpiredToken();
    DefaultOAuth2AccessToken refreshedAccessToken = new DefaultOAuth2AccessToken("refreshed-access-token");
    refreshedAccessToken.setRefreshToken(new DefaultOAuth2RefreshToken("other-refresh-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-816
    assertEquals(newAccessToken.getRefreshToken(), refreshedAccessToken.getRefreshToken());
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) 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 14 with DefaultOAuth2RefreshToken

use of org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken in project spring-security-oauth by spring-projects.

the class AccessTokenProviderChainTests method getExpiredToken.

private DefaultOAuth2AccessToken getExpiredToken() {
    Calendar tokenExpiry = Calendar.getInstance();
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("access-token");
    accessToken.setExpiration(tokenExpiry.getTime());
    accessToken.setRefreshToken(new DefaultOAuth2RefreshToken("refresh-token"));
    return accessToken;
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) Calendar(java.util.Calendar) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 15 with DefaultOAuth2RefreshToken

use of org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken in project spring-security-oauth by spring-projects.

the class AccessTokenProviderChainTests method testSunnyDayWIthExpiredTokenAndValidRefreshToken.

@Test
public void testSunnyDayWIthExpiredTokenAndValidRefreshToken() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    accessToken.setExpiration(new Date(System.currentTimeMillis() - 1000));
    accessToken.setRefreshToken(new DefaultOAuth2RefreshToken("EXP"));
    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) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)20 Test (org.junit.Test)13 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)13 Date (java.util.Date)11 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)7 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)7 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)3 HashMap (java.util.HashMap)2 JsonParser (org.springframework.security.oauth2.common.util.JsonParser)2 Approval (org.springframework.security.oauth2.provider.approval.Approval)2 Calendar (java.util.Calendar)1 LinkedHashMap (java.util.LinkedHashMap)1 Before (org.junit.Before)1 OrcidOauth2AuthInfo (org.orcid.core.oauth.OrcidOauth2AuthInfo)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 DBUnitTest (org.orcid.test.DBUnitTest)1 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)1 Transactional (org.springframework.transaction.annotation.Transactional)1