Skip to content

Instantly share code, notes, and snippets.

@rbreve
Created May 6, 2011 00:07
Show Gist options
  • Save rbreve/958226 to your computer and use it in GitHub Desktop.
Save rbreve/958226 to your computer and use it in GitHub Desktop.
def auth_required(func):
def decorator(request, *args, **kwargs):
return basic_auth(func, request, *args, **kwargs)
return decorator
def basic_auth(func, request, *args, **kwargs):
if request.META.has_key('HTTP_AUTHORIZATION'):
(authmeth, auth) = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
if authmeth.lower() == 'basic':
auth = auth.strip().decode('base64')
username, password = auth.split(':', 1)
user = authenticate(username=username, password=password)
if user:
if user.is_active:
request.user=user
return func(request, *args, **kwargs)
# return HttpResponseRedirect('/')
realm="Autenticacion Blipea"
response = HttpResponse(('Blip Authorization Required'), mimetype="text/plain")
response.status_code = 401
response['WWW-Authenticate'] = 'Basic realm="%s"' % (realm)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment