mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
refactor: 将路由层同步 DB 操作移至线程池执行,统一认证工具函数
- 管理端和用户端路由中的同步数据库操作提取为独立函数,通过 run_in_threadpool 在线程池中执行,避免阻塞事件循环(涉及 api_keys、payments、users、wallets、 provider_oauth、system、user_me、wallet 等模块) - 抽取 authenticate_user_from_bearer_token 统一 token 验证逻辑,支持 ManagementToken 和 JWT 两种认证方式,消除多处重复代码 - key_command_service 的 CRUD 操作改为线程池执行 - maintenance_scheduler 定时任务中的数据库操作改用 asyncio.to_thread - Dockerfile 中 aether-hub 增加 --worker-idle-timeout 0 防止空闲断连 - 新增 test_api_auth_conventions 和 test_auth_utils 单元测试
This commit is contained in:
25
tests/unit/test_api_auth_conventions.py
Normal file
25
tests/unit/test_api_auth_conventions.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def test_no_direct_access_token_verification_outside_pipeline() -> None:
|
||||
api_root = Path("src/api")
|
||||
allowed = {
|
||||
Path("src/api/base/pipeline.py"),
|
||||
Path("src/api/auth/routes.py"),
|
||||
}
|
||||
offenders: list[str] = []
|
||||
|
||||
for path in api_root.rglob("*.py"):
|
||||
if path in allowed:
|
||||
continue
|
||||
text = path.read_text(encoding="utf-8")
|
||||
if "verify_token(" in text and 'token_type="access"' in text:
|
||||
offenders.append(str(path))
|
||||
|
||||
assert (
|
||||
offenders == []
|
||||
), "这些 API 文件仍在手写 access token 校验,应改为走 pipeline 或 auth_utils 统一入口: " + ", ".join(
|
||||
sorted(offenders)
|
||||
)
|
||||
Reference in New Issue
Block a user