Skip to content

Instantly share code, notes, and snippets.

@pedroburon
Created January 15, 2013 18:45
Show Gist options
  • Save pedroburon/4540932 to your computer and use it in GitHub Desktop.
Save pedroburon/4540932 to your computer and use it in GitHub Desktop.
apikey authorization decorator
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