Skip to content
/ django Public

Fixed #27807 -- Made username validation configurable through AUTH_USERNAME_VALIDATORS setting#13114

Closed
saykharng wants to merge 16 commits intodjango:masterfrom
saykharng:ticket_27807
Closed

Fixed #27807 -- Made username validation configurable through AUTH_USERNAME_VALIDATORS setting#13114
saykharng wants to merge 16 commits intodjango:masterfrom
saykharng:ticket_27807

Conversation

@saykharng
Copy link
Copy Markdown

@saykharng saykharng commented Jun 26, 2020

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).

  • Added USERNAME_VALIDATORS config in settings.
  • Username Validator should be callable, like core validators.
  • Username Validator should have help_text() method, which is to display the requirement to the user.
  • Added help_text method on UnicodeUsernameValidator and ASCIIUsernameValidator
  • Added help_text method on UnicodeUsernameValidator and ASCIIUsernameValidator
  • Added Username Validation page in docs/topics
  • Added test in auth_tests

@saykharng saykharng changed the title Ticket 27807 Fixed #27807 -- Added ValidatorList and UsernameValidatorList to allow username validator cutomization Jun 27, 2020
@jacobtylerwalls
Copy link
Copy Markdown
Member

Hi Shekhar, another (prospective) first time Django contributor here. Thought I would help out a fellow first timer by commenting on your documentation.

Copy link
Copy Markdown
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@saykharng
Copy link
Copy Markdown
Author

Hi Shekhar, another (prospective) first time Django contributor here. Thought I would help out a fellow first timer by commenting on your documentation.
Hi Jacob, Thank you! I will go through each comment and make changes.

@saykharng
Copy link
Copy Markdown
Author

Hi Shekhar, another (prospective) first time Django contributor here. Thought I would help out a fellow first timer by commenting on your documentation.

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.

@jacobtylerwalls
Copy link
Copy Markdown
Member

Hi Shekhar!
Since this is a new feature, I think the next step is for you to post on django-developers to find consensus on the approach. See more here: https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/submitting-patches/
Cheers, Jacob

@saykharng
Copy link
Copy Markdown
Author

Hi Shekhar!
Since this is a new feature, I think the next step is for you to post on django-developers to find consensus on the approach. See more here: https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/submitting-patches/
Cheers, Jacob

Thank you!

Wrote a feature to allow users to customize username validators.
@saykharng saykharng marked this pull request as draft August 1, 2020 12:55
@saykharng saykharng marked this pull request as ready for review August 2, 2020 08:12
@saykharng
Copy link
Copy Markdown
Author

Hi Shekhar!
Since this is a new feature, I think the next step is for you to post on django-developers to find consensus on the approach. See more here: https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/submitting-patches/
Cheers, Jacob

Thank you!

I have opened a discussion but waiting on feedback.

@saykharng saykharng closed this Aug 2, 2020
@saykharng saykharng changed the title Fixed #27807 -- Added ValidatorList and UsernameValidatorList to allow username validator cutomization Fixed #27807 -- Added username_validation to allow username validator cutomization Aug 2, 2020
@saykharng saykharng reopened this Aug 3, 2020
Copy link
Copy Markdown
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Copy Markdown
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

Added test for Forms changes, fixed typos and spelling.
@saykharng
Copy link
Copy Markdown
Author

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 👍

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. :)

Copy link
Copy Markdown
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

@saykharng
Copy link
Copy Markdown
Author

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.

Thank you!

  • Fixed the docs based on comments (looking at a html page was much easier to find issue.)
  • Removed username_validator variable
  • Removed word 'English' from 'English letters' in UnicodeUsernameValidator

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'?

https://en.m.wikipedia.org/wiki/Latin_alphabet

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I have made the changes as suggested.

Copy link
Copy Markdown
Contributor

@matthiask matthiask left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I really like replacing "English" with "Latin alphabet"
  • Maybe you could move the existing username validators to the username_validation module too since you have been inspired by the password_validation module (and of course leave backwards compatibility shims in django.contrib.auth.validators.) (Not important, just a suggestion.)



@deconstructible
class UsernameMinimumLengthValidator:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback. Apologies for extreme delay in response.

I have made suggested changes other than moving validators to username_validation.


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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use :class:`~django.contrib.auth.validators.UnicodeUsernameValidator` here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


[
{
'NAME': 'contrib.auth.validators.UnicodeUsernameValidator'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"django." is missing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

their behavior.

Validation is controlled by the :setting:`AUTH_USERNAME_VALIDATORS` setting.
The default for the setting is a ``UnicodeUsernameValidator`` which
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

":class:`~...`" again

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
'NAME': 'django.contrib.auth.validators.UsernameMinimumLengthValidator',
'OPTIONS':{
'min_length':3,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces after : missing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

'Your username must contain at least %(min_length)d
character.',
'Your username must contain at least %(min_length)d
characters.',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange line breaks

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

…hanges to test and docs, fixed docs as per PR suggestion.
Copy link
Copy Markdown
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glad to see this moving along. some minor fixes after your last push.

'OPTIONS': {
'min_length': 3,
'limit_value': 3,
'message': 'This password is too short',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username, not password. :-)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

'OPTIONS':{
'min_length':3,
'input_value': 3,
'message': 'Password is too short.'

This comment was marked as resolved.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@jacobtylerwalls
Copy link
Copy Markdown
Member

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 global_settings.py, and if so, whether your implementation is on track, and further, whether it should be documented with an entire new page almost identical to the password validation page, or whether they should be integrated. One respondent on the ticket modeled an approach similar to yours, so that's promising.

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. 👍

@saykharng saykharng changed the title Fixed #27807 -- Added username_validation to allow username validator cutomization Fixed #27807 -- Made username validation configurable through AUTH_USERNAME_VALIDATORS setting Nov 2, 2020
@saykharng
Copy link
Copy Markdown
Author

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 global_settings.py, and if so, whether your implementation is on track, and further, whether it should be documented with an entire new page almost identical to the password validation page, or whether they should be integrated. One respondent on the ticket modeled an approach similar to yours, so that's promising.

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. :)

Copy link
Copy Markdown
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@saykharng
Copy link
Copy Markdown
Author

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
Appreciate your time and thank you for the feedback.
I did learn a lot about Django, tests and documentation while working on it.

In future, I will open up discussion/share ideas earlier.

Regards
Shekhar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants