ForcePilot/backend/package/yuxi/channel/extensions/tencent_im/signature.py
Kris 331daa1216 feat(channel): 添加腾讯 IM 渠道扩展
新增腾讯 IM(Tencent IM)渠道扩展,支持在 Yuxi 平台中集成腾讯即时通讯 IM 渠道。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- pairing: 用户配对与绑定
- security: 安全校验
- signature: 请求签名验证
- usersig: UserSig 生成
- dedupe: 消息去重
- status: 会话状态管理
- group: 群组管理
- types: 类型定义
2026-05-21 11:49:29 +08:00

47 lines
1.1 KiB
Python

import hashlib
import hmac
import json
import time
def verify_callback_signature(
body: str,
public_key: str,
sdk_appid: str,
callback_command: str,
content_type: str,
client_ip: str,
opt_platform: str,
received_sig: str,
) -> bool:
body_sorted = json.dumps(json.loads(body), sort_keys=True, separators=(",", ":"))
to_sign = f"{body_sorted}\n{sdk_appid}\n{callback_command}\n{content_type}\n{client_ip}\n{opt_platform}"
expected_sig = (
hmac.new(
public_key.encode("utf-8"),
to_sign.encode("utf-8"),
hashlib.sha256,
)
.hexdigest()
.lower()
)
return hmac.compare_digest(expected_sig, received_sig)
def verify_callback_signature_v2(
body: str,
token: str,
request_time: str,
received_sig: str,
) -> bool:
to_sign = f"{request_time}\n{token}\n{body}"
expected_sig = hashlib.sha256(to_sign.encode("utf-8")).hexdigest().lower()
return hmac.compare_digest(expected_sig, received_sig)
def check_replay_attack(timestamp: int, max_age_seconds: int = 60) -> bool:
return abs(int(time.time()) - timestamp) <= max_age_seconds