39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from django.contrib.auth import get_user_model
|
|
from django.core.exceptions import FieldError, ValidationError
|
|
from rest_framework import exceptions
|
|
from rest_framework.authentication import BaseAuthentication
|
|
|
|
from drf_uuid_auth.settings import api_settings
|
|
|
|
|
|
class UuidAuthentication(BaseAuthentication):
|
|
def authenticate(self, request):
|
|
header = request.META.get(api_settings.AUTH_HEADER.upper(), None)
|
|
if header is None or header == "" or header == "-":
|
|
return None
|
|
|
|
payload = header
|
|
user = self.authenticate_credentials(payload)
|
|
|
|
return user, payload
|
|
|
|
def authenticate_credentials(self, payload):
|
|
User = get_user_model()
|
|
|
|
try:
|
|
user = User.objects.get(uid=payload)
|
|
except User.DoesNotExist:
|
|
raise exceptions.AuthenticationFailed("Invalid user id")
|
|
except FieldError:
|
|
raise exceptions.AuthenticationFailed("Incorrect user model")
|
|
except ValidationError:
|
|
raise exceptions.AuthenticationFailed("Incorrect user id")
|
|
|
|
if not user.is_active:
|
|
raise exceptions.AuthenticationFailed("User isn't active")
|
|
|
|
return user
|
|
|
|
def authenticate_header(self, request):
|
|
pass
|