-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemail-auth.py
36 lines (33 loc) · 1.3 KB
/
email-auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.contrib.auth.models import User
import re
class BasicBackend:
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
email_re = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' # quoted-string
r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
class EmailBackend(BasicBackend):
def authenticate(self, username=None, password=None):
user = None
print username
print password
#If username is an email address, then try to pull it up
if email_re.search(username):
try:
user = User.objects.get(email=(username.lower()))
#print "(%s)" % user.password
except User.DoesNotExist:
return None
else:
return None
#We have a non-email address username we should try username
#try:
# user = User.objects.get(username=username)
#except User.DoesNotExist:
# return None
if user.check_password(password):
return user