Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / auth_backends.py @ 0e17d1ad

History | View | Annotate | Download (1.7 kB)

1 0905ccd2 Sofia Papagiannaki
from django.contrib.auth.backends import ModelBackend
2 5ed6816e Sofia Papagiannaki
from django.core.validators import email_re
3 0905ccd2 Sofia Papagiannaki
4 0905ccd2 Sofia Papagiannaki
from astakos.im.models import AstakosUser
5 0905ccd2 Sofia Papagiannaki
6 5ed6816e Sofia Papagiannaki
class TokenBackend(ModelBackend):
7 794852f2 Sofia Papagiannaki
    """
8 5ed6816e Sofia Papagiannaki
    AuthenticationBackend used to authenticate using token instead
9 794852f2 Sofia Papagiannaki
    """
10 5ed6816e Sofia Papagiannaki
    def authenticate(self, email=None, auth_token=None):
11 0905ccd2 Sofia Papagiannaki
        try:
12 5ed6816e Sofia Papagiannaki
            user = AstakosUser.objects.get(email=email)
13 5ed6816e Sofia Papagiannaki
            if user.auth_token == auth_token:
14 0905ccd2 Sofia Papagiannaki
                return user
15 0905ccd2 Sofia Papagiannaki
        except AstakosUser.DoesNotExist:
16 0905ccd2 Sofia Papagiannaki
            return None
17 0905ccd2 Sofia Papagiannaki
18 0905ccd2 Sofia Papagiannaki
    def get_user(self, user_id):
19 0905ccd2 Sofia Papagiannaki
        try:
20 0905ccd2 Sofia Papagiannaki
            return AstakosUser.objects.get(pk=user_id)
21 0905ccd2 Sofia Papagiannaki
        except AstakosUser.DoesNotExist:
22 0905ccd2 Sofia Papagiannaki
            return None
23 890b0eaf Sofia Papagiannaki
24 5ed6816e Sofia Papagiannaki
class EmailBackend(ModelBackend):
25 794852f2 Sofia Papagiannaki
    """
26 5ed6816e Sofia Papagiannaki
    If the ``username`` parameter is actually an email uses email to authenticate
27 5ed6816e Sofia Papagiannaki
    the user else tries the username.
28 5ed6816e Sofia Papagiannaki
    
29 5ed6816e Sofia Papagiannaki
    Used from ``astakos.im.forms.LoginForm`` to authenticate.
30 794852f2 Sofia Papagiannaki
    """
31 5ed6816e Sofia Papagiannaki
    def authenticate(self, username=None, password=None):
32 5ed6816e Sofia Papagiannaki
        #If username is an email address, then try to pull it up
33 5ed6816e Sofia Papagiannaki
        if email_re.search(username):
34 5ed6816e Sofia Papagiannaki
            try:
35 5ed6816e Sofia Papagiannaki
                user = AstakosUser.objects.get(email=username)
36 5ed6816e Sofia Papagiannaki
            except AstakosUser.DoesNotExist:
37 5ed6816e Sofia Papagiannaki
                return None
38 5ed6816e Sofia Papagiannaki
        else:
39 5ed6816e Sofia Papagiannaki
            #We have a non-email address username we
40 5ed6816e Sofia Papagiannaki
            #should try username
41 5ed6816e Sofia Papagiannaki
            try:
42 5ed6816e Sofia Papagiannaki
                user = AstakosUser.objects.get(username=username)
43 5ed6816e Sofia Papagiannaki
            except AstakosUser.DoesNotExist:
44 5ed6816e Sofia Papagiannaki
                return None
45 5ed6816e Sofia Papagiannaki
        if user.check_password(password):
46 5ed6816e Sofia Papagiannaki
            return user
47 5ed6816e Sofia Papagiannaki
    
48 890b0eaf Sofia Papagiannaki
    def get_user(self, user_id):
49 890b0eaf Sofia Papagiannaki
        try:
50 890b0eaf Sofia Papagiannaki
            return AstakosUser.objects.get(pk=user_id)
51 890b0eaf Sofia Papagiannaki
        except AstakosUser.DoesNotExist:
52 890b0eaf Sofia Papagiannaki
            return None