> For the complete documentation index, see [llms.txt](https://docs.coda.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.coda.co/https-coda-payments.gitbook.io-zhong-wen-coda-private-technical-documentation/codapay/tokenization-api-2.0/an-quan-yu-yan-zheng.md).

# 安全与验证

## **Headers 说明**

在调用 API 时，需要在请求头中包含以下参数：

| Header                           | 描述                                                                                                                                                                                             |
| -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **X-Partner-Id**                 | 该 ID 是提供给合作伙伴在注册此服务时的“用户名”，Partner ID 通常存储为参考，不需要额外加密。                                                                                                                                         |
| **X-Api-Key**                    | 这是为每个合作伙伴用于授权实施请求而生成的安全密钥。                                                                                                                                                                     |
| **Authorization**                | <p>每个请求生成的 <strong>JWT Token</strong>，用于安全验证请求的合法性。格式示例：<code>Bearer {{jwt}}</code>。</p><p></p><p>使用 <code>"Bearer"</code> 类型是 JWT 在 <code>Authorization</code> 头部的标准做法，可帮助区分身份验证使Token类型。</p> |
| **Content-Type**（仅适用于 `POST` 请求） | 该值为 **`application/json`**                                                                                                                                                                     |

## 构建 JWT Token

```javascript
var header = {
	'typ': 'JWT',
	'alg': 'HS256'
};

var currentTimestamp = Math.floor(Date.now() / 1000)

var data = {
	'partner_id': pm.environment.get('partner.id'),
	'iat': currentTimestamp
}


function base64url(source) {
    // Encode in classical base64
    encodedSource = CryptoJS.enc.Base64.stringify(source)
    
    // Remove padding equal characters
    encodedSource = encodedSource.replace(/=+$/, '')
    
    // Replace characters according to base64url specifications
    encodedSource = encodedSource.replace(/\+/g, '-')
    encodedSource = encodedSource.replace(/\//g, '_')
    
    return encodedSource
}

// encode header
var stringifiedHeader = CryptoJS.enc.Utf8.parse(JSON.stringify(header))
var encodedHeader = base64url(stringifiedHeader)

// encode data
var stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(data))
var encodedData = base64url(stringifiedData)

// build token
var token = `${encodedHeader}.${encodedData}`

// sign token
var signature = CryptoJS.HmacSHA256(token, jwtSecret)
signature = base64url(signature)
var signedToken = `${token}.${signature}`
```

## **Header**

* Header 对象包含两个属性：
  * **typ**，值为 `'JWT'`（表示它是一个 JWT）。
  * **alg**，值为 `'HS256'`（表示使用 HMAC SHA-256 算法进行签名）。
* 然后将 Header 转换为 **JSON 字符串**。
* 生成的 JSON 字符串采用 **UTF-8 编码**。
* UTF-8 编码后的 Header 再经过 **Base64 URL 编码**，生成 JWT Toke的第一部分。

## **Payload（载荷）**

* 数据对象包含两个属性：
  * **partner\_id**（合作伙伴 ID）。
  * **当前时间戳**（自 Unix 纪元以来的秒数，使用 `Math.floor(Date.now() / 1000)` 获取）。
* 数据对象转换为 **JSON 字符串**。
* 生成的 JSON 字符串采用 **UTF-8 编码**。
* UTF-8 编码后的数据（Payload）再经过 **Base64 URL 编码**，生成 JWT Token的第二部分。

## **生成 Token**

Token 由 **编码后的 Header** 和 **编码后的 Payload** 通过 **`.`（点号）** 连接而成。

## **签署 Token**

* Token 作为 **消息输入**，与 **密钥（jwtSecret）** 一起传递给 **HMAC SHA-256 算法**。
* **HMAC SHA-256 算法** 生成该 Token 的 **签名**。
* **签名为二进制数据**。
* **二进制签名** 经过 **Base64 URL 编码**，生成 **JWT Token 的第三部分**。

## **构造最终 JWT**

* 最终的 JWT 通过 **原始 Token（Header + Payload）**、**`.`（点号分隔符）** 和 **编码后的签名** 进行拼接。

生成的 **JWT 变量** 是一个紧凑的 **JWT Token**，可用于 **安全数据交换和身份验证**。**签名** 确保Token的 **完整性和真实性**，接收方可以使用 **jwtSecret** 进行验证，以确保其内容在传输过程中 **未被篡改**。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.coda.co/https-coda-payments.gitbook.io-zhong-wen-coda-private-technical-documentation/codapay/tokenization-api-2.0/an-quan-yu-yan-zheng.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
