## P1 数据库基础 - zqyy_app: 创建 auth/biz schema、FDW 连接 etl_feiqiu - etl_feiqiu: 创建 app schema RLS 视图、商品库存预警表 - 清理 assistant_abolish 残留数据 ## P2 ETL/DWS 扩展 - 新增 DWS 助教订单贡献度表 (dws.assistant_order_contribution) - 新增 assistant_order_contribution_task 任务及 RLS 视图 - member_consumption 增加充值字段、assistant_daily 增加处罚字段 - 更新 ODS/DWD/DWS 任务文档及业务规则文档 - 更新 consistency_checker、flow_runner、task_registry 等核心模块 ## P3 小程序鉴权系统 - 新增 xcx_auth 路由/schema(微信登录 + JWT) - 新增 wechat/role/matching/application 服务层 - zqyy_app 鉴权表迁移 + 角色权限种子数据 - auth/dependencies.py 支持小程序 JWT 鉴权 ## 文档与审计 - 新增 DOCUMENTATION-MAP 文档导航 - 新增 7 份 BD_Manual 数据库变更文档 - 更新 DDL 基线快照(etl_feiqiu 6 schema + zqyy_app auth) - 新增全栈集成审计记录、部署检查清单更新 - 新增 BACKLOG 路线图、FDW→Core 迁移计划 ## Kiro 工程化 - 新增 5 个 Spec(P1/P2/P3/全栈集成/核心业务) - 新增审计自动化脚本(agent_on_stop/build_audit_context/compliance_prescan) - 新增 6 个 Hook(合规检查/会话日志/提交审计等) - 新增 doc-map steering 文件 ## 运维与测试 - 新增 ops 脚本:迁移验证/API 健康检查/ETL 监控/集成报告 - 新增属性测试:test_dws_contribution / test_auth_system - 清理过期 export 报告文件 - 更新 .gitignore 排除规则
159 lines
4.5 KiB
Python
159 lines
4.5 KiB
Python
"""
|
||
FastAPI 依赖注入:从 JWT 提取当前用户信息。
|
||
|
||
用法:
|
||
@router.get("/protected")
|
||
async def protected_endpoint(user: CurrentUser = Depends(get_current_user)):
|
||
print(user.user_id, user.site_id)
|
||
|
||
# 允许 pending 用户(受限令牌)访问
|
||
@router.get("/apply")
|
||
async def apply_endpoint(user: CurrentUser = Depends(get_current_user_or_limited)):
|
||
if user.limited:
|
||
... # 受限逻辑
|
||
"""
|
||
|
||
from dataclasses import dataclass, field
|
||
|
||
from fastapi import Depends, HTTPException, status
|
||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||
from jose import JWTError
|
||
|
||
from app.auth.jwt import decode_access_token
|
||
|
||
# Bearer token 提取器
|
||
_bearer_scheme = HTTPBearer(auto_error=True)
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class CurrentUser:
|
||
"""从 JWT 解析出的当前用户上下文。"""
|
||
|
||
user_id: int
|
||
site_id: int = 0
|
||
roles: list[str] = field(default_factory=list)
|
||
status: str = "pending"
|
||
limited: bool = False
|
||
|
||
|
||
async def get_current_user(
|
||
credentials: HTTPAuthorizationCredentials = Depends(_bearer_scheme),
|
||
) -> CurrentUser:
|
||
"""
|
||
FastAPI 依赖:从 Authorization header 提取 JWT,验证后返回用户信息。
|
||
|
||
要求完整令牌(非 limited),失败时抛出 401。
|
||
"""
|
||
token = credentials.credentials
|
||
try:
|
||
payload = decode_access_token(token)
|
||
except JWTError:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="无效的令牌",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
# 受限令牌不允许通过此依赖
|
||
if payload.get("limited"):
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="受限令牌无法访问此端点",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
user_id_raw = payload.get("sub")
|
||
site_id = payload.get("site_id")
|
||
|
||
if user_id_raw is None or site_id is None:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="令牌缺少必要字段",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
try:
|
||
user_id = int(user_id_raw)
|
||
except (TypeError, ValueError):
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="令牌中 user_id 格式无效",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
roles = payload.get("roles", [])
|
||
|
||
return CurrentUser(
|
||
user_id=user_id,
|
||
site_id=site_id,
|
||
roles=roles,
|
||
status="approved",
|
||
limited=False,
|
||
)
|
||
|
||
|
||
async def get_current_user_or_limited(
|
||
credentials: HTTPAuthorizationCredentials = Depends(_bearer_scheme),
|
||
) -> CurrentUser:
|
||
"""
|
||
FastAPI 依赖:允许 pending 用户(受限令牌)访问。
|
||
|
||
- 受限令牌(limited=True):返回 CurrentUser(limited=True, roles=[], status="pending")
|
||
- 完整令牌:正常返回 CurrentUser
|
||
"""
|
||
token = credentials.credentials
|
||
try:
|
||
payload = decode_access_token(token)
|
||
except JWTError:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="无效的令牌",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
user_id_raw = payload.get("sub")
|
||
if user_id_raw is None:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="令牌缺少必要字段",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
try:
|
||
user_id = int(user_id_raw)
|
||
except (TypeError, ValueError):
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="令牌中 user_id 格式无效",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
# 受限令牌:pending 用户
|
||
if payload.get("limited"):
|
||
return CurrentUser(
|
||
user_id=user_id,
|
||
site_id=0,
|
||
roles=[],
|
||
status="pending",
|
||
limited=True,
|
||
)
|
||
|
||
# 完整令牌:要求 site_id
|
||
site_id = payload.get("site_id")
|
||
if site_id is None:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="令牌缺少必要字段",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
|
||
roles = payload.get("roles", [])
|
||
|
||
return CurrentUser(
|
||
user_id=user_id,
|
||
site_id=site_id,
|
||
roles=roles,
|
||
status="approved",
|
||
limited=False,
|
||
)
|