@kne/fastify-signature 1.0.0-alpha.5 → 1.0.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 +5 -7
- package/libs/services/main.js +3 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -31,6 +31,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
31
31
|
throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserModel');
|
|
32
32
|
}
|
|
33
33
|
return fastify.account.models.user;
|
|
34
|
+
},
|
|
35
|
+
getOpenApiParams: request => {
|
|
36
|
+
const { ['x-openapi-appid']: appId, ['x-openapi-timestamp']: timestamp, ['x-openapi-expire']: expire, ['x-openapi-signature']: signature } = request.headers;
|
|
37
|
+
return Object.assign({}, { appId, timestamp, expire, signature }, request.body);
|
|
34
38
|
}
|
|
35
39
|
},
|
|
36
40
|
options
|
|
@@ -53,13 +57,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
53
57
|
'authenticate',
|
|
54
58
|
{
|
|
55
59
|
openApi: async request => {
|
|
56
|
-
const {
|
|
57
|
-
const { result, message, userId } = await fastify[options.name].services.verify({
|
|
58
|
-
appId,
|
|
59
|
-
timestamp,
|
|
60
|
-
expire,
|
|
61
|
-
signature
|
|
62
|
-
});
|
|
60
|
+
const { result, message, userId } = await fastify[options.name].services.verify(options.getOpenApiParams(request));
|
|
63
61
|
if (result !== true) {
|
|
64
62
|
throw new Unauthorized(message);
|
|
65
63
|
}
|
package/libs/services/main.js
CHANGED
|
@@ -4,10 +4,10 @@ const crypto = require('crypto');
|
|
|
4
4
|
module.exports = fp(async (fastify, options) => {
|
|
5
5
|
const { models, services } = fastify[options.name];
|
|
6
6
|
|
|
7
|
-
const create = async (authenticatePayload, { description }) => {
|
|
7
|
+
const create = async (authenticatePayload, { description, userId: targetUserId }) => {
|
|
8
8
|
const { id: userId } = authenticatePayload;
|
|
9
9
|
const secretKey = crypto.randomBytes(options.secretLength).toString('hex');
|
|
10
|
-
const newSecret = await models.secret.create({ secretKey, description, userId });
|
|
10
|
+
const newSecret = await models.secret.create({ secretKey, description, userId: targetUserId || userId });
|
|
11
11
|
return { appId: newSecret.id, secretKey: newSecret.secretKey };
|
|
12
12
|
};
|
|
13
13
|
|
|
@@ -34,7 +34,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
34
34
|
if (expectedSignature !== signature) {
|
|
35
35
|
return {
|
|
36
36
|
result: false,
|
|
37
|
-
message:
|
|
37
|
+
message: `Invalid signature [${appId},${signature},${timestamp},${expire}]`
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
|