```
feat: 添加飞书集成和审计API密钥认证 - 在数据库配置中添加飞书模型导入 - 添加审计API密钥配置项和认证中间件 - 实现飞书事件重复处理防止机制 - 为审批路由添加API密钥认证 - 优化AI适配器错误处理并添加JSON解析异常捕获 - 更新测试用例以包含新的认证和事件处理逻辑 ```
This commit is contained in:
@@ -21,6 +21,8 @@ class Settings(BaseSettings):
|
||||
api_prefix: str = "/api/v1"
|
||||
api_key: str | None = None
|
||||
api_actor: str = ActorValue.API
|
||||
audit_api_key: str | None = None
|
||||
audit_api_actor: str = ActorValue.AUDITOR
|
||||
approval_api_key: str | None = None
|
||||
approval_api_actor: str = ActorValue.APPROVER
|
||||
cors_origins: list[str] = Field(default_factory=lambda: ["*"])
|
||||
|
||||
@@ -3,6 +3,7 @@ from enum import StrEnum
|
||||
|
||||
class ActorValue(StrEnum):
|
||||
API = "api"
|
||||
AUDITOR = "auditor"
|
||||
APPROVER = "approver"
|
||||
SYSTEM = "system"
|
||||
SCHEDULER = "scheduler"
|
||||
@@ -12,6 +13,7 @@ class ActorValue(StrEnum):
|
||||
class HttpHeader(StrEnum):
|
||||
AUTHORIZATION = "Authorization"
|
||||
X_API_KEY = "X-API-Key"
|
||||
X_AUDIT_API_KEY = "X-Audit-API-Key"
|
||||
X_APPROVAL_API_KEY = "X-Approval-API-Key"
|
||||
|
||||
|
||||
|
||||
@@ -53,3 +53,25 @@ def require_approval_api_key(
|
||||
detail="Invalid approval API key",
|
||||
)
|
||||
return ApiPrincipal(actor=settings.approval_api_actor)
|
||||
|
||||
|
||||
def require_audit_api_key(
|
||||
x_audit_api_key: str | None = Header(
|
||||
default=None,
|
||||
alias=HttpHeader.X_AUDIT_API_KEY,
|
||||
),
|
||||
) -> ApiPrincipal:
|
||||
"""Validate the audit API key and return the audit principal."""
|
||||
|
||||
settings = get_settings()
|
||||
if not settings.audit_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="AUDIT_API_KEY is required",
|
||||
)
|
||||
if not x_audit_api_key or not compare_digest(x_audit_api_key, settings.audit_api_key):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid audit API key",
|
||||
)
|
||||
return ApiPrincipal(actor=settings.audit_api_actor)
|
||||
|
||||
Reference in New Issue
Block a user