@kne/fastify-signature 1.0.0-alpha.3 → 1.0.0-alpha.5
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 +20 -1
- package/libs/controllers/main.js +3 -3
- package/libs/services/main.js +2 -1
- package/package.json +4 -1
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(
|
|
@@ -47,7 +48,25 @@ module.exports = fp(async (fastify, options) => {
|
|
|
47
48
|
getUserModel: options.getUserModel
|
|
48
49
|
})
|
|
49
50
|
],
|
|
50
|
-
['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, userId } = 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
|
+
request.openApiPayload = await options.getUserModel().findByPk(userId);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
]
|
|
51
70
|
]
|
|
52
71
|
});
|
|
53
72
|
});
|
package/libs/controllers/main.js
CHANGED
|
@@ -67,7 +67,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
67
67
|
type: 'object',
|
|
68
68
|
properties: {
|
|
69
69
|
appId: {
|
|
70
|
-
type: '
|
|
70
|
+
type: 'string',
|
|
71
71
|
description: '密钥ID'
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -91,7 +91,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
91
91
|
type: 'object',
|
|
92
92
|
properties: {
|
|
93
93
|
appId: {
|
|
94
|
-
type: '
|
|
94
|
+
type: 'string',
|
|
95
95
|
description: '密钥ID'
|
|
96
96
|
},
|
|
97
97
|
status: {
|
|
@@ -122,7 +122,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
122
122
|
type: 'object',
|
|
123
123
|
properties: {
|
|
124
124
|
appId: {
|
|
125
|
-
type: '
|
|
125
|
+
type: 'string',
|
|
126
126
|
description: '密钥ID'
|
|
127
127
|
},
|
|
128
128
|
timestamp: {
|
package/libs/services/main.js
CHANGED
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.5",
|
|
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
|
}
|