use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.
the class TokenServiceTest method shouldNotRefresh_notTheSameClient.
@Test
public void shouldNotRefresh_notTheSameClient() {
String clientId = "client-id";
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.setClientId("wrong-client-id");
String token = "refresh-token";
RefreshToken refreshToken = new RefreshToken();
refreshToken.setId(token);
refreshToken.setToken(token);
refreshToken.setExpireAt(new Date(System.currentTimeMillis() + 10000));
Client client = new Client();
client.setClientId(clientId);
JWT jwt = new JWT();
jwt.setJti(token);
jwt.setAud(clientId);
jwt.setExp(refreshToken.getExpireAt().getTime() / 1000l);
when(jwtService.decodeAndVerify(any(), any(Client.class))).thenReturn(Single.just(jwt));
when(refreshTokenRepository.findByToken(any())).thenReturn(Maybe.just(refreshToken));
TestObserver<Token> testObserver = tokenService.refresh(refreshToken.getToken(), tokenRequest, client).test();
testObserver.assertNotComplete();
testObserver.assertError(InvalidGrantException.class);
verify(refreshTokenRepository, times(1)).findByToken(any());
verify(refreshTokenRepository, never()).delete(anyString());
verify(accessTokenRepository, never()).create(any());
}
use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.
the class TokenServiceTest method shouldNotRefresh_refreshNotFound.
@Test
public void shouldNotRefresh_refreshNotFound() {
String clientId = "client-id";
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.setClientId(clientId);
String token = "refresh-token";
RefreshToken refreshToken = new RefreshToken();
refreshToken.setId(token);
refreshToken.setToken(token);
refreshToken.setExpireAt(new Date(System.currentTimeMillis() + 10000));
Client client = new Client();
client.setClientId(clientId);
JWT jwt = new JWT();
jwt.setJti(token);
jwt.setAud(clientId);
jwt.setExp(refreshToken.getExpireAt().getTime() / 1000l);
when(jwtService.decodeAndVerify(eq("encoded"), any(Client.class))).thenReturn(Single.just(jwt));
when(refreshTokenRepository.findByToken(any())).thenReturn(Maybe.empty());
TestObserver<Token> testObserver = tokenService.refresh("encoded", tokenRequest, client).test();
testObserver.assertNotComplete();
testObserver.assertError(InvalidGrantException.class);
verify(refreshTokenRepository, times(1)).findByToken(any());
verify(refreshTokenRepository, never()).delete(anyString());
verify(accessTokenRepository, never()).create(any());
}
use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.
the class UserInfoEndpointHandlerTest method shouldNotInvokeUserEndpoint_unknownToken.
@Test
public void shouldNotInvokeUserEndpoint_unknownToken() throws Exception {
JWT jwt = new JWT();
jwt.setAud("client-id");
router.route().order(-1).handler(createOAuth2AuthHandler(oAuth2AuthProvider(new InvalidTokenException())));
testRequest(HttpMethod.GET, "/userinfo", req -> req.putHeader(HttpHeaders.AUTHORIZATION, "Bearer test-token"), HttpStatusCode.UNAUTHORIZED_401, "Unauthorized", null);
}
use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.
the class UserInfoEndpointHandlerTest method shouldInvokeUserEndpoint_scopesRequest_roles_groups_full_profile.
@Test
public void shouldInvokeUserEndpoint_scopesRequest_roles_groups_full_profile() throws Exception {
JWT jwt = new JWT();
jwt.setJti("id-token");
jwt.setAud("client-id");
jwt.setSub("id-subject");
jwt.setScope("openid full_profile roles groups");
Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
Role role1 = new Role();
role1.setId("role1");
role1.setName("role-1");
Role role2 = new Role();
role2.setId("role2");
role2.setName("role-2");
router.route().order(-1).handler(createOAuth2AuthHandler(oAuth2AuthProvider(jwt, client)));
User user = createUser();
user.setRolesPermissions(new HashSet<>(Arrays.asList(role1, role2)));
user.setGroups(Arrays.asList("group-1", "group-2"));
when(userService.findById(anyString())).thenReturn(Maybe.just(user));
when(userService.enhance(user)).thenReturn(Single.just(user));
testRequest(HttpMethod.GET, "/userinfo", req -> req.putHeader(HttpHeaders.AUTHORIZATION, "Bearer test-token"), resp -> resp.bodyHandler(body -> {
final Map<String, Object> claims = Json.decodeValue(body.toString(), Map.class);
assertNotNull(claims);
assertEquals(23, claims.size());
assertTrue(claims.containsKey(CustomClaims.ROLES));
assertTrue(((List) claims.get(CustomClaims.ROLES)).containsAll(Arrays.asList("role-1", "role-2")));
assertTrue(claims.containsKey(CustomClaims.GROUPS));
assertTrue(((List) claims.get(CustomClaims.GROUPS)).containsAll(Arrays.asList("group-1", "group-2")));
assertTrue(claims.containsKey("custom-claim"));
assertTrue("gravitee".equals(claims.get("custom-claim")));
assertTrue("user-id".equals(claims.get("sub")));
}), HttpStatusCode.OK_200, "OK", null);
}
use of io.gravitee.am.common.jwt.JWT in project gravitee-access-management by gravitee-io.
the class UserInfoEndpointHandlerTest method shouldInvokeUserEndpoint_scopesRequest_email.
@Test
public void shouldInvokeUserEndpoint_scopesRequest_email() throws Exception {
JWT jwt = new JWT();
jwt.setJti("id-token");
jwt.setAud("client-id");
jwt.setSub("id-subject");
jwt.setScope("openid email");
Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
router.route().order(-1).handler(createOAuth2AuthHandler(oAuth2AuthProvider(jwt, client)));
User user = createUser();
when(userService.findById(anyString())).thenReturn(Maybe.just(user));
testRequest(HttpMethod.GET, "/userinfo", req -> req.putHeader(HttpHeaders.AUTHORIZATION, "Bearer test-token"), resp -> resp.bodyHandler(body -> {
final Map<String, Object> claims = Json.decodeValue(body.toString(), Map.class);
assertNotNull(claims);
assertEquals(3, claims.size());
assertTrue(claims.containsKey(StandardClaims.EMAIL));
assertTrue(claims.containsKey(StandardClaims.EMAIL_VERIFIED));
}), HttpStatusCode.OK_200, "OK", null);
}
Aggregations