From 411e44a6812086a27e4fad3e634cc138c249857d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 14 Oct 2022 10:35:58 +0200 Subject: [PATCH] [WIP] Added auth controller & updated pakages #70 --- src/bot_api/api.py | 18 ++++++--- src/bot_api/api_module.py | 2 + src/bot_api/bot-api.json | 7 +++- src/bot_api/configuration/__init__.py | 25 ++++++++++++ src/bot_api/controller/auth_controller.py | 47 +++++++++++++++++++++++ src/bot_api/filter/__init__.py | 26 +++++++++++++ src/bot_api/route/route.py | 4 +- src/bot_api/service/__init__.py | 25 ++++++++++++ src/bot_core/bot-core.json | 2 +- src/bot_data/bot-data.json | 2 +- src/modules/admin/admin.json | 2 +- src/modules/auto_role/auto-role.json | 2 +- src/modules/base/base.json | 2 +- src/modules/boot_log/boot-log.json | 2 +- src/modules/database/database.json | 2 +- src/modules/moderator/moderator.json | 2 +- src/modules/permission/permission.json | 2 +- 17 files changed, 154 insertions(+), 18 deletions(-) create mode 100644 src/bot_api/controller/auth_controller.py diff --git a/src/bot_api/api.py b/src/bot_api/api.py index c404350521..29c176fa5e 100644 --- a/src/bot_api/api.py +++ b/src/bot_api/api.py @@ -1,8 +1,10 @@ import sys from functools import partial +from blinker import Namespace from cpl_core.dependency_injection import ServiceProviderABC from flask import Flask, request +from flask_cors import CORS from bot_api.configuration.api_settings import ApiSettings from bot_api.logging.api_logger import ApiLogger @@ -27,20 +29,26 @@ class Api(Flask): self._services = services self._apt_settings = api_settings + self._cors = CORS(self, support_credentials=True) + # register before request self.before_request_funcs.setdefault(None, []).append(self.before_request) + my_signals = Namespace() + notify = my_signals.signal('notify') def _register_routes(self): for path, f in Route.registered_routes.items(): + route = f[0] + kwargs = f[1] cls = None - qual_name_split = f.__qualname__.split('.') + qual_name_split = route.__qualname__.split('.') if len(qual_name_split) > 0: - cls_type = vars(sys.modules[f.__module__])[qual_name_split[0]] + cls_type = vars(sys.modules[route.__module__])[qual_name_split[0]] cls = self._services.get_service(cls_type) - partial_f = partial(f, self if cls is None else cls) - partial_f.__name__ = f.__name__ - self.route(path)(partial_f) + partial_f = partial(route, self if cls is None else cls) + partial_f.__name__ = route.__name__ + self.route(path, **kwargs)(partial_f) def before_request(self, *args, **kwargs): self._logger.debug(__name__, f'Received GET @{request.url}') diff --git a/src/bot_api/api_module.py b/src/bot_api/api_module.py index 3f26d80c56..be126e6550 100644 --- a/src/bot_api/api_module.py +++ b/src/bot_api/api_module.py @@ -10,6 +10,7 @@ from flask import Flask from bot_api.api import Api from bot_api.api_thread import ApiThread from bot_api.controller.api_controller import ApiController +from bot_api.controller.auth_controller import AuthController from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum @@ -33,4 +34,5 @@ class ApiModule(ModuleABC): services.add_singleton(ApiThread) services.add_singleton(Flask, Api) + services.add_transient(AuthController) services.add_transient(ApiController) diff --git a/src/bot_api/bot-api.json b/src/bot_api/bot-api.json index 09e4129308..7b66f8efe5 100644 --- a/src/bot_api/bot-api.json +++ b/src/bot_api/bot-api.json @@ -17,11 +17,14 @@ "LicenseDescription": "", "Dependencies": [ "cpl-core==2022.10.0.post6", + "Flask==2.2.2", "Flask[async]==2.2.2", - "Flask-Classful==0.14.2" + "Flask-Classful==0.14.2", + "Flask-Cors==3.0.10", + "PyJWT==2.5.0" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/bot_api/configuration/__init__.py b/src/bot_api/configuration/__init__.py index 425ab6c146..86e07283fa 100644 --- a/src/bot_api/configuration/__init__.py +++ b/src/bot_api/configuration/__init__.py @@ -1 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'bot_api.configuration' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.3' + +from collections import namedtuple + + # imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='3') diff --git a/src/bot_api/controller/auth_controller.py b/src/bot_api/controller/auth_controller.py new file mode 100644 index 0000000000..40427bdab5 --- /dev/null +++ b/src/bot_api/controller/auth_controller.py @@ -0,0 +1,47 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_core.mailing import EMailClientABC, EMailClientSettings +from cpl_query.extension import List +from cpl_translation import TranslatePipe +from flask import request +from flask_cors import cross_origin + +from bot_api.api import Api +from bot_api.logging.api_logger import ApiLogger +from bot_api.model.token_dto import TokenDTO +from bot_api.route.route import Route +from bot_data.model.auth_user import AuthUser + + +class AuthController: + + def __init__( + self, + config: ConfigurationABC, + env: ApplicationEnvironmentABC, + logger: ApiLogger, + t: TranslatePipe, + api: Api, + mail_settings: EMailClientSettings, + mailer: EMailClientABC + ): + self._config = config + self._env = env + self._logger = logger + self._t = t + self._api = api + self._mail_settings = mail_settings + self._mailer = mailer + + @Route.route('/api/auth/get-all-users') + async def get_all_users(self) -> List[AuthUser]: + pass + + @Route.route('/api/auth/get-filtered-users', methods=['POST']) + async def get_filtered_users(self) -> List[AuthUser]: + pass + + @Route.route('/api/auth/login', methods=['POST']) + async def login(self) -> dict: + self._logger.warn(__name__, f'Received {request.json}') + return TokenDTO().to_dict() diff --git a/src/bot_api/filter/__init__.py b/src/bot_api/filter/__init__.py index e69de29bb2..0f9e52aa6a 100644 --- a/src/bot_api/filter/__init__.py +++ b/src/bot_api/filter/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'bot_api.filter' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.3' + +from collections import namedtuple + + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='3') diff --git a/src/bot_api/route/route.py b/src/bot_api/route/route.py index ef0e399403..9dbe7fcbfa 100644 --- a/src/bot_api/route/route.py +++ b/src/bot_api/route/route.py @@ -2,10 +2,10 @@ class Route: registered_routes = {} @classmethod - def route(cls, path=None): + def route(cls, path=None, **kwargs): # simple decorator for class based views def inner(fn): - cls.registered_routes[path] = fn + cls.registered_routes[path] = (fn, kwargs) return fn return inner diff --git a/src/bot_api/service/__init__.py b/src/bot_api/service/__init__.py index 425ab6c146..3a5cf24d73 100644 --- a/src/bot_api/service/__init__.py +++ b/src/bot_api/service/__init__.py @@ -1 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'bot_api.service' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.3' + +from collections import namedtuple + + # imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='3') diff --git a/src/bot_core/bot-core.json b/src/bot_core/bot-core.json index 6720bf0f2e..502ce6c618 100644 --- a/src/bot_core/bot-core.json +++ b/src/bot_core/bot-core.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/bot_data/bot-data.json b/src/bot_data/bot-data.json index e92af7de3c..99af9f44e4 100644 --- a/src/bot_data/bot-data.json +++ b/src/bot_data/bot-data.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/admin/admin.json b/src/modules/admin/admin.json index 446e16f30a..e76e9180f8 100644 --- a/src/modules/admin/admin.json +++ b/src/modules/admin/admin.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post5" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/auto_role/auto-role.json b/src/modules/auto_role/auto-role.json index 8e29094ce9..f87561551d 100644 --- a/src/modules/auto_role/auto-role.json +++ b/src/modules/auto_role/auto-role.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post5" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/base/base.json b/src/modules/base/base.json index 4371622ce5..a77d7c5b60 100644 --- a/src/modules/base/base.json +++ b/src/modules/base/base.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post2" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/boot_log/boot-log.json b/src/modules/boot_log/boot-log.json index bf1e0b5b1e..0090d83c50 100644 --- a/src/modules/boot_log/boot-log.json +++ b/src/modules/boot_log/boot-log.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post2" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/database/database.json b/src/modules/database/database.json index efcaea3f3e..c4b05683cf 100644 --- a/src/modules/database/database.json +++ b/src/modules/database/database.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post2" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/moderator/moderator.json b/src/modules/moderator/moderator.json index 45dca24c5b..034735a69b 100644 --- a/src/modules/moderator/moderator.json +++ b/src/modules/moderator/moderator.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post5" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/modules/permission/permission.json b/src/modules/permission/permission.json index f5c9608797..86cbc3c749 100644 --- a/src/modules/permission/permission.json +++ b/src/modules/permission/permission.json @@ -19,7 +19,7 @@ "cpl-core>=2022.10.0.post2" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": {