Search in sources :

Example 16 with DefaultOAuth2RefreshToken

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

the class JwtAccessTokenConverterTests method testRefreshTokenAdded.

@Test
public void testRefreshTokenAdded() throws Exception {
    OAuth2Authentication authentication = new OAuth2Authentication(createOAuth2Request("foo", Collections.singleton("read")), userAuthentication);
    DefaultOAuth2AccessToken original = new DefaultOAuth2AccessToken("FOO");
    original.setScope(authentication.getOAuth2Request().getScope());
    original.setRefreshToken(new DefaultOAuth2RefreshToken("BAR"));
    original.setExpiration(new Date());
    OAuth2AccessToken token = tokenEnhancer.enhance(original, authentication);
    assertNotNull(token.getValue());
    assertNotNull(token.getRefreshToken());
    JsonParser parser = JsonParserFactory.create();
    Map<String, Object> claims = parser.parseMap(JwtHelper.decode(token.getRefreshToken().getValue()).getClaims());
    assertEquals(Arrays.asList("read"), claims.get(AccessTokenConverter.SCOPE));
    assertEquals("FOO", claims.get(AccessTokenConverter.ATI));
    assertEquals("BAR", claims.get(AccessTokenConverter.JTI));
    assertNull(claims.get(AccessTokenConverter.EXP));
    tokenEnhancer.afterPropertiesSet();
    assertTrue(tokenEnhancer.isRefreshToken(tokenEnhancer.extractAccessToken(token.getRefreshToken().getValue(), tokenEnhancer.decode(token.getRefreshToken().getValue()))));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) 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) Date(java.util.Date) JsonParser(org.springframework.security.oauth2.common.util.JsonParser) Test(org.junit.Test)

Example 17 with DefaultOAuth2RefreshToken

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

the class JwtAccessTokenConverterTests method testRefreshTokenAccessTokenIdWhenDoubleEnhanced.

@Test
public void testRefreshTokenAccessTokenIdWhenDoubleEnhanced() throws Exception {
    OAuth2Authentication authentication = new OAuth2Authentication(createOAuth2Request("foo", Collections.singleton("read")), userAuthentication);
    DefaultOAuth2AccessToken original = new DefaultOAuth2AccessToken("FOO");
    original.setScope(authentication.getOAuth2Request().getScope());
    original.setRefreshToken(new DefaultOAuth2RefreshToken("BAR"));
    OAuth2AccessToken token = tokenEnhancer.enhance(original, authentication);
    token = tokenEnhancer.enhance(token, authentication);
    assertNotNull(token.getValue());
    assertNotNull(token.getRefreshToken());
    JsonParser parser = JsonParserFactory.create();
    Map<String, Object> claims = parser.parseMap(JwtHelper.decode(token.getRefreshToken().getValue()).getClaims());
    assertEquals(Arrays.asList("read"), claims.get(AccessTokenConverter.SCOPE));
    assertEquals("FOO", claims.get(AccessTokenConverter.ATI));
    assertEquals("Wrong claims: " + claims, "BAR", claims.get(AccessTokenConverter.JTI));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) 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) JsonParser(org.springframework.security.oauth2.common.util.JsonParser) Test(org.junit.Test)

Example 18 with DefaultOAuth2RefreshToken

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

the class TokenStoreBaseTests method testRefreshTokenIsNotStoredDuringAccessToken.

@Test
public void testRefreshTokenIsNotStoredDuringAccessToken() {
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false));
    DefaultOAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken("testToken");
    expectedOAuth2AccessToken.setRefreshToken(new DefaultOAuth2RefreshToken("refreshToken"));
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken("testToken");
    assertNotNull(actualOAuth2AccessToken.getRefreshToken());
    assertNull(getTokenStore().readRefreshToken("refreshToken"));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) 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 19 with DefaultOAuth2RefreshToken

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

the class TokenStoreBaseTests method testStoreRefreshToken.

@Test
public /**
	 * NB: This used to test expiring refresh tokens. That test has been moved to sub-classes since not all stores support the functionality
	 */
void testStoreRefreshToken() {
    String refreshToken = "testToken" + UUID.randomUUID();
    DefaultOAuth2RefreshToken expectedRefreshToken = new DefaultOAuth2RefreshToken(refreshToken);
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false));
    getTokenStore().storeRefreshToken(expectedRefreshToken, expectedAuthentication);
    OAuth2RefreshToken actualExpiringRefreshToken = getTokenStore().readRefreshToken(refreshToken);
    assertEquals(expectedRefreshToken, actualExpiringRefreshToken);
    assertEquals(expectedAuthentication, getTokenStore().readAuthenticationForRefreshToken(expectedRefreshToken));
    getTokenStore().removeRefreshToken(expectedRefreshToken);
    assertNull(getTokenStore().readRefreshToken(refreshToken));
    assertNull(getTokenStore().readAuthentication(expectedRefreshToken.getValue()));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 20 with DefaultOAuth2RefreshToken

use of org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken in project ORCID-Source by ORCID.

the class OrcidRandomValueTokenServicesImpl method toOAuth2AccessToken.

private OAuth2AccessToken toOAuth2AccessToken(OrcidOauth2TokenDetail token) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(token.getTokenValue());
    result.setExpiration(token.getTokenExpiration());
    result.setRefreshToken(new DefaultOAuth2RefreshToken(token.getRefreshTokenValue()));
    result.setScope(OAuth2Utils.parseParameterList(token.getScope()));
    result.setTokenType(token.getTokenType());
    result.setValue(token.getTokenValue());
    Map<String, Object> additionalInfo = new HashMap<String, Object>();
    if (token.getProfile() != null) {
        additionalInfo.put(OrcidOauth2Constants.ORCID, token.getProfile().getId());
        additionalInfo.put(OrcidOauth2Constants.NAME, profileEntityManager.retrivePublicDisplayName(token.getProfile().getId()));
    }
    result.setAdditionalInformation(additionalInfo);
    return result;
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) HashMap(java.util.HashMap) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

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