auth-remote-user.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: https://httpd.apache.org/docs/2.2/mod/mod_authnz_ldap.html
  11. .. _CAS: https://www.apereo.org/projects/cas
  12. .. _Cosign: http://weblogin.org
  13. .. _WebAuth: https://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` setting **after** the
  28. :class:`django.contrib.auth.middleware.AuthenticationMiddleware`::
  29. MIDDLEWARE = [
  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. Users with :attr:`is_active=False
  59. <django.contrib.auth.models.User.is_active>` won't be allowed to
  60. authenticate. Use
  61. :class:`~django.contrib.auth.backends.AllowAllUsersRemoteUserBackend` if
  62. you want to allow them to.
  63. .. versionchanged:: 1.10
  64. In older versions, inactive users weren't rejected as described above.
  65. If your authentication mechanism uses a custom HTTP header and not
  66. ``REMOTE_USER``, you can subclass ``RemoteUserMiddleware`` and set the
  67. ``header`` attribute to the desired ``request.META`` key. For example::
  68. from django.contrib.auth.middleware import RemoteUserMiddleware
  69. class CustomHeaderMiddleware(RemoteUserMiddleware):
  70. header = 'HTTP_AUTHUSER'
  71. .. warning::
  72. Be very careful if using a ``RemoteUserMiddleware`` subclass with a custom
  73. HTTP header. You must be sure that your front-end web server always sets or
  74. strips that header based on the appropriate authentication checks, never
  75. permitting an end-user to submit a fake (or "spoofed") header value. Since
  76. the HTTP headers ``X-Auth-User`` and ``X-Auth_User`` (for example) both
  77. normalize to the ``HTTP_X_AUTH_USER`` key in ``request.META``, you must
  78. also check that your web server doesn't allow a spoofed header using
  79. underscores in place of dashes.
  80. This warning doesn't apply to ``RemoteUserMiddleware`` in its default
  81. configuration with ``header = 'REMOTE_USER'``, since a key that doesn't
  82. start with ``HTTP_`` in ``request.META`` can only be set by your WSGI
  83. server, not directly from an HTTP request header.
  84. If you need more control, you can create your own authentication backend
  85. that inherits from :class:`~django.contrib.auth.backends.RemoteUserBackend` and
  86. override one or more of its attributes and methods.
  87. .. _persistent-remote-user-middleware-howto:
  88. Using ``REMOTE_USER`` on login pages only
  89. =========================================
  90. .. versionadded:: 1.9
  91. The ``RemoteUserMiddleware`` authentication middleware assumes that the HTTP
  92. request header ``REMOTE_USER`` is present with all authenticated requests. That
  93. might be expected and practical when Basic HTTP Auth with ``htpasswd`` or other
  94. simple mechanisms are used, but with Negotiate (GSSAPI/Kerberos) or other
  95. resource intensive authentication methods, the authentication in the front-end
  96. HTTP server is usually only set up for one or a few login URLs, and after
  97. successful authentication, the application is supposed to maintain the
  98. authenticated session itself.
  99. :class:`~django.contrib.auth.middleware.PersistentRemoteUserMiddleware`
  100. provides support for this use case. It will maintain the authenticated session
  101. until explicit logout by the user. The class can be used as a drop-in
  102. replacement of :class:`~django.contrib.auth.middleware.RemoteUserMiddleware`
  103. in the documentation above.