Search in sources :

Example 1 with OAuth2AuthProvider

use of io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider 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);
}
Also used : Role(io.gravitee.am.model.Role) Json(io.vertx.core.json.Json) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) Role(io.gravitee.am.model.Role) UserService(io.gravitee.am.service.UserService) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) InvalidTokenException(io.gravitee.am.common.exception.oauth2.InvalidTokenException) Single(io.reactivex.Single) Matchers.anyString(org.mockito.Matchers.anyString) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) ExceptionHandler(io.gravitee.am.gateway.handler.oauth2.resources.handler.ExceptionHandler) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AsyncResult(io.vertx.core.AsyncResult) OAuth2AuthHandler(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthHandler) User(io.gravitee.am.model.User) ServerErrorException(io.gravitee.am.gateway.handler.oauth2.exception.ServerErrorException) InjectMocks(org.mockito.InjectMocks) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) OAuth2AuthResponse(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthResponse) JWT(io.gravitee.am.common.jwt.JWT) JWEService(io.gravitee.am.gateway.handler.oidc.service.jwe.JWEService) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Scope(io.gravitee.am.common.oidc.Scope) Future(io.vertx.core.Future) InvalidClientException(io.gravitee.am.gateway.handler.oauth2.exception.InvalidClientException) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) Matchers.any(org.mockito.Matchers.any) MediaType(io.gravitee.common.http.MediaType) HttpMethod(io.vertx.core.http.HttpMethod) CustomClaims(io.gravitee.am.common.oidc.CustomClaims) OAuth2AuthProvider(io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) OpenIDDiscoveryService(io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.model.User) JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 2 with OAuth2AuthProvider

use of io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider 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);
}
Also used : Json(io.vertx.core.json.Json) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) Role(io.gravitee.am.model.Role) UserService(io.gravitee.am.service.UserService) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) InvalidTokenException(io.gravitee.am.common.exception.oauth2.InvalidTokenException) Single(io.reactivex.Single) Matchers.anyString(org.mockito.Matchers.anyString) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) ExceptionHandler(io.gravitee.am.gateway.handler.oauth2.resources.handler.ExceptionHandler) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AsyncResult(io.vertx.core.AsyncResult) OAuth2AuthHandler(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthHandler) User(io.gravitee.am.model.User) ServerErrorException(io.gravitee.am.gateway.handler.oauth2.exception.ServerErrorException) InjectMocks(org.mockito.InjectMocks) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) OAuth2AuthResponse(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthResponse) JWT(io.gravitee.am.common.jwt.JWT) JWEService(io.gravitee.am.gateway.handler.oidc.service.jwe.JWEService) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Scope(io.gravitee.am.common.oidc.Scope) Future(io.vertx.core.Future) InvalidClientException(io.gravitee.am.gateway.handler.oauth2.exception.InvalidClientException) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) Matchers.any(org.mockito.Matchers.any) MediaType(io.gravitee.common.http.MediaType) HttpMethod(io.vertx.core.http.HttpMethod) CustomClaims(io.gravitee.am.common.oidc.CustomClaims) OAuth2AuthProvider(io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) OpenIDDiscoveryService(io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.model.User) JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 3 with OAuth2AuthProvider

use of io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider in project gravitee-access-management by gravitee-io.

the class UserInfoEndpointHandlerTest method shouldInvokeUserEndpoint_scopesRequest.

@Test
public void shouldInvokeUserEndpoint_scopesRequest() throws Exception {
    JWT jwt = new JWT();
    jwt.setJti("id-token");
    jwt.setAud("client-id");
    jwt.setSub("id-subject");
    jwt.setScope("openid profile");
    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(15, claims.size());
    }), HttpStatusCode.OK_200, "OK", null);
}
Also used : Json(io.vertx.core.json.Json) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) Role(io.gravitee.am.model.Role) UserService(io.gravitee.am.service.UserService) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) InvalidTokenException(io.gravitee.am.common.exception.oauth2.InvalidTokenException) Single(io.reactivex.Single) Matchers.anyString(org.mockito.Matchers.anyString) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) ExceptionHandler(io.gravitee.am.gateway.handler.oauth2.resources.handler.ExceptionHandler) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AsyncResult(io.vertx.core.AsyncResult) OAuth2AuthHandler(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthHandler) User(io.gravitee.am.model.User) ServerErrorException(io.gravitee.am.gateway.handler.oauth2.exception.ServerErrorException) InjectMocks(org.mockito.InjectMocks) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) OAuth2AuthResponse(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthResponse) JWT(io.gravitee.am.common.jwt.JWT) JWEService(io.gravitee.am.gateway.handler.oidc.service.jwe.JWEService) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Scope(io.gravitee.am.common.oidc.Scope) Future(io.vertx.core.Future) InvalidClientException(io.gravitee.am.gateway.handler.oauth2.exception.InvalidClientException) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) Matchers.any(org.mockito.Matchers.any) MediaType(io.gravitee.common.http.MediaType) HttpMethod(io.vertx.core.http.HttpMethod) CustomClaims(io.gravitee.am.common.oidc.CustomClaims) OAuth2AuthProvider(io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) OpenIDDiscoveryService(io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.model.User) JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 4 with OAuth2AuthProvider

use of io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider in project gravitee-access-management by gravitee-io.

the class UserInfoEndpointHandlerTest method shouldInvokeUserEndpoint_scopesRequest_groups.

@Test
public void shouldInvokeUserEndpoint_scopesRequest_groups() throws Exception {
    JWT jwt = new JWT();
    jwt.setJti("id-token");
    jwt.setAud("client-id");
    jwt.setSub("id-subject");
    jwt.setScope("openid groups");
    Client client = new Client();
    client.setId("client-id");
    client.setClientId("client-id");
    router.route().order(-1).handler(createOAuth2AuthHandler(oAuth2AuthProvider(jwt, client)));
    User user = createUser();
    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(2, claims.size());
        assertTrue(claims.containsKey(CustomClaims.GROUPS));
        assertTrue(((List) claims.get(CustomClaims.GROUPS)).containsAll(Arrays.asList("group-1", "group-2")));
    }), HttpStatusCode.OK_200, "OK", null);
}
Also used : Json(io.vertx.core.json.Json) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) Role(io.gravitee.am.model.Role) UserService(io.gravitee.am.service.UserService) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) InvalidTokenException(io.gravitee.am.common.exception.oauth2.InvalidTokenException) Single(io.reactivex.Single) Matchers.anyString(org.mockito.Matchers.anyString) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) ExceptionHandler(io.gravitee.am.gateway.handler.oauth2.resources.handler.ExceptionHandler) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AsyncResult(io.vertx.core.AsyncResult) OAuth2AuthHandler(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthHandler) User(io.gravitee.am.model.User) ServerErrorException(io.gravitee.am.gateway.handler.oauth2.exception.ServerErrorException) InjectMocks(org.mockito.InjectMocks) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) OAuth2AuthResponse(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthResponse) JWT(io.gravitee.am.common.jwt.JWT) JWEService(io.gravitee.am.gateway.handler.oidc.service.jwe.JWEService) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Scope(io.gravitee.am.common.oidc.Scope) Future(io.vertx.core.Future) InvalidClientException(io.gravitee.am.gateway.handler.oauth2.exception.InvalidClientException) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) Matchers.any(org.mockito.Matchers.any) MediaType(io.gravitee.common.http.MediaType) HttpMethod(io.vertx.core.http.HttpMethod) CustomClaims(io.gravitee.am.common.oidc.CustomClaims) OAuth2AuthProvider(io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) OpenIDDiscoveryService(io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.model.User) JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 5 with OAuth2AuthProvider

use of io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider in project gravitee-access-management by gravitee-io.

the class UserInfoEndpointHandlerTest method shouldInvokeUserEndpoint_scopesRequest_roles_noRole.

@Test
public void shouldInvokeUserEndpoint_scopesRequest_roles_noRole() throws Exception {
    JWT jwt = new JWT();
    jwt.setJti("id-token");
    jwt.setAud("client-id");
    jwt.setSub("id-subject");
    jwt.setScope("openid roles");
    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));
    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(1, claims.size());
        assertTrue(!claims.containsKey(CustomClaims.ROLES));
    }), HttpStatusCode.OK_200, "OK", null);
}
Also used : Json(io.vertx.core.json.Json) java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) Role(io.gravitee.am.model.Role) UserService(io.gravitee.am.service.UserService) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) InvalidTokenException(io.gravitee.am.common.exception.oauth2.InvalidTokenException) Single(io.reactivex.Single) Matchers.anyString(org.mockito.Matchers.anyString) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) JWTService(io.gravitee.am.gateway.handler.common.jwt.JWTService) ExceptionHandler(io.gravitee.am.gateway.handler.oauth2.resources.handler.ExceptionHandler) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AsyncResult(io.vertx.core.AsyncResult) OAuth2AuthHandler(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthHandler) User(io.gravitee.am.model.User) ServerErrorException(io.gravitee.am.gateway.handler.oauth2.exception.ServerErrorException) InjectMocks(org.mockito.InjectMocks) StandardClaims(io.gravitee.am.common.oidc.StandardClaims) OAuth2AuthResponse(io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthResponse) JWT(io.gravitee.am.common.jwt.JWT) JWEService(io.gravitee.am.gateway.handler.oidc.service.jwe.JWEService) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Scope(io.gravitee.am.common.oidc.Scope) Future(io.vertx.core.Future) InvalidClientException(io.gravitee.am.gateway.handler.oauth2.exception.InvalidClientException) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) Matchers.any(org.mockito.Matchers.any) MediaType(io.gravitee.common.http.MediaType) HttpMethod(io.vertx.core.http.HttpMethod) CustomClaims(io.gravitee.am.common.oidc.CustomClaims) OAuth2AuthProvider(io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) OpenIDDiscoveryService(io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.model.User) JWT(io.gravitee.am.common.jwt.JWT) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Aggregations

InvalidTokenException (io.gravitee.am.common.exception.oauth2.InvalidTokenException)10 JWT (io.gravitee.am.common.jwt.JWT)10 CustomClaims (io.gravitee.am.common.oidc.CustomClaims)10 Scope (io.gravitee.am.common.oidc.Scope)10 StandardClaims (io.gravitee.am.common.oidc.StandardClaims)10 JWTService (io.gravitee.am.gateway.handler.common.jwt.JWTService)10 RxWebTestBase (io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase)10 OAuth2AuthHandler (io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthHandler)10 OAuth2AuthResponse (io.gravitee.am.gateway.handler.common.vertx.web.auth.handler.OAuth2AuthResponse)10 OAuth2AuthProvider (io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.OAuth2AuthProvider)10 InvalidClientException (io.gravitee.am.gateway.handler.oauth2.exception.InvalidClientException)10 ServerErrorException (io.gravitee.am.gateway.handler.oauth2.exception.ServerErrorException)10 ExceptionHandler (io.gravitee.am.gateway.handler.oauth2.resources.handler.ExceptionHandler)10 OpenIDDiscoveryService (io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDDiscoveryService)10 JWEService (io.gravitee.am.gateway.handler.oidc.service.jwe.JWEService)10 Role (io.gravitee.am.model.Role)10 User (io.gravitee.am.model.User)10 Client (io.gravitee.am.model.oidc.Client)10 UserService (io.gravitee.am.service.UserService)10 HttpHeaders (io.gravitee.common.http.HttpHeaders)10