Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.2 kB)

1 d5fae5f0 Sofia Papagiannaki
# Copyright 2011 GRNET S.A. All rights reserved.
2 d5fae5f0 Sofia Papagiannaki
#
3 d5fae5f0 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 d5fae5f0 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 d5fae5f0 Sofia Papagiannaki
# conditions are met:
6 d5fae5f0 Sofia Papagiannaki
#
7 d5fae5f0 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 d5fae5f0 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 d5fae5f0 Sofia Papagiannaki
#      disclaimer.
10 d5fae5f0 Sofia Papagiannaki
#
11 d5fae5f0 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 d5fae5f0 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 d5fae5f0 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 d5fae5f0 Sofia Papagiannaki
#      provided with the distribution.
15 d5fae5f0 Sofia Papagiannaki
#
16 d5fae5f0 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d5fae5f0 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d5fae5f0 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d5fae5f0 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 d5fae5f0 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d5fae5f0 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d5fae5f0 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d5fae5f0 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d5fae5f0 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d5fae5f0 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d5fae5f0 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d5fae5f0 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 d5fae5f0 Sofia Papagiannaki
#
29 d5fae5f0 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 d5fae5f0 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 d5fae5f0 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 d5fae5f0 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 d5fae5f0 Sofia Papagiannaki
34 0905ccd2 Sofia Papagiannaki
from django.contrib.auth.backends import ModelBackend
35 5ed6816e Sofia Papagiannaki
from django.core.validators import email_re
36 0905ccd2 Sofia Papagiannaki
37 0905ccd2 Sofia Papagiannaki
from astakos.im.models import AstakosUser
38 0905ccd2 Sofia Papagiannaki
39 5ed6816e Sofia Papagiannaki
class TokenBackend(ModelBackend):
40 794852f2 Sofia Papagiannaki
    """
41 5ed6816e Sofia Papagiannaki
    AuthenticationBackend used to authenticate using token instead
42 794852f2 Sofia Papagiannaki
    """
43 5ed6816e Sofia Papagiannaki
    def authenticate(self, email=None, auth_token=None):
44 0905ccd2 Sofia Papagiannaki
        try:
45 0a569195 Sofia Papagiannaki
            user = AstakosUser.objects.get(email=email, is_active=True)
46 5ed6816e Sofia Papagiannaki
            if user.auth_token == auth_token:
47 0905ccd2 Sofia Papagiannaki
                return user
48 0905ccd2 Sofia Papagiannaki
        except AstakosUser.DoesNotExist:
49 0905ccd2 Sofia Papagiannaki
            return None
50 0905ccd2 Sofia Papagiannaki
51 0905ccd2 Sofia Papagiannaki
    def get_user(self, user_id):
52 0905ccd2 Sofia Papagiannaki
        try:
53 0905ccd2 Sofia Papagiannaki
            return AstakosUser.objects.get(pk=user_id)
54 0905ccd2 Sofia Papagiannaki
        except AstakosUser.DoesNotExist:
55 0905ccd2 Sofia Papagiannaki
            return None
56 890b0eaf Sofia Papagiannaki
57 5ed6816e Sofia Papagiannaki
class EmailBackend(ModelBackend):
58 794852f2 Sofia Papagiannaki
    """
59 5ed6816e Sofia Papagiannaki
    If the ``username`` parameter is actually an email uses email to authenticate
60 5ed6816e Sofia Papagiannaki
    the user else tries the username.
61 5ed6816e Sofia Papagiannaki
    
62 5ed6816e Sofia Papagiannaki
    Used from ``astakos.im.forms.LoginForm`` to authenticate.
63 794852f2 Sofia Papagiannaki
    """
64 5ed6816e Sofia Papagiannaki
    def authenticate(self, username=None, password=None):
65 5ed6816e Sofia Papagiannaki
        #If username is an email address, then try to pull it up
66 5ed6816e Sofia Papagiannaki
        if email_re.search(username):
67 5ed6816e Sofia Papagiannaki
            try:
68 0a569195 Sofia Papagiannaki
                user = AstakosUser.objects.get(email=username, is_active=True)
69 5ed6816e Sofia Papagiannaki
            except AstakosUser.DoesNotExist:
70 5ed6816e Sofia Papagiannaki
                return None
71 5ed6816e Sofia Papagiannaki
        else:
72 5ed6816e Sofia Papagiannaki
            #We have a non-email address username we
73 5ed6816e Sofia Papagiannaki
            #should try username
74 5ed6816e Sofia Papagiannaki
            try:
75 5ed6816e Sofia Papagiannaki
                user = AstakosUser.objects.get(username=username)
76 5ed6816e Sofia Papagiannaki
            except AstakosUser.DoesNotExist:
77 5ed6816e Sofia Papagiannaki
                return None
78 5ed6816e Sofia Papagiannaki
        if user.check_password(password):
79 5ed6816e Sofia Papagiannaki
            return user
80 5ed6816e Sofia Papagiannaki
    
81 890b0eaf Sofia Papagiannaki
    def get_user(self, user_id):
82 890b0eaf Sofia Papagiannaki
        try:
83 890b0eaf Sofia Papagiannaki
            return AstakosUser.objects.get(pk=user_id)
84 890b0eaf Sofia Papagiannaki
        except AstakosUser.DoesNotExist:
85 890b0eaf Sofia Papagiannaki
            return None