Refs #16860 -- Minor edits and fixes to password validation.

This commit is contained in:
Tim Graham 2015-06-08 13:27:47 -04:00
parent a0047c6242
commit 55b3bd8468
6 changed files with 68 additions and 31 deletions

View file

@ -2774,11 +2774,11 @@ AUTH_PASSWORD_VALIDATORS
.. versionadded:: 1.9
Default: ``[]``
Default: ``[]`` (Empty list)
Sets the validators that are used to check the strength of user's passwords.
See :ref:`password-validation` for more details.
By default, no validation is performed and all passwords are accepted.
The list of validators that are used to check the strength of user's passwords.
See :ref:`password-validation` for more details. By default, no validation is
performed and all passwords are accepted.
.. _settings-messages:

View file

@ -28,7 +28,7 @@ What's new in Django 1.9
Password validation
~~~~~~~~~~~~~~~~~~~
Django now offers password validation, to help prevent the usage of weak
Django now offers password validation to help prevent the usage of weak
passwords by users. The validation is integrated in the included password
change and reset forms and is simple to integrate in any other code.
Validation is performed by one or more validators, configured in the new
@ -36,10 +36,10 @@ Validation is performed by one or more validators, configured in the new
Four validators are included in Django, which can enforce a minimum length,
compare the password to the user's attributes like their name, ensure
passwords aren't entirely numeric or check against an included list of common
passwords aren't entirely numeric, or check against an included list of common
passwords. You can combine multiple validators, and some validators have
custom configuration options. For example, you can choose to provide a custom
list of common passwords. Each validator provides a help text to explain their
list of common passwords. Each validator provides a help text to explain its
requirements to the user.
By default, no validation is performed and all passwords are accepted, so if

View file

@ -242,6 +242,10 @@ from the ``User`` model.
Password validation
===================
.. module:: django.contrib.auth.password_validation
.. versionadded:: 1.9
Users often choose poor passwords. To help mitigate this problem, Django
offers pluggable password validation. You can configure multiple password
validators at the same time. A few validators are included in Django, but it's
@ -254,14 +258,14 @@ Validators can also have optional settings to fine tune their behavior.
Validation is controlled by the :setting:`AUTH_PASSWORD_VALIDATORS` setting.
By default, validators are used in the forms to reset or change passwords.
The default for setting is an empty list, which means no validators are
The default for the setting is an empty list, which means no validators are
applied. In new projects created with the default :djadmin:`startproject`
template, a simple set of validators is enabled.
.. note::
Password validation can prevent the use of many types of weak passwords.
However, the fact that a password passes all the validators, doesn't
However, the fact that a password passes all the validators doesn't
guarantee that it is a strong password. There are many factors that can
weaken a password that are not detectable by even the most advanced
password validators.
@ -344,7 +348,7 @@ Django includes four validators:
`Mark Burnett <https://xato.net/passwords/more-top-worst-passwords/>`_.
The ``password_list_path`` can be set to the path of a custom file of
common passwords. This file should contain one password per line, and
common passwords. This file should contain one password per line and
may be plain text or gzipped.
.. class:: NumericPasswordValidator()
@ -354,8 +358,6 @@ Django includes four validators:
Integrating validation
-----------------------
.. module:: django.contrib.auth.password_validation
There are a few functions in ``django.contrib.auth.password_validation`` that
you can call from your own forms or other code to integrate password
validation. This can be useful if you use custom forms for password setting,
@ -368,14 +370,14 @@ or if you have API calls that allow passwords to be set, for example.
:exc:`~django.core.exceptions.ValidationError` with all the error messages
from the validators.
The user object is optional: if it's not provided, some validators may not
be able to perform any validation and will accept any password.
The ``user`` object is optional: if it's not provided, some validators may
not be able to perform any validation and will accept any password.
.. function:: password_changed(password, user=None, password_validators=None)
Informs all validators that the password has been changed. This can be used
by some validators, e.g. a validator that prevents password reuse. This
should be called once the password has been successfully changed.
by validators such as one that prevents password reuse. This should be
called once the password has been successfully changed.
.. function:: password_validators_help_texts(password_validators=None)
@ -440,11 +442,17 @@ Here's a basic example of a validator, with one optional setting::
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(_("This password is too short."))
raise ValidationError(
_("This password must contain at least %(min_length)d characters."),
code='password_too_short',
params={'min_length': self.min_length},
)
def get_help_text(self):
return _("Your password must contain at least %(min_length)d characters.")
return _(
"Your password must contain at least %(min_length)d characters."
% {'min_length': self.min_length}
)
You can also implement ``password_changed(password, user=None``), which will
be called after a successful password change. That can be used to prevent