from django.http import HttpResponse
from django.conf import settings
class BasicAuthMiddleware(object):
def unauthed(self):
response = HttpResponse("""
Auth required
Authorization Required
""", content_type="text/html")
response['WWW-Authenticate'] = 'Basic realm="Development"'
response.status_code = 401
return response
def process_request(self,request):
import base64
if not 'HTTP_AUTHORIZATION' in request.META:
#if not request.META.in('HTTP_AUTHORIZATION'):
return self.unauthed()
else:
authentication = request.META['HTTP_AUTHORIZATION']
(authmeth, auth) = authentication.split(' ',1)
if 'basic' != authmeth.lower():
return self.unauthed()
auth = base64.b64decode(auth.strip()).decode('utf-8')
#auth = auth.strip().decode('base64')
username, password = auth.split(':',1)
if username == settings.BASICAUTH_USERNAME and password == settings.BASICAUTH_PASSWORD:
return None
return self.unauthed()