# 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/mongoauth/db.py
# MONGO_HOST = getattr(settings, 'LOCKSMITH_MONGO_HOST', 'localhost')
# MONGO_PORT = getattr(settings, 'LOCKSMITH_MONGO_PORT', 27017)
# MONGO_DATABASE = getattr(settings, 'LOCKSMITH_MONGO_DATABASE', 'locksmith')


from uuid import UUID
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from locksmith.common import get_signature
from locksmith.mongoauth.db import db

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

@require_POST
@csrf_exempt
def create_key(request):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')
    db.keys.insert({'_id':request.POST['key'],
                    'email':request.POST['email'],
                    'status':request.POST['status']})
    return HttpResponse('OK')

@require_POST
@csrf_exempt
def update_key(request, get_by='key'):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')
    # get the key
    if get_by == 'key':

# 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/mongoauth/db.py
# MONGO_HOST = getattr(settings, 'LOCKSMITH_MONGO_HOST', 'localhost')
# MONGO_PORT = getattr(settings, 'LOCKSMITH_MONGO_PORT', 27017)
# MONGO_DATABASE = getattr(settings, 'LOCKSMITH_MONGO_DATABASE', 'locksmith')


# Path: locksmith/mongoauth/views.py
from uuid import UUID
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from locksmith.common import get_signature
from locksmith.mongoauth.db import db


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

@require_POST
@csrf_exempt
def create_key(request):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')
    db.keys.insert({'_id':request.POST['key'],
                    'email':request.POST['email'],
                    'status':request.POST['status']})
    return HttpResponse('OK')

@require_POST
@csrf_exempt
def update_key(request, get_by='key'):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')
    # get the key
    if get_by == 'key':

        key = db.keys.find_one({'_id':request.POST['key']})