0.1.0-fixes
All checks were successful
deploy / deploy (push) Successful in 1m11s

This commit is contained in:
2023-12-22 12:09:03 +03:00
parent 5c6a680832
commit e22d5468ab
12 changed files with 340 additions and 171 deletions

View File

@@ -1,4 +1,3 @@
from functools import wraps
from aiohttp import ClientSession
from starlette.exceptions import HTTPException
from strawberry.extensions import Extension
@@ -7,12 +6,13 @@ from settings import AUTH_URL
from services.db import local_session
from orm.author import Author
async def check_auth(req) -> str | None:
token = req.headers.get("Authorization")
user_id = ""
if token:
# Logging the authentication token
print(f"[services.auth] checking auth token: {token}")
# print(f"[services.auth] checking auth token: {token}")
query_name = "validate_jwt_token"
operation = "ValidateToken"
headers = {
@@ -42,11 +42,16 @@ async def check_auth(req) -> str | None:
print(f"[services.auth] errors: {errors}")
else:
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
return user_id
if user_id:
print(f"[services.auth] got user_id: {user_id}")
return user_id
except Exception as e:
# Handling and logging exceptions during authentication check
print(f"[services.auth] {e}")
if not user_id:
raise HTTPException(status_code=401, detail="Unathorized")
class LoginRequiredMiddleware(Extension):
async def on_request_start(self):
@@ -60,3 +65,5 @@ class LoginRequiredMiddleware(Extension):
if author:
context["author_id"] = author.id
context["user_id"] = user_id or None
self.execution_context.context = context