Search in sources :

Example 1 with RefreshToken

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());
}
Also used : RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) JWT(io.gravitee.am.common.jwt.JWT) TokenRequest(io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest) RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Client(io.gravitee.am.model.oidc.Client) Date(java.util.Date) Test(org.junit.Test)

Example 2 with RefreshToken

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());
}
Also used : RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) JWT(io.gravitee.am.common.jwt.JWT) TokenRequest(io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest) RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Client(io.gravitee.am.model.oidc.Client) Date(java.util.Date) Test(org.junit.Test)

Example 3 with RefreshToken

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());
}
Also used : RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) JWT(io.gravitee.am.common.jwt.JWT) TokenRequest(io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest) RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Client(io.gravitee.am.model.oidc.Client) Date(java.util.Date) Test(org.junit.Test)

Example 4 with RefreshToken

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();
}
Also used : RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) AbstractOAuthTest(io.gravitee.am.repository.oauth2.AbstractOAuthTest) Test(org.junit.Test)

Example 5 with RefreshToken

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());
}
Also used : RefreshToken(io.gravitee.am.repository.oauth2.model.RefreshToken) Instant(java.time.Instant) Date(java.util.Date) AbstractOAuthTest(io.gravitee.am.repository.oauth2.AbstractOAuthTest) Test(org.junit.Test)

Aggregations

RefreshToken (io.gravitee.am.repository.oauth2.model.RefreshToken)11 Test (org.junit.Test)10 JWT (io.gravitee.am.common.jwt.JWT)6 TokenRequest (io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest)6 Client (io.gravitee.am.model.oidc.Client)6 Date (java.util.Date)6 AccessToken (io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken)5 AbstractOAuthTest (io.gravitee.am.repository.oauth2.AbstractOAuthTest)5 PermissionRequest (io.gravitee.am.model.uma.PermissionRequest)2 JWTException (io.gravitee.am.common.exception.jwt.JWTException)1 InvalidTokenException (io.gravitee.am.common.exception.oauth2.InvalidTokenException)1 Claims (io.gravitee.am.common.jwt.Claims)1 TokenTypeHint (io.gravitee.am.common.oauth2.TokenTypeHint)1 Parameters (io.gravitee.am.common.oidc.Parameters)1 ConstantKeys (io.gravitee.am.common.utils.ConstantKeys)1 RandomString (io.gravitee.am.common.utils.RandomString)1 SecureRandomString (io.gravitee.am.common.utils.SecureRandomString)1 JWTService (io.gravitee.am.gateway.handler.common.jwt.JWTService)1 IntrospectionTokenService (io.gravitee.am.gateway.handler.common.oauth2.IntrospectionTokenService)1 ExecutionContextFactory (io.gravitee.am.gateway.handler.context.ExecutionContextFactory)1