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