auth-remote-user.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ====================================
  2. Authentication using ``REMOTE_USER``
  3. ====================================
  4. .. currentmodule:: django.contrib.auth.backends
  5. This document describes how to make use of external authentication sources
  6. (where the Web server sets the ``REMOTE_USER`` environment variable) in your
  7. Django applications. This type of authentication solution is typically seen on
  8. intranet sites, with single sign-on solutions such as IIS and Integrated
  9. Windows Authentication or Apache and `mod_authnz_ldap`_, `CAS`_, `Cosign`_,
  10. `WebAuth`_, `mod_auth_sspi`_, etc.
  11. .. _mod_authnz_ldap: http://httpd.apache.org/docs/2.2/mod/mod_authnz_ldap.html
  12. .. _CAS: http://www.jasig.org/cas
  13. .. _Cosign: http://weblogin.org
  14. .. _WebAuth: http://www.stanford.edu/services/webauth/
  15. .. _mod_auth_sspi: http://sourceforge.net/projects/mod-auth-sspi
  16. When the Web server takes care of authentication it typically sets the
  17. ``REMOTE_USER`` environment variable for use in the underlying application. In
  18. Django, ``REMOTE_USER`` is made available in the :attr:`request.META
  19. <django.http.HttpRequest.META>` attribute. Django can be configured to make
  20. use of the ``REMOTE_USER`` value using the ``RemoteUserMiddleware`` and
  21. ``RemoteUserBackend`` classes found in :mod:`django.contrib.auth`.
  22. Configuration
  23. =============
  24. .. class:: django.contrib.auth.middleware.RemoteUserMiddleware
  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 ``RemoteUserBackend`` in the :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 ``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. ``RemoteUserBackend``
  54. =====================
  55. .. class:: django.contrib.auth.backends.RemoteUserBackend
  56. If you need more control, you can create your own authentication backend
  57. that inherits from ``RemoteUserBackend`` and overrides certain parts:
  58. Attributes
  59. ~~~~~~~~~~
  60. .. attribute:: RemoteUserBackend.create_unknown_user
  61. ``True`` or ``False``. Determines whether or not a
  62. :class:`~django.contrib.auth.models.User` object is created if not already
  63. in the database. Defaults to ``True``.
  64. Methods
  65. ~~~~~~~
  66. .. method:: RemoteUserBackend.clean_username(username)
  67. Performs any cleaning on the ``username`` (e.g. stripping LDAP DN
  68. information) prior to using it to get or create a
  69. :class:`~django.contrib.auth.models.User` object. Returns the cleaned
  70. username.
  71. .. method:: RemoteUserBackend.configure_user(user)
  72. Configures a newly created user. This method is called immediately after a
  73. new user is created, and can be used to perform custom setup actions, such
  74. as setting the user's groups based on attributes in an LDAP directory.
  75. Returns the user object.