Search in sources :

Example 1 with PolicyError

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

the class PasswordCredentialProvider method createCredential.

public boolean createCredential(RealmModel realm, UserModel user, String password) {
    PasswordPolicy policy = realm.getPasswordPolicy();
    PolicyError error = session.getProvider(PasswordPolicyManagerProvider.class).validate(realm, user, password);
    if (error != null)
        throw new ModelException(error.getMessage(), error.getParameters());
    PasswordHashProvider hash = getHashProvider(policy);
    if (hash == null) {
        return false;
    }
    PasswordCredentialModel credentialModel = hash.encodedCredential(password, policy.getHashIterations());
    credentialModel.setCreatedDate(Time.currentTimeMillis());
    createCredential(realm, user, credentialModel);
    return true;
}
Also used : ModelException(org.keycloak.models.ModelException) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) PasswordPolicy(org.keycloak.models.PasswordPolicy) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) PolicyError(org.keycloak.policy.PolicyError) PasswordHashProvider(org.keycloak.credential.hash.PasswordHashProvider)

Example 2 with PolicyError

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

the class RegistrationPassword method validate.

@Override
public void validate(ValidationContext context) {
    MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
    List<FormMessage> errors = new ArrayList<>();
    context.getEvent().detail(Details.REGISTER_METHOD, "form");
    if (Validation.isBlank(formData.getFirst(RegistrationPage.FIELD_PASSWORD))) {
        errors.add(new FormMessage(RegistrationPage.FIELD_PASSWORD, Messages.MISSING_PASSWORD));
    } else if (!formData.getFirst(RegistrationPage.FIELD_PASSWORD).equals(formData.getFirst(RegistrationPage.FIELD_PASSWORD_CONFIRM))) {
        errors.add(new FormMessage(RegistrationPage.FIELD_PASSWORD_CONFIRM, Messages.INVALID_PASSWORD_CONFIRM));
    }
    if (formData.getFirst(RegistrationPage.FIELD_PASSWORD) != null) {
        PolicyError err = context.getSession().getProvider(PasswordPolicyManagerProvider.class).validate(context.getRealm().isRegistrationEmailAsUsername() ? formData.getFirst(RegistrationPage.FIELD_EMAIL) : formData.getFirst(RegistrationPage.FIELD_USERNAME), formData.getFirst(RegistrationPage.FIELD_PASSWORD));
        if (err != null)
            errors.add(new FormMessage(RegistrationPage.FIELD_PASSWORD, err.getMessage(), err.getParameters()));
    }
    if (errors.size() > 0) {
        context.error(Errors.INVALID_REGISTRATION);
        formData.remove(RegistrationPage.FIELD_PASSWORD);
        formData.remove(RegistrationPage.FIELD_PASSWORD_CONFIRM);
        context.validationError(formData, errors);
        return;
    } else {
        context.success();
    }
}
Also used : PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) ArrayList(java.util.ArrayList) PolicyError(org.keycloak.policy.PolicyError) FormMessage(org.keycloak.models.utils.FormMessage)

Aggregations

PasswordPolicyManagerProvider (org.keycloak.policy.PasswordPolicyManagerProvider)2 PolicyError (org.keycloak.policy.PolicyError)2 ArrayList (java.util.ArrayList)1 PasswordHashProvider (org.keycloak.credential.hash.PasswordHashProvider)1 ModelException (org.keycloak.models.ModelException)1 PasswordPolicy (org.keycloak.models.PasswordPolicy)1 PasswordCredentialModel (org.keycloak.models.credential.PasswordCredentialModel)1 FormMessage (org.keycloak.models.utils.FormMessage)1