liufumingren c35ac184ae feat: WMS 开放接口签名 & 消息转发模块
- 新增 extra/wms.php 统一配置(app_key/app_secret/base_url)
- 新增 WmsSign.php 签名工具(canonicalQuery + bodySign + stableStringify)
- 新增 WmsVerify.php 验签工具(恒定时间比较防时序攻击)
- 新增 func/ 目录与远端 API 对应(Index.ping / Store.list / Inbox.push)
- 重构 Forward.php 使用 Inbox.push 转发群聊消息到 WMS 站内信
- Chat.php processMessage 接入 Forward 转发,异常不阻塞原有流程
- 删除冗余 WmsSignConfig.php,配置统一从 config('wms') 读取
- 修复 deploy.js chown/chmod .user.ini 报错
2026-08-06 22:46:30 +08:00

130 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\index\library\new_wms;
/**
* WMS 开放接口验签工具
*
* 验签流程:
* 1. 提取请求中的 query 参数和 body
* 2. 使用相同的算法重新计算签名
* 3. 对比传入的 sign 与计算值是否相等(恒定时间比较防止时序攻击)
*
* 配置来源:application/extra/wms.php
*
* 使用示例:
* $verifier = new WmsVerify('abc123');
* $ok = $verifier->verify($queryParams, $body, $incomingSign);
*/
class WmsVerify
{
/** @var WmsSign */
private $signer;
/**
* @param string $appSecret 应用 Secret
*/
public function __construct($appSecret)
{
$this->signer = new WmsSign($appSecret);
}
// ==================== 公开方法 ====================
/**
* 验证签名
*
* @param array $queryParams 请求 query 参数(关联数组,应包含 sign 字段)
* @param mixed $body 请求体(数组、对象等,传 null 表示无请求体)
* @param string|null $incomingSign 传入的签名字符串;传 null 则自动从 queryParams 中取出
* @return bool 验签是否通过
*/
public function verify(array $queryParams, $body = null, $incomingSign = null)
{
// 获取传入的签名
if ($incomingSign === null) {
$incomingSign = isset($queryParams['sign']) ? $queryParams['sign'] : '';
}
if (empty($incomingSign)) {
return false;
}
// 重新计算签名
$expectedSign = $this->signer->make($queryParams, $body);
// 恒定时间比较(防止时序攻击)
return $this->hashEquals($expectedSign, $incomingSign);
}
/**
* 从 HTTP 请求中自动提取参数并验签
*
* 适用于接收 WMS 回调的场景:
* - GET 请求:query 参数从 $_GET 获取,body 为 null
* - POST 请求:query 参数从 $_GET 获取,body 从 php://input 获取
*
* @param string|null $httpMethod 请求方法(GET/POST),传 null 自动判断
* @return bool
*/
public function verifyFromRequest($httpMethod = null)
{
if ($httpMethod === null) {
$httpMethod = isset($_SERVER['REQUEST_METHOD'])
? strtoupper($_SERVER['REQUEST_METHOD'])
: 'GET';
}
$queryParams = $_GET;
$body = null;
if (in_array($httpMethod, ['POST', 'PUT', 'PATCH'], true)) {
$rawBody = file_get_contents('php://input');
if ($rawBody !== false && $rawBody !== '') {
$body = json_decode($rawBody, true);
// JSON 解析失败则保留原始字符串
if ($body === null && json_last_error() !== JSON_ERROR_NONE) {
$body = $rawBody;
}
}
}
return $this->verify($queryParams, $body);
}
// ==================== 内部方法 ====================
/**
* 恒定时间字符串比较(防止时序攻击)
*
* @param string $knownString 已知的正确签名
* @param string $userString 用户传入的签名
* @return bool
*/
private function hashEquals($knownString, $userString)
{
if (function_exists('hash_equals')) {
return hash_equals($knownString, $userString);
}
// fallback: 恒定时间比较
$knownLen = strlen($knownString);
$userLen = strlen($userString);
// 先比较长度(非恒定时间,但不会泄露内容)
if ($knownLen !== $userLen) {
// 仍然执行一次比较操作以避免通过时间差异判断长度
$knownString = $userString;
$result = 0;
} else {
$result = 0;
}
for ($i = 0; $i < $knownLen; $i++) {
$result |= ord($knownString[$i]) ^ ord($userString[$i]);
}
return $result === 0;
}
}