新增 LINE 渠道扩展,支持在 Yuxi 平台中集成 LINE 即时通讯渠道。 包含以下功能模块: - bot: LINE Bot 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - token_manager: Token 管理 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - flex_templates: Flex 模板消息 - card_command: 卡片指令处理 - template_messages: 模板消息 - rich_menu: 富菜单管理 - actions: 动作处理 - directives: 指令处理 - delivery: 消息送达确认 - loading: 加载动画 - media: 媒体资源处理 - types: 类型定义
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import base64
|
|
import hashlib
|
|
import hmac
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def validate_line_signature(raw_body: bytes, signature: str, channel_secret: str) -> bool:
|
|
if not signature or not channel_secret or not raw_body:
|
|
return False
|
|
|
|
try:
|
|
computed = base64.b64encode(
|
|
hmac.new(
|
|
channel_secret.encode("utf-8"),
|
|
raw_body,
|
|
hashlib.sha256,
|
|
).digest()
|
|
).decode("utf-8")
|
|
except Exception:
|
|
logger.exception("LINE signature computation failed")
|
|
return False
|
|
|
|
return hmac.compare_digest(computed, signature)
|
|
|
|
|
|
def match_signature_against_accounts(
|
|
raw_body: bytes,
|
|
signature: str,
|
|
accounts: list[dict],
|
|
) -> dict | None:
|
|
matches: list[dict] = []
|
|
|
|
for account in accounts:
|
|
secret = account.get("channel_secret", "")
|
|
if validate_line_signature(raw_body, signature, secret):
|
|
matches.append(account)
|
|
|
|
if len(matches) == 0:
|
|
return None
|
|
if len(matches) > 1:
|
|
logger.warning(
|
|
"LINE signature matched multiple accounts (%d), rejecting as ambiguous",
|
|
len(matches),
|
|
)
|
|
return None
|
|
return matches[0] |