This commit is contained in:
parent
aaf30ec115
commit
1ea9332a98
6
main.py
6
main.py
|
@ -33,11 +33,7 @@ async def start_up():
|
||||||
SENTRY_DSN,
|
SENTRY_DSN,
|
||||||
enable_tracing=True,
|
enable_tracing=True,
|
||||||
integrations=[
|
integrations=[
|
||||||
StrawberryIntegration(
|
StrawberryIntegration(async_execution=True),
|
||||||
# Set async_execution to True if you have
|
|
||||||
# at least one async resolver
|
|
||||||
async_execution=True
|
|
||||||
),
|
|
||||||
SqlalchemyIntegration(),
|
SqlalchemyIntegration(),
|
||||||
RedisIntegration(),
|
RedisIntegration(),
|
||||||
AioHttpIntegration(),
|
AioHttpIntegration(),
|
||||||
|
|
|
@ -81,7 +81,7 @@ class Query:
|
||||||
total=session.query(NotificationMessage).count(),
|
total=session.query(NotificationMessage).count(),
|
||||||
)
|
)
|
||||||
return nr
|
return nr
|
||||||
except Exception as ex:
|
except SQLAlchemyError as ex:
|
||||||
print(f"[resolvers.schema] {ex}")
|
print(f"[resolvers.schema] {ex}")
|
||||||
return NotificationsResult(notifications=[], total=0, unread=0)
|
return NotificationsResult(notifications=[], total=0, unread=0)
|
||||||
|
|
||||||
|
@ -101,8 +101,7 @@ class Mutation:
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
print(f"[mark_notification_as_read] error: {str(e)}")
|
print(f"[mark_notification_as_read] error: {str(e)}")
|
||||||
nsr = NotificationSeenResult(error="cant mark as read")
|
return NotificationSeenResult(error="cant mark as read")
|
||||||
return nsr
|
|
||||||
return NotificationSeenResult()
|
return NotificationSeenResult()
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -121,8 +120,7 @@ class Mutation:
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
print(f"[mark_all_notifications_as_read] error: {str(e)}")
|
print(f"[mark_all_notifications_as_read] error: {str(e)}")
|
||||||
nsr = NotificationSeenResult(error="cant mark as read")
|
return NotificationSeenResult(error="cant mark as read")
|
||||||
return nsr
|
|
||||||
return NotificationSeenResult()
|
return NotificationSeenResult()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,14 @@
|
||||||
from functools import wraps
|
|
||||||
import aiohttp
|
|
||||||
|
|
||||||
from orm.author import Author
|
|
||||||
from services.db import local_session
|
|
||||||
from settings import AUTH_URL
|
|
||||||
|
|
||||||
|
|
||||||
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 services.db import local_session
|
||||||
from settings import AUTH_URL
|
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"
|
operation = "ValidateToken"
|
||||||
|
@ -38,15 +30,12 @@ async def check_auth(req) -> (bool, int | None):
|
||||||
}
|
}
|
||||||
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:
|
||||||
|
@ -55,18 +44,14 @@ 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
|
||||||
|
@ -75,16 +60,13 @@ async def check_auth(req) -> (bool, int | None):
|
||||||
def login_required(f):
|
def login_required(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
async def decorated_function(*args, **kwargs):
|
async def decorated_function(*args, **kwargs):
|
||||||
print(args)
|
|
||||||
info = args[1]
|
info = args[1]
|
||||||
context = info.context
|
context = info.context
|
||||||
req = context.get("request")
|
req = context.get("request")
|
||||||
is_authenticated, user_id = await check_auth(req)
|
is_authenticated, user_id = await check_auth(req)
|
||||||
if not is_authenticated:
|
if not is_authenticated:
|
||||||
# Raising HTTPUnauthorized exception if the user is not authenticated
|
|
||||||
raise HTTPUnauthorized(text="Please, login first")
|
raise HTTPUnauthorized(text="Please, login first")
|
||||||
else:
|
else:
|
||||||
# Добавляем author_id и user_id в контекст
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
author = session.query(Author).filter(Author.user == user_id).first()
|
author = session.query(Author).filter(Author.user == user_id).first()
|
||||||
if author:
|
if author:
|
||||||
|
@ -92,7 +74,6 @@ def login_required(f):
|
||||||
if user_id:
|
if user_id:
|
||||||
context["user_id"] = user_id
|
context["user_id"] = user_id
|
||||||
|
|
||||||
# Если пользователь аутентифицирован, выполняем резолвер
|
|
||||||
return await f(*args, **kwargs)
|
return await f(*args, **kwargs)
|
||||||
|
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
Loading…
Reference in New Issue
Block a user