Search in sources :

Example 46 with GroupRepresentation

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

the class UserStorageTest method testUpdate.

@Test
public void testUpdate() {
    UserRepresentation thor = ApiUtil.findUserByUsername(testRealmResource(), "thor");
    // update entity
    thor.setFirstName("Stian");
    thor.setLastName("Thorgersen");
    thor.setEmailVerified(true);
    long thorCreated = System.currentTimeMillis() - 100;
    thor.setCreatedTimestamp(thorCreated);
    thor.setEmail("thor@hammer.com");
    thor.setAttributes(new HashMap<>());
    thor.getAttributes().put("test-attribute", Arrays.asList("value"));
    thor.setRequiredActions(new ArrayList<>());
    thor.getRequiredActions().add(UPDATE_PROFILE.name());
    testRealmResource().users().get(thor.getId()).update(thor);
    // check entity
    thor = ApiUtil.findUserByUsername(testRealmResource(), "thor");
    Assert.assertEquals("Stian", thor.getFirstName());
    Assert.assertEquals("Thorgersen", thor.getLastName());
    Assert.assertEquals("thor@hammer.com", thor.getEmail());
    Assert.assertTrue(thor.getAttributes().containsKey("test-attribute"));
    Assert.assertEquals(1, thor.getAttributes().get("test-attribute").size());
    Assert.assertEquals("value", thor.getAttributes().get("test-attribute").get(0));
    Assert.assertTrue(thor.isEmailVerified());
    // update group
    GroupRepresentation g = new GroupRepresentation();
    g.setName("my-group");
    String gid = ApiUtil.getCreatedId(testRealmResource().groups().add(g));
    testRealmResource().users().get(thor.getId()).joinGroup(gid);
    // check group
    boolean foundGroup = false;
    for (GroupRepresentation ug : testRealmResource().users().get(thor.getId()).groups()) {
        if (ug.getId().equals(gid)) {
            foundGroup = true;
        }
    }
    Assert.assertTrue(foundGroup);
    // check required actions
    assertTrue(thor.getRequiredActions().contains(UPDATE_PROFILE.name()));
    // remove req. actions
    thor.getRequiredActions().remove(UPDATE_PROFILE.name());
    testRealmResource().users().get(thor.getId()).update(thor);
    // change pass
    ApiUtil.resetUserPassword(testRealmResource().users().get(thor.getId()), "lightning", false);
    loginSuccessAndLogout("thor", "lightning");
    // update role
    RoleRepresentation r = new RoleRepresentation("foo-role", "foo role", false);
    testRealmResource().roles().create(r);
    ApiUtil.assignRealmRoles(testRealmResource(), thor.getId(), "foo-role");
    // check role
    boolean foundRole = false;
    for (RoleRepresentation rr : user(thor.getId()).roles().getAll().getRealmMappings()) {
        if ("foo-role".equals(rr.getName())) {
            foundRole = true;
            break;
        }
    }
    assertTrue(foundRole);
    // test removal of provider
    testRealmResource().components().component(propProviderRWId).remove();
    propProviderRWId = addComponent(newPropProviderRW());
    loginSuccessAndLogout("thor", "hammer");
    thor = ApiUtil.findUserByUsername(testRealmResource(), "thor");
    Assert.assertNull(thor.getFirstName());
    Assert.assertNull(thor.getLastName());
    Assert.assertNull(thor.getEmail());
    Assert.assertNull(thor.getAttributes());
    Assert.assertFalse(thor.isEmailVerified());
    foundGroup = false;
    for (GroupRepresentation ug : testRealmResource().users().get(thor.getId()).groups()) {
        if (ug.getId().equals(gid)) {
            foundGroup = true;
        }
    }
    Assert.assertFalse(foundGroup);
    foundRole = false;
    for (RoleRepresentation rr : user(thor.getId()).roles().getAll().getRealmMappings()) {
        if ("foo-role".equals(rr.getName())) {
            foundRole = true;
            break;
        }
    }
    assertFalse(foundRole);
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 47 with GroupRepresentation

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

the class SAMLServletAdapterTest method testUserAttributeStatementMapperUserGroupsNoAggregate.

@Test
public void testUserAttributeStatementMapperUserGroupsNoAggregate() throws Exception {
    GroupRepresentation group1 = new GroupRepresentation();
    group1.setName("group1");
    group1.setAttributes(new HashMap<>());
    group1.getAttributes().put("group-value", Arrays.asList("value1", "value2"));
    ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), AbstractSamlTest.SAML_CLIENT_ID_EMPLOYEE_2);
    ProtocolMappersResource protocolMappersResource = clientResource.getProtocolMappers();
    Map<String, String> config = new LinkedHashMap<>();
    config.put("attribute.nameformat", "Basic");
    config.put("user.attribute", "group-value");
    config.put("attribute.name", "group-attribute");
    try (AutoCloseable g1 = Creator.create(testRealmResource(), group1);
        AutoCloseable uau = UserAttributeUpdater.forUserByUsername(testRealmResource(), "bburke").setAttribute("group-value", "user-value1").setGroups("/group1").update();
        AutoCloseable c = createProtocolMapper(protocolMappersResource, "group-value", "saml", "saml-user-attribute-mapper", config)) {
        employee2ServletPage.navigateTo();
        assertCurrentUrlStartsWith(testRealmSAMLPostLoginPage);
        testRealmSAMLPostLoginPage.form().login("bburke", "password");
        driver.navigate().to(employee2ServletPage.getUriBuilder().clone().path("getAttributes").build().toURL());
        waitForPageToLoad();
        String body = driver.findElement(By.xpath("//body")).getText();
        String[] values = parseCommaSeparatedAttributes(body, "group-attribute");
        assertThat(values, arrayContaining("user-value1"));
        employee2ServletPage.logout();
        checkLoggedOut(employee2ServletPage, testRealmSAMLPostLoginPage);
    }
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) ProtocolMappersResource(org.keycloak.admin.client.resource.ProtocolMappersResource) LinkedHashMap(java.util.LinkedHashMap) AbstractSamlTest(org.keycloak.testsuite.saml.AbstractSamlTest) Test(org.junit.Test)

Example 48 with GroupRepresentation

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

the class SAMLServletAdapterTest method testUserAttributeStatementMapperUserGroupsAggregate.

@Test
public void testUserAttributeStatementMapperUserGroupsAggregate() throws Exception {
    GroupRepresentation group1 = new GroupRepresentation();
    group1.setName("group1");
    group1.setAttributes(new HashMap<>());
    group1.getAttributes().put("group-value", Arrays.asList("value1", "value2"));
    ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), AbstractSamlTest.SAML_CLIENT_ID_EMPLOYEE_2);
    ProtocolMappersResource protocolMappersResource = clientResource.getProtocolMappers();
    Map<String, String> config = new LinkedHashMap<>();
    config.put("attribute.nameformat", "Basic");
    config.put("user.attribute", "group-value");
    config.put("attribute.name", "group-attribute");
    config.put("aggregate.attrs", "true");
    try (AutoCloseable g1 = Creator.create(testRealmResource(), group1);
        AutoCloseable uau = UserAttributeUpdater.forUserByUsername(testRealmResource(), "bburke").setAttribute("group-value", "user-value1").setGroups("/group1").update();
        AutoCloseable c = createProtocolMapper(protocolMappersResource, "group-value", "saml", "saml-user-attribute-mapper", config)) {
        employee2ServletPage.navigateTo();
        assertCurrentUrlStartsWith(testRealmSAMLPostLoginPage);
        testRealmSAMLPostLoginPage.form().login("bburke", "password");
        driver.navigate().to(employee2ServletPage.getUriBuilder().clone().path("getAttributes").build().toURL());
        waitForPageToLoad();
        String body = driver.findElement(By.xpath("//body")).getText();
        String[] values = parseCommaSeparatedAttributes(body, "group-attribute");
        assertThat(values, arrayContainingInAnyOrder("user-value1", "value1", "value2"));
        employee2ServletPage.logout();
        checkLoggedOut(employee2ServletPage, testRealmSAMLPostLoginPage);
    }
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) ProtocolMappersResource(org.keycloak.admin.client.resource.ProtocolMappersResource) LinkedHashMap(java.util.LinkedHashMap) AbstractSamlTest(org.keycloak.testsuite.saml.AbstractSamlTest) Test(org.junit.Test)

Example 49 with GroupRepresentation

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

the class GroupNamePolicyTest method getGroup.

private GroupRepresentation getGroup(String path) {
    String[] parts = path.split("/");
    RealmResource realm = getRealm();
    GroupRepresentation parent = null;
    for (String part : parts) {
        if ("".equals(part)) {
            continue;
        }
        if (parent == null) {
            parent = realm.groups().groups().stream().filter(new Predicate<GroupRepresentation>() {

                @Override
                public boolean test(GroupRepresentation groupRepresentation) {
                    return part.equals(groupRepresentation.getName());
                }
            }).findFirst().get();
            continue;
        }
        GroupRepresentation group = getGroup(part, parent.getSubGroups());
        if (path.endsWith(group.getName())) {
            return group;
        }
        parent = group;
    }
    return null;
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) Predicate(java.util.function.Predicate)

Example 50 with GroupRepresentation

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

the class GroupNamePolicyTest method configureAuthorization.

@Before
public void configureAuthorization() throws Exception {
    createResource("Resource A");
    createResource("Resource B");
    createResource("Resource C");
    createGroupPolicy("Only Group A Policy", "/Group A", true);
    createGroupPolicy("Only Group B Policy", "/Group A/Group B", false);
    createGroupPolicy("Only Group C Policy", "/Group A/Group B/Group C", false);
    createResourcePermission("Resource A Permission", "Resource A", "Only Group A Policy");
    createResourcePermission("Resource B Permission", "Resource B", "Only Group B Policy");
    createResourcePermission("Resource C Permission", "Resource C", "Only Group C Policy");
    RealmResource realm = getRealm();
    GroupRepresentation group = getGroup("/Group A/Group B/Group C");
    UserRepresentation user = realm.users().search("kolo").get(0);
    realm.users().get(user.getId()).joinGroup(group.getId());
    group = getGroup("/Group A/Group B");
    user = realm.users().search("alice").get(0);
    realm.users().get(user.getId()).joinGroup(group.getId());
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Before(org.junit.Before)

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