password_validation.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import functools
  2. import gzip
  3. import re
  4. from difflib import SequenceMatcher
  5. from pathlib import Path
  6. from django.conf import settings
  7. from django.core.exceptions import (
  8. FieldDoesNotExist,
  9. ImproperlyConfigured,
  10. ValidationError,
  11. )
  12. from django.utils.functional import cached_property, lazy
  13. from django.utils.html import format_html, format_html_join
  14. from django.utils.module_loading import import_string
  15. from django.utils.translation import gettext as _
  16. from django.utils.translation import ngettext
  17. @functools.lru_cache(maxsize=None)
  18. def get_default_password_validators():
  19. return get_password_validators(settings.AUTH_PASSWORD_VALIDATORS)
  20. def get_password_validators(validator_config):
  21. validators = []
  22. for validator in validator_config:
  23. try:
  24. klass = import_string(validator["NAME"])
  25. except ImportError:
  26. msg = (
  27. "The module in NAME could not be imported: %s. Check your "
  28. "AUTH_PASSWORD_VALIDATORS setting."
  29. )
  30. raise ImproperlyConfigured(msg % validator["NAME"])
  31. validators.append(klass(**validator.get("OPTIONS", {})))
  32. return validators
  33. def validate_password(password, user=None, password_validators=None):
  34. """
  35. Validate that the password meets all validator requirements.
  36. If the password is valid, return ``None``.
  37. If the password is invalid, raise ValidationError with all error messages.
  38. """
  39. errors = []
  40. if password_validators is None:
  41. password_validators = get_default_password_validators()
  42. for validator in password_validators:
  43. try:
  44. validator.validate(password, user)
  45. except ValidationError as error:
  46. errors.append(error)
  47. if errors:
  48. raise ValidationError(errors)
  49. def password_changed(password, user=None, password_validators=None):
  50. """
  51. Inform all validators that have implemented a password_changed() method
  52. that the password has been changed.
  53. """
  54. if password_validators is None:
  55. password_validators = get_default_password_validators()
  56. for validator in password_validators:
  57. password_changed = getattr(validator, "password_changed", lambda *a: None)
  58. password_changed(password, user)
  59. def password_validators_help_texts(password_validators=None):
  60. """
  61. Return a list of all help texts of all configured validators.
  62. """
  63. help_texts = []
  64. if password_validators is None:
  65. password_validators = get_default_password_validators()
  66. for validator in password_validators:
  67. help_texts.append(validator.get_help_text())
  68. return help_texts
  69. def _password_validators_help_text_html(password_validators=None):
  70. """
  71. Return an HTML string with all help texts of all configured validators
  72. in an <ul>.
  73. """
  74. help_texts = password_validators_help_texts(password_validators)
  75. help_items = format_html_join(
  76. "", "<li>{}</li>", ((help_text,) for help_text in help_texts)
  77. )
  78. return format_html("<ul>{}</ul>", help_items) if help_items else ""
  79. password_validators_help_text_html = lazy(_password_validators_help_text_html, str)
  80. class MinimumLengthValidator:
  81. """
  82. Validate that the password is of a minimum length.
  83. """
  84. def __init__(self, min_length=8):
  85. self.min_length = min_length
  86. def validate(self, password, user=None):
  87. if len(password) < self.min_length:
  88. raise ValidationError(
  89. ngettext(
  90. "This password is too short. It must contain at least "
  91. "%(min_length)d character.",
  92. "This password is too short. It must contain at least "
  93. "%(min_length)d characters.",
  94. self.min_length,
  95. ),
  96. code="password_too_short",
  97. params={"min_length": self.min_length},
  98. )
  99. def get_help_text(self):
  100. return ngettext(
  101. "Your password must contain at least %(min_length)d character.",
  102. "Your password must contain at least %(min_length)d characters.",
  103. self.min_length,
  104. ) % {"min_length": self.min_length}
  105. def exceeds_maximum_length_ratio(password, max_similarity, value):
  106. """
  107. Test that value is within a reasonable range of password.
  108. The following ratio calculations are based on testing SequenceMatcher like
  109. this:
  110. for i in range(0,6):
  111. print(10**i, SequenceMatcher(a='A', b='A'*(10**i)).quick_ratio())
  112. which yields:
  113. 1 1.0
  114. 10 0.18181818181818182
  115. 100 0.019801980198019802
  116. 1000 0.001998001998001998
  117. 10000 0.00019998000199980003
  118. 100000 1.999980000199998e-05
  119. This means a length_ratio of 10 should never yield a similarity higher than
  120. 0.2, for 100 this is down to 0.02 and for 1000 it is 0.002. This can be
  121. calculated via 2 / length_ratio. As a result we avoid the potentially
  122. expensive sequence matching.
  123. """
  124. pwd_len = len(password)
  125. length_bound_similarity = max_similarity / 2 * pwd_len
  126. value_len = len(value)
  127. return pwd_len >= 10 * value_len and value_len < length_bound_similarity
  128. class UserAttributeSimilarityValidator:
  129. """
  130. Validate that the password is sufficiently different from the user's
  131. attributes.
  132. If no specific attributes are provided, look at a sensible list of
  133. defaults. Attributes that don't exist are ignored. Comparison is made to
  134. not only the full attribute value, but also its components, so that, for
  135. example, a password is validated against either part of an email address,
  136. as well as the full address.
  137. """
  138. DEFAULT_USER_ATTRIBUTES = ("username", "first_name", "last_name", "email")
  139. def __init__(self, user_attributes=DEFAULT_USER_ATTRIBUTES, max_similarity=0.7):
  140. self.user_attributes = user_attributes
  141. if max_similarity < 0.1:
  142. raise ValueError("max_similarity must be at least 0.1")
  143. self.max_similarity = max_similarity
  144. def validate(self, password, user=None):
  145. if not user:
  146. return
  147. password = password.lower()
  148. for attribute_name in self.user_attributes:
  149. value = getattr(user, attribute_name, None)
  150. if not value or not isinstance(value, str):
  151. continue
  152. value_lower = value.lower()
  153. value_parts = re.split(r"\W+", value_lower) + [value_lower]
  154. for value_part in value_parts:
  155. if exceeds_maximum_length_ratio(
  156. password, self.max_similarity, value_part
  157. ):
  158. continue
  159. if (
  160. SequenceMatcher(a=password, b=value_part).quick_ratio()
  161. >= self.max_similarity
  162. ):
  163. try:
  164. verbose_name = str(
  165. user._meta.get_field(attribute_name).verbose_name
  166. )
  167. except FieldDoesNotExist:
  168. verbose_name = attribute_name
  169. raise ValidationError(
  170. _("The password is too similar to the %(verbose_name)s."),
  171. code="password_too_similar",
  172. params={"verbose_name": verbose_name},
  173. )
  174. def get_help_text(self):
  175. return _(
  176. "Your password can’t be too similar to your other personal information."
  177. )
  178. class CommonPasswordValidator:
  179. """
  180. Validate that the password is not a common password.
  181. The password is rejected if it occurs in a provided list of passwords,
  182. which may be gzipped. The list Django ships with contains 20000 common
  183. passwords (lowercased and deduplicated), created by Royce Williams:
  184. https://gist.github.com/roycewilliams/281ce539915a947a23db17137d91aeb7
  185. The password list must be lowercased to match the comparison in validate().
  186. """
  187. @cached_property
  188. def DEFAULT_PASSWORD_LIST_PATH(self):
  189. return Path(__file__).resolve().parent / "common-passwords.txt.gz"
  190. def __init__(self, password_list_path=DEFAULT_PASSWORD_LIST_PATH):
  191. if password_list_path is CommonPasswordValidator.DEFAULT_PASSWORD_LIST_PATH:
  192. password_list_path = self.DEFAULT_PASSWORD_LIST_PATH
  193. try:
  194. with gzip.open(password_list_path, "rt", encoding="utf-8") as f:
  195. self.passwords = {x.strip() for x in f}
  196. except OSError:
  197. with open(password_list_path) as f:
  198. self.passwords = {x.strip() for x in f}
  199. def validate(self, password, user=None):
  200. if password.lower().strip() in self.passwords:
  201. raise ValidationError(
  202. _("This password is too common."),
  203. code="password_too_common",
  204. )
  205. def get_help_text(self):
  206. return _("Your password can’t be a commonly used password.")
  207. class NumericPasswordValidator:
  208. """
  209. Validate that the password is not entirely numeric.
  210. """
  211. def validate(self, password, user=None):
  212. if password.isdigit():
  213. raise ValidationError(
  214. _("This password is entirely numeric."),
  215. code="password_entirely_numeric",
  216. )
  217. def get_help_text(self):
  218. return _("Your password can’t be entirely numeric.")