diff --git a/app/controller/Users.py b/app/controller/Users.py index f690c4d..5c67196 100644 --- a/app/controller/Users.py +++ b/app/controller/Users.py @@ -105,3 +105,10 @@ def handle_update_notification(self, user_id: int, notification_id: int, "status": status.HTTP_200_OK, }), ) + + def handle_get_user_auth(self, user_data: dict): + response = self.users_service.get_user_auth(user_data) + return JSONResponse( + status_code=status.HTTP_200_OK, + content=response + ) diff --git a/app/main.py b/app/main.py index 55cc1ae..c6a00a0 100644 --- a/app/main.py +++ b/app/main.py @@ -8,7 +8,8 @@ UpdateUserSchema, LoginRequest, CreateNotificationSchema, - UpdateNotificationSchema + UpdateNotificationSchema, + GetUserSchema ) from query_params.QueryParams import GetUsersQueryParams from security.JWTBearer import get_current_user_id @@ -30,6 +31,11 @@ def root(): return {"message": "users service"} +@app.post("/user", tags=["Auth"]) +def get_user_auth(user_data: GetUserSchema): + return users_controller.handle_get_user_auth(user_data.dict()) + + @app.get("/users/{user_id}", tags=["Users"]) def get_user(user_id: int): return users_controller.handle_get_user(user_id) diff --git a/app/schemas/Schemas.py b/app/schemas/Schemas.py index 3b12468..5b1ebd2 100644 --- a/app/schemas/Schemas.py +++ b/app/schemas/Schemas.py @@ -63,3 +63,7 @@ def validate_date_time(cls, v): if v.minute % 5 != 0: raise ValueError('Minutes must be multiples of 5') return v + + +class GetUserSchema(BaseModel): + token: str diff --git a/app/service/Users.py b/app/service/Users.py index 68365c4..a7de311 100644 --- a/app/service/Users.py +++ b/app/service/Users.py @@ -9,6 +9,8 @@ import jwt import uuid +JWT_SECRET = os.environ.get("JWT_SECRET") +HASH_ALGORITHM = os.environ.get("HASH_ALGORITHM") TOKEN_FIELD_NAME = "x-access-token" @@ -104,6 +106,14 @@ def delete_notification(self, user_id: int, notification_id: int): self.user_repository.rollback() raise e + def get_user_auth(self, user_data): + token = user_data.get("token") + print(f"Token: {token}") + decoded_token = jwt.decode(token, + os.environ["JWT_SECRET"], + algorithms=["HS256"]) + return {"user_id": decoded_token.get("user_id")} + def _generate_nickname(self, name): # pragma: no cover name_without_spaces = name.replace(" ", "") @@ -140,6 +150,7 @@ def login(self, auth_code: str): jwt_token = jwt.encode(payload, os.environ["JWT_SECRET"], algorithm="HS256") + print(f"Token: {jwt_token}") return user, jwt_token def _get_access_token(self, authorization_code): # pragma: no cover