Private
Public Access
1
0

adding APIKey method

This commit is contained in:
2024-11-26 14:49:26 +01:00
parent 68a3ad8bcd
commit 5968d2a0e2
9 changed files with 174 additions and 3 deletions

15
rowers/authentication.py Normal file
View File

@@ -0,0 +1,15 @@
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)