@kne/fastify-webhook 0.1.2 → 0.1.3-alpha.1

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/index.js CHANGED
@@ -6,9 +6,10 @@ module.exports = fp(async (fastify, options) => {
6
6
  {},
7
7
  {
8
8
  prefix: '/api/webhook',
9
- dbTableNamePrefix: 't_webhook_',
9
+ dbTableNamePrefix: 't_',
10
10
  name: 'webhook',
11
11
  hooks: {},
12
+ rawBodyField: 'rawBody',
12
13
  getUserAuthenticate: () => {
13
14
  if (!fastify.account) {
14
15
  throw new Error('fastify-account plugin must be registered before fastify-trtc,or set options.getUserAuthenticate');
@@ -40,6 +41,7 @@ module.exports = fp(async (fastify, options) => {
40
41
  'models',
41
42
  await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
42
43
  prefix: options.dbTableNamePrefix,
44
+ modelPrefix: options.name,
43
45
  getUserModel: options.getUserModel
44
46
  })
45
47
  ],
@@ -17,7 +17,8 @@ module.exports = fp(async (fastify, options) => {
17
17
  type: { type: 'string' },
18
18
  expire: { type: 'string', format: 'date-time' },
19
19
  signatureLocation: { type: 'string' },
20
- inputLocation: { type: 'string' }
20
+ inputLocation: { type: 'string' },
21
+ shouldEncryptVerify: { type: 'boolean' }
21
22
  },
22
23
  required: ['name', 'type']
23
24
  }
@@ -81,6 +82,7 @@ module.exports = fp(async (fastify, options) => {
81
82
  { type },
82
83
  {
83
84
  body: request.body,
85
+ rawBody: request[options.rawBodyField || 'rowBody'],
84
86
  headers: request.headers,
85
87
  query: request.query
86
88
  }
@@ -111,6 +113,7 @@ module.exports = fp(async (fastify, options) => {
111
113
  { type },
112
114
  {
113
115
  body: request.body,
116
+ rawBody: request[options.rawBodyField || 'rowBody'],
114
117
  headers: request.headers,
115
118
  query: request.query
116
119
  }
@@ -28,6 +28,11 @@ module.exports = ({ DataTypes, options }) => {
28
28
  comment: '过期时间,默认为null,不过期',
29
29
  defaultValue: null
30
30
  },
31
+ shouldEncryptVerify: {
32
+ type: DataTypes.BOOLEAN,
33
+ comment: '是否加密验证',
34
+ defaultValue: true
35
+ },
31
36
  status: {
32
37
  type: DataTypes.ENUM('open', 'close'),
33
38
  comment: '状态',
@@ -4,10 +4,19 @@ const transform = require('lodash/transform');
4
4
  const groupBy = require('lodash/groupBy');
5
5
  const get = require('lodash/get');
6
6
 
7
+ function generateSignature(key, body) {
8
+ // 创建 HMAC-SHA256 哈希对象
9
+ const hmac = crypto.createHmac('sha256', key);
10
+ // 更新哈希内容(支持字符串或 Buffer)
11
+ hmac.update(body);
12
+ // 生成 Base64 编码的签名
13
+ return hmac.digest('base64');
14
+ }
15
+
7
16
  module.exports = fp(async (fastify, options) => {
8
17
  const { models } = fastify[options.name];
9
18
 
10
- const create = async ({ name, type, expire, userId, signatureLocation, inputLocation }) => {
19
+ const create = async ({ name, type, expire, userId, signatureLocation, inputLocation, shouldEncryptVerify }) => {
11
20
  if (!options.hooks[type]) {
12
21
  throw new Error(`Hook ${type} not allowed`);
13
22
  }
@@ -17,7 +26,8 @@ module.exports = fp(async (fastify, options) => {
17
26
  expire,
18
27
  userId,
19
28
  signatureLocation,
20
- inputLocation
29
+ inputLocation,
30
+ shouldEncryptVerify
21
31
  });
22
32
  };
23
33
 
@@ -76,7 +86,7 @@ module.exports = fp(async (fastify, options) => {
76
86
  await webhook.destroy();
77
87
  };
78
88
 
79
- const invoke = async ({ type, headers, body, query }) => {
89
+ const invoke = async ({ type, headers, body, rawBody, query }) => {
80
90
  //signature, input
81
91
  const list = await models.webhook.findAll({ where: { type } });
82
92
 
@@ -87,14 +97,26 @@ module.exports = fp(async (fastify, options) => {
87
97
  let currentWebhook = null;
88
98
  for (const webhook of list) {
89
99
  const signature = get({ headers, body, query }, webhook.signatureLocation);
100
+ if (webhook.expire && webhook.expire < new Date()) {
101
+ continue;
102
+ }
90
103
  const md5 = crypto
91
104
  .createHash('md5')
92
105
  .update(`${webhook.name}|${webhook.expire ? webhook.expire.getTime() : 0}`)
93
106
  .digest('hex');
94
- if (md5 === signature) {
107
+
108
+ const shouldEncryptVerify = webhook.inputLocation === 'body' && webhook.shouldEncryptVerify;
109
+
110
+ if (shouldEncryptVerify && generateSignature(md5, rawBody) === signature) {
95
111
  currentWebhook = webhook;
96
112
  break;
97
113
  }
114
+ if (!shouldEncryptVerify) {
115
+ if (md5 === signature) {
116
+ currentWebhook = webhook;
117
+ break;
118
+ }
119
+ }
98
120
  }
99
121
  if (!currentWebhook) {
100
122
  throw new Error('Webhook not registered');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-webhook",
3
- "version": "0.1.2",
3
+ "version": "0.1.3-alpha.1",
4
4
  "description": "实现一个webhook功能.",
5
5
  "main": "index.js",
6
6
  "scripts": {