ForcePilot/backend/package/yuxi/channel/channels/dingtalk/verifier.py
Kris 9e503becd3
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat(plugin): 实现完整的插件注册管理系统
新增了插件相关的完整领域模型、应用服务、基础设施实现,包括:
1. 插件状态、注册模式、来源等基础枚举和数据结构
2. 插件清单解析、发现、加载工具类
3. 插件注册表领域服务和内存存储实现
4. 插件相关的命令、查询、事件定义
5. 插件REST API接口和DTO映射
6. 集成了原有通道适配器到插件系统
7. 新增内置插件注册和自动发现能力
2026-05-31 16:44:13 +08:00

54 lines
1.7 KiB
Python

from __future__ import annotations
import base64
import hashlib
import hmac
import logging
import time
from yuxi.channel.domain.port.channel_request_verifier_port import VerifyResult
logger = logging.getLogger(__name__)
_TIMESTAMP_TOLERANCE_SECONDS = 3600
class DingTalkRequestVerifier:
def __init__(self, *, sign_secret: str = "") -> None:
self._sign_secret = sign_secret
@property
def channel_type(self) -> str:
return "dingtalk"
@property
def enabled(self) -> bool:
return bool(self._sign_secret)
async def verify(self, body: bytes, headers: dict[str, str]) -> VerifyResult:
if not self._sign_secret:
return VerifyResult(passed=True, method="dingtalk_none", reason="no_secret_configured")
timestamp = headers.get("timestamp", "")
sign = headers.get("sign", "")
if not timestamp or not sign:
return VerifyResult(passed=False, method="dingtalk_sign", reason="missing_sign_headers")
try:
ts = int(timestamp)
except (ValueError, TypeError):
return VerifyResult(passed=False, method="dingtalk_sign", reason="invalid_timestamp")
if abs(time.time() - ts / 1000) > _TIMESTAMP_TOLERANCE_SECONDS:
return VerifyResult(passed=False, method="dingtalk_sign", reason="timestamp_expired")
string_to_sign = f"{timestamp}\n{self._sign_secret}"
expected = base64.b64encode(
hmac.new(self._sign_secret.encode(), string_to_sign.encode(), hashlib.sha256).digest()
).decode()
if not hmac.compare_digest(sign, expected):
return VerifyResult(passed=False, method="dingtalk_sign", reason="sign_mismatch")
return VerifyResult(passed=True, method="dingtalk_sign")