auth-remote-user.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. =========================================
  2. How to authenticate 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://uit.stanford.edu/service/authentication
  14. .. _mod_auth_sspi: https://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. If your authentication mechanism uses a custom HTTP header and not
  64. ``REMOTE_USER``, you can subclass ``RemoteUserMiddleware`` and set the
  65. ``header`` attribute to the desired ``request.META`` key. For example::
  66. from django.contrib.auth.middleware import RemoteUserMiddleware
  67. class CustomHeaderMiddleware(RemoteUserMiddleware):
  68. header = "HTTP_AUTHUSER"
  69. .. warning::
  70. Be very careful if using a ``RemoteUserMiddleware`` subclass with a custom
  71. HTTP header. You must be sure that your front-end web server always sets or
  72. strips that header based on the appropriate authentication checks, never
  73. permitting an end-user to submit a fake (or "spoofed") header value. Since
  74. the HTTP headers ``X-Auth-User`` and ``X-Auth_User`` (for example) both
  75. normalize to the ``HTTP_X_AUTH_USER`` key in ``request.META``, you must
  76. also check that your web server doesn't allow a spoofed header using
  77. underscores in place of dashes.
  78. This warning doesn't apply to ``RemoteUserMiddleware`` in its default
  79. configuration with ``header = 'REMOTE_USER'``, since a key that doesn't
  80. start with ``HTTP_`` in ``request.META`` can only be set by your WSGI
  81. server, not directly from an HTTP request header.
  82. If you need more control, you can create your own authentication backend
  83. that inherits from :class:`~django.contrib.auth.backends.RemoteUserBackend` and
  84. override one or more of its attributes and methods.
  85. .. _persistent-remote-user-middleware-howto:
  86. Using ``REMOTE_USER`` on login pages only
  87. =========================================
  88. The ``RemoteUserMiddleware`` authentication middleware assumes that the HTTP
  89. request header ``REMOTE_USER`` is present with all authenticated requests. That
  90. might be expected and practical when Basic HTTP Auth with ``htpasswd`` or
  91. similar mechanisms are used, but with Negotiate (GSSAPI/Kerberos) or other
  92. resource intensive authentication methods, the authentication in the front-end
  93. HTTP server is usually only set up for one or a few login URLs, and after
  94. successful authentication, the application is supposed to maintain the
  95. authenticated session itself.
  96. :class:`~django.contrib.auth.middleware.PersistentRemoteUserMiddleware`
  97. provides support for this use case. It will maintain the authenticated session
  98. until explicit logout by the user. The class can be used as a drop-in
  99. replacement of :class:`~django.contrib.auth.middleware.RemoteUserMiddleware`
  100. in the documentation above.