Search in sources :

Example 91 with DefaultOAuth2AccessToken

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

the class TokenStoreBaseTests method testFindAccessTokensByClientIdAndUserName.

@Test
public void testFindAccessTokensByClientIdAndUserName() {
    String clientId = "id" + UUID.randomUUID();
    String name = "test2" + UUID.randomUUID();
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request(clientId, false), new TestAuthentication(name, false));
    OAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken("testToken");
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    Collection<OAuth2AccessToken> actualOAuth2AccessTokens = getTokenStore().findTokensByClientIdAndUserName(clientId, name);
    assertEquals(1, actualOAuth2AccessTokens.size());
}
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 92 with DefaultOAuth2AccessToken

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

the class TokenStoreBaseTests method testStoreAccessTokenTwice.

@Test
public void testStoreAccessTokenTwice() {
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false));
    OAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken("testToken");
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken("testToken");
    assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken);
    assertEquals(expectedAuthentication, getTokenStore().readAuthentication(expectedOAuth2AccessToken));
    getTokenStore().removeAccessToken(expectedOAuth2AccessToken);
    assertNull(getTokenStore().readAccessToken("testToken"));
    assertNull(getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()));
}
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 93 with DefaultOAuth2AccessToken

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

the class RedisTokenStoreTests method testExpiringAccessToken.

@Test
public void testExpiringAccessToken() throws InterruptedException {
    String accessToken = UUID.randomUUID().toString();
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false));
    DefaultOAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken);
    expectedOAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + 1500));
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken(accessToken);
    assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken);
    assertEquals(expectedAuthentication, getTokenStore().readAuthentication(expectedOAuth2AccessToken));
    // let the token expire
    Thread.sleep(1500);
    // now it should be gone
    assertNull(getTokenStore().readAccessToken(accessToken));
    assertNull(getTokenStore().readAuthentication(expectedOAuth2AccessToken));
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Date(java.util.Date) Test(org.junit.Test)

Example 94 with DefaultOAuth2AccessToken

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

the class RedisTokenStoreMockTests method storeAccessTokenWithoutRefreshTokenRemoveAccessTokenVerifyKeysRemoved.

// gh-572
@Test
public void storeAccessTokenWithoutRefreshTokenRemoveAccessTokenVerifyKeysRemoved() {
    OAuth2AccessToken oauth2AccessToken = new DefaultOAuth2AccessToken("access-token-" + UUID.randomUUID());
    OAuth2Authentication oauth2Authentication = new OAuth2Authentication(request, authentication);
    List<Object> results = Arrays.<Object>asList("access-token".getBytes(), "authentication".getBytes());
    when(connection.closePipeline()).thenReturn(results);
    RedisTokenStoreSerializationStrategy serializationStrategy = new JdkSerializationStrategy();
    serializationStrategy = spy(serializationStrategy);
    when(serializationStrategy.deserialize(any(byte[].class), eq(OAuth2Authentication.class))).thenReturn(oauth2Authentication);
    tokenStore.setSerializationStrategy(serializationStrategy);
    tokenStore.storeAccessToken(oauth2AccessToken, oauth2Authentication);
    ArgumentCaptor<byte[]> setKeyArgs = ArgumentCaptor.forClass(byte[].class);
    verify(connection, times(3)).set(setKeyArgs.capture(), any(byte[].class));
    ArgumentCaptor<byte[]> rPushKeyArgs = ArgumentCaptor.forClass(byte[].class);
    verify(connection, times(2)).rPush(rPushKeyArgs.capture(), any(byte[].class));
    tokenStore.removeAccessToken(oauth2AccessToken);
    for (byte[] key : setKeyArgs.getAllValues()) {
        verify(connection).del(key);
    }
    for (byte[] key : rPushKeyArgs.getAllValues()) {
        verify(connection).lRem(eq(key), eq(1L), any(byte[].class));
    }
}
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 95 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-boot by spring-projects.

the class UserInfoTokenServicesRefreshTokenTests method withRestTemplate.

@Test
public void withRestTemplate() {
    OAuth2ProtectedResourceDetails resource = new AuthorizationCodeResourceDetails();
    OAuth2ClientContext context = new DefaultOAuth2ClientContext();
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setRefreshToken(new DefaultExpiringOAuth2RefreshToken("BAR", new Date(0L)));
    context.setAccessToken(token);
    this.services.setRestTemplate(new OAuth2RestTemplate(resource, context));
    assertThat(this.services.loadAuthentication("FOO").getName()).isEqualTo("me");
    assertThat(context.getAccessToken().getValue()).isEqualTo("FOO");
    // The refresh token is still intact
    assertThat(context.getAccessToken().getRefreshToken()).isEqualTo(token.getRefreshToken());
}
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) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Date(java.util.Date) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)95 Test (org.junit.Test)78 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)52 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)48 Date (java.util.Date)27 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)13 HashMap (java.util.HashMap)12 Authentication (org.springframework.security.core.Authentication)12 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)12 URI (java.net.URI)9 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)9 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)8 DBUnitTest (org.orcid.test.DBUnitTest)7 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)6 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)6 Before (org.junit.Before)5 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)5 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)5 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)5