apache-auth.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .. _howto-apache-auth:
  2. =========================================================
  3. Authenticating against Django's user database from Apache
  4. =========================================================
  5. Since keeping multiple authentication databases in sync is a common problem when
  6. dealing with Apache, you can configuring Apache to authenticate against Django's
  7. :ref:`authentication system <topics-auth>` directly. For example, you
  8. could:
  9. * Serve static/media files directly from Apache only to authenticated users.
  10. * Authenticate access to a Subversion_ repository against Django users with
  11. a certain permission.
  12. * Allow certain users to connect to a WebDAV share created with mod_dav_.
  13. .. _Subversion: http://subversion.tigris.org/
  14. .. _mod_dav: http://httpd.apache.org/docs/2.0/mod/mod_dav.html
  15. Configuring Apache
  16. ==================
  17. To check against Django's authorization database from a Apache configuration
  18. file, you'll need to use mod_python's ``PythonAuthenHandler`` directive along
  19. with the standard ``Auth*`` and ``Require`` directives:
  20. .. code-block:: apache
  21. <Location /example/>
  22. AuthType Basic
  23. AuthName "example.com"
  24. Require valid-user
  25. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  26. PythonAuthenHandler django.contrib.auth.handlers.modpython
  27. </Location>
  28. .. admonition:: Using the authentication handler with Apache 2.2
  29. If you're using Apache 2.2, you'll need to take a couple extra steps.
  30. You'll need to ensure that ``mod_auth_basic`` and ``mod_authz_user``
  31. are loaded. These might be compiled statically into Apache, or you might
  32. need to use ``LoadModule`` to load them dynamically (as shown in the
  33. example at the bottom of this note).
  34. You'll also need to insert configuration directives that prevent Apache
  35. from trying to use other authentication modules, as well as specifying
  36. the ``AuthUserFile`` directive and pointing it to ``/dev/null``. Depending
  37. on which other authentication modules you have loaded, you might need one
  38. or more of the following directives::
  39. .. code-block:: apache
  40. AuthBasicAuthoritative Off
  41. AuthDefaultAuthoritative Off
  42. AuthzLDAPAuthoritative Off
  43. AuthzDBMAuthoritative Off
  44. AuthzDefaultAuthoritative Off
  45. AuthzGroupFileAuthoritative Off
  46. AuthzOwnerAuthoritative Off
  47. AuthzUserAuthoritative Off
  48. A complete configuration, with differences between Apache 2.0 and
  49. Apache 2.2 marked in bold, would look something like:
  50. .. parsed-literal::
  51. **LoadModule auth_basic_module modules/mod_auth_basic.so**
  52. **LoadModule authz_user_module modules/mod_authz_user.so**
  53. ...
  54. <Location /example/>
  55. AuthType Basic
  56. AuthName "example.com"
  57. **AuthUserFile /dev/null**
  58. **AuthBasicAuthoritative Off**
  59. Require valid-user
  60. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  61. PythonAuthenHandler django.contrib.auth.handlers.modpython
  62. </Location>
  63. By default, the authentication handler will limit access to the ``/example/``
  64. location to users marked as staff members. You can use a set of
  65. ``PythonOption`` directives to modify this behavior:
  66. ================================ =========================================
  67. ``PythonOption`` Explanation
  68. ================================ =========================================
  69. ``DjangoRequireStaffStatus`` If set to ``on`` only "staff" users (i.e.
  70. those with the ``is_staff`` flag set)
  71. will be allowed.
  72. Defaults to ``on``.
  73. ``DjangoRequireSuperuserStatus`` If set to ``on`` only superusers (i.e.
  74. those with the ``is_superuser`` flag set)
  75. will be allowed.
  76. Defaults to ``off``.
  77. ``DjangoPermissionName`` The name of a permission to require for
  78. access. See :ref:`custom permissions
  79. <custom-permissions>` for more
  80. information.
  81. By default no specific permission will be
  82. required.
  83. ================================ =========================================
  84. Note that sometimes ``SetEnv`` doesn't play well in this mod_python
  85. configuration, for reasons unknown. If you're having problems getting
  86. mod_python to recognize your ``DJANGO_SETTINGS_MODULE``, you can set it using
  87. ``PythonOption`` instead of ``SetEnv``. Therefore, these two Apache directives
  88. are equivalent::
  89. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  90. PythonOption DJANGO_SETTINGS_MODULE mysite.settings