From d3a221db662cbe1ff167a29196e11ce05931143f Mon Sep 17 00:00:00 2001 From: Pedro Crespo-Valero <32402063+pcrespov@users.noreply.github.com> Date: Wed, 11 Dec 2024 20:28:03 +0100 Subject: [PATCH] tokens --- api/specs/web-server/_users.py | 2 +- .../models_library/api_schemas_webserver/_base.py | 8 ++++++++ .../models_library/api_schemas_webserver/users.py | 9 +++++++-- .../users/{_tokens_handlers.py => _tokens_rest.py} | 12 +++++++----- .../users/{_tokens.py => _tokens_service.py} | 1 - .../src/simcore_service_webserver/users/plugin.py | 9 ++------- 6 files changed, 25 insertions(+), 16 deletions(-) rename services/web/server/src/simcore_service_webserver/users/{_tokens_handlers.py => _tokens_rest.py} (90%) rename services/web/server/src/simcore_service_webserver/users/{_tokens.py => _tokens_service.py} (97%) diff --git a/api/specs/web-server/_users.py b/api/specs/web-server/_users.py index 7a8342e34c2..1fb425a2def 100644 --- a/api/specs/web-server/_users.py +++ b/api/specs/web-server/_users.py @@ -28,7 +28,7 @@ from simcore_service_webserver.users._notifications_handlers import ( _NotificationPathParams, ) -from simcore_service_webserver.users._tokens_handlers import _TokenPathParams +from simcore_service_webserver.users._tokens_rest import _TokenPathParams from simcore_service_webserver.users.common._schemas import PreRegisteredUserGet router = APIRouter(prefix=f"/{API_VTAG}", tags=["user"]) diff --git a/packages/models-library/src/models_library/api_schemas_webserver/_base.py b/packages/models-library/src/models_library/api_schemas_webserver/_base.py index 948c4c9b3ea..a5eaa42c006 100644 --- a/packages/models-library/src/models_library/api_schemas_webserver/_base.py +++ b/packages/models-library/src/models_library/api_schemas_webserver/_base.py @@ -29,6 +29,14 @@ class InputSchema(BaseModel): ) +class OutputSchemaWithoutCamelCase(BaseModel): + model_config = ConfigDict( + populate_by_name=True, + extra="ignore", + frozen=True, + ) + + class OutputSchema(BaseModel): model_config = ConfigDict( alias_generator=snake_to_camel, diff --git a/packages/models-library/src/models_library/api_schemas_webserver/users.py b/packages/models-library/src/models_library/api_schemas_webserver/users.py index 41a92ce01e3..56b4e02ceb7 100644 --- a/packages/models-library/src/models_library/api_schemas_webserver/users.py +++ b/packages/models-library/src/models_library/api_schemas_webserver/users.py @@ -18,7 +18,12 @@ UserPermission, UserThirdPartyToken, ) -from ._base import InputSchema, InputSchemaWithoutCamelCase, OutputSchema +from ._base import ( + InputSchema, + InputSchemaWithoutCamelCase, + OutputSchema, + OutputSchemaWithoutCamelCase, +) from .groups import MyGroupsGet from .users_preferences import AggregatedPreferences @@ -224,7 +229,7 @@ def to_model(self) -> UserThirdPartyToken: ) -class MyTokenGet(OutputSchema): +class MyTokenGet(OutputSchemaWithoutCamelCase): service: str token_key: UUID token_secret: Annotated[ diff --git a/services/web/server/src/simcore_service_webserver/users/_tokens_handlers.py b/services/web/server/src/simcore_service_webserver/users/_tokens_rest.py similarity index 90% rename from services/web/server/src/simcore_service_webserver/users/_tokens_handlers.py rename to services/web/server/src/simcore_service_webserver/users/_tokens_rest.py index 42eb02e5cd7..92084def8f2 100644 --- a/services/web/server/src/simcore_service_webserver/users/_tokens_handlers.py +++ b/services/web/server/src/simcore_service_webserver/users/_tokens_rest.py @@ -15,7 +15,7 @@ from ..login.decorators import login_required from ..security.decorators import permission_required from ..utils_aiohttp import envelope_json_response -from . import _tokens +from . import _tokens_service from .common._schemas import UsersRequestContext from .exceptions import TokenNotFoundError @@ -45,7 +45,7 @@ async def _wrapper(request: web.Request) -> web.StreamResponse: @permission_required("user.tokens.*") async def list_tokens(request: web.Request) -> web.Response: req_ctx = UsersRequestContext.model_validate(request) - all_tokens = await _tokens.list_tokens(request.app, req_ctx.user_id) + all_tokens = await _tokens_service.list_tokens(request.app, req_ctx.user_id) return envelope_json_response([MyTokenGet.from_model(t) for t in all_tokens]) @@ -57,7 +57,7 @@ async def create_token(request: web.Request) -> web.Response: req_ctx = UsersRequestContext.model_validate(request) token_create = await parse_request_body_as(MyTokenCreate, request) - token = await _tokens.create_token( + token = await _tokens_service.create_token( request.app, req_ctx.user_id, token_create.to_model() ) @@ -76,7 +76,7 @@ async def get_token(request: web.Request) -> web.Response: req_ctx = UsersRequestContext.model_validate(request) req_path_params = parse_request_path_parameters_as(_TokenPathParams, request) - token = await _tokens.get_token( + token = await _tokens_service.get_token( request.app, req_ctx.user_id, req_path_params.service ) @@ -91,6 +91,8 @@ async def delete_token(request: web.Request) -> web.Response: req_ctx = UsersRequestContext.model_validate(request) req_path_params = parse_request_path_parameters_as(_TokenPathParams, request) - await _tokens.delete_token(request.app, req_ctx.user_id, req_path_params.service) + await _tokens_service.delete_token( + request.app, req_ctx.user_id, req_path_params.service + ) return web.json_response(status=status.HTTP_204_NO_CONTENT) diff --git a/services/web/server/src/simcore_service_webserver/users/_tokens.py b/services/web/server/src/simcore_service_webserver/users/_tokens_service.py similarity index 97% rename from services/web/server/src/simcore_service_webserver/users/_tokens.py rename to services/web/server/src/simcore_service_webserver/users/_tokens_service.py index 3e9efa488c9..ec66bf243d9 100644 --- a/services/web/server/src/simcore_service_webserver/users/_tokens.py +++ b/services/web/server/src/simcore_service_webserver/users/_tokens_service.py @@ -4,7 +4,6 @@ """ import sqlalchemy as sa from aiohttp import web -from models_library.api_schemas_webserver.users import UserThirdPartyToken from models_library.users import UserID, UserThirdPartyToken from models_library.utils.fastapi_encoders import jsonable_encoder from sqlalchemy import and_, literal_column diff --git a/services/web/server/src/simcore_service_webserver/users/plugin.py b/services/web/server/src/simcore_service_webserver/users/plugin.py index 53b88bf3c97..f46e0af7a38 100644 --- a/services/web/server/src/simcore_service_webserver/users/plugin.py +++ b/services/web/server/src/simcore_service_webserver/users/plugin.py @@ -9,12 +9,7 @@ from servicelib.aiohttp.application_setup import ModuleCategory, app_module_setup from servicelib.aiohttp.observer import setup_observer_registry -from . import ( - _notifications_handlers, - _preferences_handlers, - _tokens_handlers, - _users_rest, -) +from . import _notifications_handlers, _preferences_handlers, _tokens_rest, _users_rest from ._preferences_models import overwrite_user_preferences_defaults _logger = logging.getLogger(__name__) @@ -33,6 +28,6 @@ def setup_users(app: web.Application): overwrite_user_preferences_defaults(app) app.router.add_routes(_users_rest.routes) - app.router.add_routes(_tokens_handlers.routes) + app.router.add_routes(_tokens_rest.routes) app.router.add_routes(_notifications_handlers.routes) app.router.add_routes(_preferences_handlers.routes)