Search in sources :

Example 21 with UsersResource

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

the class AbstractBrokerTest method loginUser.

protected void loginUser() {
    driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
    logInWithBroker(bc);
    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 22 with UsersResource

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

the class KcOidcBrokerUiLocalesDisabledTest method loginUser.

@Override
protected void loginUser() {
    driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
    driver.navigate().to(driver.getCurrentUrl());
    log.debug("Clicking social " + bc.getIDPAlias());
    loginPage.clickSocial(bc.getIDPAlias());
    waitForPage(driver, "sign in to", true);
    Assert.assertThat("Driver should be on the provider realm page right now", driver.getCurrentUrl(), containsString("/auth/realms/" + bc.providerRealmName() + "/"));
    Assert.assertThat(UI_LOCALES_PARAM + "=" + ENGLISH.toLanguageTag() + " should be part of the url", driver.getCurrentUrl(), not(containsString(UI_LOCALES_PARAM + "=" + ENGLISH.toLanguageTag())));
    loginPage.login(bc.getUserLogin(), bc.getUserPassword());
    waitForPage(driver, "update account information", false);
    updateAccountInformationPage.assertCurrent();
    Assert.assertThat("We must be on correct realm right now", driver.getCurrentUrl(), containsString("/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 23 with UsersResource

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

the class KcOidcBrokerParameterForwardTest method loginUser.

@Override
protected void loginUser() {
    driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
    String queryString = "&" + FORWARDED_PARAMETER + "=" + FORWARDED_PARAMETER_VALUE + "&" + PARAMETER_NOT_FORWARDED + "=" + "value";
    driver.navigate().to(driver.getCurrentUrl() + queryString);
    log.debug("Clicking social " + bc.getIDPAlias());
    loginPage.clickSocial(bc.getIDPAlias());
    waitForPage(driver, "sign in to", true);
    Assert.assertThat("Driver should be on the provider realm page right now", driver.getCurrentUrl(), containsString("/auth/realms/" + bc.providerRealmName() + "/"));
    Assert.assertThat(FORWARDED_PARAMETER + "=" + FORWARDED_PARAMETER_VALUE + " should be part of the url", driver.getCurrentUrl(), containsString(FORWARDED_PARAMETER + "=" + FORWARDED_PARAMETER_VALUE));
    Assert.assertThat("\"" + PARAMETER_NOT_SET + "\"" + " should NOT be part of the url", driver.getCurrentUrl(), not(containsString(PARAMETER_NOT_SET)));
    Assert.assertThat("\"" + PARAMETER_NOT_FORWARDED + "\"" + " should be NOT part of the url", driver.getCurrentUrl(), not(containsString(PARAMETER_NOT_FORWARDED)));
    loginPage.login(bc.getUserLogin(), bc.getUserPassword());
    waitForPage(driver, "update account information", false);
    updateAccountInformationPage.assertCurrent();
    Assert.assertThat("We must be on correct realm right now", driver.getCurrentUrl(), containsString("/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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 24 with UsersResource

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

the class KcOidcBrokerTest method loginFetchingUserFromUserEndpoint.

@Test
public void loginFetchingUserFromUserEndpoint() {
    RealmResource realm = realmsResouce().realm(bc.providerRealmName());
    ClientsResource clients = realm.clients();
    ClientRepresentation brokerApp = clients.findByClientId("brokerapp").get(0);
    try {
        IdentityProviderResource identityProviderResource = realmsResouce().realm(bc.consumerRealmName()).identityProviders().get(bc.getIDPAlias());
        IdentityProviderRepresentation idp = identityProviderResource.toRepresentation();
        idp.getConfig().put(OIDCIdentityProviderConfig.JWKS_URL, getProviderRoot() + "/auth/realms/" + REALM_PROV_NAME + "/protocol/openid-connect/certs");
        identityProviderResource.update(idp);
        brokerApp.getAttributes().put(OIDCConfigAttributes.USER_INFO_RESPONSE_SIGNATURE_ALG, Algorithm.RS256);
        brokerApp.getAttributes().put("validateSignature", Boolean.TRUE.toString());
        clients.get(brokerApp.getId()).update(brokerApp);
        driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
        logInWithBroker(bc);
        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);
    } finally {
        brokerApp.getAttributes().put(OIDCConfigAttributes.USER_INFO_RESPONSE_SIGNATURE_ALG, null);
        brokerApp.getAttributes().put("validateSignature", Boolean.FALSE.toString());
        clients.get(brokerApp.getId()).update(brokerApp);
    }
}
Also used : IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) RealmResource(org.keycloak.admin.client.resource.RealmResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 25 with UsersResource

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

the class KcOidcBrokerTest method checkUpdatedUserAttributesIdP.

private void checkUpdatedUserAttributesIdP(boolean isForceSync) {
    final String IDP_NAME = getBrokerConfiguration().getIDPAlias();
    final String USERNAME = "demoUser";
    final String FIRST_NAME = "John";
    final String LAST_NAME = "Doe";
    final String EMAIL = "mail@example.com";
    final String NEW_FIRST_NAME = "Jack";
    final String NEW_LAST_NAME = "Doee";
    final String NEW_EMAIL = "mail123@example.com";
    UsersResource providerUserResource = Optional.ofNullable(realmsResouce().realm(bc.providerRealmName()).users()).orElse(null);
    assertThat("Cannot get User Resource from Provider realm", providerUserResource, Matchers.notNullValue());
    String userID = createUser(bc.providerRealmName(), USERNAME, USERNAME, FIRST_NAME, LAST_NAME, EMAIL);
    assertThat("Cannot create user : " + USERNAME, userID, Matchers.notNullValue());
    try {
        UserRepresentation user = Optional.ofNullable(providerUserResource.get(userID).toRepresentation()).orElse(null);
        assertThat("Cannot get user from provider", user, Matchers.notNullValue());
        IdentityProviderResource consumerIdentityResource = Optional.ofNullable(getIdentityProviderResource()).orElse(null);
        assertThat("Cannot get Identity Provider resource", consumerIdentityResource, Matchers.notNullValue());
        IdentityProviderRepresentation idProvider = Optional.ofNullable(consumerIdentityResource.toRepresentation()).orElse(null);
        assertThat("Cannot get Identity Provider", idProvider, Matchers.notNullValue());
        updateIdPSyncMode(idProvider, consumerIdentityResource, isForceSync ? IdentityProviderSyncMode.FORCE : IdentityProviderSyncMode.IMPORT);
        driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
        WaitUtils.waitForPageToLoad();
        assertThat(driver.getTitle(), Matchers.containsString("Sign in to " + bc.consumerRealmName()));
        logInWithIdp(IDP_NAME, USERNAME, USERNAME);
        accountUpdateProfilePage.assertCurrent();
        logoutFromRealm(getProviderRoot(), bc.providerRealmName());
        logoutFromRealm(getConsumerRoot(), bc.consumerRealmName());
        driver.navigate().to(getAccountUrl(getProviderRoot(), bc.providerRealmName()));
        WaitUtils.waitForPageToLoad();
        assertThat(driver.getTitle(), Matchers.containsString("Sign in to " + bc.providerRealmName()));
        loginPage.login(USERNAME, USERNAME);
        WaitUtils.waitForPageToLoad();
        accountUpdateProfilePage.assertCurrent();
        accountUpdateProfilePage.updateProfile(NEW_FIRST_NAME, NEW_LAST_NAME, NEW_EMAIL);
        logoutFromRealm(getProviderRoot(), bc.providerRealmName());
        driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
        WaitUtils.waitForPageToLoad();
        assertThat(driver.getTitle(), Matchers.containsString("Sign in to " + bc.consumerRealmName()));
        logInWithIdp(IDP_NAME, USERNAME, USERNAME);
        accountUpdateProfilePage.assertCurrent();
        assertThat(accountUpdateProfilePage.getEmail(), Matchers.equalTo(isForceSync ? NEW_EMAIL : EMAIL));
        assertThat(accountUpdateProfilePage.getFirstName(), Matchers.equalTo(isForceSync ? NEW_FIRST_NAME : FIRST_NAME));
        assertThat(accountUpdateProfilePage.getLastName(), Matchers.equalTo(isForceSync ? NEW_LAST_NAME : LAST_NAME));
    } finally {
        providerUserResource.delete(userID);
        assertThat("User wasn't deleted", providerUserResource.search(USERNAME).size(), Matchers.is(0));
    }
}
Also used : IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) 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