Skip to content

Commit

Permalink
Add support for login with email/password on the auth/login view
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Aug 24, 2020
1 parent a046b86 commit ab40c17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions onadata/apps/main/backends.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend as DjangoModelBackend
from django.db.models import Q


class ModelBackend(DjangoModelBackend):
def authenticate(self, request=None, username=None, password=None):
"""Username is case insensitive."""
try:
user = User.objects.get(username__iexact=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
"""
Username is case insensitive. Supports using email in place of username
"""
user = User.objects.filter(
Q(username__iexact=username) | Q(email__iexact=username)).first()

if user and user.check_password(password):
return user

return None
8 changes: 8 additions & 0 deletions onadata/apps/main/tests/test_http_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def test_http_auth(self):
**self._set_auth_headers('bob', 'bob'))
self.assertEqual(response.status_code, 200)

# Set user email
self.user.email = "bob@testing_pros.com"
self.user.save()
# headers with valid email/pass
response = self.client.get(
self.api_url, **self._set_auth_headers(self.user.email, 'bob'))
self.assertEqual(response.status_code, 200)

def test_http_auth_shared_data(self):
self.xform.shared_data = True
self.xform.save()
Expand Down

0 comments on commit ab40c17

Please sign in to comment.