-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
17289 enforce minimum password strength (#17299)
* 17289 add password validation * 17289 add password validation * 17289 fix tests * 17289 fix tests * Update netbox/utilities/password_validation.py Co-authored-by: Jeremy Stretch <[email protected]> * Update netbox/utilities/password_validation.py Co-authored-by: Jeremy Stretch <[email protected]> * Update netbox/utilities/password_validation.py Co-authored-by: Jeremy Stretch <[email protected]> * 17289 update tests * 17289 remove common password check * 17289 fix user create * 17289 revert _post_clean --------- Co-authored-by: Jeremy Stretch <[email protected]>
- Loading branch information
1 parent
b4dd57f
commit 00874ac
Showing
4 changed files
with
94 additions
and
22 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
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 |
---|---|---|
|
@@ -38,8 +38,8 @@ def setUpTestData(cls): | |
'first_name': 'firstx', | ||
'last_name': 'lastx', | ||
'email': '[email protected]', | ||
'password': 'pass1xxx', | ||
'confirm_password': 'pass1xxx', | ||
'password': 'pass1xxxABCD', | ||
'confirm_password': 'pass1xxxABCD', | ||
} | ||
|
||
cls.csv_data = ( | ||
|
@@ -60,19 +60,15 @@ def setUpTestData(cls): | |
'last_name': 'newlastname', | ||
} | ||
|
||
@override_settings(AUTH_PASSWORD_VALIDATORS=[{ | ||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
'OPTIONS': {'min_length': 8} | ||
}]) | ||
def test_password_validation_enforced(self): | ||
""" | ||
Test that any configured password validation rules (AUTH_PASSWORD_VALIDATORS) are enforced. | ||
""" | ||
self.add_permissions('users.add_user') | ||
data = { | ||
'username': 'new_user', | ||
'password': 'foo', | ||
'confirm_password': 'foo', | ||
'password': 'F1a', | ||
'confirm_password': 'F1a', | ||
} | ||
|
||
# Password too short | ||
|
@@ -84,10 +80,30 @@ def test_password_validation_enforced(self): | |
self.assertHttpStatus(response, 200) | ||
|
||
# Password long enough | ||
data['password'] = 'foobar123' | ||
data['confirm_password'] = 'foobar123' | ||
data['password'] = 'fooBarFoo123' | ||
data['confirm_password'] = 'fooBarFoo123' | ||
self.assertHttpStatus(self.client.post(**request), 302) | ||
|
||
# Password no number | ||
data['password'] = 'FooBarFooBar' | ||
data['confirm_password'] = 'FooBarFooBar' | ||
self.assertHttpStatus(self.client.post(**request), 200) | ||
|
||
# Password no letter | ||
data['password'] = '123456789123' | ||
data['confirm_password'] = '123456789123' | ||
self.assertHttpStatus(self.client.post(**request), 200) | ||
|
||
# Password no uppercase | ||
data['password'] = 'foobar123abc' | ||
data['confirm_password'] = 'foobar123abc' | ||
self.assertHttpStatus(self.client.post(**request), 200) | ||
|
||
# Password no lowercase | ||
data['password'] = 'FOOBAR123ABC' | ||
data['confirm_password'] = 'FOOBAR123ABC' | ||
self.assertHttpStatus(self.client.post(**request), 200) | ||
|
||
|
||
class GroupTestCase( | ||
ViewTestCases.GetObjectViewTestCase, | ||
|
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,27 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext as _ | ||
|
||
|
||
class AlphanumericPasswordValidator: | ||
""" | ||
Validate that the password has at least one numeral, one uppercase letter and one lowercase letter. | ||
""" | ||
|
||
def validate(self, password, user=None): | ||
if not any(char.isdigit() for char in password): | ||
raise ValidationError( | ||
_("Password must have at least one numeral."), | ||
) | ||
|
||
if not any(char.isupper() for char in password): | ||
raise ValidationError( | ||
_("Password must have at least one uppercase letter."), | ||
) | ||
|
||
if not any(char.islower() for char in password): | ||
raise ValidationError( | ||
_("Password must have at least one lowercase letter."), | ||
) | ||
|
||
def get_help_text(self): | ||
return _("Your password must contain at least one numeral, one uppercase letter and one lowercase letter.") |