From 3a02999e4f2b89af8cc8145c980207e47a9badfa Mon Sep 17 00:00:00 2001 From: Tony Rewin Date: Wed, 4 Oct 2023 20:21:04 +0300 Subject: [PATCH] build-fix --- requirements.txt | 2 +- services/auth.py | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/requirements.txt b/requirements.txt index 6255926..872a2aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/services/auth.py b/services/auth.py index 96ee73e..16c1046 100644 --- a/services/auth.py +++ b/services/auth.py @@ -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,16 +40,14 @@ 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: - return False, None - r = await 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 + async with AsyncClient() as client: + response = await client.post(AUTH_URL, headers=headers, data=gql) + if response.status_code != 200: + return False, 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 async def author_id_by_user_id(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