From bd62dac7399d927732b4c63b1c85890d04578c42 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Mon, 15 Jul 2024 09:40:01 +0200 Subject: [PATCH] remove hard coded config key in auth_manager init_app method --- src/pypnusershub/auth/auth_manager.py | 20 ++++++++++++++++++-- src/pypnusershub/tests/conftest.py | 4 +++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/pypnusershub/auth/auth_manager.py b/src/pypnusershub/auth/auth_manager.py index a0d1010..88cb4e1 100644 --- a/src/pypnusershub/auth/auth_manager.py +++ b/src/pypnusershub/auth/auth_manager.py @@ -7,6 +7,20 @@ from .authentication import Authentication +from typing import TypedDict + +Provider = TypedDict( + "Provider", + { + "id_provider": str, + "module": str, + "label": str, + "group_mapping": dict, + "logo": str, + }, +) + + class AuthManager: """ Manages authentication providers. @@ -66,7 +80,9 @@ def add_provider( raise AssertionError("Provider must be an instance of Authentication") self.provider_authentication_cls[id_provider] = provider_authentification - def init_app(self, app, prefix: str = "/auth") -> None: + def init_app( + self, app, prefix: str = "/auth", providers_declaration: list[Provider] = [] + ) -> None: """ Initializes the Flask application with the AuthManager. In addtion, it registers the authentification module blueprint. @@ -82,7 +98,7 @@ def init_app(self, app, prefix: str = "/auth") -> None: app.register_blueprint(routes, url_prefix=prefix) - for provider_config in app.config["AUTHENTICATION"].get("PROVIDERS", []): + for provider_config in providers_declaration: path_provider = provider_config.get("module") import_path, class_name = ( ".".join(path_provider.split(".")[:-1]), diff --git a/src/pypnusershub/tests/conftest.py b/src/pypnusershub/tests/conftest.py index ff681f6..2482800 100644 --- a/src/pypnusershub/tests/conftest.py +++ b/src/pypnusershub/tests/conftest.py @@ -29,7 +29,9 @@ def app(): app.testing = True db.init_app(app) ma.init_app(app) - auth_manager.init_app(app) + auth_manager.init_app( + app, providers_declaration=app.config["AUTHENTICATION"]["PROVIDERS"] + ) login_manager.init_app(app) with app.app_context():