Skip to content

Instantly share code, notes, and snippets.

@mushfiq
Last active December 29, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mushfiq/eb2d3755eb2ca12048d6 to your computer and use it in GitHub Desktop.
Save mushfiq/eb2d3755eb2ca12048d6 to your computer and use it in GitHub Desktop.
DRF custom authentication class for access key based auth checking.
from rest_framework import authentication
from rest_framework import exceptions
from apps.newspaper.models import Subscriber
class AccessKeyAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
access_key = request.GET.get("access_key", None)
if not access_key:
raise exceptions.NotFound("Access key not provided.")
try:
user = Subscriber.objects.get(access_key=access_key)
except Subscriber.DoesNotExist:
raise exceptions.PermissionDenied("No User found with the access key")
except ValueError:
raise exceptions.ValidationError("Badly formed hexadecimal UUID string")
return (user, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment