Created
January 15, 2013 18:45
-
-
Save pedroburon/4540932 to your computer and use it in GitHub Desktop.
apikey authorization decorator
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
import logging | |
from django import http | |
from apikey.models import ApiKey | |
def apikey_authorization(view): | |
def new_view(request, *args, **kwargs): | |
key_pair = request.META.get('HTTP_X_API') | |
if key_pair is not None: | |
username, apikey = key_pair.split(':') | |
try: | |
ApiKey.objects.get(user__username=username, key=apikey) | |
return view(request, *args, **kwargs) | |
except ApiKey.DoesNotExist: | |
logger.warn('Trying to access %s with keypair %s.' % (view.__name__, key_pair)) | |
else: | |
logger.warn('Does not have X-API.') | |
return http.HttpResponseForbidden() | |
return new_view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment