parent
b4284889e1
commit
be3ee43b06
|
@ -18,15 +18,7 @@ module.exports = Self => {
|
|||
Self.renewToken = async function(ctx) {
|
||||
const {accessToken: token} = ctx.req;
|
||||
|
||||
// Check if current token is valid
|
||||
|
||||
const {renewPeriod, courtesyTime} = await models.AccessTokenConfig.findOne({
|
||||
fields: ['renewPeriod', 'courtesyTime']
|
||||
});
|
||||
const now = Date.now();
|
||||
const differenceMilliseconds = now - token.created;
|
||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||
const isNotExceeded = differenceSeconds < renewPeriod - courtesyTime;
|
||||
const isNotExceeded = await Self.validateToken(ctx);
|
||||
if (isNotExceeded)
|
||||
return token;
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('validateToken', {
|
||||
description: 'Validates the current logged user token',
|
||||
accepts: [],
|
||||
accessType: 'READ',
|
||||
returns: {
|
||||
type: 'Boolean',
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/validateToken`,
|
||||
verb: 'GET'
|
||||
}
|
||||
});
|
||||
|
||||
Self.validateToken = async function(ctx) {
|
||||
const {accessToken: token} = ctx.req;
|
||||
|
||||
// Check if current token is valid
|
||||
const {renewPeriod, courtesyTime} = await models.AccessTokenConfig.findOne({
|
||||
fields: ['renewPeriod', 'courtesyTime']
|
||||
});
|
||||
const now = Date.now();
|
||||
const differenceMilliseconds = now - token.created;
|
||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||
const isNotExceeded = differenceSeconds < renewPeriod - courtesyTime;
|
||||
return isNotExceeded;
|
||||
};
|
||||
};
|
|
@ -15,6 +15,7 @@ module.exports = function(Self) {
|
|||
require('../methods/vn-user/renew-token')(Self);
|
||||
require('../methods/vn-user/share-token')(Self);
|
||||
require('../methods/vn-user/update-user')(Self);
|
||||
require('../methods/vn-user/validate-token')(Self);
|
||||
|
||||
Self.definition.settings.acls = Self.definition.settings.acls.filter(acl => acl.property !== 'create');
|
||||
|
||||
|
|
|
@ -113,6 +113,13 @@
|
|||
"principalId": "$everyone",
|
||||
"permission": "ALLOW"
|
||||
},
|
||||
{
|
||||
"property": "validateToken",
|
||||
"accessType": "EXECUTE",
|
||||
"principalType": "ROLE",
|
||||
"principalId": "$authenticated",
|
||||
"permission": "ALLOW"
|
||||
},
|
||||
{
|
||||
"property": "privileges",
|
||||
"accessType": "*",
|
||||
|
|
Loading…
Reference in New Issue