Search in sources :

Example 1 with IDTokenServiceImpl

use of io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl 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 2 with IDTokenServiceImpl

use of io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl in project gravitee-access-management by gravitee-io.

the class IDTokenServiceTest method shouldCreateIDToken_withUser_claimsRequest.

@Test
public void shouldCreateIDToken_withUser_claimsRequest() {
    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\":{\"name\":{\"essential\":true}}}"));
    oAuth2Request.setParameters(requestParameters);
    Client client = new Client();
    client.setCertificate("certid");
    User user = createUser();
    JWT expectedJwt = new JWT();
    expectedJwt.setSub(user.getId());
    expectedJwt.setAud("client-id");
    expectedJwt.setIss(null);
    expectedJwt.put(StandardClaims.NAME, user.getAdditionalInformation().get(StandardClaims.NAME));
    expectedJwt.setIat(System.currentTimeMillis() / 1000l);
    expectedJwt.setExp(expectedJwt.getIat() + 14400);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.empty());
    when(certificateManager.defaultCertificateProvider()).thenReturn(new io.gravitee.am.gateway.certificate.CertificateProvider(defaultCertificateProvider));
    when(certificateManager.get(anyString())).thenReturn(Maybe.just(new io.gravitee.am.gateway.certificate.CertificateProvider(certificateProvider)));
    when(jwtService.encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just("test"));
    ((IDTokenServiceImpl) idTokenService).setObjectMapper(objectMapper);
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, user).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(certificateManager, times(1)).get(anyString());
    verify(jwtService, times(1)).encode(eq(expectedJwt), any(io.gravitee.am.gateway.certificate.CertificateProvider.class));
}
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 IDTokenServiceImpl

use of io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl in project gravitee-access-management by gravitee-io.

the class IDTokenServiceTest method shouldCreateIDToken_withUser_scopesRequest_and_claimsRequest.

@Test
public void shouldCreateIDToken_withUser_scopesRequest_and_claimsRequest() {
    OAuth2Request oAuth2Request = new OAuth2Request();
    oAuth2Request.setClientId("client-id");
    oAuth2Request.setScopes(new HashSet<>(Arrays.asList("openid", "email", "address")));
    oAuth2Request.setSubject("subject");
    MultiValueMap<String, String> requestParameters = new LinkedMultiValueMap<>();
    requestParameters.put("claims", Collections.singletonList("{\"id_token\":{\"name\":{\"essential\":true}}}"));
    oAuth2Request.setParameters(requestParameters);
    Client client = new Client();
    User user = createUser();
    JWT expectedJwt = new JWT();
    expectedJwt.setSub(user.getId());
    expectedJwt.setAud("client-id");
    expectedJwt.put(StandardClaims.ADDRESS, user.getAdditionalInformation().get(StandardClaims.ADDRESS));
    expectedJwt.put(StandardClaims.EMAIL_VERIFIED, user.getAdditionalInformation().get(StandardClaims.EMAIL_VERIFIED));
    expectedJwt.setIss(null);
    expectedJwt.put(StandardClaims.NAME, user.getAdditionalInformation().get(StandardClaims.NAME));
    expectedJwt.setExp((System.currentTimeMillis() / 1000l) + 14400);
    expectedJwt.setIat(System.currentTimeMillis() / 1000l);
    expectedJwt.put(StandardClaims.EMAIL, user.getAdditionalInformation().get(StandardClaims.EMAIL));
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContextFactory.create(any())).thenReturn(executionContext);
    when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.empty());
    when(certificateManager.defaultCertificateProvider()).thenReturn(new io.gravitee.am.gateway.certificate.CertificateProvider(defaultCertificateProvider));
    when(certificateManager.get(any())).thenReturn(Maybe.just(new io.gravitee.am.gateway.certificate.CertificateProvider(certificateProvider)));
    when(jwtService.encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just("test"));
    ((IDTokenServiceImpl) idTokenService).setObjectMapper(objectMapper);
    TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, user).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(certificateManager, times(1)).get(any());
    verify(jwtService, times(1)).encode(eq(expectedJwt), any(io.gravitee.am.gateway.certificate.CertificateProvider.class));
}
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)

Aggregations

CertificateProvider (io.gravitee.am.certificate.api.CertificateProvider)3 JWT (io.gravitee.am.common.jwt.JWT)3 OAuth2Request (io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request)3 IDTokenServiceImpl (io.gravitee.am.gateway.handler.oidc.service.idtoken.impl.IDTokenServiceImpl)3 User (io.gravitee.am.model.User)3 Client (io.gravitee.am.model.oidc.Client)3 LinkedMultiValueMap (io.gravitee.common.util.LinkedMultiValueMap)3 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)3 Test (org.junit.Test)3