This commit is contained in:
parent
1ea9332a98
commit
f061d8a523
|
@ -8,9 +8,13 @@ from services.db import local_session
|
||||||
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
|
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
|
||||||
import strawberry
|
import strawberry
|
||||||
from strawberry.schema.config import StrawberryConfig
|
from strawberry.schema.config import StrawberryConfig
|
||||||
|
import logging
|
||||||
|
|
||||||
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
|
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
|
||||||
|
|
||||||
|
# Инициализация логгера
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@strawberry_sqlalchemy_mapper.type(NotificationMessage)
|
@strawberry_sqlalchemy_mapper.type(NotificationMessage)
|
||||||
class Notification:
|
class Notification:
|
||||||
|
@ -81,8 +85,11 @@ class Query:
|
||||||
total=session.query(NotificationMessage).count(),
|
total=session.query(NotificationMessage).count(),
|
||||||
)
|
)
|
||||||
return nr
|
return nr
|
||||||
except SQLAlchemyError as ex:
|
except Exception as ex:
|
||||||
print(f"[resolvers.schema] {ex}")
|
import traceback
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
|
logger.error(f"[load_notifications] Ошибка при выполнении запроса к базе данных: {ex}")
|
||||||
return NotificationsResult(notifications=[], total=0, unread=0)
|
return NotificationsResult(notifications=[], total=0, unread=0)
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,7 +107,9 @@ class Mutation:
|
||||||
session.commit()
|
session.commit()
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
print(f"[mark_notification_as_read] error: {str(e)}")
|
logger.error(
|
||||||
|
f"[mark_notification_as_read] Ошибка при обновлении статуса прочтения уведомления: {str(e)}"
|
||||||
|
)
|
||||||
return NotificationSeenResult(error="cant mark as read")
|
return NotificationSeenResult(error="cant mark as read")
|
||||||
return NotificationSeenResult()
|
return NotificationSeenResult()
|
||||||
|
|
||||||
|
@ -119,7 +128,9 @@ class Mutation:
|
||||||
session.commit()
|
session.commit()
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
print(f"[mark_all_notifications_as_read] error: {str(e)}")
|
logger.error(
|
||||||
|
f"[mark_all_notifications_as_read] Ошибка при обновлении статуса прочтения всех уведомлений: {str(e)}"
|
||||||
|
)
|
||||||
return NotificationSeenResult(error="cant mark as read")
|
return NotificationSeenResult(error="cant mark as read")
|
||||||
return NotificationSeenResult()
|
return NotificationSeenResult()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp.web import HTTPUnauthorized
|
from aiohttp.web import HTTPUnauthorized
|
||||||
|
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from settings import AUTH_URL
|
from settings import AUTH_URL
|
||||||
|
@ -9,9 +10,10 @@ from settings import AUTH_URL
|
||||||
async def check_auth(req) -> (bool, int | None):
|
async def check_auth(req) -> (bool, int | None):
|
||||||
token = req.headers.get("Authorization")
|
token = req.headers.get("Authorization")
|
||||||
if token:
|
if token:
|
||||||
|
# Logging the authentication token
|
||||||
print(f"[services.auth] checking auth token: {token}")
|
print(f"[services.auth] checking auth token: {token}")
|
||||||
query_name = "validate_jwt_token"
|
query_name = "validate_jwt_token"
|
||||||
operation = "ValidateToken"
|
opeation = "ValidateToken"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
@ -24,18 +26,21 @@ async def check_auth(req) -> (bool, int | None):
|
||||||
}
|
}
|
||||||
|
|
||||||
gql = {
|
gql = {
|
||||||
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||||
"variables": variables,
|
"variables": variables,
|
||||||
"operationName": operation,
|
"operationName": opeation,
|
||||||
}
|
}
|
||||||
print(f"[services.auth] Graphql: {gql}")
|
# print(f"[services.auth] Graphql: {gql}")
|
||||||
try:
|
try:
|
||||||
|
# Asynchronous HTTP request to the authentication server
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
||||||
|
# Logging the GraphQL response
|
||||||
response_text = await response.text()
|
response_text = await response.text()
|
||||||
print(f"[services.auth] GraphQL Response: {response_text}")
|
print(f"[services.auth] GraphQL Response: {response_text}")
|
||||||
|
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
|
# Parsing JSON response
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
errors = data.get("errors")
|
errors = data.get("errors")
|
||||||
if errors:
|
if errors:
|
||||||
|
@ -44,14 +49,18 @@ async def check_auth(req) -> (bool, int | None):
|
||||||
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
|
# Logging the retrieved user ID
|
||||||
print(f"[services.auth] User ID retrieved: {user_id}")
|
print(f"[services.auth] User ID retrieved: {user_id}")
|
||||||
return True, user_id
|
return True, user_id
|
||||||
else:
|
else:
|
||||||
|
# Logging when no user ID is found in the response
|
||||||
print("[services.auth] No user ID found in the response")
|
print("[services.auth] No user ID found in the response")
|
||||||
else:
|
else:
|
||||||
|
# Logging when the request to the authentication server fails
|
||||||
print(f"[services.auth] Request failed with status: {response.status}")
|
print(f"[services.auth] Request failed with status: {response.status}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# Handling and logging exceptions during authentication check
|
||||||
print(f"[services.auth] {e}")
|
print(f"[services.auth] {e}")
|
||||||
|
|
||||||
return False, None
|
return False, None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user