-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
39 lines (30 loc) · 1.27 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from tastypie.authentication import ApiKeyAuthentication
from tastypie.compat import (get_user_model, get_username_field)
from tastypie.http import HttpUnauthorized
class ApiKeyPatch(ApiKeyAuthentication):
def is_authenticated(self, request, **kwargs):
"""
Finds the user and checks their API key.
Should return either ``True`` if allowed, ``False`` if not or an
``HttpResponse`` if you need something custom.
"""
try:
username, api_key = self.extract_credentials(request)
except ValueError:
return self._unauthorized()
if not username or not api_key:
return self._unauthorized()
username_field = get_username_field()
User = get_user_model()
lookup_kwargs = {username_field: username}
try:
user = User.objects.prefetch_related('api_key').get(
**lookup_kwargs)
except (User.DoesNotExist, User.MultipleObjectsReturned):
return self._unauthorized()
if not self.check_active(user):
return False
key_auth_check = self.get_key(user, api_key)
if key_auth_check and not isinstance(key_auth_check, HttpUnauthorized):
request.user = user
return key_auth_check