2021-07-29 17:11:42 +00:00
|
|
|
==============================================================
|
|
|
|
How to authenticate against Django's user database from Apache
|
|
|
|
==============================================================
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
Since keeping multiple authentication databases in sync is a common problem when
|
|
|
|
dealing with Apache, you can configure Apache to authenticate against Django's
|
2012-12-28 19:00:11 +00:00
|
|
|
:doc:`authentication system </topics/auth/index>` directly. This requires Apache
|
2012-09-24 05:48:13 +00:00
|
|
|
version >= 2.2 and mod_wsgi >= 2.0. For example, you could:
|
|
|
|
|
|
|
|
* Serve static/media files directly from Apache only to authenticated users.
|
|
|
|
|
|
|
|
* Authenticate access to a Subversion_ repository against Django users with
|
|
|
|
a certain permission.
|
|
|
|
|
|
|
|
* Allow certain users to connect to a WebDAV share created with mod_dav_.
|
|
|
|
|
2012-10-02 11:19:44 +00:00
|
|
|
.. note::
|
2016-11-23 20:03:33 +00:00
|
|
|
If you have installed a :ref:`custom user model <auth-custom-user>` and
|
2013-08-15 11:14:10 +00:00
|
|
|
want to use this default auth handler, it must support an ``is_active``
|
2012-10-02 11:19:44 +00:00
|
|
|
attribute. If you want to use group based authorization, your custom user
|
|
|
|
must have a relation named 'groups', referring to a related object that has
|
|
|
|
a 'name' field. You can also specify your own custom mod_wsgi
|
|
|
|
auth handler if your custom cannot conform to these requirements.
|
|
|
|
|
2018-01-07 13:28:41 +00:00
|
|
|
.. _Subversion: https://subversion.apache.org/
|
2015-11-29 16:29:46 +00:00
|
|
|
.. _mod_dav: https://httpd.apache.org/docs/2.2/mod/mod_dav.html
|
2012-09-24 05:48:13 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
Authentication with ``mod_wsgi``
|
|
|
|
================================
|
2012-09-24 05:48:13 +00:00
|
|
|
|
2014-09-09 13:32:19 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The use of ``WSGIApplicationGroup %{GLOBAL}`` in the configurations below
|
|
|
|
presumes that your Apache instance is running only one Django application.
|
2015-04-13 10:16:21 +00:00
|
|
|
If you are running more than one Django application, please refer to the
|
2014-09-09 13:32:19 +00:00
|
|
|
`Defining Application Groups`_ section of the mod_wsgi docs for more
|
|
|
|
information about this setting.
|
|
|
|
|
2012-09-24 05:48:13 +00:00
|
|
|
Make sure that mod_wsgi is installed and activated and that you have
|
2021-08-15 19:11:25 +00:00
|
|
|
followed the steps to set up :doc:`Apache with mod_wsgi
|
2014-09-09 13:32:19 +00:00
|
|
|
</howto/deployment/wsgi/modwsgi>`.
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
Next, edit your Apache configuration to add a location that you want
|
|
|
|
only authenticated users to be able to view:
|
|
|
|
|
|
|
|
.. code-block:: apache
|
|
|
|
|
2012-09-30 04:46:32 +00:00
|
|
|
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
|
2013-12-25 11:06:25 +00:00
|
|
|
WSGIPythonPath /path/to/mysite.com
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
WSGIProcessGroup %{GLOBAL}
|
2014-09-09 13:32:19 +00:00
|
|
|
WSGIApplicationGroup %{GLOBAL}
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
<Location "/secret">
|
|
|
|
AuthType Basic
|
|
|
|
AuthName "Top Secret"
|
|
|
|
Require valid-user
|
|
|
|
AuthBasicProvider wsgi
|
2012-09-30 04:46:32 +00:00
|
|
|
WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
|
2012-09-24 05:48:13 +00:00
|
|
|
</Location>
|
|
|
|
|
|
|
|
The ``WSGIAuthUserScript`` directive tells mod_wsgi to execute the
|
|
|
|
``check_password`` function in specified wsgi script, passing the user name and
|
|
|
|
password that it receives from the prompt. In this example, the
|
|
|
|
``WSGIAuthUserScript`` is the same as the ``WSGIScriptAlias`` that defines your
|
2014-07-26 11:21:52 +00:00
|
|
|
application :doc:`that is created by django-admin startproject
|
2012-09-24 05:48:13 +00:00
|
|
|
</howto/deployment/wsgi/index>`.
|
|
|
|
|
|
|
|
.. admonition:: Using Apache 2.2 with authentication
|
|
|
|
|
|
|
|
Make sure that ``mod_auth_basic`` and ``mod_authz_user`` are loaded.
|
|
|
|
|
|
|
|
These might be compiled statically into Apache, or you might need to use
|
|
|
|
LoadModule to load them dynamically in your ``httpd.conf``:
|
|
|
|
|
|
|
|
.. code-block:: apache
|
|
|
|
|
|
|
|
LoadModule auth_basic_module modules/mod_auth_basic.so
|
|
|
|
LoadModule authz_user_module modules/mod_authz_user.so
|
|
|
|
|
2013-03-11 21:48:03 +00:00
|
|
|
Finally, edit your WSGI script ``mysite.wsgi`` to tie Apache's authentication
|
|
|
|
to your site's authentication mechanisms by importing the ``check_password``
|
2014-08-18 14:30:44 +00:00
|
|
|
function::
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
|
2012-09-24 05:48:13 +00:00
|
|
|
|
2012-09-30 04:46:32 +00:00
|
|
|
from django.contrib.auth.handlers.modwsgi import check_password
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
from django.core.handlers.wsgi import WSGIHandler
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2012-09-24 05:48:13 +00:00
|
|
|
application = WSGIHandler()
|
|
|
|
|
|
|
|
|
|
|
|
Requests beginning with ``/secret/`` will now require a user to authenticate.
|
|
|
|
|
|
|
|
The mod_wsgi `access control mechanisms documentation`_ provides additional
|
|
|
|
details and information about alternative methods of authentication.
|
|
|
|
|
2016-04-28 14:09:57 +00:00
|
|
|
.. _Defining Application Groups: https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups
|
|
|
|
.. _access control mechanisms documentation: https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html
|
2012-09-24 05:48:13 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
Authorization with ``mod_wsgi`` and Django groups
|
|
|
|
-------------------------------------------------
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
mod_wsgi also provides functionality to restrict a particular location to
|
|
|
|
members of a group.
|
|
|
|
|
|
|
|
In this case, the Apache configuration should look like this:
|
|
|
|
|
|
|
|
.. code-block:: apache
|
|
|
|
|
2012-09-30 04:46:32 +00:00
|
|
|
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
WSGIProcessGroup %{GLOBAL}
|
2014-09-09 13:32:19 +00:00
|
|
|
WSGIApplicationGroup %{GLOBAL}
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
<Location "/secret">
|
|
|
|
AuthType Basic
|
|
|
|
AuthName "Top Secret"
|
|
|
|
AuthBasicProvider wsgi
|
2012-09-30 04:46:32 +00:00
|
|
|
WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
|
|
|
|
WSGIAuthGroupScript /path/to/mysite.com/mysite/wsgi.py
|
2012-09-24 05:48:13 +00:00
|
|
|
Require group secret-agents
|
|
|
|
Require valid-user
|
|
|
|
</Location>
|
|
|
|
|
|
|
|
To support the ``WSGIAuthGroupScript`` directive, the same WSGI script
|
|
|
|
``mysite.wsgi`` must also import the ``groups_for_user`` function which
|
|
|
|
returns a list groups the given user belongs to.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2012-09-30 04:46:32 +00:00
|
|
|
from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
|
2012-09-24 05:48:13 +00:00
|
|
|
|
|
|
|
Requests for ``/secret/`` will now also require user to be a member of the
|
|
|
|
"secret-agents" group.
|