This commit is contained in:
@@ -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
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from typing import Any, List
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
@@ -11,7 +11,7 @@ api_base = API_BASE or "https://core.discours.io"
|
||||
async def _request_endpoint(query_name, body) -> Any:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(API_BASE, headers=headers, json=body) as response:
|
||||
print(f"[services.core] {query_name} response: <{response.status}> {await response.text()}")
|
||||
print(f"[services.core] {query_name} HTTP Response {response.status} {await response.text()}")
|
||||
if response.status == 200:
|
||||
r = await response.json()
|
||||
if r:
|
||||
@@ -27,10 +27,23 @@ async def get_followed_shouts(author_id: int):
|
||||
{query_name}(author_id: $author_id, limit: $limit, offset: $offset) {{ id slug title }}
|
||||
}}"""
|
||||
|
||||
body = {
|
||||
gql = {
|
||||
"query": query,
|
||||
"operationName": operation,
|
||||
"variables": {"author_id": author_id, "limit": 1000, "offset": 0}, # FIXME: too big limit
|
||||
}
|
||||
|
||||
return await _request_endpoint(query_name, body)
|
||||
return await _request_endpoint(query_name, gql)
|
||||
|
||||
|
||||
async def get_shout(shout_id):
|
||||
query_name = "get_shout"
|
||||
operation = "GetShout"
|
||||
|
||||
query = f"""query {operation}($slug: String, $shout_id: Int) {{
|
||||
{query_name}(slug: $slug, shout_id: $shout_id) {{ id slug title authors {{ id slug name pic }} }}
|
||||
}}"""
|
||||
|
||||
gql = {"query": query, "operationName": operation, "variables": {"slug": None, "shout_id": shout_id}}
|
||||
|
||||
return await _request_endpoint(query_name, gql)
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import redis.asyncio as aredis
|
||||
@@ -49,19 +48,19 @@ class RedisCache:
|
||||
return
|
||||
await self._client.publish(channel, data)
|
||||
|
||||
async def listen(self, channel):
|
||||
async def listen(self, pattern):
|
||||
if self._client:
|
||||
pubsub = self._client.pubsub()
|
||||
await pubsub.subscribe(channel)
|
||||
await pubsub.psubscribe(pattern)
|
||||
|
||||
while True:
|
||||
message = await pubsub.get_message()
|
||||
if message and isinstance(message["data"], (str, bytes, bytearray)):
|
||||
print(f"[services.rediscache] msg: {message}")
|
||||
try:
|
||||
yield json.loads(message["data"])
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error decoding JSON: {e}")
|
||||
await asyncio.sleep(0.1)
|
||||
yield json.loads(message["data"]), message.get("channel")
|
||||
except Exception as e:
|
||||
print(f"[servoces.rediscache] Error: {e}")
|
||||
|
||||
|
||||
redis = RedisCache()
|
||||
|
Reference in New Issue
Block a user