-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauthentication.py
More file actions
29 lines (21 loc) · 850 Bytes
/
authentication.py
File metadata and controls
29 lines (21 loc) · 850 Bytes
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
import logging
from django.contrib.auth.models import AnonymousUser
from rest_framework import authentication
from rest_framework.authentication import TokenAuthentication
logger = logging.getLogger(__name__)
class AnonymousAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
"""
Authenticate the request for anyone!
"""
return AnonymousUser(), None
class AnonymousOrAuthenticatedAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
"""
Authenticate the request for anyone or if a valid token is provided, a user.
"""
try:
return TokenAuthentication.authenticate(TokenAuthentication(), request)
except Exception as e:
logger.debug(e)
return AnonymousUser(), None