build-fix

This commit is contained in:
Tony Rewin 2023-10-04 20:21:04 +03:00
parent 1360692b57
commit 3a02999e4f
2 changed files with 12 additions and 14 deletions

View File

@ -6,7 +6,7 @@ sqlalchemy~=2.0.21
graphql-core
gql
uvicorn~=0.23.2
aiohttp~=3.8.5
httpx
itsdangerous
pydantic~=2.4.2
psycopg2-binary

View File

@ -1,11 +1,11 @@
from typing import Optional
from aiohttp.web import HTTPUnauthorized
from aiohttp.client import ClientSession
from pydantic import BaseModel
from functools import wraps
from starlette.authentication import AuthenticationBackend
from starlette.requests import HTTPConnection
from httpx import AsyncClient
from httpx._exceptions import HTTPError
from services.db import local_session
from settings import AUTH_URL
from orm.author import Author
@ -40,14 +40,12 @@ async def check_auth(req):
else {"query": "{ session { user { id } } }"}
)
headers = {"Authorization": token, "Content-Type": "application/json"}
async with ClientSession(headers=headers) as session:
async with session.post(AUTH_URL, data=gql) as response:
if response.status != 200:
async with AsyncClient() as client:
response = await client.post(AUTH_URL, headers=headers, data=gql)
if response.status_code != 200:
return False, None
r = await response.json()
user_id = (
r.get("data", {}).get("session", {}).get("user", {}).get("id", None)
)
r = response.json()
user_id = r.get("data", {}).get("session", {}).get("user", {}).get("id", None)
is_authenticated = user_id is not None
return is_authenticated, user_id
@ -84,7 +82,7 @@ def auth_request(f):
req = args[0]
is_authenticated, user_id = await check_auth(req)
if not is_authenticated:
raise HTTPUnauthorized()
raise HTTPError("please, login first")
else:
author_id = await author_id_by_user_id(user_id)
req["author_id"] = author_id