You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

  1. import json
  2. import re
  3. from functools import wraps
  4. from django.contrib.auth.models import User
  5. from django.http import (HttpResponseBadRequest, HttpResponseForbidden,
  6. JsonResponse)
  7. from hc.lib.jsonschema import ValidationError, validate
  8. RE_UUID = re.compile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
  9. def uuid_or_400(f):
  10. @wraps(f)
  11. def wrapper(request, *args, **kwds):
  12. if not RE_UUID.match(args[0]):
  13. return HttpResponseBadRequest()
  14. return f(request, *args, **kwds)
  15. return wrapper
  16. def make_error(msg):
  17. return JsonResponse({"error": msg}, status=400)
  18. def check_api_key(f):
  19. @wraps(f)
  20. def wrapper(request, *args, **kwds):
  21. request.json = {}
  22. if request.body:
  23. try:
  24. request.json = json.loads(request.body.decode("utf-8"))
  25. except ValueError:
  26. return make_error("could not parse request body")
  27. if "HTTP_X_API_KEY" in request.META:
  28. api_key = request.META["HTTP_X_API_KEY"]
  29. else:
  30. api_key = request.json.get("api_key", "")
  31. if api_key == "":
  32. return make_error("wrong api_key")
  33. try:
  34. request.user = User.objects.get(profile__api_key=api_key)
  35. except User.DoesNotExist:
  36. return HttpResponseForbidden()
  37. return f(request, *args, **kwds)
  38. return wrapper
  39. def validate_json(schema):
  40. """ Validate request.json contents against `schema`.
  41. Supports a tiny subset of JSON schema spec.
  42. """
  43. def decorator(f):
  44. @wraps(f)
  45. def wrapper(request, *args, **kwds):
  46. try:
  47. validate(request.json, schema)
  48. except ValidationError as e:
  49. return make_error("json validation error: %s" % e)
  50. return f(request, *args, **kwds)
  51. return wrapper
  52. return decorator