from django.contrib.auth.models import AnonymousUser
from rest_framework import authentication
from rest_framework.authentication import TokenAuthentication
[docs]class AnonymousAuthentication(authentication.BaseAuthentication):
[docs] def authenticate(self, request):
"""
Authenticate the request for anyone!
"""
return AnonymousUser(), None
[docs]class AnonymousOrAuthenticatedAuthentication(authentication.BaseAuthentication):
[docs] 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:
return AnonymousUser(), None