How to do it...

Follow these steps to set up stronger password validation for your project:

  1. Let's customize the settings for the validators that are included with Django by adding some options:
# myproject/settings/_base.py
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation."
"UserAttributeSimilarityValidator",
"OPTIONS": {"max_similarity": 0.5},
},
{
"NAME": "django.contrib.auth.password_validation."
"MinimumLengthValidator"
,
"OPTIONS": {"min_length": 12},
},
{"NAME": "django.contrib.auth.password_validation."
"CommonPasswordValidator"
},
{"NAME": "django.contrib.auth.password_validation."
"NumericPasswordValidator"
},
]
  1. Add the MaximumLengthValidator class to the password_validation.py file in the new auth_extra app, as follows:
# myproject/apps/auth_extra/password_validation.py
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _


class MaximumLengthValidator:
def __init__(self, max_length=24):
self.max_length = max_length

def validate(self, password, user=None):
if len(password) > self.max_length:
raise ValidationError(
self.get_help_text(pronoun="this"),
code="password_too_long",
params={'max_length': self.max_length},
)

def get_help_text(self, pronoun="your"):
return _(f"{pronoun.capitalize()} password must contain "
f"no more than {self.max_length} characters")
  1. In the same file, create the SpecialCharacterInclusionValidator class:
class SpecialCharacterInclusionValidator:
DEFAULT_SPECIAL_CHARACTERS = ('$', '%', ':', '#', '!')

def __init__(self, special_chars=DEFAULT_SPECIAL_CHARACTERS):
self.special_chars = special_chars

def validate(self, password, user=None):
has_specials_chars = False
for char in self.special_chars:
if char in password:
has_specials_chars = True
break
if not has_specials_chars:
raise ValidationError(
self.get_help_text(pronoun="this"),
code="password_missing_special_chars"
)

def get_help_text(self, pronoun="your"):
return _(f"{pronoun.capitalize()} password must contain at"
" least one of the following special characters: "
f"{', '.join(self.special_chars)}")
  1. Then, add the new validators to the settings:
# myproject/settings/_base.py
from myproject.apps.auth_extra.password_validation import (
SpecialCharacterInclusionValidator,
)

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation."
"UserAttributeSimilarityValidator",
"OPTIONS": {"max_similarity": 0.5},
},
{
"NAME": "django.contrib.auth.password_validation."
"MinimumLengthValidator"
,
"OPTIONS": {"min_length": 12},
},
{"NAME": "django.contrib.auth.password_validation."
"CommonPasswordValidator"
},
{"NAME": "django.contrib.auth.password_validation."
"NumericPasswordValidator"
},
{
"NAME": "myproject.apps.auth_extra.password_validation."
"MaximumLengthValidator",

"OPTIONS": {"max_length": 32},
},
{
"NAME": "myproject.apps.auth_extra.password_validation."
"SpecialCharacterInclusionValidator",

"OPTIONS": {
"special_chars": ("{", "}", "^", "&")
+ SpecialCharacterInclusionValidator
.DEFAULT_SPECIAL_CHARACTERS

},
},
]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.42.94