# Path: locksmith/common.py
# def get_signature(params, signkey):
#     # sorted k,v pairs of everything but signature
#     data = sorted([(k,unicode(v).encode('utf-8'))
#                    for k,v in params.iteritems()
#                    if k != 'signature'])
#     qs = urllib.urlencode(data)
#     return hmac.new(str(signkey), qs, hashlib.sha1).hexdigest()
# 
# Path: locksmith/auth/models.py
# class ApiKey(models.Model):
#     key = models.CharField(max_length=32, primary_key=True)
#     email = models.EmailField('Email Address')
#     status = models.CharField(max_length=1, choices=KEY_STATUSES, default='U')
# 
#     issued_on = models.DateTimeField(default=datetime.datetime.now, editable=False)
# 
#     def active(self):
#         return status == 'A'
# 
#     def __unicode__(self):
#         return '%s (%s) [%s]' % (self.email, self.key, self.get_status_display())
# 
#     class Meta:
#         db_table = 'locksmith_auth_apikey'


from uuid import UUID
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from locksmith.common import get_signature
from locksmith.auth.models import ApiKey

def verify_signature(post):
    return get_signature(post, settings.LOCKSMITH_SIGNING_KEY) == post['signature']

@csrf_exempt
@require_POST
def create_key(request):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')

# Path: locksmith/common.py
# def get_signature(params, signkey):
#     # sorted k,v pairs of everything but signature
#     data = sorted([(k,unicode(v).encode('utf-8'))
#                    for k,v in params.iteritems()
#                    if k != 'signature'])
#     qs = urllib.urlencode(data)
#     return hmac.new(str(signkey), qs, hashlib.sha1).hexdigest()
# 
# Path: locksmith/auth/models.py
# class ApiKey(models.Model):
#     key = models.CharField(max_length=32, primary_key=True)
#     email = models.EmailField('Email Address')
#     status = models.CharField(max_length=1, choices=KEY_STATUSES, default='U')
# 
#     issued_on = models.DateTimeField(default=datetime.datetime.now, editable=False)
# 
#     def active(self):
#         return status == 'A'
# 
#     def __unicode__(self):
#         return '%s (%s) [%s]' % (self.email, self.key, self.get_status_display())
# 
#     class Meta:
#         db_table = 'locksmith_auth_apikey'


# Path: locksmith/auth/views.py
from uuid import UUID
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from locksmith.common import get_signature
from locksmith.auth.models import ApiKey


def verify_signature(post):
    return get_signature(post, settings.LOCKSMITH_SIGNING_KEY) == post['signature']

@csrf_exempt
@require_POST
def create_key(request):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')

    ApiKey.objects.create(key=request.POST['key'],