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