29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
"""API 依赖:鉴权上下文(架构 §5.7 · 归属校验统一在此层)。
|
||
|
||
AuthContext 模型在 A4 冻结(开发计划 v1.1);get_auth_context 完整实现归 B6:
|
||
dev(app_env=development)从 X-Debug-Role / X-Debug-Actor 请求头构造,
|
||
非 dev 环境启动时检测 debug 依赖注册即拒绝;T-01 就绪后仅替换工厂内部为 JWT 解析。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from pydantic import BaseModel, Field
|
||
|
||
|
||
class AuthContext(BaseModel):
|
||
"""统一鉴权上下文(全部 API 依赖层的产出;service 层签名接收此类型)。"""
|
||
|
||
actor_id: str = Field(..., description="操作者 ID:staff_id 或 customer_id")
|
||
roles: list[str] = Field(default_factory=list, description="角色集合,如 ['risk_officer','risk_demo']")
|
||
customer_id: str | None = Field(None, description="customer 角色时 = 本人 customer_id;其余为空")
|
||
|
||
def has_role(self, *roles: str) -> bool:
|
||
return any(r in self.roles for r in roles)
|
||
|
||
def is_customer(self) -> bool:
|
||
return "customer" in self.roles
|
||
|
||
|
||
def get_auth_context() -> AuthContext:
|
||
raise NotImplementedError("implemented in B6 (X-Debug-* in dev / JWT in T-01)")
|