From 751d91562ec8f7a3928d03408b79c7b4e1b88725 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 24 Jan 2024 15:26:16 +0300 Subject: [PATCH] inbox-debug-7 --- resolvers/load.py | 2 ++ services/auth.py | 50 +++++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/resolvers/load.py b/resolvers/load.py index fa5fdbd..ab1bc43 100644 --- a/resolvers/load.py +++ b/resolvers/load.py @@ -28,6 +28,7 @@ async def load_messages( chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None ): """load :limit messages for :chat_id with :offset""" + logger.info("load_messages") messages = [] try: message_ids = [] + (ids or []) @@ -63,6 +64,7 @@ async def load_messages( @login_required async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Union[List[Dict[str, Any]], None]]: """load :limit chats of current user with :offset""" + logger.info("load_chats") author_id = info.context["author_id"] chats = [] try: diff --git a/services/auth.py b/services/auth.py index 8153f00..d1117aa 100644 --- a/services/auth.py +++ b/services/auth.py @@ -12,29 +12,30 @@ logger.setLevel(logging.DEBUG) async def check_auth(req) -> str | None: - token = req.headers.get("Authorization") + logger.debug("checking auth...") user_id = "" - if token: - # Logging the authentication token - query_name = "validate_jwt_token" - operation = "ValidateToken" - headers = { - "Content-Type": "application/json", - } - - variables = { - "params": { - "token_type": "access_token", - "token": token, + try: + token = req.headers.get("Authorization") + if token: + # Logging the authentication token + query_name = "validate_jwt_token" + operation = "ValidateToken" + headers = { + "Content-Type": "application/json", } - } - gql = { - "query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}", - "variables": variables, - "operationName": operation, - } - try: + variables = { + "params": { + "token_type": "access_token", + "token": token, + } + } + + gql = { + "query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}", + "variables": variables, + "operationName": operation, + } # Asynchronous HTTP request to the authentication server async with ClientSession() as session: async with session.post(AUTH_URL, json=gql, headers=headers) as response: @@ -47,9 +48,9 @@ async def check_auth(req) -> str | None: user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub") logger.info(f"[services.auth] got user_id: {user_id}") return user_id - except Exception as e: - # Handling and logging exceptions during authentication check - logger.error(e) + except Exception as e: + # Handling and logging exceptions during authentication check + logger.error(e) if not user_id: raise HTTPException(status_code=401, detail="Unauthorized") @@ -67,6 +68,9 @@ def login_required(f): author = get_author_by_user(user_id) if author and "id" in author: context["author_id"] = author["id"] + else: + logger.debug(author) + HTTPException(status_code=401, detail="Unauthorized") return await f(*args, **kwargs) return decorated_function