inbox-debug-7

This commit is contained in:
Untone 2024-01-24 15:26:16 +03:00
parent cccaf16817
commit 751d91562e
2 changed files with 29 additions and 23 deletions

View File

@ -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:

View File

@ -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