Private
Public Access
1
0
Files
rowsandall/rowers/authentication.py

16 lines
558 B
Python

from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from rowers.models import APIKey
class APIKeyAuthentication(BaseAuthentication):
def authenticate(self, request):
api_key = request.META.get('HTTP_AUTHORIZATION')
if not api_key:
return None
try:
api_key = APIKey.objects.get(key=api_key, is_active=True)
except APIKey.DoesNotExist:
raise AuthenticationFailed('Invalid API key')
return (api_key.user, None)