From 954c3740cd65caf890db6e8b27def4526820dcb0 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 12 Dec 2023 08:00:46 +0300 Subject: [PATCH] authorizer-connector-fix-7 --- services/auth.py | 53 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/services/auth.py b/services/auth.py index 40383c9b..934f4a83 100644 --- a/services/auth.py +++ b/services/auth.py @@ -6,34 +6,35 @@ from settings import AUTH_URL async def check_auth(req): token = req.headers.get("Authorization") - print(f"[services.auth] checking auth token: {token}") + if token: + print(f"[services.auth] checking auth token: {token}") - headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"} - # query getSession($params: SessionQueryInput){ session(params: $params) { message user { id } } } - gql = { - "query": "query GetSession($params: SessionQueryInput)" - + "{ session(params: $params) { message user { id } } }", - "operationName": "GetSession", - "variables": {}, - } + headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"} + # query getSession($params: SessionQueryInput){ session(params: $params) { message user { id } } } + gql = { + "query": "query GetSession($params: SessionQueryInput)" + + "{ session(params: $params) { message user { id } } }", + "operationName": "GetSession", + "variables": {}, + } - async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30.0)) as session: - async with session.post(AUTH_URL, headers=headers, json=gql) as response: - if response.status != 200: - return False, None - r = await response.json() - print(f"[services.auth] response: {r}") - try: - data = r.get("data") - is_authenticated = False - user_id = None - if data: - user_id = data.get("session", {}).get("user", {}).get("id", None) - is_authenticated = user_id is not None - return is_authenticated, user_id - except Exception as e: - print(f"{e}: {r}") - return False, None + async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30.0)) as session: + async with session.post(AUTH_URL, headers=headers, json=gql) as response: + if response.status != 200: + return False, None + r = await response.json() + print(f"[services.auth] response: {r}") + try: + data = r.get("data") + is_authenticated = False + user_id = None + if data: + user_id = data.get("session", {}).get("user", {}).get("id", None) + is_authenticated = user_id is not None + return is_authenticated, user_id + except Exception as e: + print(f"{e}: {r}") + return False, None def login_required(f):