Skip to content

Instantly share code, notes, and snippets.

@jcarbaugh
Created July 17, 2009 15:14
Show Gist options
  • Save jcarbaugh/149101 to your computer and use it in GitHub Desktop.
Save jcarbaugh/149101 to your computer and use it in GitHub Desktop.
from django.contrib.auth.models import User
import base64
class HTTPBasicAuthMiddleware(object):
def process_request(self, request):
authorization = request.META.get('HTTP_AUTHORIZATION', None)
if authorization:
try:
(scheme, token) = authorization.split(' ')
if scheme.lower() == 'basic':
user_pass = base64.b64decode(token)
(username, password) = user_pass.split(':')
user = User.objects.get(username=username)
if user.check_password(password):
request.user = user
except User.DoesNotExist:
pass # handle case of valid auth header, but user does not exist
except:
pass # handle case of invalid auth header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment