adding APIKey method
This commit is contained in:
15
rowers/authentication.py
Normal file
15
rowers/authentication.py
Normal 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)
|
||||
Reference in New Issue
Block a user