diff --git a/main.py b/main.py index 0c4b5ee..2283d84 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,6 @@ from sentry_sdk.integrations.strawberry import StrawberryIntegration from strawberry.asgi import GraphQL from starlette.applications import Starlette -from services.auth import TokenMiddleware from services.rediscache import redis from resolvers.listener import reactions_worker from resolvers.schema import schema @@ -50,5 +49,4 @@ async def shutdown(): app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown]) -app.add_middleware(TokenMiddleware) app.mount("/", GraphQL(schema, debug=True)) diff --git a/resolvers/schema.py b/resolvers/schema.py index ea6aa6f..669c6ac 100644 --- a/resolvers/schema.py +++ b/resolvers/schema.py @@ -6,7 +6,6 @@ from sqlalchemy.exc import SQLAlchemyError from orm.author import Author from orm.notification import Notification as NotificationMessage, NotificationSeen from services.auth import check_auth -from aiohttp.web import HTTPUnauthorized from services.db import local_session import strawberry from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper @@ -141,16 +140,14 @@ class LoginRequiredMiddleware(Extension): context = self.execution_context.context req = context.get("request") is_authenticated, user_id = await check_auth(req) - if not is_authenticated: - raise HTTPUnauthorized(text="Please, login first") - else: + if is_authenticated: with local_session() as session: author = session.query(Author).filter(Author.user == user_id).first() if author: context["author_id"] = author.id if user_id: context["user_id"] = user_id - context["user_id"] = user_id + context["user_id"] = user_id or None schema = strawberry.Schema( diff --git a/services/auth.py b/services/auth.py index 67bd246..010287f 100644 --- a/services/auth.py +++ b/services/auth.py @@ -1,9 +1,5 @@ -from functools import wraps import aiohttp from aiohttp.web import HTTPUnauthorized - -from orm.author import Author -from services.db import local_session from settings import AUTH_URL @@ -62,27 +58,6 @@ async def check_auth(req) -> (bool, int | None): except Exception as e: # Handling and logging exceptions during authentication check print(f"[services.auth] {e}") + raise HTTPUnauthorized(message="Please, login first") return False, None - - -def login_required(f): - @wraps(f) - async def decorated_function(*args, **kwargs): - info = args[1] - context = info.context - req = context.get("request") - is_authenticated, user_id = await check_auth(req) - if not is_authenticated: - raise HTTPUnauthorized(text="Please, login first") - else: - with local_session() as session: - author = session.query(Author).filter(Author.user == user_id).first() - if author: - context["author_id"] = author.id - if user_id: - context["user_id"] = user_id - - return await f(*args, **kwargs) - - return decorated_function