From d6d621c5c5e2f86da4df9e6f60725552c002f292 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Wed, 6 Nov 2024 15:30:21 -0800 Subject: [PATCH] Fix bug that reset tokens' `creation_timestamp` upon refresh Update imports to use `neon_data_models` Mark old imports in `schema` as deprecated Better document usage, including token management --- README.md | 28 ++++++++++++++++++++++++---- neon_hana/auth/client_manager.py | 5 +++-- neon_hana/mq_service_api.py | 4 ++-- neon_hana/schema/assist_requests.py | 4 ++-- neon_hana/schema/node_model.py | 5 ++++- neon_hana/schema/user_profile.py | 5 ++++- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 074b470..5e3e5ba 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,27 @@ docker run -p 8080:8080 -v ~/.config/neon:/config/neon ghcr.io/neongeckocom/neon are using the default port 8080 ## Usage -Full API documentation is available at `/docs`. The `/auth/login` endpoint should -be used to generate a `client_id`, `access_token`, and `refresh_token`. The -`access_token` should be included in every request and upon expiration of the -`access_token`, a new token can be obtained from the `auth/refresh` endpoint. +Full API documentation is available at `/docs`. + +### Registration +The `/auth/register` endpoint may be used to create a new user if auth is enabled. +If auth is disabled, any login requests will return a successful response. + +### Token Generation +The `/auth/login` endpoint should be used to generate a `client_id`, +`access_token`, and `refresh_token`. The `access_token` should be included in +every request and upon expiration of the `access_token`, a new token can be +obtained from the `auth/refresh` endpoint. Tokens are client-specific and clients +are expected to include the same `client_id` and valid tokens for that client +with every request. + +### Token Management +`access_token`s should not be saved to persistent storage; they are only valid +for a short period of time and a new `access_token` should be generated for +every new session. + +`refresh_token`s should be saved to persistent storage and used to generate a new +`access_token` and `refresh_token` at the beginning of a session, or when the +current `access_token` expires. A `refresh_token` may only be used once; a new +`refresh_token` returned from the `/auth/refresh` endpoint will replace the one +included in the request. diff --git a/neon_hana/auth/client_manager.py b/neon_hana/auth/client_manager.py index 3af6d25..4b6c146 100644 --- a/neon_hana/auth/client_manager.py +++ b/neon_hana/auth/client_manager.py @@ -295,10 +295,11 @@ def _add_token_to_userdb(self, user: User, new_token: TokenConfig): print("No MQ Connection to a user database") return for idx, token in enumerate(user.tokens): + # If the token is already defined, maintain the original + # token_id and creation timestamp if token.token_id == new_token.token_id: - # Tokens don't contain `token_name`, so use the same one as is - # being replaced new_token.token_name = token.token_name + new_token.creation_timestamp = token.creation_timestamp user.tokens.remove(token) user.tokens.append(new_token) self._mq_connector.update_user(user) diff --git a/neon_hana/mq_service_api.py b/neon_hana/mq_service_api.py index 87c96c8..7bd8f04 100644 --- a/neon_hana/mq_service_api.py +++ b/neon_hana/mq_service_api.py @@ -31,9 +31,9 @@ from uuid import uuid4 from fastapi import HTTPException -from neon_hana.schema.node_model import NodeData -from neon_hana.schema.user_profile import UserProfile from neon_mq_connector.utils.client_utils import send_mq_request +from neon_data_models.models.client.node import NodeData +from neon_data_models.models.user.neon_profile import UserProfile from neon_data_models.models.user import User diff --git a/neon_hana/schema/assist_requests.py b/neon_hana/schema/assist_requests.py index 7af09b7..472b708 100644 --- a/neon_hana/schema/assist_requests.py +++ b/neon_hana/schema/assist_requests.py @@ -27,8 +27,8 @@ from typing import List, Optional from pydantic import BaseModel -from neon_hana.schema.node_model import NodeData -from neon_hana.schema.user_profile import UserProfile +from neon_data_models.models.client.node import NodeData +from neon_data_models.models.user.neon_profile import UserProfile class STTRequest(BaseModel): diff --git a/neon_hana/schema/node_model.py b/neon_hana/schema/node_model.py index 7345497..b9f0080 100644 --- a/neon_hana/schema/node_model.py +++ b/neon_hana/schema/node_model.py @@ -25,4 +25,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from neon_data_models.models.client.node import NodeSoftware, NodeNetworking, NodeLocation, NodeData -# TODO: Mark for deprecation +from ovos_utils.log import log_deprecation + +log_deprecation('Imports moved to `neon_data_models.models.client.node`', + '1.0.0') diff --git a/neon_hana/schema/user_profile.py b/neon_hana/schema/user_profile.py index 85f5f61..231c39b 100644 --- a/neon_hana/schema/user_profile.py +++ b/neon_hana/schema/user_profile.py @@ -25,4 +25,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from neon_data_models.models.user.neon_profile import * -# TODO: Mark for deprecation +from ovos_utils.log import log_deprecation + +log_deprecation('Imports moved to `neon_data_models.models.user.neon_profile`', + '1.0.0')