query-fix
This commit is contained in:
parent
bcca1624d7
commit
ddda4c025f
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from httpx import AsyncClient, HTTPError
|
from httpx import AsyncClient, HTTPError
|
||||||
from settings import AUTH_URL
|
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):
|
async def check_auth(req):
|
||||||
token = req.headers.get("Authorization")
|
token = req.headers.get("Authorization")
|
||||||
print(f"[services.auth] checking auth token: {token}")
|
print(f"[services.auth] checking auth token: {token}")
|
||||||
gql = ("mutation { getSession { user { id } } }"
|
|
||||||
if INTERNAL_AUTH_SERVER
|
query_name = "getSession" if INTERNAL_AUTH_SERVER else "session"
|
||||||
else "query { session { user { id } } }"
|
query_type = "mutation" if INTERNAL_AUTH_SERVER else "query"
|
||||||
)
|
operation = "GetUserId"
|
||||||
headers = {"Authorization": token, "Content-Type": "application/json"}
|
|
||||||
|
headers = {
|
||||||
|
"Authorization": token,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
gql = {
|
||||||
|
"query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }",
|
||||||
|
"operationName": operation,
|
||||||
|
"variables": "{}"
|
||||||
|
}
|
||||||
|
|
||||||
async with AsyncClient() as client:
|
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}")
|
print(f"{response.text}")
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return False, None
|
return False, None
|
||||||
r = response.json()
|
r = response.json()
|
||||||
user_id = (
|
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
|
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
|
is_authenticated = user_id is not None
|
||||||
return is_authenticated, user_id
|
return is_authenticated, user_id
|
||||||
|
|
Loading…
Reference in New Issue
Block a user