-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from longguikeji/zzr/strong_password
Zzr/strong password
- Loading branch information
Showing
7 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
自定义密码复杂度校验集合 | ||
""" | ||
import re | ||
|
||
from django.conf import settings | ||
from django.utils.translation import ugettext_lazy as _ | ||
from rest_framework.exceptions import ValidationError | ||
|
||
PASSWORD_COMPLEXITY = getattr(settings, "PASSWORD_COMPLEXITY", None) | ||
|
||
|
||
# pylint: disable=useless-object-inheritance | ||
# pylint: disable=too-many-branches | ||
class _ComplexityValidator(object): | ||
"""自定义密码复杂度校验规则""" | ||
message = _("密码必须更加复杂 (%s)") | ||
code = "password_complexity" | ||
|
||
def __init__(self, complexities): | ||
self.complexities = complexities | ||
|
||
def __call__(self, value): | ||
if self.complexities is None: | ||
return | ||
uppercase, lowercase, letters, digits, special = set(), set(), set(), set(), set() | ||
errors = [] | ||
|
||
for _character in value: | ||
if _character.isupper(): | ||
uppercase.add(_character) | ||
letters.add(_character) | ||
continue | ||
if _character.islower(): | ||
lowercase.add(_character) | ||
letters.add(_character) | ||
continue | ||
if _character.isdigit(): | ||
digits.add(_character) | ||
continue | ||
if not _character.isspace(): | ||
special.add(_character) | ||
|
||
words = set(re.findall(r'\b\w+', value, re.UNICODE)) | ||
|
||
if len(uppercase) < self.complexities.get("UPPER", 0): | ||
errors.append(_("%(UPPER)s 个及以上不同的大写字母") % self.complexities) | ||
if len(lowercase) < self.complexities.get("LOWER", 0): | ||
errors.append(_("%(LOWER)s 个及以上不同的小写字母") % self.complexities) | ||
if len(letters) < self.complexities.get("LETTERS", 0): | ||
errors.append(_("%(LETTERS)s 个及以上不同的大小写字母") % self.complexities) | ||
if len(digits) < self.complexities.get("DIGITS", 0): | ||
errors.append(_("%(DIGITS)s 个及以上不同的数字") % self.complexities) | ||
if len(special) < self.complexities.get("SPECIAL", 0): | ||
errors.append(_("%(SPECIAL)s 个及以上的特殊字符") % self.complexities) | ||
if len(words) < self.complexities.get("WORDS", 0): | ||
errors.append(_("%(WORDS)s 个及以上不同的单词") % self.complexities) | ||
if len(value) < self.complexities.get("LENGTH", 0): | ||
errors.append(_("%(LENGTH)s 长度及以上的密码") % self.complexities) | ||
if errors: | ||
raise ValidationError(self.message % (_(u'必须包含 ') + u', '.join(errors), ), code=self.code) | ||
|
||
|
||
_COMPLEXITY = _ComplexityValidator(PASSWORD_COMPLEXITY) | ||
|
||
|
||
# pylint: disable=missing-function-docstring | ||
class ComplexityValidator: | ||
""" | ||
Wrapper for validators.ComplexityValidator which is compatible | ||
with the Django 1.9+ password validation API | ||
""" | ||
def __init__(self): | ||
self.validator = _COMPLEXITY | ||
|
||
# pylint: disable=no-self-use | ||
def get_help_text(self): | ||
return _("密码需满足ArkID的复杂性要求.") | ||
|
||
# pylint: disable=unused-argument | ||
def validate(self, value, user=None): | ||
return self.validator(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ processes = %(%k + 1) | |
thunder-lock = true | ||
harakiri = 30 | ||
|
||
listen = 65535 | ||
;listen = 65535 | ||
|
||
disable-logging = true |