Search in sources :

Example 1 with OAuth2Request

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

the class IDTokenServiceTest method shouldCreateIDToken_clientOnly_clientIdTokenCertificate.

@Test
public void shouldCreateIDToken_clientOnly_clientIdTokenCertificate() {
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.setClientId("client-id");
    oAuth2Request.setScopes(Collections.singleton("openid"));
    Client client = new Client();
    client.setCertificate("client-certificate");
    String idTokenPayload = "payload";
    io.gravitee.am.gateway.certificate.CertificateProvider idTokenCert = new io.gravitee.am.gateway.certificate.CertificateProvider(certificateProvider);
    io.gravitee.am.gateway.certificate.CertificateProvider clientCert = new io.gravitee.am.gateway.certificate.CertificateProvider(certificateProvider);
    io.gravitee.am.gateway.certificate.CertificateProvider defaultCert = new io.gravitee.am.gateway.certificate.CertificateProvider(defaultCertificateProvider);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.just(idTokenCert));
    when(certificateManager.get(anyString())).thenReturn(Maybe.just(clientCert));
    when(certificateManager.defaultCertificateProvider()).thenReturn(defaultCert);
    when(jwtService.encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just(idTokenPayload));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, null).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(certificateManager, times(1)).findByAlgorithm(any());
    verify(certificateManager, times(1)).get(anyString());
    verify(certificateManager, times(1)).defaultCertificateProvider();
    verify(jwtService, times(1)).encode(any(), eq(idTokenCert));
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) CertificateProvider(io.gravitee.am.certificate.api.CertificateProvider) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 2 with OAuth2Request

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

the class IDTokenServiceTest method shouldCreateIDToken_withUser_claimsRequest_acrValues.

@Test
public void shouldCreateIDToken_withUser_claimsRequest_acrValues() {
    Client client = new Client();
    User user = createUser();
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.setClientId("client-id");
    oAuth2Request.setScopes(Collections.singleton("openid"));
    oAuth2Request.setSubject("subject");
    MultiValueMap<String, String> requestParameters = new LinkedMultiValueMap<>();
    requestParameters.put("claims", Collections.singletonList("{\"id_token\":{\"acr\":{\"value\":\"urn:mace:incommon:iap:silver\",\"essential\":true}}}"));
    oAuth2Request.setParameters(requestParameters);
    io.gravitee.am.gateway.certificate.CertificateProvider defaultCert = new io.gravitee.am.gateway.certificate.CertificateProvider(defaultCertificateProvider);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.empty());
    when(certificateManager.get(any())).thenReturn(Maybe.empty());
    when(certificateManager.defaultCertificateProvider()).thenReturn(defaultCert);
    when(jwtService.encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just("test"));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    ((IDTokenServiceImpl) idTokenService).setObjectMapper(objectMapper);
    TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, user).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    ArgumentCaptor<JWT> tokenArgumentCaptor = ArgumentCaptor.forClass(JWT.class);
    verify(jwtService).encode(tokenArgumentCaptor.capture(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class));
    JWT idToken = tokenArgumentCaptor.getValue();
    assertTrue(idToken.containsKey(Claims.acr) && idToken.get(Claims.acr).equals("urn:mace:incommon:iap:silver"));
}
Also used : User(io.gravitee.am.model.User) LinkedMultiValueMap(io.gravitee.common.util.LinkedMultiValueMap) JWT(io.gravitee.am.common.jwt.JWT) IDTokenServiceImpl(io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl) OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) CertificateProvider(io.gravitee.am.certificate.api.CertificateProvider) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 3 with OAuth2Request

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

the class IDTokenServiceTest method shouldCreateIDToken_clientOnly_defaultCertificate_withEncryption.

@Test
public void shouldCreateIDToken_clientOnly_defaultCertificate_withEncryption() {
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.setClientId("client-id");
    oAuth2Request.setScopes(Collections.singleton("openid"));
    Client client = new Client();
    client.setCertificate("certificate-client");
    client.setIdTokenEncryptedResponseAlg("expectEncryption");
    String idTokenPayload = "payload";
    io.gravitee.am.gateway.certificate.CertificateProvider defaultCert = new io.gravitee.am.gateway.certificate.CertificateProvider(defaultCertificateProvider);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.empty());
    when(certificateManager.get(any())).thenReturn(Maybe.empty());
    when(certificateManager.defaultCertificateProvider()).thenReturn(defaultCert);
    when(jwtService.encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just(idTokenPayload));
    when(jweService.encryptIdToken(anyString(), any())).thenReturn(Single.just("encryptedToken"));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, null).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(certificateManager, times(1)).findByAlgorithm(any());
    verify(certificateManager, times(1)).get(anyString());
    verify(certificateManager, times(1)).defaultCertificateProvider();
    verify(jwtService, times(1)).encode(any(), eq(defaultCert));
    verify(jweService, times(1)).encryptIdToken(anyString(), any());
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) CertificateProvider(io.gravitee.am.certificate.api.CertificateProvider) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 4 with OAuth2Request

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

the class IDTokenServiceTest method shouldCreateIDToken_clientOnly_clientCertificate.

@Test
public void shouldCreateIDToken_clientOnly_clientCertificate() {
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.setClientId("client-id");
    oAuth2Request.setScopes(Collections.singleton("openid"));
    Client client = new Client();
    client.setCertificate("client-certificate");
    String idTokenPayload = "payload";
    io.gravitee.am.gateway.certificate.CertificateProvider clientCert = new io.gravitee.am.gateway.certificate.CertificateProvider(certificateProvider);
    io.gravitee.am.gateway.certificate.CertificateProvider defaultCert = new io.gravitee.am.gateway.certificate.CertificateProvider(defaultCertificateProvider);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.empty());
    when(certificateManager.get(anyString())).thenReturn(Maybe.just(clientCert));
    when(certificateManager.defaultCertificateProvider()).thenReturn(defaultCert);
    when(jwtService.encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just(idTokenPayload));
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, null).test();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(certificateManager, times(1)).findByAlgorithm(any());
    verify(certificateManager, times(1)).get(anyString());
    verify(certificateManager, times(1)).defaultCertificateProvider();
    verify(jwtService, times(1)).encode(any(), eq(clientCert));
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) CertificateProvider(io.gravitee.am.certificate.api.CertificateProvider) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 5 with OAuth2Request

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

the class ImplicitFlow method prepareResponse.

@Override
protected Single<AuthorizationResponse> prepareResponse(AuthorizationRequest authorizationRequest, Client client, User endUser) {
    OAuth2Request oAuth2Request = authorizationRequest.createOAuth2Request();
    oAuth2Request.setGrantType(GrantType.IMPLICIT);
    oAuth2Request.setSupportRefreshToken(false);
    oAuth2Request.setSubject(endUser.getId());
    oAuth2Request.getContext().put(Claims.s_hash, authorizationRequest.getState());
    if (io.gravitee.am.common.oidc.ResponseType.ID_TOKEN.equals(authorizationRequest.getResponseType())) {
        return idTokenService.create(oAuth2Request, client, endUser).map(idToken -> {
            IDTokenResponse response = new IDTokenResponse();
            response.setRedirectUri(authorizationRequest.getRedirectUri());
            response.setIdToken(idToken);
            response.setState(authorizationRequest.getState());
            return response;
        });
    } else {
        return tokenService.create(oAuth2Request, client, endUser).map(accessToken -> {
            ImplicitResponse response = new ImplicitResponse();
            response.setRedirectUri(authorizationRequest.getRedirectUri());
            response.setAccessToken(accessToken);
            response.setState(authorizationRequest.getState());
            return response;
        });
    }
}
Also used : OAuth2Request(io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request) ImplicitResponse(io.gravitee.am.gateway.handler.oauth2.service.response.ImplicitResponse) IDTokenResponse(io.gravitee.am.gateway.handler.oauth2.service.response.IDTokenResponse)

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