-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new authentication backend for case-insensitive emails #31
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c367c8a
Add a new authentication backend for case-insensitive emails as a pos…
2353bcb
Add **kwargs to method signature, check to see if username is passed in
999786e
Add cautionary message to potential users of the backend
23ec089
Add mixin to make the case-insensitive email auth backend more flexible
50aa976
Move docstring to mixin, and remove redundant functionality in CaseIn…
70f8804
Add docs for new authentication backend classes
bfd7dd6
Add new UserCreationForm subclass and corresponding documentation
4ab3445
Add suggestion in backends docs, fix typo in backends.py
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from django.contrib.auth.backends import ModelBackend | ||
|
||
|
||
class CaseInsensitiveEmailBackendMixin(object): | ||
def authenticate(self, username=None, password=None, **kwargs): | ||
""" | ||
This authentication backend assumes that usernames are email addresses and simply | ||
lowercases a username before an attempt is made to authenticate said username using a | ||
superclass's authenticate method. This superclass should be either a user-defined | ||
authentication backend, or a Django-provided authentication backend (e.g., ModelBackend). | ||
|
||
Example usage: | ||
See CaseInsensitiveEmailModelBackend, below. | ||
|
||
NOTE: | ||
A word of caution. Use of this backend presupposes a way to ensure that users cannot | ||
create usernames that differ only in case (e.g., [email protected] and [email protected]). It is | ||
advised that you use this backend in conjunction with the | ||
EmailInsensitiveCreateUserForm provided in the forms module. | ||
""" | ||
if username is not None: | ||
username = username.lower() | ||
|
||
return super(CaseInsensitiveEmailBackendMixin, self).authenticate( | ||
username=username, | ||
password=password, | ||
**kwargs | ||
) | ||
|
||
|
||
class CaseInsensitiveEmailModelBackend(CaseInsensitiveEmailBackendMixin, ModelBackend): | ||
pass |
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,23 @@ | ||
Authentication Backends | ||
===== | ||
|
||
.. currentmodule:: authtools.backends | ||
|
||
django-authtools provides two authorization backend classes. These backends offer more customization | ||
for how your :class:`authtool.models.User` class is authenticated. | ||
|
||
.. class:: CaseInsensitiveEmailBackendMixin | ||
|
||
This mixin simply calls the ``authenticate`` method of its superclass after lowercasing the | ||
provided username. This superclass should be a user-defined or Django-provided authentication | ||
backend, such as ``django.contrib.auth.backends.ModelBackend``. | ||
|
||
.. warning: | ||
Use of this mixin presupposes that all usernames are stored in their lowercase form, and | ||
that there is no way to have usernames differing only in case. If usernames can differ in | ||
case, this authentication backend mixin could cause errors in user authentication. | ||
|
||
.. class:: CaseInsensitiveEmailModelBackend | ||
A subclass of the ``CaseInsentiveEmailBackendMixin`` with | ||
``django.contrib.auth.backends.ModelBackend`` as its chosen authentication backend superclass. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a sentence like: to use this, put this in your settings: AUTHENTICATION_BACKENDS = (
'authtools.backends.CaseInsensitiveEmailModelBackend',
) And then mention that if they have any other authentication backends that they need to mix the mixin into them. |
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nitpicky, but it would be nice to just provide the qualname for the form:
It's just so that I don't have to parse "the forms module"