@kne/fastify-signature 1.0.0-alpha.2 → 1.0.0-alpha.4

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
@@ -1,5 +1,6 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const path = require('node:path');
3
+ const { Unauthorized } = require('http-error');
3
4
 
4
5
  module.exports = fp(async (fastify, options) => {
5
6
  options = Object.assign(
@@ -12,10 +13,16 @@ module.exports = fp(async (fastify, options) => {
12
13
  dbTableNamePrefix: 't_signature_',
13
14
  getAdminAuthenticate: () => {
14
15
  if (!fastify.account) {
15
- throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserAuthenticate');
16
+ throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getAdminAuthenticate');
16
17
  }
17
18
  return fastify.account.authenticate.admin;
18
19
  },
20
+ getUserAuthenticate: () => {
21
+ if (!fastify.account) {
22
+ throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserAuthenticate');
23
+ }
24
+ return fastify.account.authenticate.user;
25
+ },
19
26
  getUserInfo: request => {
20
27
  return request.userInfo;
21
28
  },
@@ -33,6 +40,7 @@ module.exports = fp(async (fastify, options) => {
33
40
  name: options.name,
34
41
  options,
35
42
  modules: [
43
+ ['controllers', path.resolve(__dirname, './libs/controllers')],
36
44
  [
37
45
  'models',
38
46
  await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
@@ -40,7 +48,24 @@ module.exports = fp(async (fastify, options) => {
40
48
  getUserModel: options.getUserModel
41
49
  })
42
50
  ],
43
- ['services', path.resolve(__dirname, './libs/services')]
51
+ ['services', path.resolve(__dirname, './libs/services')],
52
+ [
53
+ 'authenticate',
54
+ {
55
+ openApi: async request => {
56
+ const { appId, timestamp, expire, signature } = request.headers;
57
+ const { result, message } = await fastify[options.name].services.verify({
58
+ appId,
59
+ timestamp,
60
+ expire,
61
+ signature
62
+ });
63
+ if (result !== true) {
64
+ throw new Unauthorized(message);
65
+ }
66
+ }
67
+ }
68
+ ]
44
69
  ]
45
70
  });
46
71
  });
@@ -3,23 +3,27 @@ const fp = require('fastify-plugin');
3
3
  module.exports = fp(async (fastify, options) => {
4
4
  const { services } = fastify[options.name];
5
5
  const adminAuth = options.getAdminAuthenticate();
6
+ const userAuth = options.getUserAuthenticate();
6
7
  fastify.get(
7
8
  `${options.prefix}/list`,
8
9
  {
9
- onRequest: [adminAuth],
10
+ onRequest: [userAuth, adminAuth],
10
11
  schema: {
11
12
  description: '获取密钥列表',
12
13
  summary: '获取密钥列表',
13
14
  query: {
14
- currentPage: {
15
- type: 'number',
16
- description: '页码',
17
- default: 1
18
- },
19
- perPage: {
20
- type: 'number',
21
- description: '每页数量',
22
- default: 20
15
+ type: 'object',
16
+ properties: {
17
+ currentPage: {
18
+ type: 'number',
19
+ description: '页码',
20
+ default: 1
21
+ },
22
+ perPage: {
23
+ type: 'number',
24
+ description: '每页数量',
25
+ default: 20
26
+ }
23
27
  }
24
28
  }
25
29
  }
@@ -32,7 +36,7 @@ module.exports = fp(async (fastify, options) => {
32
36
  fastify.post(
33
37
  `${options.prefix}/create`,
34
38
  {
35
- onRequest: [adminAuth],
39
+ onRequest: [userAuth, adminAuth],
36
40
  schema: {
37
41
  description: '创建密钥',
38
42
  summary: '创建密钥',
@@ -55,7 +59,7 @@ module.exports = fp(async (fastify, options) => {
55
59
  fastify.post(
56
60
  `${options.prefix}/remove`,
57
61
  {
58
- onRequest: [adminAuth],
62
+ onRequest: [userAuth, adminAuth],
59
63
  schema: {
60
64
  description: '删除密钥',
61
65
  summary: '删除密钥',
@@ -63,7 +67,7 @@ module.exports = fp(async (fastify, options) => {
63
67
  type: 'object',
64
68
  properties: {
65
69
  appId: {
66
- type: 'number',
70
+ type: 'string',
67
71
  description: '密钥ID'
68
72
  }
69
73
  }
@@ -79,7 +83,7 @@ module.exports = fp(async (fastify, options) => {
79
83
  fastify.post(
80
84
  `${options.prefix}/update`,
81
85
  {
82
- onRequest: [adminAuth],
86
+ onRequest: [userAuth, adminAuth],
83
87
  schema: {
84
88
  description: '更新密钥',
85
89
  summary: '更新密钥',
@@ -87,7 +91,7 @@ module.exports = fp(async (fastify, options) => {
87
91
  type: 'object',
88
92
  properties: {
89
93
  appId: {
90
- type: 'number',
94
+ type: 'string',
91
95
  description: '密钥ID'
92
96
  },
93
97
  status: {
@@ -118,7 +122,7 @@ module.exports = fp(async (fastify, options) => {
118
122
  type: 'object',
119
123
  properties: {
120
124
  appId: {
121
- type: 'number',
125
+ type: 'string',
122
126
  description: '密钥ID'
123
127
  },
124
128
  timestamp: {
@@ -1,9 +1,10 @@
1
1
  const fp = require('fastify-plugin');
2
+ const crypto = require('crypto');
2
3
 
3
4
  module.exports = fp(async (fastify, options) => {
4
- const { models, services } = [options.name];
5
+ const { models, services } = fastify[options.name];
5
6
 
6
- const add = async (authenticatePayload, { description }) => {
7
+ const create = async (authenticatePayload, { description }) => {
7
8
  const { id: userId } = authenticatePayload;
8
9
  const secretKey = crypto.randomBytes(options.secretLength).toString('hex');
9
10
  const newSecret = await models.secret.create({ secretKey, description, userId });
@@ -59,7 +60,7 @@ module.exports = fp(async (fastify, options) => {
59
60
  return {
60
61
  id: item.id,
61
62
  appId: item.id,
62
- secretKey: item.secretKey.substring(0, 3) + '*'.repeat(item.secretKey.length - 6) + item.secretKey.substring(item.secretKey.length - 3),
63
+ secretKey: item.secretKey.substring(0, 3) + '*'.repeat(8) + item.secretKey.substring(item.secretKey.length - 3),
63
64
  description: item.description,
64
65
  lastVisitedAt: item.lastVisitedAt,
65
66
  status: item.status,
@@ -96,7 +97,7 @@ module.exports = fp(async (fastify, options) => {
96
97
  };
97
98
 
98
99
  Object.assign(fastify[options.name].services, {
99
- add,
100
+ create,
100
101
  verify,
101
102
  list,
102
103
  remove,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-signature",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "用于实现一个签名和验证算法",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -51,5 +51,8 @@
51
51
  "nodemon": "^3.1.3",
52
52
  "prettier": "^3.2.5",
53
53
  "sqlite3": "^5.1.7"
54
+ },
55
+ "dependencies": {
56
+ "http-error": "^0.0.6"
54
57
  }
55
58
  }