Skip to content

Commit

Permalink
alternative method for password policy validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dasniko committed Jan 24, 2025
1 parent 3598b1f commit 66cc392
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import org.keycloak.models.GroupModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelException;
import org.keycloak.models.PasswordPolicy;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.credential.PasswordCredentialModel;
import org.keycloak.policy.PasswordPolicyProvider;
import org.keycloak.policy.PasswordPolicyManagerProvider;
import org.keycloak.policy.PolicyError;
import org.keycloak.storage.StorageId;
import org.keycloak.storage.UserStorageProvider;
Expand Down Expand Up @@ -73,18 +72,25 @@ public boolean updateCredential(RealmModel realm, UserModel user, CredentialInpu
}

if (usePasswordPolicy()) {
PasswordPolicy passwordPolicy = realm.getPasswordPolicy();
if (passwordPolicy != null) {
for (String policy : passwordPolicy.getPolicies()) {
PasswordPolicyProvider provider = session.getProvider(PasswordPolicyProvider.class, policy);
if (provider != null) {
PolicyError policyError = provider.validate(user.getUsername(), cred.getChallengeResponse());
if (policyError != null) {
throw new ModelException(policyError.getMessage(), policyError.getParameters());
}
}
}
PolicyError policyError = session.getProvider(PasswordPolicyManagerProvider.class)
.validate(realm, user, cred.getChallengeResponse());
if (policyError != null) {
throw new ModelException(policyError.getMessage(), policyError.getParameters());
}

// alternatively to above code:
// PasswordPolicy passwordPolicy = realm.getPasswordPolicy();
// if (passwordPolicy != null) {
// for (String policy : passwordPolicy.getPolicies()) {
// PasswordPolicyProvider provider = session.getProvider(PasswordPolicyProvider.class, policy);
// if (provider != null) {
// PolicyError policyError = provider.validate(user.getUsername(), cred.getChallengeResponse());
// if (policyError != null) {
// throw new ModelException(policyError.getMessage(), policyError.getParameters());
// }
// }
// }
// }
}

Credential credential = new Credential("password", cred.getChallengeResponse());
Expand Down

0 comments on commit 66cc392

Please sign in to comment.