This commit is contained in:
@@ -95,9 +95,10 @@ async def get_auth_token(request: Any) -> Optional[str]:
|
||||
# 2. Проверяем наличие auth_token в scope (приоритет)
|
||||
if hasattr(request, "scope") and isinstance(request.scope, dict) and "auth_token" in request.scope:
|
||||
token = request.scope.get("auth_token")
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Токен получен из request.scope['auth_token']: {token_len}")
|
||||
return token
|
||||
if token is not None:
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Токен получен из request.scope['auth_token']: {token_len}")
|
||||
return token
|
||||
logger.debug("[decorators] request.scope['auth_token'] НЕ найден")
|
||||
|
||||
# Стандартная система сессий уже обрабатывает кэширование
|
||||
@@ -147,10 +148,11 @@ async def get_auth_token(request: Any) -> Optional[str]:
|
||||
if hasattr(request, "scope") and isinstance(request.scope, dict) and "auth" in request.scope:
|
||||
auth_info = request.scope.get("auth", {})
|
||||
if isinstance(auth_info, dict) and "token" in auth_info:
|
||||
token = auth_info["token"]
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Токен получен из request.scope['auth']: {token_len}")
|
||||
return token
|
||||
token = auth_info.get("token")
|
||||
if token is not None:
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Токен получен из request.scope['auth']: {token_len}")
|
||||
return token
|
||||
|
||||
# 4. Проверяем заголовок Authorization
|
||||
headers = get_safe_headers(request)
|
||||
@@ -164,18 +166,20 @@ async def get_auth_token(request: Any) -> Optional[str]:
|
||||
logger.debug(f"[decorators] Токен получен из заголовка {SESSION_TOKEN_HEADER}: {token_len}")
|
||||
return token
|
||||
token = auth_header.strip()
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Прямой токен получен из заголовка {SESSION_TOKEN_HEADER}: {token_len}")
|
||||
return token
|
||||
if token:
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Прямой токен получен из заголовка {SESSION_TOKEN_HEADER}: {token_len}")
|
||||
return token
|
||||
|
||||
# Затем проверяем стандартный заголовок Authorization, если основной не определен
|
||||
if SESSION_TOKEN_HEADER.lower() != "authorization":
|
||||
auth_header = headers.get("authorization", "")
|
||||
if auth_header and auth_header.startswith("Bearer "):
|
||||
token = auth_header[7:].strip()
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Токен получен из заголовка Authorization: {token_len}")
|
||||
return token
|
||||
if token:
|
||||
token_len = len(token) if hasattr(token, "__len__") else "unknown"
|
||||
logger.debug(f"[decorators] Токен получен из заголовка Authorization: {token_len}")
|
||||
return token
|
||||
|
||||
# 5. Проверяем cookie
|
||||
if hasattr(request, "cookies") and request.cookies:
|
||||
@@ -232,14 +236,15 @@ async def validate_graphql_context(info: GraphQLResolveInfo) -> None:
|
||||
return
|
||||
|
||||
# Если аутентификации нет в request.auth, пробуем получить ее из scope
|
||||
token: Optional[str] = None
|
||||
if hasattr(request, "scope") and "auth" in request.scope:
|
||||
auth_cred = request.scope.get("auth")
|
||||
if isinstance(auth_cred, AuthCredentials) and getattr(auth_cred, "logged_in", False):
|
||||
logger.debug(f"[validate_graphql_context] Пользователь авторизован через scope: {auth_cred.author_id}")
|
||||
return
|
||||
|
||||
# Если авторизации нет ни в auth, ни в scope, пробуем получить и проверить токен
|
||||
token = await get_auth_token(request)
|
||||
# Если авторизации нет ни в auth, ни в scope, пробуем получить и проверить токен
|
||||
token = await get_auth_token(request)
|
||||
if not token:
|
||||
# Если токен не найден, логируем как предупреждение, но не бросаем GraphQLError
|
||||
client_info = {
|
||||
@@ -261,7 +266,8 @@ async def validate_graphql_context(info: GraphQLResolveInfo) -> None:
|
||||
return
|
||||
|
||||
# Логируем информацию о найденном токене
|
||||
logger.debug(f"[validate_graphql_context] Токен найден, длина: {len(token)}")
|
||||
token_len = len(token) if hasattr(token, "__len__") else 0
|
||||
logger.debug(f"[validate_graphql_context] Токен найден, длина: {token_len}")
|
||||
|
||||
# Используем единый механизм проверки токена из auth.internal
|
||||
auth_state = await authenticate(request)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import Any
|
||||
|
||||
from ariadne.asgi.handlers import GraphQLHTTPHandler
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
@@ -62,20 +64,22 @@ class EnhancedGraphQLHTTPHandler(GraphQLHTTPHandler):
|
||||
# Добавляем данные авторизации только если они доступны
|
||||
# Проверяем наличие данных авторизации в scope
|
||||
if hasattr(request, "scope") and isinstance(request.scope, dict) and "auth" in request.scope:
|
||||
auth_cred = request.scope.get("auth")
|
||||
auth_cred: Any | None = request.scope.get("auth")
|
||||
context["auth"] = auth_cred
|
||||
# Безопасно логируем информацию о типе объекта auth
|
||||
logger.debug(f"[graphql] Добавлены данные авторизации в контекст из scope: {type(auth_cred).__name__}")
|
||||
|
||||
# Проверяем, есть ли токен в auth_cred
|
||||
if hasattr(auth_cred, "token") and auth_cred.token:
|
||||
logger.debug(f"[graphql] Токен найден в auth_cred: {len(auth_cred.token)}")
|
||||
if auth_cred is not None and hasattr(auth_cred, "token") and getattr(auth_cred, "token"):
|
||||
token_val = auth_cred.token
|
||||
token_len = len(token_val) if hasattr(token_val, "__len__") else 0
|
||||
logger.debug(f"[graphql] Токен найден в auth_cred: {token_len}")
|
||||
else:
|
||||
logger.debug("[graphql] Токен НЕ найден в auth_cred")
|
||||
|
||||
# Добавляем author_id в контекст для RBAC
|
||||
author_id = None
|
||||
if hasattr(auth_cred, "author_id") and auth_cred.author_id:
|
||||
if auth_cred is not None and hasattr(auth_cred, "author_id") and getattr(auth_cred, "author_id"):
|
||||
author_id = auth_cred.author_id
|
||||
elif isinstance(auth_cred, dict) and "author_id" in auth_cred:
|
||||
author_id = auth_cred["author_id"]
|
||||
|
||||
@@ -135,7 +135,7 @@ class AuthMiddleware:
|
||||
|
||||
# Роли пользователя будут определяться в контексте конкретной операции
|
||||
# через RBAC систему, а не здесь
|
||||
roles = []
|
||||
roles: list[str] = []
|
||||
|
||||
# Обновляем last_seen
|
||||
author.last_seen = int(time.time())
|
||||
|
||||
@@ -55,12 +55,16 @@ class BatchTokenOperations(BaseTokenManager):
|
||||
valid_tokens = []
|
||||
|
||||
for token, payload in zip(token_batch, decoded_payloads):
|
||||
if isinstance(payload, Exception) or not payload:
|
||||
if isinstance(payload, Exception) or payload is None:
|
||||
results[token] = False
|
||||
continue
|
||||
|
||||
# payload может быть словарем или объектом, обрабатываем оба случая
|
||||
user_id = payload.user_id if hasattr(payload, "user_id") else payload.get("user_id")
|
||||
user_id = (
|
||||
payload.user_id
|
||||
if hasattr(payload, "user_id")
|
||||
else (payload.get("user_id") if isinstance(payload, dict) else None)
|
||||
)
|
||||
if not user_id:
|
||||
results[token] = False
|
||||
continue
|
||||
@@ -119,10 +123,18 @@ class BatchTokenOperations(BaseTokenManager):
|
||||
# Декодируем токены и подготавливаем операции
|
||||
for token in token_batch:
|
||||
payload = await self._safe_decode_token(token)
|
||||
if payload:
|
||||
if payload is not None:
|
||||
# payload может быть словарем или объектом, обрабатываем оба случая
|
||||
user_id = payload.user_id if hasattr(payload, "user_id") else payload.get("user_id")
|
||||
username = payload.username if hasattr(payload, "username") else payload.get("username")
|
||||
user_id = (
|
||||
payload.user_id
|
||||
if hasattr(payload, "user_id")
|
||||
else (payload.get("user_id") if isinstance(payload, dict) else None)
|
||||
)
|
||||
username = (
|
||||
payload.username
|
||||
if hasattr(payload, "username")
|
||||
else (payload.get("username") if isinstance(payload, dict) else None)
|
||||
)
|
||||
|
||||
if not user_id:
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user