Password policies like NoUsername should compare in case-insensitive way

closes #37431

Signed-off-by: mposolda <mposolda@gmail.com>
This commit is contained in:
mposolda
2025-02-18 10:41:10 +01:00
committed by Marek Posolda
parent 74417fae33
commit 2bcd2dbe74
4 changed files with 21 additions and 3 deletions

View File

@@ -36,7 +36,7 @@ public class NotContainsUsernamePasswordPolicyProvider implements PasswordPolicy
if (username == null) {
return null;
}
return password.contains(username) ? new PolicyError(ERROR_MESSAGE) : null;
return password.toLowerCase().contains(username.toLowerCase()) ? new PolicyError(ERROR_MESSAGE) : null;
}
@Override

View File

@@ -42,7 +42,7 @@ public class NotEmailPasswordPolicyProvider implements PasswordPolicyProvider {
if (email == null) {
return null;
}
return email.equals(password) ? POLICY_ERROR : null;
return email.equalsIgnoreCase(password) ? POLICY_ERROR : null;
}
@Override

View File

@@ -39,7 +39,7 @@ public class NotUsernamePasswordPolicyProvider implements PasswordPolicyProvider
if (username == null) {
return null;
}
return username.equals(password) ? new PolicyError(ERROR_MESSAGE) : null;
return username.equalsIgnoreCase(password) ? new PolicyError(ERROR_MESSAGE) : null;
}
@Override

View File

@@ -580,6 +580,12 @@ public class RegisterTest extends AbstractTestRealmKeycloakTest {
assertTrue(registerPage.isCurrent());
assertEquals("Invalid password: must not be equal to the username.", registerPage.getInputPasswordErrors().getPasswordError());
// Case-sensitivity - still should not allow to create password when lower-cased
registerPage.register("firstName", "lastName", "registerUserNotUsername@email", "registerUserNotUsername", "registerusernotusername", "registerusernotusername");
assertTrue(registerPage.isCurrent());
assertEquals("Invalid password: must not be equal to the username.", registerPage.getInputPasswordErrors().getPasswordError());
try (Response response = adminClient.realm("test").users().create(UserBuilder.create().username("registerUserNotUsername").build())) {
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
}
@@ -615,6 +621,12 @@ public class RegisterTest extends AbstractTestRealmKeycloakTest {
assertTrue(registerPage.isCurrent());
assertEquals("Invalid password: Can not contain the username.", registerPage.getInputPasswordErrors().getPasswordError());
// Case-sensitivity - still should not allow to create password when lower-cased
registerPage.register("firstName", "lastName", "registerUserNotUsername@email", "Bob", "123bob", "123bob");
assertTrue(registerPage.isCurrent());
assertEquals("Invalid password: Can not contain the username.", registerPage.getInputPasswordErrors().getPasswordError());
try (Response response = adminClient.realm("test").users().create(UserBuilder.create().username("Bob").build())) {
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
}
@@ -648,6 +660,12 @@ public class RegisterTest extends AbstractTestRealmKeycloakTest {
assertTrue(registerPage.isCurrent());
assertEquals("Invalid password: must not be equal to the email.", registerPage.getInputPasswordErrors().getPasswordError());
// Case-sensitivity - still should not allow to create password when lower-cased
registerPage.registerWithEmailAsUsername("firstName", "lastName", "registerUserNotEmail@email", "registerusernotemail@email", "registerusernotemail@email");
assertTrue(registerPage.isCurrent());
assertEquals("Invalid password: must not be equal to the email.", registerPage.getInputPasswordErrors().getPasswordError());
}
}