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));
}
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"));
}
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());
}
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));
}
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;
});
}
}
Aggregations