Files
NJUPT-Suan-API/router/enhance/auth.py
MangoFanFanw b284c3c260 Python 后端提交
Python 后端(FastAPI + FastMCP + ...)的初始版本号设定为 0.1.0,这是 uv 在 pypriject.toml
里给我自动设置的,我觉得有道理。
2026-04-21 13:38:46 +08:00

22 lines
730 B
Python

from pathlib import Path
from typing import Annotated
import aiofiles
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
security = HTTPBearer()
async def verify_token(credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]) -> str:
token = credentials.credentials
async with aiofiles.open(file=Path.cwd() / "data" / "token.txt", mode="r") as f:
if (await f.readline()).strip() == token:
return token
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or missing token. (Suan API WebUI)",
headers={"WWW-Authenticate": "Bearer"},
)