Search in sources :

Example 6 with PasswordPolicyManagerProvider

use of org.keycloak.policy.PasswordPolicyManagerProvider 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 7 with PasswordPolicyManagerProvider

use of org.keycloak.policy.PasswordPolicyManagerProvider 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)

Example 8 with PasswordPolicyManagerProvider

use of org.keycloak.policy.PasswordPolicyManagerProvider in project keycloak by keycloak.

the class PasswordPolicyTest method testComplex.

@Test
public void testComplex() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "length(8) and maxLength(32) and digits(2) and lowerCase(2) and upperCase(2) and specialChars(2) and notUsername()"));
        Assert.assertNotNull(policyManager.validate("jdoe", "12aaBB&"));
        Assert.assertNotNull(policyManager.validate("jdoe", "aaaaBB&-"));
        Assert.assertNotNull(policyManager.validate("jdoe", "12AABB&-"));
        Assert.assertNotNull(policyManager.validate("jdoe", "12aabb&-"));
        Assert.assertNotNull(policyManager.validate("jdoe", "12aaBBcc"));
        Assert.assertNotNull(policyManager.validate("12aaBB&-", "12aaBB&-"));
        Assert.assertNotNull(policyManager.validate("jdoe", "12aaBB&-12aaBB&-12aaBB&-12aaBB&-1"));
        assertNull(policyManager.validate("jdoe", "12aaBB&-"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 9 with PasswordPolicyManagerProvider

use of org.keycloak.policy.PasswordPolicyManagerProvider in project keycloak by keycloak.

the class PasswordPolicyTest method testInvalidPolicyName.

@Test
public void testInvalidPolicyName() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        try {
            realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "noSuchPolicy"));
            Assert.fail("Expected exception");
        } catch (ModelException e) {
            assertEquals("Password policy not found", e.getMessage());
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ModelException(org.keycloak.models.ModelException) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 10 with PasswordPolicyManagerProvider

use of org.keycloak.policy.PasswordPolicyManagerProvider in project keycloak by keycloak.

the class PasswordPolicyTest method testMaximumLength.

@Test
public void testMaximumLength() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "maxLength"));
        Assert.assertEquals("invalidPasswordMaxLengthMessage", policyManager.validate("jdoe", "12345678901234567890123456789012345678901234567890123456789012345").getMessage());
        Assert.assertArrayEquals(new Object[] { MaximumLengthPasswordPolicyProviderFactory.DEFAULT_MAX_LENGTH }, policyManager.validate("jdoe", "12345678901234567890123456789012345678901234567890123456789012345").getParameters());
        assertNull(policyManager.validate("jdoe", "1234567890123456789012345678901234567890123456789012345678901234"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "maxLength(24)"));
        Assert.assertEquals("invalidPasswordMaxLengthMessage", policyManager.validate("jdoe", "1234567890123456789012345").getMessage());
        Assert.assertArrayEquals(new Object[] { 24 }, policyManager.validate("jdoe", "1234567890123456789012345").getParameters());
        assertNull(policyManager.validate("jdoe", "123456789012345678901234"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

Test (org.junit.Test)11 RealmModel (org.keycloak.models.RealmModel)11 PasswordPolicyManagerProvider (org.keycloak.policy.PasswordPolicyManagerProvider)11 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)11 ModelException (org.keycloak.models.ModelException)2 PasswordPolicy (org.keycloak.models.PasswordPolicy)1