query-fix

This commit is contained in:
Untone 2023-10-11 21:19:44 +03:00
parent bcca1624d7
commit ddda4c025f

View File

@ -1,3 +1,4 @@
import json
from functools import wraps
from httpx import AsyncClient, HTTPError
from settings import AUTH_URL
@ -9,21 +10,32 @@ INTERNAL_AUTH_SERVER = "v2.discours" in AUTH_URL or "testapi.discours" in AUTH_U
async def check_auth(req):
token = req.headers.get("Authorization")
print(f"[services.auth] checking auth token: {token}")
gql = ("mutation { getSession { user { id } } }"
if INTERNAL_AUTH_SERVER
else "query { session { user { id } } }"
)
headers = {"Authorization": token, "Content-Type": "application/json"}
query_name = "getSession" if INTERNAL_AUTH_SERVER else "session"
query_type = "mutation" if INTERNAL_AUTH_SERVER else "query"
operation = "GetUserId"
headers = {
"Authorization": token,
"Content-Type": "application/json"
}
gql = {
"query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }",
"operationName": operation,
"variables": "{}"
}
async with AsyncClient() as client:
response = await client.post(AUTH_URL, headers=headers, data=gql)
response = await client.post(AUTH_URL, headers=headers, data=json.dumps(gql))
print(f"{response.text}")
if response.status_code != 200:
return False, None
r = response.json()
user_id = (
r.get("data", {}).get("getSession", {}).get("user", {}).get("id", None)
r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
if INTERNAL_AUTH_SERVER
else r.get("data", {}).get("session", {}).get("user", {}).get("id", None)
else r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
)
is_authenticated = user_id is not None
return is_authenticated, user_id