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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
from django import http
from django import shortcuts
from django.conf import settings
import base64
class GlobalLoginMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_view(self, request, callback, callback_args, callback_kwargs):
if not settings.GLOBAL_LOGIN_USER or not settings.GLOBAL_LOGIN_PASSWORD:
# Not configured to do global auth
return None
if getattr(callback, 'global_login_exempt', False):
# No global auth on this specific url
return None
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) != 2:
return http.HttpResponseForbidden("Invalid authentication")
if auth[0].lower() == "basic":
user, pwd = base64.b64decode(auth[1]).decode('utf8').split(':')
if user == settings.GLOBAL_LOGIN_USER and pwd == settings.GLOBAL_LOGIN_PASSWORD:
return None
# Else we fall through and request a login prompt
response = http.HttpResponse()
response.status_code = 401
response['WWW-Authenticate'] = 'Basic realm={0}'.format(settings.SITEBASE)
return response
# Ability to redirect using raise()
class RedirectException(Exception):
def __init__(self, url):
self.url = url
class RedirectMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_exception(self, request, exception):
if isinstance(exception, RedirectException):
return shortcuts.redirect(exception.url)
|