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

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
@@ -12,10 +12,16 @@ module.exports = fp(async (fastify, options) => {
12
12
  dbTableNamePrefix: 't_signature_',
13
13
  getAdminAuthenticate: () => {
14
14
  if (!fastify.account) {
15
- throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserAuthenticate');
15
+ throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getAdminAuthenticate');
16
16
  }
17
17
  return fastify.account.authenticate.admin;
18
18
  },
19
+ getUserAuthenticate: () => {
20
+ if (!fastify.account) {
21
+ throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserAuthenticate');
22
+ }
23
+ return fastify.account.authenticate.user;
24
+ },
19
25
  getUserInfo: request => {
20
26
  return request.userInfo;
21
27
  },
@@ -33,6 +39,7 @@ module.exports = fp(async (fastify, options) => {
33
39
  name: options.name,
34
40
  options,
35
41
  modules: [
42
+ ['controllers', path.resolve(__dirname, './libs/controllers')],
36
43
  [
37
44
  'models',
38
45
  await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
@@ -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: '删除密钥',
@@ -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: '更新密钥',
@@ -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.3",
4
4
  "description": "用于实现一个签名和验证算法",
5
5
  "main": "index.js",
6
6
  "scripts": {