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.

82 lines
2.4 KiB

  1. import json
  2. import uuid
  3. from functools import wraps
  4. from django.contrib.auth.models import User
  5. from django.http import HttpResponseBadRequest, JsonResponse
  6. from six import string_types
  7. def uuid_or_400(f):
  8. @wraps(f)
  9. def wrapper(request, *args, **kwds):
  10. try:
  11. uuid.UUID(args[0])
  12. except ValueError:
  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 make_error("wrong api_key")
  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. for key, spec in schema["properties"].items():
  47. if key not in request.json:
  48. continue
  49. value = request.json[key]
  50. if spec["type"] == "string":
  51. if not isinstance(value, string_types):
  52. return make_error("%s is not a string" % key)
  53. elif spec["type"] == "number":
  54. if not isinstance(value, int):
  55. return make_error("%s is not a number" % key)
  56. if "minimum" in spec and value < spec["minimum"]:
  57. return make_error("%s is too small" % key)
  58. if "maximum" in spec and value > spec["maximum"]:
  59. return make_error("%s is too large" % key)
  60. return f(request, *args, **kwds)
  61. return wrapper
  62. return decorator