From 1104bde5b43efde9a5863629283d1e2a3ca27f5c Mon Sep 17 00:00:00 2001 From: Daniel Grams Date: Fri, 12 Feb 2021 09:03:34 +0100 Subject: [PATCH] Make Swagger Auth URLs https #107 --- project/init_data.py | 13 +++++++++++-- tests/test_init_data.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/test_init_data.py diff --git a/project/init_data.py b/project/init_data.py index 28c6500..ab59ac0 100644 --- a/project/init_data.py +++ b/project/init_data.py @@ -6,14 +6,23 @@ from project.services.event import upsert_event_category from project.models import Location from flask import url_for from apispec.exceptions import DuplicateComponentNameError +import os @app.before_first_request def add_oauth2_scheme(): + # At some sites the https scheme is not set yet + if os.getenv("AUTHLIB_INSECURE_TRANSPORT", "False").lower() in ["true", "1"]: + authorizationUrl = url_for("authorize", _external=True) + tokenUrl = url_for("issue_token", _external=True) + else: + authorizationUrl = url_for("authorize", _external=True, _scheme="https") + tokenUrl = url_for("issue_token", _external=True, _scheme="https") + oauth2_scheme = { "type": "oauth2", - "authorizationUrl": url_for("authorize", _external=True), - "tokenUrl": url_for("issue_token", _external=True), + "authorizationUrl": authorizationUrl, + "tokenUrl": tokenUrl, "flow": "accessCode", "scopes": scopes, } diff --git a/tests/test_init_data.py b/tests/test_init_data.py new file mode 100644 index 0000000..a7dceb7 --- /dev/null +++ b/tests/test_init_data.py @@ -0,0 +1,14 @@ +import pytest + + +@pytest.mark.parametrize("insecure", [None, "0", "1"]) +def test_add_oauth2_scheme(app, utils, insecure): + import os + + if insecure: + os.environ["AUTHLIB_INSECURE_TRANSPORT"] = insecure + else: + del os.environ["AUTHLIB_INSECURE_TRANSPORT"] + + url = utils.get_url("home") + utils.get_ok(url)