root / astakos / im / auth_backends.py @ 794852f2
History | View | Annotate | Download (1.8 kB)
1 |
from django.conf import settings |
---|---|
2 |
from django.contrib.auth.backends import ModelBackend |
3 |
#from django.core.exceptions import ImproperlyConfigured
|
4 |
#from django.db.models import get_model
|
5 |
|
6 |
from astakos.im.models import AstakosUser |
7 |
|
8 |
class AstakosUserModelCredentialsBackend(ModelBackend): |
9 |
"""
|
10 |
AuthenticationBackend used to authenticate user creadentials
|
11 |
"""
|
12 |
def authenticate(self, username=None, password=None): |
13 |
try:
|
14 |
user = AstakosUser.objects.get(username=username) |
15 |
if user.check_password(password):
|
16 |
return user
|
17 |
except AstakosUser.DoesNotExist:
|
18 |
return None |
19 |
|
20 |
def get_user(self, user_id): |
21 |
try:
|
22 |
return AstakosUser.objects.get(pk=user_id)
|
23 |
except AstakosUser.DoesNotExist:
|
24 |
return None |
25 |
|
26 |
#@property
|
27 |
#def user_class(self):
|
28 |
# if not hasattr(self, '_user_class'):
|
29 |
# #self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
|
30 |
# self._user_class = get_model('astakos.im', 'astakosuser')
|
31 |
# print '#', self._user_class
|
32 |
# if not self._user_class:
|
33 |
# raise ImproperlyConfigured('Could not get custom user model')
|
34 |
# return self._user_class
|
35 |
|
36 |
class AstakosUserModelTokenBackend(ModelBackend): |
37 |
"""
|
38 |
AuthenticationBackend used to authenticate using token instead
|
39 |
"""
|
40 |
def authenticate(self, username=None, auth_token=None): |
41 |
try:
|
42 |
user = AstakosUser.objects.get(username=username) |
43 |
if user.auth_token == auth_token:
|
44 |
return user
|
45 |
except AstakosUser.DoesNotExist:
|
46 |
return None |
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 |