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.
 
 
 
 

135 lines
5.6 KiB

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