Last active
December 29, 2015 22:17
-
-
Save mushfiq/eb2d3755eb2ca12048d6 to your computer and use it in GitHub Desktop.
DRF custom authentication class for access key based auth checking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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