Search in sources :

Example 1 with RefreshToken

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());
}
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.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());
}
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.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());
}
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.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");
}
Also used : RefreshToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) RefreshToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken) Client(io.gravitee.am.model.oidc.Client) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 5 with RefreshToken

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));
    });
}
Also used : TokenTypeHint(io.gravitee.am.common.oauth2.TokenTypeHint) AccessTokenRepository(io.gravitee.am.repository.oauth2.api.AccessTokenRepository) java.util(java.util) Client(io.gravitee.am.model.oidc.Client) Completable(io.reactivex.Completable) SecureRandomString(io.gravitee.am.common.utils.SecureRandomString) TokenManager(io.gravitee.am.gateway.handler.oauth2.service.token.TokenManager) MultiValueMap(io.gravitee.common.util.MultiValueMap) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ConstantKeys(io.gravitee.am.common.utils.ConstantKeys) InvalidTokenException(io.gravitee.am.common.exception.oauth2.InvalidTokenException) TokenService(io.gravitee.am.gateway.handler.oauth2.service.token.TokenService) RefreshTokenRepository(io.gravitee.am.repository.oauth2.api.RefreshTokenRepository) Single(io.reactivex.Single) TokenEnhancer(io.gravitee.am.gateway.handler.oauth2.service.token.TokenEnhancer) JWTException(io.gravitee.am.common.exception.jwt.JWTException) IntrospectionTokenService(io.gravitee.am.gateway.handler.common.oauth2.IntrospectionTokenService) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) User(io.gravitee.am.model.User) ExecutionContextFactory(io.gravitee.am.gateway.handler.context.ExecutionContextFactory) InvalidGrantException(io.gravitee.am.gateway.handler.oauth2.exception.InvalidGrantException) TokenClaim(io.gravitee.am.model.TokenClaim) ClientProperties(io.gravitee.am.model.safe.ClientProperties) PermissionRequest(io.gravitee.am.model.uma.PermissionRequest) Logger(org.slf4j.Logger) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) JWT(io.gravitee.am.common.jwt.JWT) TokenRequest(io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest) Instant(java.time.Instant) RandomString(io.gravitee.am.common.utils.RandomString) Maps(io.gravitee.common.util.Maps) Token(io.gravitee.am.gateway.handler.oauth2.service.token.Token) Parameters(io.gravitee.am.common.oidc.Parameters) Claims(io.gravitee.am.common.jwt.Claims) UserProperties(io.gravitee.am.model.safe.UserProperties) OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) SimpleExecutionContext(io.gravitee.gateway.api.context.SimpleExecutionContext) OpenIDDiscoveryService(io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService) JWT(io.gravitee.am.common.jwt.JWT) SecureRandomString(io.gravitee.am.common.utils.SecureRandomString) RandomString(io.gravitee.am.common.utils.RandomString)

Aggregations

Client (io.gravitee.am.model.oidc.Client)10 Test (org.junit.Test)8 TokenRequest (io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest)7 AccessToken (io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken)7 JWT (io.gravitee.am.common.jwt.JWT)6 RefreshToken (io.gravitee.am.repository.oauth2.model.RefreshToken)5 Date (java.util.Date)5 InvalidGrantException (io.gravitee.am.gateway.handler.oauth2.exception.InvalidGrantException)3 Token (io.gravitee.am.gateway.handler.oauth2.service.token.Token)3 OAuth2Request (io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request)2 TokenService (io.gravitee.am.gateway.handler.oauth2.service.token.TokenService)2 RefreshToken (io.gravitee.am.gateway.handler.oauth2.service.token.impl.RefreshToken)2 User (io.gravitee.am.model.User)2 PermissionRequest (io.gravitee.am.model.uma.PermissionRequest)2 LinkedMultiValueMap (io.gravitee.common.util.LinkedMultiValueMap)2 Maybe (io.reactivex.Maybe)2 Single (io.reactivex.Single)2 JWTException (io.gravitee.am.common.exception.jwt.JWTException)1 InvalidRequestException (io.gravitee.am.common.exception.oauth2.InvalidRequestException)1 InvalidTokenException (io.gravitee.am.common.exception.oauth2.InvalidTokenException)1