Skip to content

Instantly share code, notes, and snippets.

@asanoboy
Created December 3, 2012 12:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asanoboy/4194673 to your computer and use it in GitHub Desktop.
Save asanoboy/4194673 to your computer and use it in GitHub Desktop.
Django Middleware to protect by HTTP Basic Authorization for staging.
from django.http import HttpResponse
from django.conf import settings
import base64
ID = settings.STAGING_HTTP_BASIC_AUTH_ID or False
PW = settings.STAGING_HTTP_BASIC_AUTH_PW or False
class AuthMiddleware(object):
def process_request(self, request):
if not ID or not PW:
return None
if request.META.has_key('HTTP_AUTHORIZATION'):
(method, encoded) = request.META['HTTP_AUTHORIZATION'].split()
if method.lower() == 'basic':
(username, password) = base64.b64decode(encoded).split(":")
if username == ID and password == PW:
return None
response = HttpResponse(status=401)
response['WWW-Authenticate'] = 'Basic realm="Secret File"'
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment