PageRenderTime 43ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/common/djangoapps/django_future/csrf.py

https://github.com/ngocchung75/edx-platform
Python | 83 lines | 79 code | 3 blank | 1 comment | 1 complexity | 0f0590ac15aa786b95730e547bb0bf4b MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. # Taken from Django 1.4
  2. import warnings
  3. from django.middleware.csrf import CsrfViewMiddleware, get_token
  4. from django.utils.decorators import decorator_from_middleware, available_attrs
  5. from functools import wraps
  6. csrf_protect = decorator_from_middleware(CsrfViewMiddleware)
  7. csrf_protect.__name__ = "csrf_protect"
  8. csrf_protect.__doc__ = """
  9. This decorator adds CSRF protection in exactly the same way as
  10. CsrfViewMiddleware, but it can be used on a per view basis. Using both, or
  11. using the decorator multiple times, is harmless and efficient.
  12. """
  13. class _EnsureCsrfToken(CsrfViewMiddleware):
  14. # We need this to behave just like the CsrfViewMiddleware, but not reject
  15. # requests.
  16. def _reject(self, request, reason):
  17. return None
  18. requires_csrf_token = decorator_from_middleware(_EnsureCsrfToken)
  19. requires_csrf_token.__name__ = 'requires_csrf_token'
  20. requires_csrf_token.__doc__ = """
  21. Use this decorator on views that need a correct csrf_token available to
  22. RequestContext, but without the CSRF protection that csrf_protect
  23. enforces.
  24. """
  25. class _EnsureCsrfCookie(CsrfViewMiddleware):
  26. def _reject(self, request, reason):
  27. return None
  28. def process_view(self, request, callback, callback_args, callback_kwargs):
  29. retval = super(_EnsureCsrfCookie, self).process_view(request, callback, callback_args, callback_kwargs)
  30. # Forces process_response to send the cookie
  31. get_token(request)
  32. return retval
  33. ensure_csrf_cookie = decorator_from_middleware(_EnsureCsrfCookie)
  34. ensure_csrf_cookie.__name__ = 'ensure_csrf_cookie'
  35. ensure_csrf_cookie.__doc__ = """
  36. Use this decorator to ensure that a view sets a CSRF cookie, whether or not it
  37. uses the csrf_token template tag, or the CsrfViewMiddleware is used.
  38. """
  39. def csrf_response_exempt(view_func):
  40. """
  41. Modifies a view function so that its response is exempt
  42. from the post-processing of the CSRF middleware.
  43. """
  44. warnings.warn("csrf_response_exempt is deprecated. It no longer performs a "
  45. "function, and calls to it can be removed.",
  46. PendingDeprecationWarning)
  47. return view_func
  48. def csrf_view_exempt(view_func):
  49. """
  50. Marks a view function as being exempt from CSRF view protection.
  51. """
  52. warnings.warn("csrf_view_exempt is deprecated. Use csrf_exempt instead.",
  53. PendingDeprecationWarning)
  54. return csrf_exempt(view_func)
  55. def csrf_exempt(view_func):
  56. """
  57. Marks a view function as being exempt from the CSRF view protection.
  58. """
  59. # We could just do view_func.csrf_exempt = True, but decorators
  60. # are nicer if they don't have side-effects, so we return a new
  61. # function.
  62. def wrapped_view(*args, **kwargs):
  63. return view_func(*args, **kwargs)
  64. wrapped_view.csrf_exempt = True
  65. return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)