Created
December 3, 2012 12:21
-
-
Save asanoboy/4194673 to your computer and use it in GitHub Desktop.
Django Middleware to protect by HTTP Basic Authorization for staging.
This file contains hidden or 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.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