From 725b06d0846a19112e6a14e89b5514751f05f133 Mon Sep 17 00:00:00 2001 From: jasquat Date: Fri, 3 Jun 2022 11:40:19 -0400 Subject: [PATCH] added api to get process groups and added some cors support w/ burnettk --- conftest.py | 3 ++- src/spiffworkflow_backend/__init__.py | 6 ++++++ src/spiffworkflow_backend/config/default.py | 3 +++ .../routes/admin_blueprint/admin_blueprint.py | 11 +++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 82125f4a..4e073ddf 100644 --- a/conftest.py +++ b/conftest.py @@ -20,7 +20,8 @@ from spiffworkflow_backend import create_app # noqa: E402 def app() -> Flask: """App.""" os.environ["FLASK_ENV"] = "testing" - os.environ["FLASK_SESSION_SECRET_KEY"] = "this_is_testing_secret_key" + # os.environ["FLASK_SESSION_SECRET_KEY"] = "this_is_testing_secret_key" + os.environ["FLASK_SESSION_SECRET_KEY"] = "super_secret_key" app = create_app() # NOTE: set this here since nox shoves tests and src code to diff --git a/src/spiffworkflow_backend/__init__.py b/src/spiffworkflow_backend/__init__.py index ff427d0d..ba8ebd28 100644 --- a/src/spiffworkflow_backend/__init__.py +++ b/src/spiffworkflow_backend/__init__.py @@ -6,12 +6,14 @@ import flask.app from flask_bpmn.api.api_error import api_error_blueprint from flask_bpmn.models.db import db from flask_bpmn.models.db import migrate +from flask_cors import CORS from spiffworkflow_backend.config import setup_config from spiffworkflow_backend.routes.admin_blueprint.admin_blueprint import admin_blueprint from spiffworkflow_backend.routes.api_blueprint import api_blueprint from spiffworkflow_backend.routes.process_api_blueprint import process_api_blueprint from spiffworkflow_backend.routes.user_blueprint import user_blueprint +from spiffworkflow_backend.models.user import UserModel def create_app() -> flask.app.Flask: @@ -44,6 +46,10 @@ def create_app() -> flask.app.Flask: app.register_blueprint(process_api_blueprint) app.register_blueprint(api_error_blueprint) app.register_blueprint(admin_blueprint, url_prefix="/admin") + + origins_re = [r"^https?:\/\/%s(.*)" % o.replace('.', '\.') for o in app.config['CORS_ALLOW_ORIGINS']] + CORS(app, origins=origins_re) + connexion_app.add_api("api.yml", base_path="/v1.0") return app diff --git a/src/spiffworkflow_backend/config/default.py b/src/spiffworkflow_backend/config/default.py index 9f2a19ad..70d41447 100644 --- a/src/spiffworkflow_backend/config/default.py +++ b/src/spiffworkflow_backend/config/default.py @@ -1,4 +1,7 @@ """Default.""" from os import environ +import re BPMN_SPEC_ABSOLUTE_DIR = environ.get("BPMN_SPEC_ABSOLUTE_DIR", default="") +CORS_DEFAULT = '*' +CORS_ALLOW_ORIGINS = re.split(r',\s*', environ.get('CORS_ALLOW_ORIGINS', default=CORS_DEFAULT)) diff --git a/src/spiffworkflow_backend/routes/admin_blueprint/admin_blueprint.py b/src/spiffworkflow_backend/routes/admin_blueprint/admin_blueprint.py index 3e8e1d59..20ec1f50 100644 --- a/src/spiffworkflow_backend/routes/admin_blueprint/admin_blueprint.py +++ b/src/spiffworkflow_backend/routes/admin_blueprint/admin_blueprint.py @@ -7,6 +7,7 @@ from flask import redirect from flask import render_template from flask import request from flask import url_for +from flask import current_app from flask_bpmn.models.db import db from spiffworkflow_backend.models.user import UserModel @@ -26,6 +27,16 @@ admin_blueprint = Blueprint( ALLOWED_BPMN_EXTENSIONS = {"bpmn", "dmn"} +@admin_blueprint.route("/token", methods=["GET"]) +def token(): + if current_app.env != "development": + return "Not authorized" + + user = UserModel.query.first() + auth_token = user.encode_auth_token() + return(f"auth_token: {auth_token}") + + @admin_blueprint.route("/process-groups", methods=["GET"]) def process_groups_list(): """Process_groups_list."""