Search in sources :

Example 11 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class AbstractServletAuthzAdapterTest method testGrantAdministrativePermissions.

@Test
public void testGrantAdministrativePermissions() throws Exception {
    performTests(() -> {
        login("jdoe", "jdoe");
        navigateToAdminPage();
        assertWasDenied();
        RealmResource realmResource = realmsResouce().realm(REALM_NAME);
        UsersResource usersResource = realmResource.users();
        List<UserRepresentation> users = usersResource.search("jdoe", null, null, null, null, null);
        assertFalse(users.isEmpty());
        UserResource userResource = usersResource.get(users.get(0).getId());
        RoleRepresentation adminRole = realmResource.roles().get("admin").toRepresentation();
        userResource.roles().realmLevel().add(Arrays.asList(adminRole));
        login("jdoe", "jdoe");
        navigateToAdminPage();
        assertWasNotDenied();
    });
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) UserResource(org.keycloak.admin.client.resource.UserResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 12 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class AbstractIdentityProviderMapperTest method findUser.

protected UserRepresentation findUser(String realm, String userName, String email) {
    UsersResource consumerUsers = adminClient.realm(realm).users();
    List<UserRepresentation> users = consumerUsers.list();
    assertThat("There must be exactly one user", users, hasSize(1));
    UserRepresentation user = users.get(0);
    assertThat("Username has to match", user.getUsername(), equalTo(userName));
    assertThat("Email has to match", user.getEmail(), equalTo(email));
    MappingsRepresentation roles = consumerUsers.get(user.getId()).roles().getAll();
    List<String> realmRoles = roles.getRealmMappings().stream().map(RoleRepresentation::getName).collect(Collectors.toList());
    user.setRealmRoles(realmRoles);
    Map<String, List<String>> clientRoles = new HashMap<>();
    if (roles.getClientMappings() != null) {
        roles.getClientMappings().forEach((key, value) -> clientRoles.put(key, value.getMappings().stream().map(RoleRepresentation::getName).collect(Collectors.toList())));
    }
    user.setClientRoles(clientRoles);
    return user;
}
Also used : MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) HashMap(java.util.HashMap) UsersResource(org.keycloak.admin.client.resource.UsersResource) List(java.util.List) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 13 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class KcOidcBrokerNoLoginHintTest method loginUser.

@Override
protected void loginUser() {
    driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
    driver.navigate().to(driver.getCurrentUrl() + "&login_hint=" + USER_EMAIL);
    log.debug("Clicking social " + bc.getIDPAlias());
    loginPage.clickSocial(bc.getIDPAlias());
    waitForPage(driver, "sign in to", true);
    Assert.assertTrue("Driver should be on the provider realm page right now", driver.getCurrentUrl().contains("/auth/realms/" + bc.providerRealmName() + "/"));
    Assert.assertTrue("User identifiant should not be fullfilled", StringUtils.isBlank(loginPage.getUsername()));
    log.debug("Logging in");
    loginPage.login(bc.getUserLogin(), bc.getUserPassword());
    waitForPage(driver, "update account information", false);
    updateAccountInformationPage.assertCurrent();
    Assert.assertTrue("We must be on correct realm right now", driver.getCurrentUrl().contains("/auth/realms/" + bc.consumerRealmName() + "/"));
    log.debug("Updating info on updateAccount page");
    updateAccountInformationPage.updateAccountInformation(bc.getUserLogin(), bc.getUserEmail(), "Firstname", "Lastname");
    UsersResource consumerUsers = adminClient.realm(bc.consumerRealmName()).users();
    int userCount = consumerUsers.count();
    Assert.assertTrue("There must be at least one user", userCount > 0);
    List<UserRepresentation> users = consumerUsers.search("", 0, userCount);
    boolean isUserFound = false;
    for (UserRepresentation user : users) {
        if (user.getUsername().equals(bc.getUserLogin()) && user.getEmail().equals(bc.getUserEmail())) {
            isUserFound = true;
            break;
        }
    }
    Assert.assertTrue("There must be user " + bc.getUserLogin() + " in realm " + bc.consumerRealmName(), isUserFound);
}
Also used : UsersResource(org.keycloak.admin.client.resource.UsersResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 14 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class UserPolicyManagementTest method configureTest.

@Before
public void configureTest() {
    super.configureTest();
    UsersResource users = testRealmResource().users();
    users.create(UserBuilder.create().username("user a").build());
    users.create(UserBuilder.create().username("user b").build());
    users.create(UserBuilder.create().username("user c").build());
}
Also used : UsersResource(org.keycloak.admin.client.resource.UsersResource) Before(org.junit.Before)

Example 15 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project openremote by openremote.

the class KeycloakDemoSetup method onStart.

@Override
public void onStart() throws Exception {
    super.onStart();
    // Tenants
    masterTenant = identityService.getIdentityProvider().getTenantForRealm(Constants.MASTER_REALM);
    Tenant customerA = new Tenant();
    customerA.setRealm("customerA");
    customerA.setDisplayName("Customer A");
    customerA.setEnabled(true);
    keycloakProvider.createTenant(new ClientRequestInfo(null, accessToken), customerA, emailConfig);
    customerATenant = keycloakProvider.getTenantForRealm(customerA.getRealm());
    Tenant customerB = new Tenant();
    customerB.setRealm("customerB");
    customerB.setDisplayName("Customer B");
    customerB.setEnabled(true);
    keycloakProvider.createTenant(new ClientRequestInfo(null, accessToken), customerB, emailConfig);
    customerBTenant = keycloakProvider.getTenantForRealm(customerB.getRealm());
    // Users
    String masterClientObjectId = getClientObjectId(masterClientsResource);
    RolesResource masterRolesResource = masterClientsResource.get(masterClientObjectId).roles();
    UserRepresentation testuser1 = new UserRepresentation();
    testuser1.setUsername("testuser1");
    testuser1.setFirstName("Testuserfirst");
    testuser1.setLastName("Testuserlast");
    testuser1.setEnabled(true);
    masterUsersResource.create(testuser1);
    testuser1 = masterUsersResource.search("testuser1", null, null, null, null, null).get(0);
    this.testuser1Id = testuser1.getId();
    CredentialRepresentation testuser1Credentials = new CredentialRepresentation();
    testuser1Credentials.setType("password");
    testuser1Credentials.setValue("testuser1");
    testuser1Credentials.setTemporary(false);
    masterUsersResource.get(testuser1.getId()).resetPassword(testuser1Credentials);
    masterUsersResource.get(testuser1.getId()).roles().clientLevel(masterClientObjectId).add(Arrays.asList(masterRolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(), masterRolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(), masterRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(), masterRolesResource.get(ClientRole.READ_RULES.getValue()).toRepresentation(), masterRolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation(), masterRolesResource.get(ClientRole.WRITE_RULES.getValue()).toRepresentation()));
    LOG.info("Added demo user '" + testuser1.getUsername() + "' with password '" + testuser1Credentials.getValue() + "'");
    UsersResource customerAUsersResource = keycloakProvider.getRealms(accessToken).realm("customerA").users();
    ClientsResource customerAClientsResource = keycloakProvider.getRealms(accessToken).realm("customerA").clients();
    String customerAClientObjectId = getClientObjectId(customerAClientsResource);
    RolesResource customerARolesResource = customerAClientsResource.get(customerAClientObjectId).roles();
    UserRepresentation testuser2 = new UserRepresentation();
    testuser2.setUsername("testuser2");
    testuser2.setFirstName("Testuserfirst");
    testuser2.setLastName("Testuserlast");
    testuser2.setEnabled(true);
    customerAUsersResource.create(testuser2);
    testuser2 = customerAUsersResource.search("testuser2", null, null, null, null, null).get(0);
    this.testuser2Id = testuser2.getId();
    CredentialRepresentation testuser2Credentials = new CredentialRepresentation();
    testuser2Credentials.setType("password");
    testuser2Credentials.setValue("testuser2");
    testuser2Credentials.setTemporary(false);
    customerAUsersResource.get(testuser2.getId()).resetPassword(testuser2Credentials);
    customerAUsersResource.get(testuser2.getId()).roles().clientLevel(customerAClientObjectId).add(Arrays.asList(customerARolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation()));
    LOG.info("Added demo user '" + testuser2.getUsername() + "' with password '" + testuser2Credentials.getValue() + "'");
    UserRepresentation testuser3 = new UserRepresentation();
    testuser3.setUsername("testuser3");
    testuser3.setFirstName("Testuserfirst");
    testuser3.setLastName("Testuserlast");
    testuser3.setEnabled(true);
    customerAUsersResource.create(testuser3);
    testuser3 = customerAUsersResource.search("testuser3", null, null, null, null, null).get(0);
    this.testuser3Id = testuser3.getId();
    CredentialRepresentation testuser3Credentials = new CredentialRepresentation();
    testuser3Credentials.setType("password");
    testuser3Credentials.setValue("testuser3");
    testuser3Credentials.setTemporary(false);
    customerAUsersResource.get(testuser3.getId()).resetPassword(testuser3Credentials);
    customerAUsersResource.get(testuser3.getId()).roles().clientLevel(customerAClientObjectId).add(Arrays.asList(customerARolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.WRITE_RULES.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation(), customerARolesResource.get(ClientRole.READ_RULES.getValue()).toRepresentation()));
    LOG.info("Added demo user '" + testuser3.getUsername() + "' with password '" + testuser3Credentials.getValue() + "'");
}
Also used : CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) Tenant(org.openremote.model.security.Tenant) UsersResource(org.keycloak.admin.client.resource.UsersResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) ClientRequestInfo(org.openremote.container.web.ClientRequestInfo) RolesResource(org.keycloak.admin.client.resource.RolesResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Aggregations

UsersResource (org.keycloak.admin.client.resource.UsersResource)36 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)32 Test (org.junit.Test)18 UserResource (org.keycloak.admin.client.resource.UserResource)10 RealmResource (org.keycloak.admin.client.resource.RealmResource)9 Response (javax.ws.rs.core.Response)7 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)7 Map (java.util.Map)6 List (java.util.List)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Before (org.junit.Before)5 UserSessionRepresentation (org.keycloak.representations.idm.UserSessionRepresentation)5 IdentityProviderResource (org.keycloak.admin.client.resource.IdentityProviderResource)4 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Matchers.hasSize (org.hamcrest.Matchers.hasSize)3 Assert.assertThat (org.junit.Assert.assertThat)3 RolesResource (org.keycloak.admin.client.resource.RolesResource)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3