Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment