Skip to content

Commit

Permalink
Fix bug that reset tokens' creation_timestamp upon refresh
Browse files Browse the repository at this point in the history
Update imports to use `neon_data_models`
Mark old imports in `schema` as deprecated
Better document usage, including token management
  • Loading branch information
NeonDaniel committed Nov 6, 2024
1 parent 37087b5 commit d6d621c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 3 additions & 2 deletions neon_hana/auth/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions neon_hana/mq_service_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions neon_hana/schema/assist_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion neon_hana/schema/node_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
5 changes: 4 additions & 1 deletion neon_hana/schema/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit d6d621c

Please sign in to comment.