From 110f30162b562e238e3928ab8397a1b023148ed2 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 19 Jul 2018 16:11:10 +0200 Subject: [PATCH 6/9] move all password related functions in authentic2.passwords (#24439) --- src/authentic2/passwords.py | 21 +++++++++++++++++++++ src/authentic2/validators.py | 29 +++++------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/authentic2/passwords.py b/src/authentic2/passwords.py index 01b0f449..605f5877 100644 --- a/src/authentic2/passwords.py +++ b/src/authentic2/passwords.py @@ -2,9 +2,13 @@ import string import random import re import abc +import six from django.utils.translation import ugettext as _ from django.utils.module_loading import import_string +from django.utils.functional import lazy +from django.core.exceptions import ValidationError + from . import app_settings @@ -101,3 +105,20 @@ class DefaultPasswordChecker(PasswordChecker): def get_password_checker(*args, **kwargs): return import_string(app_settings.A2_PASSWORD_POLICY_CLASS)(*args, **kwargs) + + +def validate_password(password): + error = password_help_text(password, only_errors=True) + if error: + raise ValidationError(error) + + +def password_help_text(password='', only_errors=False): + password_checker = get_password_checker() + criteria = [check.label for check in password_checker(password) if not (only_errors and check.result)] + if criteria: + return _('In order to create a secure password, please use at least: %s') % (', '.join(criteria)) + else: + return '' + +password_help_text = lazy(password_help_text, six.text_type) diff --git a/src/authentic2/validators.py b/src/authentic2/validators.py index d1bce8db..e4f90661 100644 --- a/src/authentic2/validators.py +++ b/src/authentic2/validators.py @@ -1,21 +1,20 @@ from __future__ import unicode_literals -import string -import re -import six import smtplib -from django.utils.translation import ugettext_lazy as _, ugettext +from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import force_text from django.core.exceptions import ValidationError from django.core.validators import RegexValidator -from django.utils.functional import lazy import socket import dns.resolver import dns.exception -from . import app_settings, passwords +from . import app_settings +# keep those symbols here for retrocompatibility +from .passwords import password_help_text, validate_password + # copied from http://www.djangotips.com/real-email-validation class EmailValidator(object): @@ -39,7 +38,6 @@ class EmailValidator(object): pass return [] - def __call__(self, value): try: hostname = value.split('@')[-1] @@ -85,20 +83,3 @@ class UsernameValidator(RegexValidator): def __init__(self, *args, **kwargs): self.regex = app_settings.A2_REGISTRATION_FORM_USERNAME_REGEX super(UsernameValidator, self).__init__(*args, **kwargs) - - -def validate_password(password): - error = password_help_text(password, only_errors=True) - if error: - raise ValidationError(error) - - -def password_help_text(password='', only_errors=False): - password_checker = passwords.get_password_checker() - criteria = [check.label for check in password_checker(password) if not (only_errors and check.result)] - if criteria: - return ugettext('In order to create a secure password, please use at least: %s') % (', '.join(criteria)) - else: - return '' - -password_help_text = lazy(password_help_text, six.text_type) -- 2.18.0