Java Code Examples for org.keycloak.representations.idm.RealmRepresentation#setPasswordPolicy()

The following examples show how to use org.keycloak.representations.idm.RealmRepresentation#setPasswordPolicy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void createUserWithInvalidPolicyPassword() {
    RealmRepresentation rep = realm.toRepresentation();
    String passwordPolicy = rep.getPasswordPolicy();
    rep.setPasswordPolicy("length(8)");
    realm.update(rep);
    UserRepresentation user = new UserRepresentation();
    user.setUsername("user4");
    user.setEmail("user4@localhost");
    CredentialRepresentation rawPassword = new CredentialRepresentation();
    rawPassword.setValue("ABCD");
    rawPassword.setType(CredentialRepresentation.PASSWORD);
    user.setCredentials(Arrays.asList(rawPassword));
    Response response = realm.users().create(user);
    assertEquals(400, response.getStatus());
    ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
    Assert.assertEquals("Password policy not met", error.getErrorMessage());
    rep.setPasswordPolicy(passwordPolicy);
    realm.update(rep);
    response.close();
}
 
Example 2
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void testPasswordHistoryPolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("passwordHistory(2)");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("firstPassword");
    assertTrue("Setting the first password should succeed.", alert.isDisplayed() && alert.isSuccess());

    testUserCredentialsPage.resetPassword("secondPassword");
    assertTrue("Setting the second password should succeed.", alert.isDisplayed() && alert.isSuccess());
    
    testUserCredentialsPage.resetPassword("firstPassword");
    assertTrue("Setting a password from recent history should fail.", alert.isDisplayed() && alert.isDanger());

    testUserCredentialsPage.resetPassword("thirdPassword");
    assertTrue("Setting the third password should succeed.", alert.isDisplayed() && alert.isSuccess());

    testUserCredentialsPage.resetPassword("firstPassword");
    assertTrue("Setting an older password should succeed.", alert.isDisplayed() && alert.isSuccess());
}
 
Example 3
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegexPatternsPolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("regexPattern(^[A-Z]+#[a-z]{8}$) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("invalidPassword");
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("VALID#password");
    assertAlertSuccess();
}
 
Example 4
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotUsernamePolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("notUsername(1) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword(testUser.getUsername());
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("validpassword");
    assertAlertSuccess();
}
 
Example 5
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpecialCharsPolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("specialChars(2) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("invalidPassword*");
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("validPassword*#");
    assertAlertSuccess();
}
 
Example 6
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpperCasePolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("upperCase(2) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("Invalidpassword");
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("VAlidpassword");
    assertAlertSuccess();
}
 
Example 7
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testLengthPolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("length(8) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("1234567");
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("12345678");
    assertAlertSuccess();
}
 
Example 8
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testDigitsPolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("digits(2) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("invalidPassword1");
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("validPassword12");
    assertAlertSuccess();
}
 
Example 9
Source File: PasswordPolicyTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testLowerCasePolicy() {
    RealmRepresentation realm = testRealmResource().toRepresentation();
    realm.setPasswordPolicy("lowerCase(2) and ");
    testRealmResource().update(realm);

    testUserCredentialsPage.navigateTo();
    testUserCredentialsPage.resetPassword("iNVALIDPASSWORD");
    assertAlertDanger();

    testUserCredentialsPage.resetPassword("vaLIDPASSWORD");
    assertAlertSuccess();
}
 
Example 10
Source File: HtmlUnitLoginLogoutPerfTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void addAdapterTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation examplesRealm = loadRealm("/examples-realm.json");
    examplesRealm.setPasswordPolicy("hashIterations(" + PASSWORD_HASH_ITERATIONS + ")");
    testRealms.add(examplesRealm);
}
 
Example 11
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static void readRealm(JsonParser p) throws IOException {

        // as soon as we encounter users, roles, clients we create a CreateRealmJob
        // TODO: if after that point in a realm we encounter realm attribute, we report a warning but continue

        boolean skip = false;
        try {
            RealmRepresentation r = new RealmRepresentation();
            JsonToken t = p.nextToken();
            outer:
            while (t != JsonToken.END_OBJECT && !skip) {

                //System.out.println(t + ", name: " + p.getCurrentName() + ", text: '" + p.getText() + "', value: " + p.getValueAsString());

                switch (p.getCurrentName()) {
                    case "realm":
                        r.setRealm(getStringValue(p));
                        skip = !started && realmSkipped(r.getRealm()) ;
                        if (skip) {
                            break outer;
                        }
                        break;
                    case "enabled":
                        r.setEnabled(getBooleanValue(p));
                        break;
                    case "accessTokenLifespan":
                        r.setAccessCodeLifespan(getIntegerValue(p));
                        break;
                    case "registrationAllowed":
                        r.setRegistrationAllowed(getBooleanValue(p));
                        break;
                    case "passwordPolicy":
                        r.setPasswordPolicy(getStringValue(p));
                        break;
                    case "sslRequired":
                        r.setSslRequired(getStringValue(p));
                        break;
                    case "users":
                        ensureRealm(r);
                        if (seekToStart()) {
                            enqueueFetchRealmRoles(r);
                            completePending();
                        }
                        readUsers(r, p);
                        break;
                    case "roles":
                        ensureRealm(r);
                        readRoles(r, p);
                        break;
                    case "clients":
                        ensureRealm(r);
                        readClients(r, p);
                        completePending();
                        if (seekToStart()) {
                            enqueueFetchMissingClients(r);
                            completePending();
                        }
                        break;
                    default: {
                        // if we don't understand the field we ignore it - but report that
                        log.warn("Realm attribute ignored: " + p.getCurrentName());
                        consumeAttribute(p);
                        continue; // skip p.nextToken() at end of loop - consumeAttribute() already did it
                    }
                }

                t = p.nextToken();
            }

            if (skip) {
                log.info("Realm skipped: " + r.getRealm());
                consumeParent(p);
            }

        } finally {
            // we wait for realm to complete
            completePending();

            // reset realm specific cache
            realmCreated = false;
            clientIdMap.clear();
            realmRoleIdMap.clear();
            clientRoleIdMap.clear();
        }
    }
 
Example 12
Source File: HttpClientLoginLogoutPerfTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void addAdapterTestRealms(List<RealmRepresentation> testRealms) {
    RealmRepresentation examplesRealm = loadRealm("/test-realm.json");
    examplesRealm.setPasswordPolicy("hashIterations(" + PASSWORD_HASH_ITERATIONS + ")");
    testRealms.add(examplesRealm);
}
 
Example 13
Source File: AccountFormServiceTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setPasswordPolicy(String policy) {
    RealmRepresentation testRealm = testRealm().toRepresentation();
    testRealm.setPasswordPolicy(policy);
    testRealm().update(testRealm);
}
 
Example 14
Source File: PasswordHistoryPolicyTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setPasswordHistory(String passwordHistory) {
    log.info(String.format("Setting %s", passwordHistory));
    RealmRepresentation testRealmRepresentation = testRealmResource().toRepresentation();
    testRealmRepresentation.setPasswordPolicy(passwordHistory);
    testRealmResource().update(testRealmRepresentation);
}
 
Example 15
Source File: LoginTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setPasswordPolicy(String policy) {
    RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
    realmRep.setPasswordPolicy(policy);
    adminClient.realm("test").update(realmRep);
}
 
Example 16
Source File: ResetPasswordTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setPasswordPolicy(String policy) {
    RealmRepresentation realmRep = testRealm().toRepresentation();
    realmRep.setPasswordPolicy(policy);
    testRealm().update(realmRep);
}
 
Example 17
Source File: RegisterTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void registerPasswordPolicy() {
    /*keycloakRule.configure(new KeycloakRule.KeycloakSetup() {
        @Override
        public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
            appRealm.setPasswordPolicy(new PasswordPolicy("length"));
        }
    });*/
    RealmRepresentation realm = testRealm().toRepresentation();
    realm.setPasswordPolicy("length");
    testRealm().update(realm);

    try {
        loginPage.open();
        loginPage.clickRegister();
        registerPage.assertCurrent();

        registerPage.register("firstName", "lastName", "registerPasswordPolicy@email", "registerPasswordPolicy", "pass", "pass");

        registerPage.assertCurrent();
        assertEquals("Invalid password: minimum length 8.", registerPage.getError());

        events.expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email")
                .removeDetail(Details.USERNAME)
                .removeDetail(Details.EMAIL)
                .user((String) null).error("invalid_registration").assertEvent();

        registerPage.register("firstName", "lastName", "registerPasswordPolicy@email", "registerPasswordPolicy", "password", "password");
        assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());

        String userId = events.expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email").assertEvent().getUserId();

        events.expectLogin().user(userId).detail(Details.USERNAME, "registerpasswordpolicy").assertEvent();
    } finally {
        /*keycloakRule.configure(new KeycloakRule.KeycloakSetup() {
            @Override
            public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
                appRealm.setPasswordPolicy(new PasswordPolicy(null));
            }
        });*/
    }
}
 
Example 18
Source File: PasswordHashingTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setPasswordPolicy(String policy) {
    RealmRepresentation realmRep = testRealm().toRepresentation();
    realmRep.setPasswordPolicy(policy);
    testRealm().update(realmRep);
}
 
Example 19
Source File: RealmTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void createRealmCheckDefaultPasswordPolicy() {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setRealm("new-realm");

    adminClient.realms().create(rep);

    assertEquals(null, adminClient.realm("new-realm").toRepresentation().getPasswordPolicy());

    adminClient.realms().realm("new-realm").remove();

    rep.setPasswordPolicy("length(8)");

    adminClient.realms().create(rep);

    assertEquals("length(8)", adminClient.realm("new-realm").toRepresentation().getPasswordPolicy());

    adminClient.realms().realm("new-realm").remove();
}