liufumingren c00708f58c feat: 入站 notify 对接完成(ping/reply + 入站签名算法)
- 新增 notify/PingHandler.php(notify.ping 连通性测试)
- 新增 notify/ReplyHandler.php(notify.reply 消息回复,Redis 幂等 + 推送到 IM)
- Dispatcher 自动注册内置处理器,入站签名改为 MD5(secret+timestamp+bodyJson)
- WmsSign 新增 getAppSecret() 供验签使用
- ReplyHandler 通过 ext_data.source 判断消息来源(wx/qiwe)
2026-08-07 22:52:46 +08:00

157 lines
4.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);
}
// ==================== 内部方法 ====================
// ==================== 入站通知专用验签 ====================
/**
* 验证入站通知签名(算法不同:MD5(secret + timestamp + bodyJson))
*
* @param array $queryParams URL query 参数(含 sign / timestamp)
* @param string $rawBody 原始请求体字符串
* @return bool
*/
public function verifyNotify(array $queryParams, $rawBody)
{
$incomingSign = isset($queryParams['sign']) ? $queryParams['sign'] : '';
$timestamp = isset($queryParams['timestamp']) ? $queryParams['timestamp'] : 0;
if (empty($incomingSign) || empty($timestamp)) {
return false;
}
// 入站签名: MD5(secret + timestamp + bodyJson)
$signText = $this->signer->getAppSecret() . $timestamp . $rawBody;
$expectedSign = md5($signText);
return $this->hashEquals($expectedSign, $incomingSign);
}
// ==================== 内部方法 ====================
/**
* 恒定时间字符串比较(防止时序攻击)
*
* @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;
}
}