Skip to content
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 8 commits into from
Apr 2, 2015
19 changes: 19 additions & 0 deletions authtools/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.contrib.auth.backends import ModelBackend


class CaseInsensitiveEmailBackend(ModelBackend):
"""
This authentication backend assumes that usernames are email addresses and simply lowercases
a username before an attempt is made to authenticate said username using Django's ModelBackend.

Example usage:
# In settings.py
AUTHENTICATION_BACKENDS = ('authtools.backends.CaseInsensitiveEmailBackend',)
"""
def authenticate(self, username=None, password=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

I have an app that requires me to pass in the request to the authenticate calls.

Choose a reason for hiding this comment

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

@rockymeza Are you saying this because of the method signature?

If so, django handles the TypeError https://github.com/django/django/blob/master/django/contrib/auth/__init__.py#L69

Copy link
Author

Choose a reason for hiding this comment

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

He wants the lower() to be called, though, so this should just be changed to be **credentials instead.

username = username.lower()

Choose a reason for hiding this comment

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

Also, I think this is going to raise an AttributeError that will bubble out of authenticate if called like authenticate(password='foo').

Copy link
Contributor

Choose a reason for hiding this comment

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

def authenticate(self, username, **credentials):

This would require that a username is passed in.


return super(CaseInsensitiveEmailBackend, self).authenticate(
username=username,
password=password
)