From ddda4c025f260d895fc7ef7a05c99ba03dd8e83c Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 11 Oct 2023 21:19:44 +0300 Subject: [PATCH] query-fix --- services/auth.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/services/auth.py b/services/auth.py index 6e9f8b2..840ee7f 100644 --- a/services/auth.py +++ b/services/auth.py @@ -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