Java Code Examples for org.springframework.security.oauth2.common.util.SerializationUtils#serialize()

The following examples show how to use org.springframework.security.oauth2.common.util.SerializationUtils#serialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoTokenStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldStoreAccessToken() {
    //Given
    final OAuth2AccessToken auth2AccessToken = OAuth2AccessTokenBuilder.oAuth2AccessTokenBuilder().build();
    final byte[] token = SerializationUtils.serialize(auth2AccessToken);

    //And
    final OAuth2Authentication oAuth2Authentication = OAuth2AuthenticationBuilder.oAuth2AuthenticationBuilder().build();

    //And
    given(mongoOAuth2AccessTokenRepository.findByTokenId(any(String.class)))
            .willReturn(MongoOAuth2AccessTokenBuilder.mongoOAuth2AccessTokenBuilder().token(token).build());

    //When
    mongoTokenStore.storeAccessToken(auth2AccessToken, oAuth2Authentication);

    //Then
    verify(mongoOAuth2AccessTokenRepository).deleteByTokenId(any(String.class));
    verify(mongoOAuth2AccessTokenRepository).save(any(MongoOAuth2AccessToken.class));
}
 
Example 2
Source File: MongoTokenStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldStoreRefreshToken() {
    //Given
    final OAuth2RefreshToken oAuth2RefreshToken = OAuth2RefreshTokenBuilder.oAuth2RefreshToken().build();

    //And
    final OAuth2Authentication oAuth2Authentication = OAuth2AuthenticationBuilder.oAuth2AuthenticationBuilder().build();

    //And
    final ArgumentCaptor<MongoOAuth2RefreshToken> argumentCaptor = ArgumentCaptor.forClass(MongoOAuth2RefreshToken.class);

    //When
    mongoTokenStore.storeRefreshToken(oAuth2RefreshToken, oAuth2Authentication);

    //Then
    verify(mongoOAuth2RefreshTokenRepository).save(argumentCaptor.capture());
    final MongoOAuth2RefreshToken refreshToken = argumentCaptor.getValue();
    final byte[] expectedResult = SerializationUtils.serialize(oAuth2RefreshToken);
    assertThat(refreshToken.getToken()).isEqualTo(expectedResult);

}
 
Example 3
Source File: MongoTokenStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldReadRefreshToken() {
    //Given
    final String tokenValue = string().next();
    final OAuth2RefreshToken oAuth2RefreshToken = OAuth2RefreshTokenBuilder.oAuth2RefreshToken().build();
    final byte[] oAuth2RefreshTokenSer = SerializationUtils.serialize(oAuth2RefreshToken);

    //And
    given(mongoOAuth2RefreshTokenRepository.findByTokenId(any(String.class)))
            .willReturn(MongoOAuth2RefreshTokenBuilder.mongoOAuth2RefreshTokenBuilder().token(oAuth2RefreshTokenSer).build());

    //When
    final OAuth2RefreshToken result = mongoTokenStore.readRefreshToken(tokenValue);

    //Then
    assertThat(result.getValue()).isEqualTo(oAuth2RefreshToken.getValue());
}
 
Example 4
Source File: MongoTokenStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldReadAuthenticationForRefreshToken() {
    //Given
    final OAuth2RefreshToken oAuth2RefreshToken = OAuth2RefreshTokenBuilder.oAuth2RefreshToken().build();

    //And
    final OAuth2Authentication authentication = OAuth2AuthenticationBuilder.oAuth2AuthenticationBuilder().build();
    final byte[] authenticationSer = SerializationUtils.serialize(authentication);

    //And
    given(mongoOAuth2RefreshTokenRepository.findByTokenId(any(String.class)))
            .willReturn(MongoOAuth2RefreshTokenBuilder.mongoOAuth2RefreshTokenBuilder()
                    .authentication(authenticationSer)
                    .build());
    //When
    final OAuth2Authentication oAuth2Authentication = mongoTokenStore.readAuthenticationForRefreshToken(oAuth2RefreshToken);

    //Then
    assertThat(oAuth2Authentication.getPrincipal()).isEqualTo(authentication.getPrincipal());
    assertThat(oAuth2Authentication.getCredentials()).isEqualTo(authentication.getCredentials());
}
 
Example 5
Source File: MongoClientTokenServices.java    From spring-security-mongo with MIT License 5 votes vote down vote up
@Override
public void saveAccessToken(final OAuth2ProtectedResourceDetails resource,
                            final Authentication authentication,
                            final OAuth2AccessToken accessToken) {
    removeAccessToken(resource, authentication);
    final MongoOAuth2ClientToken mongoOAuth2ClientToken = new MongoOAuth2ClientToken(UUID.randomUUID().toString(),
            accessToken.getValue(),
            SerializationUtils.serialize(accessToken),
            clientKeyGenerator.extractKey(resource, authentication),
            authentication.getName(),
            resource.getClientId());

    mongoOAuth2ClientTokenRepository.save(mongoOAuth2ClientToken);
}
 
Example 6
Source File: MongoTokenStoreTest.java    From spring-security-mongo with MIT License 5 votes vote down vote up
@Test
public void shouldGetAccessToken() {
    //Given
    final OAuth2Authentication oAuth2Authentication = OAuth2AuthenticationBuilder.oAuth2AuthenticationBuilder().build();

    //And
    final String value = string().next();
    doReturn(value).doReturn(value).when(authenticationKeyGenerator).extractKey(any());

    //And
    final OAuth2AccessToken oAuth2AccessToken = OAuth2AccessTokenBuilder.oAuth2AccessTokenBuilder().build();

    final byte[] oAuth2AccessTokenSer = SerializationUtils.serialize(oAuth2AccessToken);
    given(mongoOAuth2AccessTokenRepository.findByAuthenticationId(value))
            .willReturn(MongoOAuth2AccessTokenBuilder.mongoOAuth2AccessTokenBuilder()
                    .token(oAuth2AccessTokenSer)
                    .build());

    //And
    given(mongoOAuth2AccessTokenRepository.findByTokenId(any()))
            .willReturn(MongoOAuth2AccessTokenBuilder.mongoOAuth2AccessTokenBuilder().build());

    //When
    mongoTokenStore.getAccessToken(oAuth2Authentication);

    //Then
    verify(mongoOAuth2AccessTokenRepository, never()).deleteByTokenId(any(String.class));
    verify(mongoOAuth2AccessTokenRepository, never()).save(any(MongoOAuth2AccessToken.class));
}
 
Example 7
Source File: AccessTokenDO.java    From oauth-server with Apache License 2.0 4 votes vote down vote up
public void setValue(OAuth2AccessToken value) {
    this.value = value;
    this.token = SerializationUtils.serialize(value);
}
 
Example 8
Source File: AccessTokenDO.java    From oauth-server with Apache License 2.0 4 votes vote down vote up
public void setAuth2Authentication(OAuth2Authentication oauth2Authentication) {
    this.auth2Authentication = oauth2Authentication;
    this.authentication = SerializationUtils.serialize(oauth2Authentication);
}