middleware.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from django.contrib import auth
  2. from django.contrib.auth import load_backend
  3. from django.contrib.auth.backends import RemoteUserBackend
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.utils.crypto import constant_time_compare
  6. from django.utils.functional import SimpleLazyObject
  7. def get_user(request):
  8. if not hasattr(request, '_cached_user'):
  9. request._cached_user = auth.get_user(request)
  10. return request._cached_user
  11. class AuthenticationMiddleware(object):
  12. def process_request(self, request):
  13. assert hasattr(request, 'session'), (
  14. "The Django authentication middleware requires session middleware "
  15. "to be installed. Edit your MIDDLEWARE_CLASSES setting to insert "
  16. "'django.contrib.sessions.middleware.SessionMiddleware' before "
  17. "'django.contrib.auth.middleware.AuthenticationMiddleware'."
  18. )
  19. request.user = SimpleLazyObject(lambda: get_user(request))
  20. class SessionAuthenticationMiddleware(object):
  21. """
  22. Middleware for invalidating a user's sessions that don't correspond to the
  23. user's current session authentication hash (generated based on the user's
  24. password for AbstractUser).
  25. """
  26. def process_request(self, request):
  27. user = request.user
  28. if user and hasattr(user, 'get_session_auth_hash'):
  29. session_hash = request.session.get(auth.HASH_SESSION_KEY)
  30. session_hash_verified = session_hash and constant_time_compare(
  31. session_hash,
  32. user.get_session_auth_hash()
  33. )
  34. if not session_hash_verified:
  35. auth.logout(request)
  36. class RemoteUserMiddleware(object):
  37. """
  38. Middleware for utilizing Web-server-provided authentication.
  39. If request.user is not authenticated, then this middleware attempts to
  40. authenticate the username passed in the ``REMOTE_USER`` request header.
  41. If authentication is successful, the user is automatically logged in to
  42. persist the user in the session.
  43. The header used is configurable and defaults to ``REMOTE_USER``. Subclass
  44. this class and change the ``header`` attribute if you need to use a
  45. different header.
  46. """
  47. # Name of request header to grab username from. This will be the key as
  48. # used in the request.META dictionary, i.e. the normalization of headers to
  49. # all uppercase and the addition of "HTTP_" prefix apply.
  50. header = "REMOTE_USER"
  51. def process_request(self, request):
  52. # AuthenticationMiddleware is required so that request.user exists.
  53. if not hasattr(request, 'user'):
  54. raise ImproperlyConfigured(
  55. "The Django remote user auth middleware requires the"
  56. " authentication middleware to be installed. Edit your"
  57. " MIDDLEWARE_CLASSES setting to insert"
  58. " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
  59. " before the RemoteUserMiddleware class.")
  60. try:
  61. username = request.META[self.header]
  62. except KeyError:
  63. # If specified header doesn't exist then remove any existing
  64. # authenticated remote-user, or return (leaving request.user set to
  65. # AnonymousUser by the AuthenticationMiddleware).
  66. if request.user.is_authenticated():
  67. self._remove_invalid_user(request)
  68. return
  69. # If the user is already authenticated and that user is the user we are
  70. # getting passed in the headers, then the correct user is already
  71. # persisted in the session and we don't need to continue.
  72. if request.user.is_authenticated():
  73. if request.user.get_username() == self.clean_username(username, request):
  74. return
  75. else:
  76. # An authenticated user is associated with the request, but
  77. # it does not match the authorized user in the header.
  78. self._remove_invalid_user(request)
  79. # We are seeing this user for the first time in this session, attempt
  80. # to authenticate the user.
  81. user = auth.authenticate(remote_user=username)
  82. if user:
  83. # User is valid. Set request.user and persist user in the session
  84. # by logging the user in.
  85. request.user = user
  86. auth.login(request, user)
  87. def clean_username(self, username, request):
  88. """
  89. Allows the backend to clean the username, if the backend defines a
  90. clean_username method.
  91. """
  92. backend_str = request.session[auth.BACKEND_SESSION_KEY]
  93. backend = auth.load_backend(backend_str)
  94. try:
  95. username = backend.clean_username(username)
  96. except AttributeError: # Backend has no clean_username method.
  97. pass
  98. return username
  99. def _remove_invalid_user(self, request):
  100. """
  101. Removes the current authenticated user in the request which is invalid
  102. but only if the user is authenticated via the RemoteUserBackend.
  103. """
  104. try:
  105. stored_backend = load_backend(request.session.get(auth.BACKEND_SESSION_KEY, ''))
  106. except ImportError:
  107. # backend failed to load
  108. auth.logout(request)
  109. else:
  110. if isinstance(stored_backend, RemoteUserBackend):
  111. auth.logout(request)