49 lines
1.0 KiB
Python
49 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import Request
|
|
|
|
from app.auth import hash_token, parse_bearer_token
|
|
from app.cache import get_cached_user
|
|
from app.errors import not_found, unauthorized
|
|
from app.store import store
|
|
from app.validation import parse_uuid
|
|
|
|
|
|
async def current_user(request: Request) -> tuple[dict, str]:
|
|
token = parse_bearer_token(
|
|
request.headers.get("authorization")
|
|
)
|
|
|
|
if token is None:
|
|
raise unauthorized()
|
|
|
|
token_digest = hash_token(token)
|
|
|
|
user_id = await store.get_session_user_id(token_digest)
|
|
|
|
if user_id is None:
|
|
raise unauthorized()
|
|
|
|
user = await get_cached_user(user_id)
|
|
|
|
if user is None:
|
|
raise unauthorized()
|
|
|
|
return user, token_digest
|
|
|
|
|
|
async def accessible_chat(
|
|
raw_chat_id: str,
|
|
user_id: UUID,
|
|
) -> tuple[UUID, dict]:
|
|
chat_id = parse_uuid(raw_chat_id)
|
|
|
|
chat = await store.get_accessible_chat(chat_id, user_id)
|
|
|
|
if chat is None:
|
|
raise not_found()
|
|
|
|
return chat_id, chat
|