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 graphql-core
gql gql
uvicorn~=0.23.2 uvicorn~=0.23.2
aiohttp~=3.8.5 httpx
itsdangerous itsdangerous
pydantic~=2.4.2 pydantic~=2.4.2
psycopg2-binary psycopg2-binary

View File

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