File indexing completed on 2026-08-12 09:36:21
0001 """Keep anonymous traffic off the tunnel.
0002
0003 Every page under /prod/ is rendered by swf-monitor at BNL and reached over the
0004 SSH tunnel, so an anonymous request that gets as far as a view costs an
0005 upstream page build. Refusing it here means it never crosses the tunnel at
0006 all, which is the point: enforcing the same rule upstream would still let the
0007 traffic arrive before rejecting it.
0008
0009 This runs as middleware rather than a per-view decorator because
0010 remote_app/urls.py ends in catch-all proxy routes, deliberately, so that new
0011 swf-monitor pages need no route here. A decorator would silently miss every
0012 one of them.
0013 """
0014
0015 from django.conf import settings
0016 from django.contrib.auth.views import redirect_to_login
0017
0018
0019 class LoginWallMiddleware:
0020 """Send anonymous requests to the login page, except on open paths.
0021
0022 Paths are matched on path_info, which excludes the /prod script name.
0023 """
0024
0025 OPEN_PREFIXES = (
0026 '/accounts/',
0027 '/static/',
0028
0029
0030
0031 '/pcs/api/v1/',
0032
0033
0034 '/api/schema/',
0035 )
0036
0037
0038
0039
0040 OPEN_EXACT = ('/', '/prod/')
0041
0042 def __init__(self, get_response):
0043 self.get_response = get_response
0044
0045 def is_open(self, path):
0046 return path in self.OPEN_EXACT or path.startswith(self.OPEN_PREFIXES)
0047
0048 def __call__(self, request):
0049 if not request.user.is_authenticated and not self.is_open(request.path_info):
0050 return redirect_to_login(request.get_full_path(), settings.LOGIN_URL)
0051 return self.get_response(request)