django.contrib.auth.middleware: 35 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/auth/middleware.py

Stats: 0 executed, 32 missed, 3 excluded, 45 ignored

  1. from django.contrib import auth
  2. from django.core.exceptions import ImproperlyConfigured
  3. from django.utils.functional import SimpleLazyObject
  4. def get_user(request):
  5. if not hasattr(request, '_cached_user'):
  6. request._cached_user = auth.get_user(request)
  7. return request._cached_user
  8. class AuthenticationMiddleware(object):
  9. def process_request(self, request):
  10. assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
  11. request.user = SimpleLazyObject(lambda: get_user(request))
  12. class RemoteUserMiddleware(object):
  13. """
  14. Middleware for utilizing Web-server-provided authentication.
  15. If request.user is not authenticated, then this middleware attempts to
  16. authenticate the username passed in the ``REMOTE_USER`` request header.
  17. If authentication is successful, the user is automatically logged in to
  18. persist the user in the session.
  19. The header used is configurable and defaults to ``REMOTE_USER``. Subclass
  20. this class and change the ``header`` attribute if you need to use a
  21. different header.
  22. """
  23. # Name of request header to grab username from. This will be the key as
  24. # used in the request.META dictionary, i.e. the normalization of headers to
  25. # all uppercase and the addition of "HTTP_" prefix apply.
  26. header = "REMOTE_USER"
  27. def process_request(self, request):
  28. # AuthenticationMiddleware is required so that request.user exists.
  29. if not hasattr(request, 'user'):
  30. raise ImproperlyConfigured(
  31. "The Django remote user auth middleware requires the"
  32. " authentication middleware to be installed. Edit your"
  33. " MIDDLEWARE_CLASSES setting to insert"
  34. " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
  35. " before the RemoteUserMiddleware class.")
  36. try:
  37. username = request.META[self.header]
  38. except KeyError:
  39. # If specified header doesn't exist then return (leaving
  40. # request.user set to AnonymousUser by the
  41. # AuthenticationMiddleware).
  42. return
  43. # If the user is already authenticated and that user is the user we are
  44. # getting passed in the headers, then the correct user is already
  45. # persisted in the session and we don't need to continue.
  46. if request.user.is_authenticated():
  47. if request.user.username == self.clean_username(username, request):
  48. return
  49. # We are seeing this user for the first time in this session, attempt
  50. # to authenticate the user.
  51. user = auth.authenticate(remote_user=username)
  52. if user:
  53. # User is valid. Set request.user and persist user in the session
  54. # by logging the user in.
  55. request.user = user
  56. auth.login(request, user)
  57. def clean_username(self, username, request):
  58. """
  59. Allows the backend to clean the username, if the backend defines a
  60. clean_username method.
  61. """
  62. backend_str = request.session[auth.BACKEND_SESSION_KEY]
  63. backend = auth.load_backend(backend_str)
  64. try:
  65. username = backend.clean_username(username)
  66. except AttributeError: # Backend has no clean_username method.
  67. pass
  68. return username