This commit is contained in:
parent
cc02711ab3
commit
bb48a8ef11
|
@ -1,35 +1,67 @@
|
||||||
|
from services.core import get_author
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from aiohttp.web import HTTPUnauthorized
|
||||||
|
|
||||||
from services.core import get_author
|
|
||||||
from settings import AUTH_URL
|
from settings import AUTH_URL
|
||||||
|
|
||||||
|
|
||||||
async def check_auth(req):
|
async def check_auth(req) -> (bool, int | None):
|
||||||
token = req.headers.get("Authorization")
|
token = req.headers.get("Authorization")
|
||||||
headers = {"Authorization": token, "Content-Type": "application/json"} # "Bearer " + removed
|
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 = "session"
|
opeation = "ValidateToken"
|
||||||
query_type = "query"
|
headers = {
|
||||||
operation = "GetUserId"
|
"Content-Type": "application/json",
|
||||||
|
|
||||||
gql = {
|
|
||||||
"query": query_type + " " + operation + " { " + query_name + " { user { id } } }",
|
|
||||||
"operationName": operation,
|
|
||||||
"variables": None,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30.0)) as session:
|
variables = {
|
||||||
async with session.post(AUTH_URL, headers=headers, json=gql) as response:
|
"params": {
|
||||||
print(f"[services.auth] {AUTH_URL} response: {response.status}")
|
"token_type": "access_token",
|
||||||
if response.status != 200:
|
"token": token.encode("utf-8"),
|
||||||
return False, None
|
}
|
||||||
r = await response.json()
|
}
|
||||||
if r:
|
|
||||||
user_id = r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
|
gql = {
|
||||||
is_authenticated = user_id is not None
|
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||||
return is_authenticated, user_id
|
"variables": variables,
|
||||||
|
"operationName": opeation,
|
||||||
|
}
|
||||||
|
print(f"[services.auth] Graphql: {gql}")
|
||||||
|
try:
|
||||||
|
# Asynchronous HTTP request to the authentication server
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
||||||
|
# Logging the GraphQL response
|
||||||
|
response_text = await response.text()
|
||||||
|
print(f"[services.auth] GraphQL Response: {response_text}")
|
||||||
|
|
||||||
|
if response.status == 200:
|
||||||
|
# Parsing JSON response
|
||||||
|
data = await response.json()
|
||||||
|
errors = data.get("errors")
|
||||||
|
if errors:
|
||||||
|
print(f"[services.auth] errors: {errors}")
|
||||||
|
else:
|
||||||
|
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
||||||
|
|
||||||
|
if user_id:
|
||||||
|
# Logging the retrieved user ID
|
||||||
|
print(f"[services.auth] User ID retrieved: {user_id}")
|
||||||
|
return True, user_id
|
||||||
|
else:
|
||||||
|
# Logging when no user ID is found in the response
|
||||||
|
print("[services.auth] No user ID found in the response")
|
||||||
|
else:
|
||||||
|
# Logging when the request to the authentication server fails
|
||||||
|
print(f"[services.auth] Request failed with status: {response.status}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# Handling and logging exceptions during authentication check
|
||||||
|
print(f"[services.auth] {e}")
|
||||||
|
|
||||||
return False, None
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +73,8 @@ def login_required(f):
|
||||||
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:
|
||||||
raise Exception("You are not logged in")
|
# Raising HTTPUnauthorized exception if the user is not authenticated
|
||||||
|
raise HTTPUnauthorized(text="Please, login first")
|
||||||
else:
|
else:
|
||||||
# Добавляем author_id и user_id в контекст
|
# Добавляем author_id и user_id в контекст
|
||||||
context["author_id"] = await get_author(user_id)
|
context["author_id"] = await get_author(user_id)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user