Skip to content
Browse files

added decorator 'basic_auth_required' to be used with whatever privat…

…e thing we want to use it with

git-svn-id: https://django-notification.googlecode.com/svn/trunk@82 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information...
1 parent b4508fa commit a154cab166a4bb5da151ffba826656c4c6eb042a @jezdez jezdez committed Jul 13, 2008
Showing with 38 additions and 0 deletions.
  1. +38 −0 notification/decorators.py
View
38 notification/decorators.py
@@ -0,0 +1,38 @@
+from django.utils.translation import ugettext as _
+from django.http import HttpResponse
+from django.contrib.auth import authenticate, login
+from django.conf import settings
+
+try:
+ from functools import wraps
+except ImportError:
+ from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
+
+def basic_auth_required(view_func):
+ def basic_auth(request, test_func=None, realm=None, *args, **kwargs):
+ if realm is None:
+ realm = getattr(settings, 'HTTP_AUTHENTICATION_REALM', _('Restricted Access'))
+ if test_func is None:
+ test_func = lambda u: u.is_authenticated()
+ # Just return the original view because already logged in
+ if test_func(request.user):
+ return view_func(request, *args, **kwargs)
+
+ # Not logged in, look if login credentials are provided
+ if 'HTTP_AUTHORIZATION' in request.META:
+ auth_method, auth = request.META['HTTP_AUTHORIZATION'].split(' ',1)
+ if 'basic' == auth_method.lower():
+ auth = auth.strip().decode('base64')
+ username, password = auth.split(':',1)
+ user = authenticate(username=username, password=password)
+ if user is not None:
+ if user.is_active:
+ login(request, user)
+ request.user = user
+ return view_func(request, *args, **kwargs)
+
+ response = HttpResponse(_('Authorization Required'), mimetype="text/plain")
+ response.status_code = 401
+ response['WWW-Authenticate'] = 'Basic realm="%s"' % realm
+ return response
+ return wraps(view_func)(basic_auth)

0 comments on commit a154cab

Please sign in to comment.
Something went wrong with that request. Please try again.