Created
April 24, 2012 11:33
-
-
Save stevejalim/2478912 to your computer and use it in GitHub Desktop.
Django StrongPasswordField
This file contains hidden or 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
| import re | |
| from django import forms | |
| def is_password_strong_enough(value): | |
| """ | |
| "All passwords should be: | |
| 10+ characters long | |
| contain at least one upper-case character | |
| contain at least one lower-case character | |
| contain at least one numeric digit | |
| ^.* | |
| (?=.{10,}) # at least 10 chars | |
| (?=.*\d) # at least 1 numeric digit | |
| (?=.*[A-Z]) #at least one uppercase letter | |
| (?=.*[a-z]) #at least one lowercase letter | |
| .*$ | |
| """ | |
| hit = re.match("^.*(?=.{6,})(?=.*\d)(?=.*[A-Z]).*$", value) | |
| return bool(hit) | |
| class StrongPasswordField(forms.CharField): | |
| """ | |
| Checks that a password meets the minimum criteria defined in check_password_security() | |
| """ | |
| widget = forms.widgets.PasswordInput | |
| def validate(self, value): | |
| # Use the parent's handling of required fields, etc. | |
| super(forms.CharField, self).validate(value) | |
| #then our own validator | |
| if value and not is_password_strong_enough(value): | |
| raise forms.ValidationError("Passwords must be at least 10 characters long with at least one upper-case letter, at least one lower-case letter and at least one number.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment