use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.
the class TokenEnhancerTest method shouldEnhanceToken_withoutIDToken.
@Test
public void shouldEnhanceToken_withoutIDToken() {
OAuth2Request oAuth2Request = new OAuth2Request();
oAuth2Request.setClientId("client-id");
// no openid scope for the request
Client client = new Client();
Token accessToken = new AccessToken("token-id");
TestObserver<Token> testObserver = tokenEnhancer.enhance(accessToken, oAuth2Request, client, null, null).test();
testObserver.assertComplete();
testObserver.assertNoErrors();
testObserver.assertValue(accessToken1 -> accessToken1.getAdditionalInformation().isEmpty());
}
use of io.gravitee.am.gateway.handler.oauth2.service.request.OAuth2Request in project gravitee-access-management by gravitee-io.
the class TokenServiceTest method shouldCreateWithPermissions.
@Test
public void shouldCreateWithPermissions() {
OAuth2Request oAuth2Request = new OAuth2Request();
oAuth2Request.setPermissions(Arrays.asList(new PermissionRequest().setResourceId("rs_one")));
Client client = new Client();
client.setClientId("my-client-id");
ExecutionContext executionContext = mock(ExecutionContext.class);
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();
assertTrue(jwt != null && jwt.get("permissions") != null);
verify(tokenManager, times(1)).storeAccessToken(any());
verify(accessTokenRepository, never()).delete(anyString());
verify(refreshTokenRepository, never()).delete(anyString());
}
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_scopesRequest_email_address.
@Test
public void shouldCreateIDToken_withUser_scopesRequest_email_address() {
OAuth2Request oAuth2Request = new OAuth2Request();
oAuth2Request.setClientId("client-id");
oAuth2Request.setScopes(new HashSet<>(Arrays.asList("openid", "email", "address")));
oAuth2Request.setSubject("subject");
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.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);
ArgumentCaptor<JWT> jwtCaptor = ArgumentCaptor.forClass(JWT.class);
when(certificateManager.findByAlgorithm(any())).thenReturn(Maybe.just(new io.gravitee.am.gateway.certificate.CertificateProvider(certificateProvider)));
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(jwtCaptor.capture(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class))).thenReturn(Single.just("test"));
TestObserver<String> testObserver = idTokenService.create(oAuth2Request, client, user).test();
testObserver.awaitTerminalEvent();
testObserver.assertComplete();
testObserver.assertNoErrors();
verify(certificateManager).findByAlgorithm(any());
verify(certificateManager).get(any());
verify(jwtService, times(1)).encode(any(), any(io.gravitee.am.gateway.certificate.CertificateProvider.class));
JWT jwt = jwtCaptor.getValue();
assertNotNull(jwt);
assertTrue(jwt.get("sub") != null && expectedJwt.getSub().equals(jwt.get("sub")));
assertTrue(jwt.get("aud") != null && expectedJwt.getAud().equals(jwt.get("aud")));
assertTrue(jwt.get("email") != null && expectedJwt.get(StandardClaims.EMAIL).equals(jwt.get("email")));
assertTrue(jwt.get("address") != null && expectedJwt.get(StandardClaims.ADDRESS).equals(jwt.get("address")));
}
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.
@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));
}
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.
@Test
public void shouldCreateIDToken_clientOnly_defaultCertificate() {
OAuth2Request oAuth2Request = new OAuth2Request();
oAuth2Request.setClientId("client-id");
oAuth2Request.setScopes(Collections.singleton("openid"));
Client client = new Client();
client.setCertificate("certificate-client");
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(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));
}
Aggregations