This commit is contained in:
2023-10-04 20:14:06 +03:00
parent 34b7d0021d
commit 1360692b57
6 changed files with 42 additions and 38 deletions

View File

@@ -1,10 +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 graphql.error import GraphQLError
from httpx import AsyncClient
from services.db import local_session
from settings import AUTH_URL
from orm.author import Author
@@ -24,19 +25,13 @@ class AuthCredentials(BaseModel):
class JWTAuthenticate(AuthenticationBackend):
async def authenticate(self, request: HTTPConnection):
scopes = {} # TODO: integrate await user.get_permission
logged_in, user_id = await check_auth(request)
return (
AuthCredentials(user_id=user_id, scopes=scopes, logged_in=logged_in),
AuthUser(user_id=user_id, username=""),
AuthCredentials(user_id=user_id, logged_in=logged_in),
AuthUser(user_id=user_id),
)
class Unauthorized(GraphQLError):
code = 401
message = "401 Unauthorized"
async def check_auth(req):
token = req.headers.get("Authorization")
gql = (
@@ -45,14 +40,16 @@ async def check_auth(req):
else {"query": "{ session { user { id } } }"}
)
headers = {"Authorization": token, "Content-Type": "application/json"}
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 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 def author_id_by_user_id(user_id):
@@ -87,7 +84,7 @@ def auth_request(f):
req = args[0]
is_authenticated, user_id = await check_auth(req)
if not is_authenticated:
raise Unauthorized("You are not logged in")
raise HTTPUnauthorized()
else:
author_id = await author_id_by_user_id(user_id)
req["author_id"] = author_id

11
services/presence.py Normal file
View File

@@ -0,0 +1,11 @@
import json
from redis import redis
async def notify_message(message, chat_id: str):
channel_name = f"chat:{chat_id}"
data = {**message, "kind": "new_message"}
try:
await redis.execute_pubsub("PUBLISH", channel_name, json.dumps(data))
except Exception as e:
print(f"Failed to publish to channel {channel_name}: {e}")