1 from django.contrib.auth.backends import ModelBackend
2 from django.core.validators import email_re
4 from astakos.im.models import AstakosUser
6 class TokenBackend(ModelBackend):
8 AuthenticationBackend used to authenticate using token instead
10 def authenticate(self, email=None, auth_token=None):
12 user = AstakosUser.objects.get(email=email, is_active=True)
13 if user.auth_token == auth_token:
15 except AstakosUser.DoesNotExist:
18 def get_user(self, user_id):
20 return AstakosUser.objects.get(pk=user_id)
21 except AstakosUser.DoesNotExist:
24 class EmailBackend(ModelBackend):
26 If the ``username`` parameter is actually an email uses email to authenticate
27 the user else tries the username.
29 Used from ``astakos.im.forms.LoginForm`` to authenticate.
31 def authenticate(self, username=None, password=None):
32 #If username is an email address, then try to pull it up
33 if email_re.search(username):
35 user = AstakosUser.objects.get(email=username, is_active=True)
36 except AstakosUser.DoesNotExist:
39 #We have a non-email address username we
42 user = AstakosUser.objects.get(username=username)
43 except AstakosUser.DoesNotExist:
45 if user.check_password(password):
48 def get_user(self, user_id):
50 return AstakosUser.objects.get(pk=user_id)
51 except AstakosUser.DoesNotExist: