ForcePilot/backend/package/yuxi/channel/interfaces/rest/auth/depends.py

30 lines
849 B
Python
Raw Normal View History

from __future__ import annotations
import logging
from fastapi import Depends, Header, HTTPException, Query
from yuxi.channel.container import ChannelContainer, get_channel
logger = logging.getLogger(__name__)
async def channel_auth_depends(
authorization: str | None = Header(None),
token_query: str | None = Query(None, alias="token"),
channel: ChannelContainer = Depends(get_channel),
) -> bool:
auth_value = authorization or (f"Bearer {token_query}" if token_query else None)
passed, reason = await channel.auth_service.authenticate(
auth_value or "",
client_id="mgmt",
)
if not passed:
if "rate limited" in reason:
raise HTTPException(status_code=429, detail=reason)
raise HTTPException(status_code=401, detail=reason or "authorization required")
return True