This commit is contained in:
parent
b1b7bf4dc2
commit
284a69085f
|
@ -1,20 +1,21 @@
|
|||
from functools import wraps
|
||||
|
||||
import aiohttp
|
||||
from aiohttp.web import HTTPUnauthorized
|
||||
from aiohttp import ClientSession
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from models.member import ChatMember
|
||||
from services.core import get_author
|
||||
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
async def check_auth(req) -> (bool, int | None):
|
||||
async def check_auth(req) -> str | None:
|
||||
token = req.headers.get("Authorization")
|
||||
user_id = ""
|
||||
if token:
|
||||
# Logging the authentication token
|
||||
print(f"[services.auth] checking auth token: {token}")
|
||||
query_name = "validate_jwt_token"
|
||||
opeation = "ValidateToken"
|
||||
operation = "ValidateToken"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
@ -27,13 +28,13 @@ async def check_auth(req) -> (bool, int | None):
|
|||
}
|
||||
|
||||
gql = {
|
||||
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||
"variables": variables,
|
||||
"operationName": opeation,
|
||||
"operationName": operation,
|
||||
}
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
# Asynchronous HTTP request to the authentication server
|
||||
async with ClientSession() as session:
|
||||
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
|
@ -42,15 +43,13 @@ async def check_auth(req) -> (bool, int | None):
|
|||
print(f"[services.auth] errors: {errors}")
|
||||
else:
|
||||
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
||||
return bool(user_id), user_id
|
||||
|
||||
return user_id
|
||||
except Exception as e:
|
||||
import traceback
|
||||
# Handling and logging exceptions during authentication check
|
||||
print(f"[services.auth] {e}")
|
||||
|
||||
traceback.print_exc()
|
||||
print(f"[services.auth] check_auth error: {e}")
|
||||
|
||||
return False, None
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401,detail="Unauthorized")
|
||||
|
||||
|
||||
def login_required(f):
|
||||
|
@ -59,19 +58,12 @@ def login_required(f):
|
|||
info = args[1]
|
||||
context = info.context
|
||||
req = context.get("request")
|
||||
print(req)
|
||||
is_authenticated, user_id = await check_auth(req)
|
||||
if not is_authenticated:
|
||||
# Raising HTTPUnauthorized exception if the user is not authenticated
|
||||
raise HTTPUnauthorized(text="Please, login first")
|
||||
else:
|
||||
# Добавляем author_id и user_id в контекст
|
||||
user_id = await check_auth(req)
|
||||
if user_id:
|
||||
context["user_id"] = user_id
|
||||
author: ChatMember | None = await get_author(user_id)
|
||||
if author:
|
||||
context["author_id"] = author["id"]
|
||||
context["user_id"] = user_id
|
||||
|
||||
# Если пользователь аутентифицирован, выполняем резолвер
|
||||
return await f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
|
Loading…
Reference in New Issue
Block a user