Search in sources :

Example 36 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class UserStorageTest method testEntityRemovalHooksCascade.

@Test
@Ignore
public void testEntityRemovalHooksCascade() {
    testingClient.server().run(session -> {
        UserMapStorage.realmRemovals.set(0);
        UserMapStorage.groupRemovals.set(0);
        UserMapStorage.roleRemovals.set(0);
    });
    GroupRepresentation g1 = new GroupRepresentation();
    g1.setName("group1");
    GroupRepresentation g2 = new GroupRepresentation();
    g2.setName("group2");
    String gid1 = ApiUtil.getCreatedId(testRealmResource().groups().add(g1));
    String gid2 = ApiUtil.getCreatedId(testRealmResource().groups().add(g2));
    RoleRepresentation role1 = new RoleRepresentation();
    role1.setName("role1");
    RoleRepresentation role2 = new RoleRepresentation();
    role2.setName("role2");
    testRealmResource().roles().create(role1);
    testRealmResource().roles().create(role2);
    // remove realm with groups and roles in it
    testRealmResource().remove();
    testingClient.server().run(session -> {
        Assert.assertEquals(1, UserMapStorage.realmRemovals.get());
        // check if group removal hooks were called
        Assert.assertEquals(2, UserMapStorage.groupRemovals.get());
        // check if role removal hooks were called
        Assert.assertEquals(2, UserMapStorage.roleRemovals.get());
    });
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) Ignore(org.junit.Ignore) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 37 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class UserStorageTest method testEntityRemovalHooks.

@Test
public void testEntityRemovalHooks() {
    testingClient.server().run(session -> {
        UserMapStorage.realmRemovals.set(0);
        UserMapStorage.groupRemovals.set(0);
        UserMapStorage.roleRemovals.set(0);
    });
    // remove group
    GroupRepresentation g1 = new GroupRepresentation();
    g1.setName("group1");
    GroupRepresentation g2 = new GroupRepresentation();
    g2.setName("group2");
    String gid1 = ApiUtil.getCreatedId(testRealmResource().groups().add(g1));
    String gid2 = ApiUtil.getCreatedId(testRealmResource().groups().add(g2));
    testRealmResource().groups().group(gid1).remove();
    testRealmResource().groups().group(gid2).remove();
    testingClient.server().run(session -> {
        Assert.assertEquals(2, UserMapStorage.groupRemovals.get());
        UserMapStorage.realmRemovals.set(0);
    });
    // remove role
    RoleRepresentation role1 = new RoleRepresentation();
    role1.setName("role1");
    RoleRepresentation role2 = new RoleRepresentation();
    role2.setName("role2");
    testRealmResource().roles().create(role1);
    testRealmResource().roles().create(role2);
    testRealmResource().roles().get("role1").remove();
    testRealmResource().roles().get("role2").remove();
    testingClient.server().run(session -> {
        Assert.assertEquals(2, UserMapStorage.roleRemovals.get());
        UserMapStorage.realmRemovals.set(0);
    });
    // remove realm
    RealmRepresentation testRealmRepresentation = testRealmResource().toRepresentation();
    testRealmResource().remove();
    testingClient.server().run(session -> {
        Assert.assertEquals(1, UserMapStorage.realmRemovals.get());
        UserMapStorage.realmRemovals.set(0);
    });
    // Re-create realm
    RealmRepresentation repOrig = testContext.getTestRealmReps().get(0);
    adminClient.realms().create(repOrig);
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 38 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class GroupResource method getGroup.

/**
 * @return
 */
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public GroupRepresentation getGroup() {
    this.auth.groups().requireView(group);
    GroupRepresentation rep = ModelToRepresentation.toGroupHierarchy(group, true);
    rep.setAccess(auth.groups().getAccess(group));
    return rep;
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 39 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class CustomAuthFlowOTPTest method conditionalOTPRoleForceViaGroup.

@Test
public void conditionalOTPRoleForceViaGroup() {
    // prepare config - role, default to skip
    Map<String, String> config = new HashMap<>();
    config.put(FORCE_OTP_ROLE, "otp_role");
    config.put(DEFAULT_OTP_OUTCOME, SKIP);
    setConditionalOTPForm(config);
    // create otp group with role included
    GroupRepresentation group = getOrCreateOTPRoleInGroup();
    // add group to user
    testRealmResource().users().get(testUser.getId()).joinGroup(group.getId());
    // test OTP is required
    testRealmAccountManagementPage.navigateTo();
    testRealmLoginPage.form().login(testUser);
    assertTrue(loginConfigTotpPage.isCurrent());
    configureOTP();
    testRealmLoginPage.form().login(testUser);
    // verify that the page is login page, not totp setup
    assertCurrentUrlStartsWith(testLoginOneTimeCodePage);
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 40 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class RolePolicyTest method testUserWithGroupRole.

@Test
public void testUserWithGroupRole() throws InterruptedException {
    AuthzClient authzClient = getAuthzClient();
    PermissionRequest request = new PermissionRequest();
    request.setResourceId("Resource C");
    String ticket = authzClient.protection().permission().create(request).getTicket();
    assertNotNull(authzClient.authorization("alice", "password").authorize(new AuthorizationRequest(ticket)));
    UserRepresentation user = getRealm().users().search("alice").get(0);
    GroupRepresentation groupB = getRealm().groups().groups().stream().filter(representation -> "Group B".equals(representation.getName())).findFirst().get();
    getRealm().users().get(user.getId()).leaveGroup(groupB.getId());
    try {
        authzClient.authorization("alice", "password").authorize(new AuthorizationRequest(ticket));
        fail("Should fail because user is not granted with expected role");
    } catch (AuthorizationDeniedException ignore) {
    }
    request.setResourceId("Resource A");
    ticket = authzClient.protection().permission().create(request).getTicket();
    try {
        authzClient.authorization("alice", "password").authorize(new AuthorizationRequest(ticket));
        fail("Should fail because user is not granted with expected role");
    } catch (AuthorizationDeniedException ignore) {
    }
    GroupRepresentation groupA = getRealm().groups().groups().stream().filter(representation -> "Group A".equals(representation.getName())).findFirst().get();
    getRealm().users().get(user.getId()).joinGroup(groupA.getId());
    assertNotNull(authzClient.authorization("alice", "password").authorize(new AuthorizationRequest(ticket)));
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) AuthorizationDeniedException(org.keycloak.authorization.client.AuthorizationDeniedException) AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

GroupRepresentation (org.keycloak.representations.idm.GroupRepresentation)81 Test (org.junit.Test)62 RealmResource (org.keycloak.admin.client.resource.RealmResource)36 Response (javax.ws.rs.core.Response)24 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)23 List (java.util.List)17 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)17 ProtocolMappersResource (org.keycloak.admin.client.resource.ProtocolMappersResource)14 UserResource (org.keycloak.admin.client.resource.UserResource)13 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 IDToken (org.keycloak.representations.IDToken)10 OAuthClient (org.keycloak.testsuite.util.OAuthClient)10 LinkedList (java.util.LinkedList)8 Before (org.junit.Before)8 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)7 Map (java.util.Map)6 NotFoundException (javax.ws.rs.NotFoundException)6 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)6