From 40df900dc77d58db9304490dd6f4b890f18adcb0 Mon Sep 17 00:00:00 2001 From: Omer Hamerman Date: Thu, 29 Jul 2021 18:39:47 +0100 Subject: [PATCH] Allowing full host override (#143) --- README.rst | 2 ++ snappass/main.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 3c80fe3..651fef8 100644 --- a/README.rst +++ b/README.rst @@ -98,6 +98,8 @@ need to change this. ``REDIS_PREFIX``: (optional, defaults to ``"snappass"``) prefix used on redis keys to prevent collisions with other potential clients +``HOST_OVERRIDE``: (optional) Used to override the base URL if the app is unaware. Useful when running behind reverse proxies like an identity-aware SSO. Example: ``sub.domain.com`` + Docker ------ diff --git a/snappass/main.py b/snappass/main.py index 3e04d0d..907d9bf 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -13,6 +13,7 @@ from distutils.util import strtobool NO_SSL = bool(strtobool(os.environ.get('NO_SSL', 'False'))) URL_PREFIX = os.environ.get('URL_PREFIX', None) +HOST_OVERRIDE = os.environ.get('HOST_OVERRIDE', None) TOKEN_SEPARATOR = '~' @@ -164,9 +165,15 @@ def handle_password(): token = set_password(password, ttl) if NO_SSL: - base_url = request.url_root + if HOST_OVERRIDE: + base_url = f'http://{HOST_OVERRIDE}/' + else: + base_url = request.url_root else: - base_url = request.url_root.replace("http://", "https://") + if HOST_OVERRIDE: + base_url = f'https://{HOST_OVERRIDE}/' + else: + base_url = request.url_root.replace("http://", "https://") if URL_PREFIX: base_url = base_url + URL_PREFIX.strip("/") + "/" link = base_url + url_quote_plus(token)