Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / auth_backends.py @ f289215d

History | View | Annotate | Download (1.7 kB)

1
from django.contrib.auth.backends import ModelBackend
2
from django.core.validators import email_re
3

    
4
from astakos.im.models import AstakosUser
5

    
6
class TokenBackend(ModelBackend):
7
    """
8
    AuthenticationBackend used to authenticate using token instead
9
    """
10
    def authenticate(self, email=None, auth_token=None):
11
        try:
12
            user = AstakosUser.objects.get(email=email)
13
            if user.auth_token == auth_token:
14
                return user
15
        except AstakosUser.DoesNotExist:
16
            return None
17

    
18
    def get_user(self, user_id):
19
        try:
20
            return AstakosUser.objects.get(pk=user_id)
21
        except AstakosUser.DoesNotExist:
22
            return None
23

    
24
class EmailBackend(ModelBackend):
25
    """
26
    If the ``username`` parameter is actually an email uses email to authenticate
27
    the user else tries the username.
28
    
29
    Used from ``astakos.im.forms.LoginForm`` to authenticate.
30
    """
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):
34
            try:
35
                user = AstakosUser.objects.get(email=username)
36
            except AstakosUser.DoesNotExist:
37
                return None
38
        else:
39
            #We have a non-email address username we
40
            #should try username
41
            try:
42
                user = AstakosUser.objects.get(username=username)
43
            except AstakosUser.DoesNotExist:
44
                return None
45
        if user.check_password(password):
46
            return user
47
    
48
    def get_user(self, user_id):
49
        try:
50
            return AstakosUser.objects.get(pk=user_id)
51
        except AstakosUser.DoesNotExist:
52
            return None