> ## Documentation Index
> Fetch the complete documentation index at: https://docs.y.uno/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Test in the sandbox first: base URL https://api-sandbox.y.uno (Test Mode in the Dashboard). Production is https://api.y.uno (Live Mode).
> Every API request needs the public-api-key and private-secret-key headers. Get them in the Dashboard at https://dashboard.y.uno/developers (Developers > Credentials); sandbox and production keys are different.
> Send an X-Idempotency-Key (a UUID) when creating payments, refunds and cancels, so retries do not create duplicates.
> Start with the Quickstart (https://docs.y.uno/docs/quickstart.md) and the agent guide (https://docs.y.uno/agents.md). To search these docs from a coding agent, connect the docs MCP server at https://docs.y.uno/mcp.

# 验证 Webhook 签名（HMAC）

> 通过验证请求标头中发送的 HMAC SHA256 签名来验证 webhook 的真实性

使用 HMAC（基于哈希的消息认证码）验证 webhook 签名，可确保发送到您服务器的 webhook 确实来自 Yuno，并且在传输过程中未被拦截或修改。这在 OAuth 等认证方式之外增加了一层额外的安全保障。

OAuth 提供认证（验证谁在发送请求），而 HMAC 签名提供：

* **数据完整性**：确认 webhook payload 未被篡改
* **真实性**：验证 webhook 确实来自 Yuno
* **防护**：防范中间人攻击。签名无法阻止被截获的请求再次发送，因此还应[对事件去重](https://docs.y.uno/zh-hans/docs/webhooks.md#de-duplication)
* **合规**：帮助满足处理敏感支付数据的 PCI DSS 要求

## HMAC 签名的工作原理

当 Yuno 向您的服务器发送 webhook 时：

1. **签名生成**：Yuno 使用 SHA-256 算法，以您的客户端密钥对 webhook payload 进行哈希运算，生成 HMAC 签名
2. **加入标头**：签名包含在 `x-hmac-signature` HTTP 标头中
3. **验证**：您的服务器从标头中提取签名，使用相同的 payload 和密钥重新生成签名，然后进行比较
4. **校验**：如果签名匹配，则 webhook 真实且未被修改

## 配置

要启用 HMAC 签名验证，请在 [Yuno Dashboard](https://dashboard.y.uno/) 中创建或编辑 webhook 时勾选 **Use HMAC Authentication**，或通过 [Webhooks API](https://docs.y.uno/reference/webhooks/create-webhook.md) 设置 `hmac_client_secret`。两种方式的验证过程相同。

有关这两种方式的分步说明，请参阅[配置 Webhooks](https://docs.y.uno/zh-hans/docs/webhooks/configure-webhooks.md)。

## 启用 HMAC 后的变化

启用 HMAC 签名验证后，Yuno webhooks 会额外包含一个 `x-hmac-signature` HTTP 标头。webhook JSON payload 保持不变。

### 带 HMAC 的 webhook 请求示例

以下是在其他已配置的认证标头之外添加 `x-hmac-signature` 标头后的 webhook 请求示例。

```http theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
POST /your-webhook-endpoint HTTP/1.1
Host: your-server.com
Content-Type: application/json
x-api-key: your-api-key
x-secret: your-secret
x-hmac-signature: K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=

{
  "type": "payment",
  "type_event": "payment.purchase",
  "account_id": "2c05976d-1234-1234-1234-6421883de48d",
  "retry": 0,
  "version": 2,
  "data": {
    "payment": {
      "id": "a546c566-1703-4fba-b334-c46e89bc97f7",
      "status": "SUCCEEDED"
    }
  }
}
```

## 验证代码示例

使用**原始请求体**（解析 JSON 之前）以及在 Yuno Dashboard 中为该 webhook 配置的相同密钥来验证 `x-hmac-signature` 标头。不要先重新序列化 JSON。键的顺序或空白字符发生变化，都会生成与 Yuno 发送的签名不同的签名。

\*\*Node.js（Express）：\*\*为 webhook 路由使用保留原始请求体的 body parser（例如，为该路径使用 `express.raw()`），然后：

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
const crypto = require('crypto');

function verifyWebhookSignature(rawBody, signatureHeader, secret) {
  if (typeof signatureHeader !== 'string') return false;
  const expected = crypto
    .createHmac('sha256', secret)
    .update(rawBody)
    .digest('base64');
  const received = Buffer.from(signatureHeader);
  const computed = Buffer.from(expected);
  // timingSafeEqual throws when lengths differ, so check the length first
  return received.length === computed.length && crypto.timingSafeEqual(received, computed);
}

// Example: const isValid = verifyWebhookSignature(req.body, req.headers['x-hmac-signature'], process.env.WEBHOOK_SECRET);
```

\*\*Python：\*\*使用原始请求体字节和 `x-hmac-signature` 标头：

```python theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
import hmac
import hashlib
import base64

def verify_webhook_signature(raw_body: bytes, signature_header: str, secret: str) -> bool:
    expected = base64.b64encode(
        hmac.new(secret.encode(), raw_body, hashlib.sha256).digest()
    ).decode()
    return hmac.compare_digest(signature_header, expected)
```

如果计算出的签名与标头不匹配，请拒绝该 webhook（例如，返回 401）。

## 相关文档

* [配置 Webhooks](https://docs.y.uno/zh-hans/docs/webhooks/configure-webhooks.md)
* [Payloads 和示例](https://docs.y.uno/zh-hans/docs/webhooks/object-and-examples.md)
* [Webhooks 概述](https://docs.y.uno/zh-hans/docs/webhooks.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.