Created
July 17, 2009 15:14
-
-
Save jcarbaugh/149101 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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