Skip to content

Commit

Permalink
Add user route with authenticated get and update endpoints.
Browse files Browse the repository at this point in the history
Add helper method for `update` requests to allow for changing the current auth method (password or token)
  • Loading branch information
NeonDaniel committed Oct 30, 2024
1 parent c36ea4b commit 5ead36c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions neon_hana/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from neon_hana.app.routers.llm import llm_route
from neon_hana.app.routers.mq_backend import mq_route
from neon_hana.app.routers.auth import auth_route
from neon_hana.app.routers.user import user_route
from neon_hana.app.routers.util import util_route
from neon_hana.app.routers.node_server import node_route
from neon_hana.version import __version__
Expand All @@ -49,5 +50,6 @@ def create_app(config: dict):
app.include_router(llm_route)
app.include_router(util_route)
app.include_router(node_route)
app.include_router(user_route)

return app
45 changes: 45 additions & 0 deletions neon_hana/app/routers/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from fastapi import APIRouter, Depends
from neon_hana.app.dependencies import jwt_bearer, mq_connector
from neon_hana.schema.user_requests import GetUserRequest, UpdateUserRequest
from neon_users_service.models import User

user_route = APIRouter(tags=["user"], dependencies=[Depends(jwt_bearer)])


@user_route.post("/get")
async def get_user(request: GetUserRequest,
token: str = Depends(jwt_bearer)) -> User:
return mq_connector.get_user_profile(access_token=token, **dict(request))


@user_route.post("/update")
async def update_user(request: UpdateUserRequest,
token: str = Depends(jwt_bearer)) -> User:
return mq_connector.handle_update_user_request(access_token=token,
**dict(request))
15 changes: 15 additions & 0 deletions neon_hana/mq_service_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ def update_user(self, user: User) -> User:
raise HTTPException(status_code=code, detail=err_or_user)
return err_or_user

def handle_update_user_request(self, user: User, access_token: str):
"""
Handle a request to update a user. This accepts an `auth_token` to
account for requests to change the password or registered tokens.
@param user: Updated User object to write to the database
@param access_token: JWT auth token submitted with the request
"""
stat, code, err_or_user = self._query_users_api("update",
username=user.username,
access_token=access_token,
user=user)
if not stat:
raise HTTPException(status_code=code, detail=err_or_user)
return err_or_user

def query_api_proxy(self, service_name: str, query_params: dict,
timeout: int = 10):
query_params['service'] = service_name
Expand Down
49 changes: 49 additions & 0 deletions neon_hana/schema/user_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from pydantic import BaseModel

from neon_users_service.models import User


class GetUserRequest(BaseModel):
username: str = "guest"

model_config = {
"json_schema_extra": {
"examples": [{
"username": "guest"
}]}}


class UpdateUserRequest(BaseModel):
user: User

model_config = {
"json_schema_extra": {
"examples": [{
"user": User(username="guest").model_dump()
}]}}

0 comments on commit 5ead36c

Please sign in to comment.