@kne/fastify-webhook 0.1.1 → 0.1.3-alpha.0

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
  ],
@@ -15,7 +15,10 @@ module.exports = fp(async (fastify, options) => {
15
15
  properties: {
16
16
  name: { type: 'string' },
17
17
  type: { type: 'string' },
18
- expire: { type: 'string', format: 'date-time' }
18
+ expire: { type: 'string', format: 'date-time' },
19
+ signatureLocation: { type: 'string' },
20
+ inputLocation: { type: 'string' },
21
+ shouldEncryptVerify: { type: 'boolean' }
19
22
  },
20
23
  required: ['name', 'type']
21
24
  }
@@ -79,6 +82,7 @@ module.exports = fp(async (fastify, options) => {
79
82
  { type },
80
83
  {
81
84
  body: request.body,
85
+ rawBody: request[options.rawBodyField || 'rowBody'],
82
86
  headers: request.headers,
83
87
  query: request.query
84
88
  }
@@ -109,6 +113,7 @@ module.exports = fp(async (fastify, options) => {
109
113
  { type },
110
114
  {
111
115
  body: request.body,
116
+ rawBody: request[options.rawBodyField || 'rowBody'],
112
117
  headers: request.headers,
113
118
  query: request.query
114
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 }) => {
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
  }
@@ -15,7 +24,10 @@ module.exports = fp(async (fastify, options) => {
15
24
  name,
16
25
  type,
17
26
  expire,
18
- userId
27
+ userId,
28
+ signatureLocation,
29
+ inputLocation,
30
+ shouldEncryptVerify
19
31
  });
20
32
  };
21
33
 
@@ -74,7 +86,7 @@ module.exports = fp(async (fastify, options) => {
74
86
  await webhook.destroy();
75
87
  };
76
88
 
77
- const invoke = async ({ type, headers, body, query }) => {
89
+ const invoke = async ({ type, headers, body, rawBody, query }) => {
78
90
  //signature, input
79
91
  const list = await models.webhook.findAll({ where: { type } });
80
92
 
@@ -85,14 +97,23 @@ module.exports = fp(async (fastify, options) => {
85
97
  let currentWebhook = null;
86
98
  for (const webhook of list) {
87
99
  const signature = get({ headers, body, query }, webhook.signatureLocation);
100
+ if (webhook.expire && webhook.expire < new Date()) {
101
+ continue;
102
+ }
88
103
  const md5 = crypto
89
104
  .createHash('md5')
90
105
  .update(`${webhook.name}|${webhook.expire ? webhook.expire.getTime() : 0}`)
91
106
  .digest('hex');
92
- if (md5 === signature) {
107
+ if (webhook.shouldEncryptVerify && generateSignature(md5, rawBody) === signature) {
93
108
  currentWebhook = webhook;
94
109
  break;
95
110
  }
111
+ if (!webhook.shouldEncryptVerify) {
112
+ if (md5 === signature) {
113
+ currentWebhook = webhook;
114
+ break;
115
+ }
116
+ }
96
117
  }
97
118
  if (!currentWebhook) {
98
119
  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.1",
3
+ "version": "0.1.3-alpha.0",
4
4
  "description": "实现一个webhook功能.",
5
5
  "main": "index.js",
6
6
  "scripts": {