2019-03-20 21:42:47 +00:00
|
|
|
"""passbook app_gw middleware"""
|
2019-03-22 13:51:09 +00:00
|
|
|
from django.views.generic import RedirectView
|
2019-03-20 21:42:47 +00:00
|
|
|
|
2019-04-13 14:05:11 +00:00
|
|
|
from passbook.app_gw.proxy.handler import RequestHandler
|
2019-03-22 13:51:09 +00:00
|
|
|
from passbook.lib.config import CONFIG
|
2019-03-20 21:42:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationGatewayMiddleware:
|
|
|
|
"""Check if request should be proxied or handeled normally"""
|
|
|
|
|
2019-04-13 14:05:11 +00:00
|
|
|
_app_gw_cache = {}
|
2019-03-20 21:42:47 +00:00
|
|
|
|
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
2019-04-13 14:05:11 +00:00
|
|
|
# Rudimentary cache
|
|
|
|
host_header = request.META.get('HTTP_HOST')
|
|
|
|
if host_header not in self._app_gw_cache:
|
|
|
|
self._app_gw_cache[host_header] = RequestHandler.find_app_gw_for_request(request)
|
|
|
|
if self._app_gw_cache[host_header]:
|
|
|
|
return self.dispatch(request, self._app_gw_cache[host_header])
|
|
|
|
return self.get_response(request)
|
|
|
|
|
|
|
|
def dispatch(self, request, app_gw):
|
2019-03-20 21:42:47 +00:00
|
|
|
"""Build proxied request and pass to upstream"""
|
2019-04-13 14:05:11 +00:00
|
|
|
handler = RequestHandler(app_gw, request)
|
|
|
|
|
|
|
|
if not handler.check_permission():
|
2019-03-22 13:51:09 +00:00
|
|
|
to_url = 'https://%s/?next=%s' % (CONFIG.get('domains')[0], request.get_full_path())
|
|
|
|
return RedirectView.as_view(url=to_url)(request)
|
2019-03-21 13:53:47 +00:00
|
|
|
|
2019-04-13 14:05:11 +00:00
|
|
|
return handler.get_response()
|