Opened 2 days ago
Last modified 5 hours ago
#36015 closed New feature
Separate the validation process of password validators. — at Version 2
Reported by: | Antoliny | Owned by: | Antoliny |
---|---|---|---|
Component: | contrib.auth | Version: | 5.1 |
Severity: | Normal | Keywords: | password validators |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Currently, password validators serve the same role as Django's standard validators but are categorized separately.
The reason for this, in my opinion, is that password validators receive the user instance when performing validation.
Therefore, the validation process of password validators is actually called during the Form's _post_clean
stage when it is invoked.
class SetPasswordMixin: ... def validate_password_for_user(self, user, password_field_name="password2"): password = self.cleaned_data.get(password_field_name) if password: try: password_validation.validate_password(password, user) # run password validation except ValidationError as error: self.add_error(password_field_name, error)
I think that among the password validators provided by Django, the only validator that requires an instance during the validation process is UserAttributeSimilarityValidator
. The other validators can be fully integrated into standard validators and should be provided as customized versions of standard validators.
Integrating the password validator into the standard validator will improve consistency(#35693) and enhance reusability.
e.g.)
CharField(..., validators=[MinimumLengthValidator()])
Although it can be used as shown below, it would be better for consistency if it could be called through __call__
, making it more user friendly.
CharField(..., validators=[MinimumLengthValidator().validate])
pre validation(Validated through is_valid()
.)
- MinimumLengthValidator
- CommonPasswordValidator
- NumericPasswordValidator
post validation(Validated through password_validation.validate_password()
.)
- UserAttributeSimilarityValidator
If integrated into the standard validator, the integrated validator will perform validation through is_valid, while validators that require an instance and cannot be integrated will be handled in the existing validation step, the _post_clean
method.
Therefore, it is necessary to distinguish the validators that will perform the validation in the validate_password function where the password validators are executed.
def validate_password(password, user=None, password_validators=None): """ Validate that the password meets all validator requirements. If the password is valid, return ``None``. If the password is invalid, raise ValidationError with all error messages. """ errors = [] if password_validators is None: password_validators = get_default_password_validators() for validator in password_validators: try: validator.validate(password, user) # -> Perform validation only for post password validators. except ValidationError as error: errors.append(error) if errors: raise ValidationError(errors)
Conclusion
It would be good to integrate password validators that do not require an instance into a customized form of the standard validator, allowing validation to be performed through the is_valid
call, while validators that require an instance continue to be called in the existing way.
Change History (2)
comment:1 by , 2 days ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:2 by , 2 days ago
Description: | modified (diff) |
---|