Browse files

added docstring to basic auth decorator and ability to get passed a c…

…allback function, that is here for example used to login a user after a successful authentication

git-svn-id: 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information...
1 parent 49625dd commit 451205c3b97054393ad22c075140d90bb96114b5 @jezdez jezdez committed Jul 23, 2008
Showing with 29 additions and 3 deletions.
  1. +29 −3 notification/
@@ -3,7 +3,33 @@
from django.contrib.auth import authenticate, login
from django.conf import settings
-def basic_auth_required(test_func=None, realm=None):
+def simple_basic_auth_callback(request, user, *args, **kwargs):
+ """
+ Simple callback to automatically login the given user after a successful
+ basic authentication.
+ """
+ login(request, user)
+ request.user = user
+def basic_auth_required(realm=None, test_func=None, callback_func=None):
+ """
+ This decorator should be used with views that need simple authentication
+ against Django's authentication framework.
+ The ``realm`` string is shown during the basic auth query.
+ It takes a ``test_func`` argument that is used to validate the given
+ credentials and return the decorated function if successful.
+ If unsuccessful the decorator will try to authenticate and checks if the
+ user has the ``is_active`` field set to True.
+ In case of a successful authentication the ``callback_func`` will be
+ called by passing the ``request`` and the ``user`` object. After that the
+ actual view function will be called.
+ If all of the above fails a "Authorization Required" message will be shown.
+ """
if realm is None:
realm = getattr(settings, 'HTTP_AUTHENTICATION_REALM', _('Restricted Access'))
if test_func is None:
@@ -24,8 +50,8 @@ def basic_auth(request, *args, **kwargs):
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
- login(request, user)
- request.user = user
+ if callback_func is not None and callable(callback_func):
+ callback_func(request, user, *args, **kwargs)
return view_func(request, *args, **kwargs)
response = HttpResponse(_('Authorization Required'), mimetype="text/plain")

0 comments on commit 451205c

Please sign in to comment.