Skip to content

Commit 3cb20c4

Browse files
Moved Apache auth handler to django/contrib/auth/handlers/modpython.py
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1500 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 5066fe5 commit 3cb20c4

File tree

4 files changed

+57
-56
lines changed

4 files changed

+57
-56
lines changed

django/contrib/auth/handlers/__init__.py

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from mod_python import apache
2+
import os
3+
4+
def authenhandler(req, **kwargs):
5+
"""
6+
Authentication handler that checks against Django's auth database.
7+
"""
8+
9+
# mod_python fakes the environ, and thus doesn't process SetEnv. This fixes
10+
# that so that the following import works
11+
os.environ.update(req.subprocess_env)
12+
13+
from django.models.auth import users
14+
15+
# check for PythonOptions
16+
_str_to_bool = lambda s: s.lower() in '1', 'true', 'on', 'yes'
17+
18+
options = req.get_options()
19+
permission_name = options.get('DjangoPermissionName', None)
20+
staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on"))
21+
superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off"))
22+
23+
# check that the username is valid
24+
kwargs = {'username__exact': req.user, 'is_active__exact': True}
25+
if staff_only:
26+
kwargs['is_staff__exact'] = True
27+
if superuser_only:
28+
kwargs['is_superuser__exact'] = True
29+
try:
30+
user = users.get_object(**kwargs)
31+
except users.UserDoesNotExist:
32+
return apache.HTTP_UNAUTHORIZED
33+
34+
# check the password and any permission given
35+
if user.check_password(req.get_basic_auth_pw()):
36+
if permission_name:
37+
if user.has_perm(permission_name):
38+
return apache.OK
39+
else:
40+
return apache.HTTP_UNAUTHORIZED
41+
else:
42+
return apache.OK
43+
else:
44+
return apache.HTTP_UNAUTHORIZED

django/core/handlers/modpython.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -163,46 +163,3 @@ def populate_apache_request(http_response, mod_python_req):
163163
def handler(req):
164164
# mod_python hooks into this function.
165165
return ModPythonHandler()(req)
166-
167-
def authenhandler(req, **kwargs):
168-
"""
169-
Authentication handler that checks against Django's auth database.
170-
"""
171-
from mod_python import apache
172-
173-
# mod_python fakes the environ, and thus doesn't process SetEnv. This fixes
174-
# that so that the following import works
175-
os.environ.update(req.subprocess_env)
176-
from django.models.auth import users
177-
178-
# check for PythonOptions
179-
_str_to_bool = lambda s: s.lower() in '1', 'true', 'on', 'yes'
180-
181-
options = req.get_options()
182-
permission_name = options.get('DjangoPermissionName', None)
183-
staff_only = _str_to_bool(options.get('DjangoRequireStaffStatus', "on"))
184-
superuser_only = _str_to_bool(options.get('DjangoRequireSuperuserStatus', "off"))
185-
186-
# check that the username is valid
187-
kwargs = {'username__exact': req.user, 'is_active__exact': True}
188-
if staff_only:
189-
kwargs['is_staff__exact'] = True
190-
if superuser_only:
191-
kwargs['is_superuser__exact'] = True
192-
try:
193-
user = users.get_object(**kwargs)
194-
except users.UserDoesNotExist:
195-
return apache.HTTP_UNAUTHORIZED
196-
197-
# check the password and any permission given
198-
if user.check_password(req.get_basic_auth_pw()):
199-
if permission_name:
200-
if user.has_perm(permission_name):
201-
return apache.OK
202-
else:
203-
return apache.HTTP_UNAUTHORIZED
204-
else:
205-
return apache.OK
206-
else:
207-
return apache.HTTP_UNAUTHORIZED
208-

docs/apache_auth.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ dealing with Apache, you can configuring Apache to authenticate against Django's
77
`authentication system`_ directly. For example, you could:
88

99
* Serve media files directly from Apache only to authenticated users.
10-
10+
1111
* Authenticate access to a Subversion_ repository against Django users with
1212
a certain permission.
13-
13+
1414
* Allow certain users to connect to a WebDAV share created with mod_dav_.
15-
15+
1616
Configuring Apache
1717
==================
1818

@@ -24,9 +24,9 @@ with the standard ``Auth*`` and ``Require`` directives::
2424
AuthType basic
2525
AuthName "example.com"
2626
Require valid-user
27-
27+
2828
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
29-
PythonAuthenHandler django.core.handlers.modpython
29+
PythonAuthenHandler django.contrib.auth.handlers.modpython
3030
</Location>
3131

3232
By default, the authentication handler will limit access to the ``/example/``
@@ -37,26 +37,26 @@ location to users marked as staff members. You can use a set of
3737
``PythonOption`` Explanation
3838
================================ =========================================
3939
``DjangoRequireStaffStatus`` If set to ``on`` only "staff" users (i.e.
40-
those with the ``is_staff`` flag set)
40+
those with the ``is_staff`` flag set)
4141
will be allowed.
42-
42+
4343
Defaults to ``on``.
4444

4545
``DjangoRequireSuperuserStatus`` If set to ``on`` only superusers (i.e.
4646
those with the ``is_superuser`` flag set)
4747
will be allowed.
48-
48+
4949
Defaults to ``off``.
50-
50+
5151
``DjangoPermissionName`` The name of a permission to require for
52-
access. See `custom permissions`_ for
52+
access. See `custom permissions`_ for
5353
more information.
54-
54+
5555
By default no specific permission will be
5656
required.
5757
================================ =========================================
58-
58+
5959
.. _authentication system: http://www.djangoproject.com/documentation/authentication/
6060
.. _Subversion: http://subversion.tigris.org/
6161
.. _mod_dav: http://httpd.apache.org/docs/2.0/mod/mod_dav.html
62-
.. _custom permissions: http://www.djangoproject.com/documentation/authentication/#custom-permissions
62+
.. _custom permissions: http://www.djangoproject.com/documentation/authentication/#custom-permissions

0 commit comments

Comments
 (0)