Fixed #27807 -- Made username validation configurable through AUTH_USERNAME_VALIDATORS setting#13114
Fixed #27807 -- Made username validation configurable through AUTH_USERNAME_VALIDATORS setting#13114saykharng wants to merge 16 commits intodjango:masterfrom
Conversation
|
Hi Shekhar, another (prospective) first time Django contributor here. Thought I would help out a fellow first timer by commenting on your documentation. |
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Also, this would need a test that UnicodeUsernameValidator is correctly defaulted when settings are not provided, since this is what nearly all users will experience if this approach is implemented.
|
Hi Jacob, my sincere apologies on taking long time on this. Currently, I am following the django core validators pattern where the validator should be callable. Therefore implementing validators with call method. Is this ok? or should I make it exactly like a password validators with validate() function and make necessary changes? Please let me know. |
|
Hi Shekhar! |
Thank you! |
Wrote a feature to allow users to customize username validators.
I have opened a discussion but waiting on feedback. |
jacobtylerwalls
left a comment
There was a problem hiding this comment.
heya @saykharng I have a few more suggestions on tests to include and some documentation fixes. I'll let a more experienced contributor mark this as ready for checkin once they've given it their own review 👍
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
tests/auth_tests/test_validators.py
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
tests/auth_tests/test_validators.py
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Sorry this feedback has been so piecemeal, but I noticed a few more things when I looked again at this today. Lmk if you have any questions!
docs/releases/3.2.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
django/contrib/auth/models.py
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
tests/auth_tests/test_validators.py
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
tests/auth_tests/test_validators.py
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/ref/settings.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Added test for Forms changes, fixed typos and spelling.
23dcfb6 to
845e0d0
Compare
Hi Jacob, thanks a lot for your feedback and sorry it's taking a while. I have learned a lot about development and django by working on this feature. I have added few more test and made all the changes as suggested. Appreciate your time. :) |
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Happy to do it, @saykharng, I left a few more comments that might say "outdated" because they were made in between your force-pushes, but I gave them a quick scan and I think they're all still relevant.
Also, be careful about the files you check into the repo, I noticed you checked in a .swp file.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
django/contrib/auth/models.py
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/ref/settings.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/releases/3.2.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/releases/3.2.txt
Outdated
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
6a6eb28 to
836aef9
Compare
fixed incorrect line break
Thank you!
|
django/contrib/auth/validators.py
Outdated
There was a problem hiding this comment.
Not sure about the phrase 'English letters'. (As an English speaker this phrase makes me cringe. French, German, Spanish etc all use these characters as well, do folk who speak these languages really call this set of letters 'English'?)
Could be 'letters from the Latin alphabet'?
There was a problem hiding this comment.
From my experience speaking those languages and as a native speaker of English, I also vote for 'letters from the Latin alphabet'. Some languages (like Chinese) tend to use "English" for everything to do with Europe, but none of the European languages I speak do in my experience!
There was a problem hiding this comment.
Thank you! I have made the changes as suggested.
matthiask
left a comment
There was a problem hiding this comment.
- I really like replacing "English" with "Latin alphabet"
- Maybe you could move the existing username validators to the
username_validationmodule too since you have been inspired by thepassword_validationmodule (and of course leave backwards compatibility shims indjango.contrib.auth.validators.) (Not important, just a suggestion.)
django/contrib/auth/validators.py
Outdated
|
|
||
|
|
||
| @deconstructible | ||
| class UsernameMinimumLengthValidator: |
There was a problem hiding this comment.
I'm unsure whether there's a good reason for this validator, since django.core.validators.MinLengthValidator (https://docs.djangoproject.com/en/3.1/ref/validators/#minlengthvalidator) exists already.
I know there's a MinimumLengthValidator in the password_validation.py module but maybe that one is a bit redundant too.
There was a problem hiding this comment.
Thank you for the feedback. Apologies for extreme delay in response.
I have made suggested changes other than moving validators to username_validation.
docs/ref/settings.txt
Outdated
|
|
||
| The list of validators that are used to check username requirement. | ||
| See :ref:`username-validation` for more details. By default, | ||
| UnicodeUsernameValidator is used to perform the validation. |
There was a problem hiding this comment.
Use :class:`~django.contrib.auth.validators.UnicodeUsernameValidator` here
docs/ref/settings.txt
Outdated
|
|
||
| [ | ||
| { | ||
| 'NAME': 'contrib.auth.validators.UnicodeUsernameValidator' |
docs/topics/auth/username.txt
Outdated
| their behavior. | ||
|
|
||
| Validation is controlled by the :setting:`AUTH_USERNAME_VALIDATORS` setting. | ||
| The default for the setting is a ``UnicodeUsernameValidator`` which |
docs/topics/auth/username.txt
Outdated
| { | ||
| 'NAME': 'django.contrib.auth.validators.UsernameMinimumLengthValidator', | ||
| 'OPTIONS':{ | ||
| 'min_length':3, |
docs/topics/auth/username.txt
Outdated
| 'Your username must contain at least %(min_length)d | ||
| character.', | ||
| 'Your username must contain at least %(min_length)d | ||
| characters.', |
…hanges to test and docs, fixed docs as per PR suggestion.
jacobtylerwalls
left a comment
There was a problem hiding this comment.
glad to see this moving along. some minor fixes after your last push.
tests/auth_tests/test_validators.py
Outdated
| 'OPTIONS': { | ||
| 'min_length': 3, | ||
| 'limit_value': 3, | ||
| 'message': 'This password is too short', |
There was a problem hiding this comment.
username, not password. :-)
There was a problem hiding this comment.
glad to see this moving along. some minor fixes after your last push.
Thank you! that is so embarrassing. I actually read everything in the docs this time in html.
-- Fixed all the typos and the line break.
-- Added the new method help_text for Unicode and ASCII username validator in the docs
-- Added the UsernameMinimumLength validator in list of Validators as well.
docs/topics/auth/username.txt
Outdated
| self.min_length | ||
| ) % {'min_length': self.min_length} | ||
| 'Your username must contain at least %(limit_value)d | ||
| character.','Your username must contain at least |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
| The minimum length can be customized with the ``min_length`` parameter. | ||
| Validates whether the username meets a minimum length using | ||
| ``limit_value``. This validator is extended from | ||
| ``django.core.validators.MinLengthValidator``. The text given in ``message`` |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
|
|
||
| Here's a basic example of a validator, with one optional setting:: | ||
| Here's a basic example of a validator. This validator extends | ||
| ``django.core.validators.MinLengthValidator``:: |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
docs/topics/auth/username.txt
Outdated
| 'OPTIONS':{ | ||
| 'min_length':3, | ||
| 'input_value': 3, | ||
| 'message': 'Password is too short.' |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
…lude the new method help_text.
|
Hi again. I think to move this forward what you likely need is proof that this has support to get into Django. I was glad to see you post on the mailing list when I suggested it in July, but perhaps the question you asked was so narrow it didn't introduce the subject properly. I think you need to ask more broadly whether this feature should be fashioned after password validation with a dict of validators in Minor things to make this easier to review: please update the body of the description of the PR with the ticket number (ticket-27807), and make the PR title describe the approach you used, something like: "Made username validation configurable through AUTH_USERNAME_VALIDATORS setting" Even if people end up suggesting a different approach, you will have still made a significant contribution in terms of putting forth a concrete approach for folks to look at. 👍 |
Thank you again. I have started a group conversation and updated the detail in PR as well. :) |
carltongibson
left a comment
There was a problem hiding this comment.
Hi @saykharng. Thanks for the input here, but I think we have to close this as wontfix.
The suggested fix here adds a setting, as signal, and sub-api to validators — all of which are significant changes, and I think none of which are justified by the issues.
How do I customize username validation? First and foremost, it's not clear you should: UnicodeUsernameValidator is a good default. But if you want to, use a custom user model. If you can't do that, add the validation you want at the form level, or in clean(), or if you really must monkey patch the field in a ready() handler, and so on.
There are lots of option that don't need so much structure.
I hope that makes sense. I will update the ticket accordingly.
@carltongibson In future, I will open up discussion/share ideas earlier. Regards |
ticket-27807
Solution allows Username Validation to be configured through AUTH_USERNAME_VALIDATORS setting.
The setting follows similar approach to password validators with slight difference. Username Validator must be callable like core validators and requires a help_text method (returns a string explaining the requirement of validation).