auth-remote-user.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ====================================
  2. Authentication using ``REMOTE_USER``
  3. ====================================
  4. This document describes how to make use of external authentication sources
  5. (where the Web server sets the ``REMOTE_USER`` environment variable) in your
  6. Django applications. This type of authentication solution is typically seen on
  7. intranet sites, with single sign-on solutions such as IIS and Integrated
  8. Windows Authentication or Apache and `mod_authnz_ldap`_, `CAS`_, `Cosign`_,
  9. `WebAuth`_, `mod_auth_sspi`_, etc.
  10. .. _mod_authnz_ldap: http://httpd.apache.org/docs/2.2/mod/mod_authnz_ldap.html
  11. .. _CAS: https://www.apereo.org/cas
  12. .. _Cosign: http://weblogin.org
  13. .. _WebAuth: http://www.stanford.edu/services/webauth/
  14. .. _mod_auth_sspi: http://sourceforge.net/projects/mod-auth-sspi
  15. When the Web server takes care of authentication it typically sets the
  16. ``REMOTE_USER`` environment variable for use in the underlying application. In
  17. Django, ``REMOTE_USER`` is made available in the :attr:`request.META
  18. <django.http.HttpRequest.META>` attribute. Django can be configured to make
  19. use of the ``REMOTE_USER`` value using the ``RemoteUserMiddleware`` and
  20. :class:`~django.contrib.auth.backends.RemoteUserBackend` classes found in
  21. :mod:`django.contrib.auth`.
  22. Configuration
  23. =============
  24. First, you must add the
  25. :class:`django.contrib.auth.middleware.RemoteUserMiddleware` to the
  26. :setting:`MIDDLEWARE_CLASSES` setting **after** the
  27. :class:`django.contrib.auth.middleware.AuthenticationMiddleware`::
  28. MIDDLEWARE_CLASSES = (
  29. '...',
  30. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  31. 'django.contrib.auth.middleware.RemoteUserMiddleware',
  32. '...',
  33. )
  34. Next, you must replace the :class:`~django.contrib.auth.backends.ModelBackend`
  35. with :class:`~django.contrib.auth.backends.RemoteUserBackend` in the
  36. :setting:`AUTHENTICATION_BACKENDS` setting::
  37. AUTHENTICATION_BACKENDS = (
  38. 'django.contrib.auth.backends.RemoteUserBackend',
  39. )
  40. With this setup, ``RemoteUserMiddleware`` will detect the username in
  41. ``request.META['REMOTE_USER']`` and will authenticate and auto-login that user
  42. using the :class:`~django.contrib.auth.backends.RemoteUserBackend`.
  43. .. note::
  44. Since the ``RemoteUserBackend`` inherits from ``ModelBackend``, you will
  45. still have all of the same permissions checking that is implemented in
  46. ``ModelBackend``.
  47. If your authentication mechanism uses a custom HTTP header and not
  48. ``REMOTE_USER``, you can subclass ``RemoteUserMiddleware`` and set the
  49. ``header`` attribute to the desired ``request.META`` key. For example::
  50. from django.contrib.auth.middleware import RemoteUserMiddleware
  51. class CustomHeaderMiddleware(RemoteUserMiddleware):
  52. header = 'HTTP_AUTHUSER'
  53. If you need more control, you can create your own authentication backend
  54. that inherits from :class:`~django.contrib.auth.backends.RemoteUserBackend` and
  55. override one or more of its attributes and methods.