-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdrf_authentication.py
More file actions
37 lines (29 loc) · 1.19 KB
/
drf_authentication.py
File metadata and controls
37 lines (29 loc) · 1.19 KB
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
from django.utils.functional import SimpleLazyObject
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from django_firebase_auth.conf import user_getter
from django_firebase_auth.views import verify_firebase_account, AuthError, NoAuthHeader
class LazyUser(SimpleLazyObject):
is_authenticated = True
is_anonymous = False
def __init__(self, func, firebase_uid):
self.__dict__['firebase_uid'] = firebase_uid
super().__init__(func)
def __bool__(self):
return True
class JWTAuthentication(BaseAuthentication):
"""
Use Django's session framework for authentication.
"""
def authenticate(self, request):
"""
Returns a `User` if the request session currently has a logged in user.
Otherwise returns `None`.
"""
try:
jwt_payload = verify_firebase_account(request.headers)
except NoAuthHeader:
return None
except AuthError as ex:
raise AuthenticationFailed(code=ex.error_type, detail=ex.error_type)
return LazyUser(lambda: user_getter.get_or_create_user(jwt_payload), jwt_payload['uid']), None