You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
766 B
24 lines
766 B
from rest_framework_api_key.permissions import BaseHasAPIKey
|
|
from .models import APIKey
|
|
|
|
|
|
class HasAPIKey(BaseHasAPIKey):
|
|
model = APIKey
|
|
|
|
def has_permission(self, request, view):
|
|
# First check if we have a valid API key
|
|
has_api_key = super().has_permission(request, view)
|
|
|
|
if has_api_key:
|
|
# Get the API key from the request
|
|
key = self.get_key(request)
|
|
if key:
|
|
try:
|
|
api_key = APIKey.objects.get_from_key(key)
|
|
# Set the request.user to the user associated with the API key
|
|
request.user = api_key.user
|
|
return True
|
|
except APIKey.DoesNotExist:
|
|
pass
|
|
|
|
return False
|
|
|