ForcePilot/backend/package/yuxi/channel/extensions/nextcloud_talk/signature.py
Kris 6fc9a4a94c feat(channel): 添加 Nextcloud Talk 渠道扩展
新增 Nextcloud Talk 渠道扩展,支持在 Yuxi 平台中集成 Nextcloud Talk 即时通讯渠道。

包含以下功能模块:
- accounts: 账户管理
- bot_admin: Bot 管理
- chat_api: 聊天 API 封装
- config_schema: 配置模式
- conversation: 会话管理
- gateway: SSE/WebSocket 网关接入
- inbound: 入站消息处理
- send: 消息发送
- webhook_server: Webhook 服务
- format: 消息格式转换
- normalize: 消息规范化
- poll_api: 投票 API
- reaction_api: 表情反应 API
- policy: 安全策略
- probe: 健康探测
- replay_guard: 重放防护
- signature: 签名验证
- room_info: 房间信息
- types: 类型定义
2026-05-21 11:29:56 +08:00

50 lines
1.1 KiB
Python

from __future__ import annotations
import hashlib
import hmac
import secrets
def verify_nextcloud_talk_signature(
signature: str,
random: str,
body: str,
secret: str,
) -> bool:
expected = hmac.new(
secret.encode("utf-8"),
(random + body).encode("utf-8"),
hashlib.sha256,
).hexdigest()
return hmac.compare_digest(expected, signature)
def generate_nextcloud_talk_signature(
body: str,
secret: str,
) -> tuple[str, str]:
random = secrets.token_hex(32)
signature = hmac.new(
secret.encode("utf-8"),
(random + body).encode("utf-8"),
hashlib.sha256,
).hexdigest()
return random, signature
def extract_nextcloud_talk_headers(
headers: dict,
) -> dict | None:
signature = headers.get("X-Nextcloud-Talk-Signature")
random = headers.get("X-Nextcloud-Talk-Random")
backend = headers.get("X-Nextcloud-Talk-Backend")
if not all([signature, random, backend]):
return None
return {
"signature": signature,
"random": random,
"backend": backend,
}