auth-connector-fix
Some checks failed
deploy / deploy (push) Failing after 1m4s

This commit is contained in:
2023-12-17 09:43:45 +03:00
parent 1ea9332a98
commit f061d8a523
2 changed files with 28 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
from functools import wraps
import aiohttp
from aiohttp.web import HTTPUnauthorized
from orm.author import Author
from services.db import local_session
from settings import AUTH_URL
@@ -9,9 +10,10 @@ from settings import AUTH_URL
async def check_auth(req) -> (bool, int | None):
token = req.headers.get("Authorization")
if token:
# Logging the authentication token
print(f"[services.auth] checking auth token: {token}")
query_name = "validate_jwt_token"
operation = "ValidateToken"
opeation = "ValidateToken"
headers = {
"Content-Type": "application/json",
}
@@ -24,18 +26,21 @@ async def check_auth(req) -> (bool, int | None):
}
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,
"operationName": operation,
"operationName": opeation,
}
print(f"[services.auth] Graphql: {gql}")
# 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:
@@ -44,14 +49,18 @@ async def check_auth(req) -> (bool, int | None):
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