@kne/fastify-signature 1.0.0-alpha.0 → 1.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/generateSignature.js +16 -0
- package/index.js +2 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -259,14 +259,14 @@ console.log(result);
|
|
|
259
259
|
}
|
|
260
260
|
```
|
|
261
261
|
|
|
262
|
-
|
|
262
|
+
### 签名生成说明
|
|
263
263
|
|
|
264
264
|
1. 签名使用 HMAC-SHA256 算法
|
|
265
265
|
2. 签名字符串格式:`${appId}|${timestamp}|${expire}`
|
|
266
266
|
3. 使用 secretKey 作为密钥进行签名
|
|
267
267
|
4. 签名结果使用 hex 编码
|
|
268
268
|
|
|
269
|
-
|
|
269
|
+
### 错误处理
|
|
270
270
|
|
|
271
271
|
所有接口在发生错误时会返回以下格式:
|
|
272
272
|
|
|
@@ -282,7 +282,7 @@ console.log(result);
|
|
|
282
282
|
- Signature expired: 签名已过期
|
|
283
283
|
- Invalid signature: 签名验证失败
|
|
284
284
|
|
|
285
|
-
|
|
285
|
+
### 注意事项
|
|
286
286
|
|
|
287
287
|
1. 所有需要管理员认证的接口必须在请求头中包含有效的认证信息
|
|
288
288
|
2. 时间戳和过期时间使用 Unix 时间戳(秒)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
function generateSignature(appId, appSecret, expireInSeconds) {
|
|
4
|
+
const timestamp = Math.floor(Date.now() / 1000); // 当前时间戳(秒)
|
|
5
|
+
const expire = timestamp + expireInSeconds; // 过期时间戳
|
|
6
|
+
const dataToSign = `${appId}|${timestamp}|${expire}`;
|
|
7
|
+
|
|
8
|
+
// 使用 HMAC-SHA256 生成签名
|
|
9
|
+
const hmac = crypto.createHmac('sha256', appSecret);
|
|
10
|
+
hmac.update(dataToSign);
|
|
11
|
+
const signature = hmac.digest('hex');
|
|
12
|
+
|
|
13
|
+
return { appId, timestamp, expire, signature };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = generateSignature;
|
package/index.js
CHANGED
|
@@ -36,7 +36,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
36
36
|
[
|
|
37
37
|
'models',
|
|
38
38
|
await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
|
|
39
|
-
prefix: options.dbTableNamePrefix
|
|
39
|
+
prefix: options.dbTableNamePrefix,
|
|
40
|
+
getUserModel: options.getUserModel
|
|
40
41
|
})
|
|
41
42
|
],
|
|
42
43
|
['services', path.resolve(__dirname, './libs/services')]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-signature",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "用于实现一个签名和验证算法",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"index.js",
|
|
26
|
-
"libs"
|
|
26
|
+
"libs",
|
|
27
|
+
"generateSignature.js"
|
|
27
28
|
],
|
|
28
29
|
"repository": {
|
|
29
30
|
"type": "git",
|