~ubuntu-branches/ubuntu/utopic/python-django/utopic

« back to all changes in this revision

Viewing changes to docs/howto/deployment/wsgi/apache-auth.txt

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

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