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"));
});
}
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"));
});
}
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&-"));
});
}
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());
}
});
}
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"));
});
}
Aggregations