This code based on Basic Auth Middleware by joshsharp. URL : https://djangosnippets.org/snippets/2468/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from django.http import HttpResponse
from django.conf import settings
class BasicAuthMiddleware(object):
def unauthed(self):
response = HttpResponse("""<html><title>Auth required</title><body>
<h1>Authorization Required</h1></body></html>""", 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()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.