layerindex-web/password_validation.py
Paul Eggleton 5d308d943e Enable password strength validation by default
Use Django's built-in password validators with reasonable settings, and
add a basic complexity validator since there isn't one provided.

Additionally, fix the registration form so that it shows the help text
which includes a description of what the password requirements are.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
2018-11-06 13:58:32 +13:00

26 lines
867 B
Python

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
import re
class ComplexityValidator(object):
def validate(self, password, user=None):
score = 0
if re.search('[0-9]', password):
score += 1
if password.lower() != password:
score += 1
if re.search('[^a-zA-Z0-9]', password):
score += 1
if score < 2:
raise ValidationError(
_("This password does not contain at least two of: upper/lowercase characters; a number; a special (non-alphanumeric) character."),
code='password_too_simple'
)
def get_help_text(self):
return _(
"Your password must contain at least two of: upper/lowercase characters; a number; a special (non-alphanumeric) character"
)