use of io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken in project gravitee-access-management by gravitee-io.
the class TokenServiceTest method shouldRefresh.
@Test
public void shouldRefresh() {
String clientId = "client-id";
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.setClientId(clientId);
Client client = new Client();
client.setId(clientId);
client.setClientId(clientId);
String token = "refresh-token";
RefreshToken refreshToken = new RefreshToken();
refreshToken.setId(token);
refreshToken.setToken(token);
refreshToken.setSubject("subject");
refreshToken.setExpireAt(new Date(System.currentTimeMillis() + 10000));
JWT jwt = new JWT();
jwt.setJti(token);
jwt.setAud(clientId);
jwt.setExp(refreshToken.getExpireAt().getTime() / 1000l);
when(jwtService.decodeAndVerify(any(), any(Client.class))).thenReturn(Single.just(jwt));
when(refreshTokenRepository.findByToken(any())).thenReturn(Maybe.just(refreshToken));
when(refreshTokenRepository.delete(anyString())).thenReturn(Completable.complete());
TestObserver<Token> testObserver = tokenService.refresh(refreshToken.getToken(), tokenRequest, client).test();
testObserver.assertComplete();
testObserver.assertNoErrors();
verify(refreshTokenRepository, times(1)).findByToken(any());
verify(refreshTokenRepository, times(1)).delete(anyString());
}
use of io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken in project gravitee-access-management by gravitee-io.
the class TokenServiceTest method shouldNotRefresh_notTheSameClient.
@Test
public void shouldNotRefresh_notTheSameClient() {
String clientId = "client-id";
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.setClientId("wrong-client-id");
String token = "refresh-token";
RefreshToken refreshToken = new RefreshToken();
refreshToken.setId(token);
refreshToken.setToken(token);
refreshToken.setExpireAt(new Date(System.currentTimeMillis() + 10000));
Client client = new Client();
client.setClientId(clientId);
JWT jwt = new JWT();
jwt.setJti(token);
jwt.setAud(clientId);
jwt.setExp(refreshToken.getExpireAt().getTime() / 1000l);
when(jwtService.decodeAndVerify(any(), any(Client.class))).thenReturn(Single.just(jwt));
when(refreshTokenRepository.findByToken(any())).thenReturn(Maybe.just(refreshToken));
TestObserver<Token> testObserver = tokenService.refresh(refreshToken.getToken(), tokenRequest, client).test();
testObserver.assertNotComplete();
testObserver.assertError(InvalidGrantException.class);
verify(refreshTokenRepository, times(1)).findByToken(any());
verify(refreshTokenRepository, never()).delete(anyString());
verify(accessTokenRepository, never()).create(any());
}
use of io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken in project gravitee-access-management by gravitee-io.
the class TokenServiceTest method shouldNotRefresh_refreshNotFound.
@Test
public void shouldNotRefresh_refreshNotFound() {
String clientId = "client-id";
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.setClientId(clientId);
String token = "refresh-token";
RefreshToken refreshToken = new RefreshToken();
refreshToken.setId(token);
refreshToken.setToken(token);
refreshToken.setExpireAt(new Date(System.currentTimeMillis() + 10000));
Client client = new Client();
client.setClientId(clientId);
JWT jwt = new JWT();
jwt.setJti(token);
jwt.setAud(clientId);
jwt.setExp(refreshToken.getExpireAt().getTime() / 1000l);
when(jwtService.decodeAndVerify(eq("encoded"), any(Client.class))).thenReturn(Single.just(jwt));
when(refreshTokenRepository.findByToken(any())).thenReturn(Maybe.empty());
TestObserver<Token> testObserver = tokenService.refresh("encoded", tokenRequest, client).test();
testObserver.assertNotComplete();
testObserver.assertError(InvalidGrantException.class);
verify(refreshTokenRepository, times(1)).findByToken(any());
verify(refreshTokenRepository, never()).delete(anyString());
verify(accessTokenRepository, never()).create(any());
}
use of io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken in project gravitee-access-management by gravitee-io.
the class RevocationServiceTest method shouldRevoke_refreshToken.
@Test
public void shouldRevoke_refreshToken() {
final RevocationTokenRequest revocationTokenRequest = new RevocationTokenRequest("token");
revocationTokenRequest.setHint(TokenTypeHint.REFRESH_TOKEN);
Client client = new Client();
client.setClientId("client-id");
Token refreshToken = new RefreshToken("token");
refreshToken.setClientId("client-id");
when(tokenService.getRefreshToken("token", client)).thenReturn(Maybe.just(refreshToken));
when(tokenService.deleteRefreshToken("token")).thenReturn(Completable.complete());
TestObserver testObserver = revocationTokenService.revoke(revocationTokenRequest, client).test();
testObserver.assertComplete();
testObserver.assertNoErrors();
verify(tokenService, times(1)).getRefreshToken("token", client);
verify(tokenService, times(1)).deleteRefreshToken("token");
verify(tokenService, never()).getAccessToken("token", client);
verify(tokenService, never()).deleteAccessToken("token");
}
use of io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken in project gravitee-access-management by gravitee-io.
the class TokenServiceImpl method create.
@Override
public Single<Token> create(OAuth2Request oAuth2Request, Client client, User endUser) {
// create execution context
return Single.fromCallable(() -> createExecutionContext(oAuth2Request, client, endUser)).flatMap(executionContext -> {
// create JWT access token
JWT accessToken = createAccessTokenJWT(oAuth2Request, client, endUser, executionContext);
// create JWT refresh token
JWT refreshToken = oAuth2Request.isSupportRefreshToken() ? createRefreshTokenJWT(oAuth2Request, client, endUser, accessToken) : null;
// and create token response (+ enhance information)
return Single.zip(jwtService.encode(accessToken, client), (refreshToken != null ? jwtService.encode(refreshToken, client).map(Optional::of) : Single.just(Optional.<String>empty())), (encodedAccessToken, optionalEncodedRefreshToken) -> convert(accessToken, encodedAccessToken, optionalEncodedRefreshToken.orElse(null), oAuth2Request)).flatMap(accessToken1 -> tokenEnhancer.enhance(accessToken1, oAuth2Request, client, endUser, executionContext)).doOnSuccess(token -> storeTokens(accessToken, refreshToken, oAuth2Request));
});
}
Aggregations