use of io.gravitee.am.repository.oauth2.model.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.repository.oauth2.model.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.repository.oauth2.model.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.repository.oauth2.model.RefreshToken in project gravitee-access-management by gravitee-io.
the class RefreshTokenRepositoryTest method shouldFindToken.
@Test
public void shouldFindToken() {
RefreshToken token = new RefreshToken();
token.setId(RandomString.generate());
token.setToken("my-token");
TestObserver<RefreshToken> observer = refreshTokenRepository.create(token).toCompletable().andThen(refreshTokenRepository.findByToken("my-token")).test();
observer.awaitTerminalEvent();
observer.assertComplete();
observer.assertValueCount(1);
observer.assertNoErrors();
}
use of io.gravitee.am.repository.oauth2.model.RefreshToken in project gravitee-access-management by gravitee-io.
the class RefreshTokenRepositoryPurgeTest method shouldPurge.
@Test
public void shouldPurge() {
Instant now = Instant.now();
RefreshToken token1 = new RefreshToken();
token1.setId("my-token");
token1.setToken("my-token");
token1.setClient("client-id");
token1.setDomain("domain-id");
token1.setSubject("user-id");
token1.setExpireAt(new Date(now.plus(1, ChronoUnit.MINUTES).toEpochMilli()));
RefreshToken token2 = new RefreshToken();
token2.setId("my-token2");
token2.setToken("my-token2");
token2.setClient("client-id2");
token2.setDomain("domain-id2");
token2.setSubject("user-id2");
token2.setExpireAt(new Date(now.minus(1, ChronoUnit.MINUTES).toEpochMilli()));
TestObserver<Void> testObserver = refreshTokenRepository.bulkWrite(Arrays.asList(token1, token2)).test();
testObserver.awaitTerminalEvent();
testObserver.assertNoErrors();
assertNotNull(refreshTokenRepository.findByToken("my-token").blockingGet());
assertNull(refreshTokenRepository.findByToken("my-token2").blockingGet());
TestObserver<Void> testPurge = refreshTokenRepository.purgeExpiredData().test();
testPurge.awaitTerminalEvent();
testPurge.assertNoErrors();
assertNotNull(refreshTokenRepository.findByToken("my-token").blockingGet());
assertNull(refreshTokenRepository.findByToken("my-token2").blockingGet());
}
Aggregations