apache-auth.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. =========================================================
  2. Authenticating against Django's user database from Apache
  3. =========================================================
  4. Since keeping multiple authentication databases in sync is a common problem when
  5. dealing with Apache, you can configure Apache to authenticate against Django's
  6. :doc:`authentication system </topics/auth/index>` directly. This requires Apache
  7. version >= 2.2 and mod_wsgi >= 2.0. For example, you could:
  8. * Serve static/media files directly from Apache only to authenticated users.
  9. * Authenticate access to a Subversion_ repository against Django users with
  10. a certain permission.
  11. * Allow certain users to connect to a WebDAV share created with mod_dav_.
  12. .. note::
  13. If you have installed a :ref:`custom User model <auth-custom-user>` and
  14. want to use this default auth handler, it must support an ``is_active``
  15. attribute. If you want to use group based authorization, your custom user
  16. must have a relation named 'groups', referring to a related object that has
  17. a 'name' field. You can also specify your own custom mod_wsgi
  18. auth handler if your custom cannot conform to these requirements.
  19. .. _Subversion: http://subversion.tigris.org/
  20. .. _mod_dav: http://httpd.apache.org/docs/2.2/mod/mod_dav.html
  21. Authentication with mod_wsgi
  22. ============================
  23. Make sure that mod_wsgi is installed and activated and that you have
  24. followed the steps to setup
  25. :doc:`Apache with mod_wsgi </howto/deployment/wsgi/modwsgi>`
  26. Next, edit your Apache configuration to add a location that you want
  27. only authenticated users to be able to view:
  28. .. code-block:: apache
  29. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  30. WSGIPythonPath /path/to/mysite.com
  31. WSGIProcessGroup %{GLOBAL}
  32. WSGIApplicationGroup django
  33. <Location "/secret">
  34. AuthType Basic
  35. AuthName "Top Secret"
  36. Require valid-user
  37. AuthBasicProvider wsgi
  38. WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
  39. </Location>
  40. The ``WSGIAuthUserScript`` directive tells mod_wsgi to execute the
  41. ``check_password`` function in specified wsgi script, passing the user name and
  42. password that it receives from the prompt. In this example, the
  43. ``WSGIAuthUserScript`` is the same as the ``WSGIScriptAlias`` that defines your
  44. application :doc:`that is created by django-admin.py startproject
  45. </howto/deployment/wsgi/index>`.
  46. .. admonition:: Using Apache 2.2 with authentication
  47. Make sure that ``mod_auth_basic`` and ``mod_authz_user`` are loaded.
  48. These might be compiled statically into Apache, or you might need to use
  49. LoadModule to load them dynamically in your ``httpd.conf``:
  50. .. code-block:: apache
  51. LoadModule auth_basic_module modules/mod_auth_basic.so
  52. LoadModule authz_user_module modules/mod_authz_user.so
  53. Finally, edit your WSGI script ``mysite.wsgi`` to tie Apache's authentication
  54. to your site's authentication mechanisms by importing the ``check_password``
  55. function:
  56. .. code-block:: python
  57. import os
  58. os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
  59. from django.contrib.auth.handlers.modwsgi import check_password
  60. from django.core.handlers.wsgi import WSGIHandler
  61. application = WSGIHandler()
  62. Requests beginning with ``/secret/`` will now require a user to authenticate.
  63. The mod_wsgi `access control mechanisms documentation`_ provides additional
  64. details and information about alternative methods of authentication.
  65. .. _access control mechanisms documentation: http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms
  66. Authorization with mod_wsgi and Django groups
  67. ---------------------------------------------
  68. mod_wsgi also provides functionality to restrict a particular location to
  69. members of a group.
  70. In this case, the Apache configuration should look like this:
  71. .. code-block:: apache
  72. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  73. WSGIProcessGroup %{GLOBAL}
  74. WSGIApplicationGroup django
  75. <Location "/secret">
  76. AuthType Basic
  77. AuthName "Top Secret"
  78. AuthBasicProvider wsgi
  79. WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
  80. WSGIAuthGroupScript /path/to/mysite.com/mysite/wsgi.py
  81. Require group secret-agents
  82. Require valid-user
  83. </Location>
  84. To support the ``WSGIAuthGroupScript`` directive, the same WSGI script
  85. ``mysite.wsgi`` must also import the ``groups_for_user`` function which
  86. returns a list groups the given user belongs to.
  87. .. code-block:: python
  88. from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
  89. Requests for ``/secret/`` will now also require user to be a member of the
  90. "secret-agents" group.