Search in sources :

Example 51 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testLength.

@Test
public void testLength() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "length"));
        Assert.assertEquals("invalidPasswordMinLengthMessage", policyManager.validate("jdoe", "1234567").getMessage());
        Assert.assertArrayEquals(new Object[] { 8 }, policyManager.validate("jdoe", "1234567").getParameters());
        assertNull(policyManager.validate("jdoe", "12345678"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "length(4)"));
        Assert.assertEquals("invalidPasswordMinLengthMessage", policyManager.validate("jdoe", "123").getMessage());
        Assert.assertArrayEquals(new Object[] { 4 }, policyManager.validate("jdoe", "123").getParameters());
        assertNull(policyManager.validate("jdoe", "1234"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 52 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testLowerCase.

@Test
public void testLowerCase() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "lowerCase"));
        Assert.assertEquals("invalidPasswordMinLowerCaseCharsMessage", policyManager.validate("jdoe", "ABCD1234").getMessage());
        Assert.assertArrayEquals(new Object[] { 1 }, policyManager.validate("jdoe", "ABCD1234").getParameters());
        assertNull(policyManager.validate("jdoe", "ABcD1234"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "lowerCase(2)"));
        Assert.assertEquals("invalidPasswordMinLowerCaseCharsMessage", policyManager.validate("jdoe", "ABcD1234").getMessage());
        Assert.assertArrayEquals(new Object[] { 2 }, policyManager.validate("jdoe", "ABcD1234").getParameters());
        assertNull(policyManager.validate("jdoe", "aBcD1234"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 53 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testNotUsername.

@Test
public void testNotUsername() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "notUsername"));
        Assert.assertEquals("invalidPasswordNotUsernameMessage", policyManager.validate("jdoe", "jdoe").getMessage());
        assertNull(policyManager.validate("jdoe", "ab&d1234"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 54 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testDigits.

@Test
public void testDigits() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "digits"));
        Assert.assertEquals("invalidPasswordMinDigitsMessage", policyManager.validate("jdoe", "abcd").getMessage());
        Assert.assertArrayEquals(new Object[] { 1 }, policyManager.validate("jdoe", "abcd").getParameters());
        assertNull(policyManager.validate("jdoe", "abcd1"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "digits(2)"));
        Assert.assertEquals("invalidPasswordMinDigitsMessage", policyManager.validate("jdoe", "abcd1").getMessage());
        Assert.assertArrayEquals(new Object[] { 2 }, policyManager.validate("jdoe", "abcd1").getParameters());
        assertNull(policyManager.validate("jdoe", "abcd12"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 55 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testRegexPatterns.

@Test
public void testRegexPatterns() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        PasswordPolicy policy = null;
        try {
            realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern"));
            fail("Expected NullPointerException: Regex Pattern cannot be null.");
        } catch (ModelException e) {
            assertEquals("Invalid config for regexPattern: Config required", e.getMessage());
        }
        try {
            realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern(*)"));
            fail("Expected PatternSyntaxException: Regex Pattern cannot be null.");
        } catch (ModelException e) {
            assertEquals("Invalid config for regexPattern: Not a valid regular expression", e.getMessage());
        }
        try {
            realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern(*,**)"));
            fail("Expected PatternSyntaxException: Regex Pattern cannot be null.");
        } catch (ModelException e) {
            assertEquals("Invalid config for regexPattern: Not a valid regular expression", e.getMessage());
        }
        // Fails to match one of the regex pattern
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern(jdoe) and regexPattern(j*d)"));
        Assert.assertEquals("invalidPasswordRegexPatternMessage", policyManager.validate("jdoe", "jdoe").getMessage());
        // //Fails to match all of the regex patterns
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern(j*p) and regexPattern(j*d) and regexPattern(adoe)"));
        Assert.assertEquals("invalidPasswordRegexPatternMessage", policyManager.validate("jdoe", "jdoe").getMessage());
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern([a-z][a-z][a-z][a-z][0-9])"));
        Assert.assertEquals("invalidPasswordRegexPatternMessage", policyManager.validate("jdoe", "jdoe").getMessage());
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern(jdoe)"));
        assertNull(policyManager.validate("jdoe", "jdoe"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "regexPattern([a-z][a-z][a-z][a-z][0-9])"));
        assertNull(policyManager.validate("jdoe", "jdoe0"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ModelException(org.keycloak.models.ModelException) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) PasswordPolicy(org.keycloak.models.PasswordPolicy) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

RealmModel (org.keycloak.models.RealmModel)591 Test (org.junit.Test)249 UserModel (org.keycloak.models.UserModel)225 KeycloakSession (org.keycloak.models.KeycloakSession)152 ClientModel (org.keycloak.models.ClientModel)149 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)90 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)84 ComponentModel (org.keycloak.component.ComponentModel)83 RoleModel (org.keycloak.models.RoleModel)73 UserSessionModel (org.keycloak.models.UserSessionModel)64 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)62 List (java.util.List)55 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)51 GroupModel (org.keycloak.models.GroupModel)47 HashMap (java.util.HashMap)38 Collectors (java.util.stream.Collectors)34 CachedUserModel (org.keycloak.models.cache.CachedUserModel)34 Path (javax.ws.rs.Path)30 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)30 Map (java.util.Map)29