auth-remote-user.txt 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. .. warning::
  54. Be very careful if using a ``RemoteUserMiddleware`` subclass with a custom
  55. HTTP header. You must be sure that your front-end web server always sets or
  56. strips that header based on the appropriate authentication checks, never
  57. permitting an end-user to submit a fake (or "spoofed") header value. Since
  58. the HTTP headers ``X-Auth-User`` and ``X-Auth_User`` (for example) both
  59. normalize to the ``HTTP_X_AUTH_USER`` key in ``request.META``, you must
  60. also check that your web server doesn't allow a spoofed header using
  61. underscores in place of dashes.
  62. This warning doesn't apply to ``RemoteUserMiddleware`` in its default
  63. configuration with ``header = 'REMOTE_USER'``, since a key that doesn't
  64. start with ``HTTP_`` in ``request.META`` can only be set by your WSGI
  65. server, not directly from an HTTP request header.
  66. If you need more control, you can create your own authentication backend
  67. that inherits from :class:`~django.contrib.auth.backends.RemoteUserBackend` and
  68. override one or more of its attributes and methods.