Search in sources :

Example 1 with Role

use of io.gravitee.am.model.Role 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 Role

use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.

the class RolesResourceTest method shouldSearchRoles.

@Test
public void shouldSearchRoles() {
    final String domainId = "domain-1";
    final Domain mockDomain = new Domain();
    mockDomain.setId(domainId);
    final Role mockRole = new Role();
    mockRole.setId("role-1-id");
    mockRole.setName("role-1-name");
    mockRole.setReferenceId(domainId);
    final Role mockRole2 = new Role();
    mockRole2.setId("role-2-id");
    mockRole2.setName("role-2-name");
    mockRole2.setReferenceId(domainId);
    final Set<Role> roles = new HashSet<>(Arrays.asList(mockRole, mockRole2));
    final Page<Role> pagedRoles = new Page<>(roles, 0, 2);
    doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
    doReturn(Single.just(roles)).when(roleService).findByDomain(domainId);
    doReturn(Single.just(pagedRoles)).when(roleService).searchByDomain(domainId, "*role-2-name*", 0, 50);
    final Response response = target("domains").path(domainId).path("roles").queryParam("q", "*role-2-name*").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    JsonArray pageArray = new JsonObject(readEntity(response, String.class)).getJsonArray("data");
    assertTrue(pageArray.size() == 2);
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) Response(javax.ws.rs.core.Response) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Page(io.gravitee.am.model.common.Page) Domain(io.gravitee.am.model.Domain) HashSet(java.util.HashSet) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 3 with Role

use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.

the class RolesResourceTest method shouldGetRoles.

@Test
public void shouldGetRoles() {
    final String domainId = "domain-1";
    final Domain mockDomain = new Domain();
    mockDomain.setId(domainId);
    final Role mockRole = new Role();
    mockRole.setId("role-1-id");
    mockRole.setName("role-1-name");
    mockRole.setReferenceId(domainId);
    final Role mockRole2 = new Role();
    mockRole2.setId("role-2-id");
    mockRole2.setName("role-2-name");
    mockRole2.setReferenceId(domainId);
    final Set<Role> roles = new HashSet<>(Arrays.asList(mockRole, mockRole2));
    final Page<Role> pagedRoles = new Page<>(roles, 0, 2);
    doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
    doReturn(Single.just(pagedRoles)).when(roleService).findByDomain(domainId, 0, 50);
    final Response response = target("domains").path(domainId).path("roles").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    JsonArray pageArray = new JsonObject(readEntity(response, String.class)).getJsonArray("data");
    assertTrue(pageArray.size() == 2);
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) Response(javax.ws.rs.core.Response) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Page(io.gravitee.am.model.common.Page) Domain(io.gravitee.am.model.Domain) HashSet(java.util.HashSet) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 4 with Role

use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.

the class RolesResourceTest method shouldCreate.

@Test
public void shouldCreate() {
    final String domainId = "domain-1";
    final Domain mockDomain = new Domain();
    mockDomain.setId(domainId);
    NewRole newRole = new NewRole();
    newRole.setName("role-name");
    Role role = new Role();
    role.setId("role-id");
    role.setName("role-name");
    doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
    doReturn(Single.just(role)).when(roleService).create(eq(domainId), any(), any());
    final Response response = target("domains").path(domainId).path("roles").request().post(Entity.json(newRole));
    assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) Response(javax.ws.rs.core.Response) NewRole(io.gravitee.am.service.model.NewRole) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 5 with Role

use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.

the class JdbcRoleRepository method search.

@Override
public Single<Page<Role>> search(ReferenceType referenceType, String referenceId, String query, int page, int size) {
    LOGGER.debug("search({}, {}, {}, {}, {})", referenceType, referenceId, query, page, size);
    boolean wildcardSearch = query.contains("*");
    String wildcardValue = query.replaceAll("\\*+", "%");
    String search = this.databaseDialectHelper.buildSearchRoleQuery(wildcardSearch, page, size);
    String count = this.databaseDialectHelper.buildCountRoleQuery(wildcardSearch);
    return fluxToFlowable(template.getDatabaseClient().sql(search).bind("value", wildcardSearch ? wildcardValue : query).bind("refId", referenceId).bind("refType", referenceType.name()).map(row -> rowMapper.read(JdbcRole.class, row)).all()).map(this::toEntity).flatMap(role -> completeWithScopes(Maybe.just(role), role.getId()).toFlowable()).toList().flatMap(data -> monoToSingle(template.getDatabaseClient().sql(count).bind("value", wildcardSearch ? wildcardValue : query).bind("refId", referenceId).bind("refType", referenceType.name()).map(row -> row.get(0, Long.class)).first()).map(total -> new Page<Role>(data, page, total)));
}
Also used : SpringRoleRepository(io.gravitee.am.repository.jdbc.management.api.spring.role.SpringRoleRepository) Completable(io.reactivex.Completable) Role(io.gravitee.am.model.Role) Maybe(io.reactivex.Maybe) Query(org.springframework.data.relational.core.query.Query) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Criteria.where(org.springframework.data.relational.core.query.Criteria.where) Single(io.reactivex.Single) Function(java.util.function.Function) InitializingBean(org.springframework.beans.factory.InitializingBean) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) AbstractJdbcRepository(io.gravitee.am.repository.jdbc.management.AbstractJdbcRepository) Flowable(io.reactivex.Flowable) SpringRoleOauthScopeRepository(io.gravitee.am.repository.jdbc.management.api.spring.role.SpringRoleOauthScopeRepository) ReferenceType(io.gravitee.am.model.ReferenceType) Sort(org.springframework.data.domain.Sort) Repository(org.springframework.stereotype.Repository) Page(io.gravitee.am.model.common.Page) JdbcRole(io.gravitee.am.repository.jdbc.management.api.model.JdbcRole) Publisher(org.reactivestreams.Publisher) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) PageRequest(org.springframework.data.domain.PageRequest) Mono(reactor.core.publisher.Mono) RandomString(io.gravitee.am.common.utils.RandomString) RoleRepository(io.gravitee.am.repository.management.api.RoleRepository) Flux(reactor.core.publisher.Flux) List(java.util.List) RxJava2Adapter(reactor.adapter.rxjava.RxJava2Adapter) JdbcRole(io.gravitee.am.repository.jdbc.management.api.model.JdbcRole) Page(io.gravitee.am.model.common.Page) RandomString(io.gravitee.am.common.utils.RandomString)

Aggregations

Role (io.gravitee.am.model.Role)70 Test (org.junit.Test)53 NewRole (io.gravitee.am.service.model.NewRole)24 TestObserver (io.reactivex.observers.TestObserver)24 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)23 UpdateRole (io.gravitee.am.service.model.UpdateRole)19 User (io.gravitee.am.model.User)16 ReferenceType (io.gravitee.am.model.ReferenceType)13 Client (io.gravitee.am.model.oidc.Client)12 ApplicationScopeSettings (io.gravitee.am.model.application.ApplicationScopeSettings)10 Page (io.gravitee.am.model.common.Page)10 SystemRole (io.gravitee.am.model.permissions.SystemRole)10 RoleService (io.gravitee.am.service.RoleService)10 Maybe (io.reactivex.Maybe)10 Single (io.reactivex.Single)10 Domain (io.gravitee.am.model.Domain)9 Permission (io.gravitee.am.model.permissions.Permission)9 Event (io.gravitee.am.model.common.event.Event)8 AbstractManagementTest (io.gravitee.am.repository.management.AbstractManagementTest)8 Acl (io.gravitee.am.model.Acl)7