apache-auth.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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>` 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. .. _Subversion: http://subversion.tigris.org/
  13. .. _mod_dav: http://httpd.apache.org/docs/2.2/mod/mod_dav.html
  14. Authentication with mod_wsgi
  15. ============================
  16. Make sure that mod_wsgi is installed and activated and that you have
  17. followed the steps to setup
  18. :doc:`Apache with mod_wsgi </howto/deployment/wsgi/modwsgi>`
  19. Next, edit your Apache configuration to add a location that you want
  20. only authenticated users to be able to view:
  21. .. code-block:: apache
  22. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  23. WSGIProcessGroup %{GLOBAL}
  24. WSGIApplicationGroup django
  25. <Location "/secret">
  26. AuthType Basic
  27. AuthName "Top Secret"
  28. Require valid-user
  29. AuthBasicProvider wsgi
  30. WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
  31. </Location>
  32. The ``WSGIAuthUserScript`` directive tells mod_wsgi to execute the
  33. ``check_password`` function in specified wsgi script, passing the user name and
  34. password that it receives from the prompt. In this example, the
  35. ``WSGIAuthUserScript`` is the same as the ``WSGIScriptAlias`` that defines your
  36. application :doc:`that is created by django-admin.py startproject
  37. </howto/deployment/wsgi/index>`.
  38. .. admonition:: Using Apache 2.2 with authentication
  39. Make sure that ``mod_auth_basic`` and ``mod_authz_user`` are loaded.
  40. These might be compiled statically into Apache, or you might need to use
  41. LoadModule to load them dynamically in your ``httpd.conf``:
  42. .. code-block:: apache
  43. LoadModule auth_basic_module modules/mod_auth_basic.so
  44. LoadModule authz_user_module modules/mod_authz_user.so
  45. Finally, edit your WSGI script ``mysite.wsgi`` to tie Apache's
  46. authentication to your site's authentication mechanisms by importing the
  47. check_user function:
  48. .. code-block:: python
  49. import os
  50. import sys
  51. os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
  52. from django.contrib.auth.handlers.modwsgi import check_password
  53. from django.core.handlers.wsgi import WSGIHandler
  54. application = WSGIHandler()
  55. Requests beginning with ``/secret/`` will now require a user to authenticate.
  56. The mod_wsgi `access control mechanisms documentation`_ provides additional
  57. details and information about alternative methods of authentication.
  58. .. _access control mechanisms documentation: http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms
  59. Authorization with mod_wsgi and Django groups
  60. ---------------------------------------------
  61. mod_wsgi also provides functionality to restrict a particular location to
  62. members of a group.
  63. In this case, the Apache configuration should look like this:
  64. .. code-block:: apache
  65. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  66. WSGIProcessGroup %{GLOBAL}
  67. WSGIApplicationGroup django
  68. <Location "/secret">
  69. AuthType Basic
  70. AuthName "Top Secret"
  71. AuthBasicProvider wsgi
  72. WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
  73. WSGIAuthGroupScript /path/to/mysite.com/mysite/wsgi.py
  74. Require group secret-agents
  75. Require valid-user
  76. </Location>
  77. To support the ``WSGIAuthGroupScript`` directive, the same WSGI script
  78. ``mysite.wsgi`` must also import the ``groups_for_user`` function which
  79. returns a list groups the given user belongs to.
  80. .. code-block:: python
  81. from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
  82. Requests for ``/secret/`` will now also require user to be a member of the
  83. "secret-agents" group.