Search in sources :

Example 31 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class TokenServiceTest method shouldCreate.

@Test
public void shouldCreate() {
    OAuth2Request oAuth2Request = new OAuth2Request();
    Client client = new Client();
    client.setClientId("my-client-id");
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(jwtService.encode(any(), any(Client.class))).thenReturn(Single.just(""));
    when(tokenEnhancer.enhance(any(), any(), any(), any(), any())).thenReturn(Single.just(new AccessToken("token-id")));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    doNothing().when(tokenManager).storeAccessToken(any());
    TestObserver<Token> testObserver = tokenService.create(oAuth2Request, client, null).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(tokenManager, times(1)).storeAccessToken(any());
    verify(accessTokenRepository, never()).delete(anyString());
    verify(refreshTokenRepository, never()).delete(anyString());
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) ReactableExecutionContext(io.gravitee.am.gateway.handler.context.ReactableExecutionContext) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) 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) Test(org.junit.Test)

Example 32 with OAuth2Request

use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.

the class TokenServiceTest method shouldCreateWithCustomClaims.

@Test
public void shouldCreateWithCustomClaims() {
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.getContext().put(ConstantKeys.AUTH_FLOW_CONTEXT_ATTRIBUTES_KEY, new HashMap<>());
    TokenClaim customClaim = new TokenClaim();
    customClaim.setTokenType(TokenTypeHint.ACCESS_TOKEN);
    customClaim.setClaimName("iss");
    customClaim.setClaimValue("https://custom-iss");
    TokenClaim customClaim2 = new TokenClaim();
    customClaim2.setTokenType(TokenTypeHint.ACCESS_TOKEN);
    customClaim2.setClaimName("aud");
    customClaim2.setClaimValue("my-api");
    Client client = new Client();
    client.setClientId("my-client-id");
    client.setTokenCustomClaims(Arrays.asList(customClaim, customClaim2));
    ReactableExecutionContext executionContext = mock(ReactableExecutionContext.class);
    TemplateEngine templateEngine = mock(TemplateEngine.class);
    when(templateEngine.getValue("https://custom-iss", Object.class)).thenReturn("https://custom-iss");
    when(templateEngine.getValue("my-api", Object.class)).thenReturn("my-api");
    when(executionContext.getTemplateEngine()).thenReturn(templateEngine);
    ArgumentCaptor<JWT> jwtCaptor = ArgumentCaptor.forClass(JWT.class);
    when(jwtService.encode(jwtCaptor.capture(), any(Client.class))).thenReturn(Single.just(""));
    when(tokenEnhancer.enhance(any(), any(), any(), any(), any())).thenReturn(Single.just(new AccessToken("token-id")));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    doNothing().when(tokenManager).storeAccessToken(any());
    TestObserver<Token> testObserver = tokenService.create(oAuth2Request, client, null).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    JWT jwt = jwtCaptor.getValue();
    assertNotNull(jwt);
    assertTrue(jwt.get("iss") != null && "https://custom-iss".equals(jwt.get("iss")));
    assertTrue(jwt.get("aud") != null && "my-api".equals(jwt.get("aud")));
    verify(tokenManager, times(1)).storeAccessToken(any());
    verify(accessTokenRepository, never()).delete(anyString());
    verify(refreshTokenRepository, never()).delete(anyString());
    verify(executionContext).setAttribute(eq(ConstantKeys.AUTH_FLOW_CONTEXT_ATTRIBUTES_KEY), any());
}
Also used : TemplateEngine(io.gravitee.el.TemplateEngine) OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) TokenClaim(io.gravitee.am.model.TokenClaim) JWT(io.gravitee.am.common.jwt.JWT) AccessToken(io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken) ReactableExecutionContext(io.gravitee.am.gateway.handler.context.ReactableExecutionContext) 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) Test(org.junit.Test)

Aggregations

OAuth2Request (io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request)32 Test (org.junit.Test)27 Client (io.gravitee.am.model.oidc.Client)21 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)17 AccessToken (io.gravitee.am.gateway.handler.oauth2.service.token.impl.AccessToken)15 CertificateProvider (io.gravitee.am.certificate.api.CertificateProvider)12 JWT (io.gravitee.am.common.jwt.JWT)12 Token (io.gravitee.am.gateway.handler.oauth2.service.token.Token)12 User (io.gravitee.am.model.User)10 LinkedMultiValueMap (io.gravitee.common.util.LinkedMultiValueMap)5 ReactableExecutionContext (io.gravitee.am.gateway.handler.context.ReactableExecutionContext)3 IDTokenServiceImpl (io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl)3 TokenClaim (io.gravitee.am.model.TokenClaim)3 PermissionRequest (io.gravitee.am.model.uma.PermissionRequest)3 RefreshToken (io.gravitee.am.repository.oauth2.model.RefreshToken)3 InvalidTokenException (io.gravitee.am.common.exception.oauth2.InvalidTokenException)2 JWTService (io.gravitee.am.gateway.handler.common.jwt.JWTService)2 ExecutionContextFactory (io.gravitee.am.gateway.handler.context.ExecutionContextFactory)2 InvalidGrantException (io.gravitee.am.gateway.handler.oauth2.exception.InvalidGrantException)2 TokenRequest (io.gravitee.am.gateway.handler.oauth2.service.request.TokenRequest)2