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)