auth-remote-user.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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``
  20. or ``PersistentRemoteUserMiddleware``, and
  21. :class:`~django.contrib.auth.backends.RemoteUserBackend` classes found in
  22. :mod:`django.contrib.auth`.
  23. Configuration
  24. =============
  25. First, you must add the
  26. :class:`django.contrib.auth.middleware.RemoteUserMiddleware` to the
  27. :setting:`MIDDLEWARE_CLASSES` setting **after** the
  28. :class:`django.contrib.auth.middleware.AuthenticationMiddleware`::
  29. MIDDLEWARE_CLASSES = [
  30. '...',
  31. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  32. 'django.contrib.auth.middleware.RemoteUserMiddleware',
  33. '...',
  34. ]
  35. Next, you must replace the :class:`~django.contrib.auth.backends.ModelBackend`
  36. with :class:`~django.contrib.auth.backends.RemoteUserBackend` in the
  37. :setting:`AUTHENTICATION_BACKENDS` setting::
  38. AUTHENTICATION_BACKENDS = [
  39. 'django.contrib.auth.backends.RemoteUserBackend',
  40. ]
  41. With this setup, ``RemoteUserMiddleware`` will detect the username in
  42. ``request.META['REMOTE_USER']`` and will authenticate and auto-login that user
  43. using the :class:`~django.contrib.auth.backends.RemoteUserBackend`.
  44. Be aware that this particular setup disables authentication with the default
  45. ``ModelBackend``. This means that if the ``REMOTE_USER`` value is not set
  46. then the user is unable to log in, even using Django's admin interface.
  47. Adding ``'django.contrib.auth.backends.ModelBackend'`` to the
  48. ``AUTHENTICATION_BACKENDS`` list will use ``ModelBackend`` as a fallback
  49. if ``REMOTE_USER`` is absent, which will solve these issues.
  50. Django's user management, such as the views in ``contrib.admin`` and
  51. the :djadmin:`createsuperuser` management command, doesn't integrate with
  52. remote users. These interfaces work with users stored in the database
  53. regardless of ``AUTHENTICATION_BACKENDS``.
  54. .. note::
  55. Since the ``RemoteUserBackend`` inherits from ``ModelBackend``, you will
  56. still have all of the same permissions checking that is implemented in
  57. ``ModelBackend``.
  58. If your authentication mechanism uses a custom HTTP header and not
  59. ``REMOTE_USER``, you can subclass ``RemoteUserMiddleware`` and set the
  60. ``header`` attribute to the desired ``request.META`` key. For example::
  61. from django.contrib.auth.middleware import RemoteUserMiddleware
  62. class CustomHeaderMiddleware(RemoteUserMiddleware):
  63. header = 'HTTP_AUTHUSER'
  64. .. warning::
  65. Be very careful if using a ``RemoteUserMiddleware`` subclass with a custom
  66. HTTP header. You must be sure that your front-end web server always sets or
  67. strips that header based on the appropriate authentication checks, never
  68. permitting an end-user to submit a fake (or "spoofed") header value. Since
  69. the HTTP headers ``X-Auth-User`` and ``X-Auth_User`` (for example) both
  70. normalize to the ``HTTP_X_AUTH_USER`` key in ``request.META``, you must
  71. also check that your web server doesn't allow a spoofed header using
  72. underscores in place of dashes.
  73. This warning doesn't apply to ``RemoteUserMiddleware`` in its default
  74. configuration with ``header = 'REMOTE_USER'``, since a key that doesn't
  75. start with ``HTTP_`` in ``request.META`` can only be set by your WSGI
  76. server, not directly from an HTTP request header.
  77. If you need more control, you can create your own authentication backend
  78. that inherits from :class:`~django.contrib.auth.backends.RemoteUserBackend` and
  79. override one or more of its attributes and methods.
  80. .. _persistent-remote-user-middleware-howto:
  81. Using ``REMOTE_USER`` on login pages only
  82. =========================================
  83. .. versionadded:: 1.9
  84. The ``RemoteUserMiddleware`` authentication middleware assumes that the HTTP
  85. request header ``REMOTE_USER`` is present with all authenticated requests. That
  86. might be expected and practical when Basic HTTP Auth with ``htpasswd`` or other
  87. simple mechanisms are used, but with Negotiate (GSSAPI/Kerberos) or other
  88. resource intensive authentication methods, the authentication in the front-end
  89. HTTP server is usually only set up for one or a few login URLs, and after
  90. successful authentication, the application is supposed to maintain the
  91. authenticated session itself.
  92. :class:`~django.contrib.auth.middleware.PersistentRemoteUserMiddleware`
  93. provides support for this use case. It will maintain the authenticated session
  94. until explicit logout by the user. The class can be used as a drop-in
  95. replacement of :class:`~django.contrib.auth.middleware.RemoteUserMiddleware`
  96. in the documentation above.