Merge branch '6264-renewToken' of https://gitea.verdnatura.es/verdnatura/salix into 6274-loginWorkerTimeControl
This commit is contained in:
commit
bb186bafe0
|
@ -1,5 +1,15 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
|
|
||||||
|
const DEFAULT_COURTESY_TIME = 60;
|
||||||
|
const handlePromiseLogout = (Self, {id}, courtesyTime = DEFAULT_COURTESY_TIME) => {
|
||||||
|
new Promise(res => {
|
||||||
|
setTimeout(() => {
|
||||||
|
res(Self.logout(id));
|
||||||
|
}
|
||||||
|
, courtesyTime * 1000);
|
||||||
|
});
|
||||||
|
};
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('renewToken', {
|
Self.remoteMethodCtx('renewToken', {
|
||||||
description: 'Checks if the token has more than renewPeriod seconds to live and if so, renews it',
|
description: 'Checks if the token has more than renewPeriod seconds to live and if so, renews it',
|
||||||
|
@ -16,20 +26,18 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.renewToken = async function(ctx) {
|
Self.renewToken = async function(ctx) {
|
||||||
const models = Self.app.models;
|
const {accessToken: token} = ctx.req;
|
||||||
const token = ctx.req.accessToken;
|
|
||||||
|
|
||||||
const now = new Date();
|
// Check if current token is valid
|
||||||
const differenceMilliseconds = now - token.created;
|
const isValid = await Self.validateToken(token);
|
||||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
if (isValid) throw new UserError(`The renew period has not been exceeded`, 'periodNotExceeded');
|
||||||
|
|
||||||
const fields = ['renewPeriod', 'courtesyTime'];
|
const {courtesyTime} = await models.AccessTokenConfig.findOne({fields: ['renewPeriod', 'courtesyTime']});
|
||||||
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields});
|
|
||||||
|
|
||||||
if (differenceSeconds < accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime)
|
// Schedule to remove current token
|
||||||
throw new UserError(`The renew period has not been exceeded`, 'periodNotExceeded');
|
handlePromiseLogout(Self, token, courtesyTime);
|
||||||
|
|
||||||
await Self.logout(token.id);
|
// Create new accessToken
|
||||||
const user = await Self.findById(token.userId);
|
const user = await Self.findById(token.userId);
|
||||||
const accessToken = await user.createAccessToken();
|
const accessToken = await user.createAccessToken();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
|
describe('Renew Token', () => {
|
||||||
|
const startingTime = Date.now();
|
||||||
|
let ctx = null;
|
||||||
|
beforeAll(async() => {
|
||||||
|
const unAuthCtx = {
|
||||||
|
req: {
|
||||||
|
headers: {},
|
||||||
|
connection: {
|
||||||
|
remoteAddress: '127.0.0.1'
|
||||||
|
},
|
||||||
|
getLocale: () => 'en'
|
||||||
|
},
|
||||||
|
args: {}
|
||||||
|
};
|
||||||
|
let login = await models.VnUser.signIn(unAuthCtx, 'salesAssistant', 'nightmare');
|
||||||
|
let accessToken = await models.AccessToken.findById(login.token);
|
||||||
|
ctx = {req: {accessToken: accessToken}};
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jasmine.clock().install();
|
||||||
|
jasmine.clock().mockDate(new Date(startingTime));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jasmine.clock().uninstall();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should renew process', async() => {
|
||||||
|
jasmine.clock().mockDate(new Date(startingTime + 21600000));
|
||||||
|
const {id} = await models.VnUser.renewToken(ctx);
|
||||||
|
|
||||||
|
expect(id).not.toEqual(ctx.req.accessToken.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('NOT should renew', async() => {
|
||||||
|
let error;
|
||||||
|
try {
|
||||||
|
await models.VnUser.renewToken(ctx);
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).toBeDefined();
|
||||||
|
expect(error.statusCode).toBe(400);
|
||||||
|
expect(error.message).toEqual('The renew period has not been exceeded');
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,43 @@
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
|
|
||||||
|
describe('Validate Token', () => {
|
||||||
|
const startingTime = Date.now();
|
||||||
|
let ctx = null;
|
||||||
|
beforeAll(async() => {
|
||||||
|
const unAuthCtx = {
|
||||||
|
req: {
|
||||||
|
headers: {},
|
||||||
|
connection: {
|
||||||
|
remoteAddress: '127.0.0.1'
|
||||||
|
},
|
||||||
|
getLocale: () => 'en'
|
||||||
|
},
|
||||||
|
args: {}
|
||||||
|
};
|
||||||
|
let login = await models.VnUser.signIn(unAuthCtx, 'salesAssistant', 'nightmare');
|
||||||
|
let accessToken = await models.AccessToken.findById(login.token);
|
||||||
|
ctx = {req: {accessToken: accessToken}};
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jasmine.clock().install();
|
||||||
|
jasmine.clock().mockDate(new Date(startingTime));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jasmine.clock().uninstall();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Token is not expired', async() => {
|
||||||
|
const isValid = await models.VnUser.validateToken(ctx.req.accessToken);
|
||||||
|
|
||||||
|
expect(isValid).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Token is expired', async() => {
|
||||||
|
jasmine.clock().mockDate(new Date(startingTime + 21600000));
|
||||||
|
const isValid = await models.VnUser.validateToken(ctx.req.accessToken);
|
||||||
|
|
||||||
|
expect(isValid).toBeFalse();
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,3 +1,4 @@
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('validateToken', {
|
Self.remoteMethod('validateToken', {
|
||||||
description: 'Validates the current logged user token',
|
description: 'Validates the current logged user token',
|
||||||
|
@ -11,7 +12,13 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.validateToken = async function() {
|
Self.validateToken = async function(token) {
|
||||||
return true;
|
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod', 'courtesyTime']});
|
||||||
|
const now = Date.now();
|
||||||
|
const differenceMilliseconds = now - token.created;
|
||||||
|
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||||
|
const isValid = differenceSeconds < accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime;
|
||||||
|
|
||||||
|
return isValid;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
-- Auto-generated SQL script #202311061003
|
||||||
|
UPDATE salix.accessTokenConfig
|
||||||
|
SET courtesyTime=60
|
||||||
|
WHERE id=1;
|
|
@ -2968,9 +2968,9 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`)
|
||||||
(2, 1, 50, 2),
|
(2, 1, 50, 2),
|
||||||
(3, 1, 0, 3);
|
(3, 1, 0, 3);
|
||||||
|
|
||||||
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`)
|
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `courtesyTime`, `renewInterval`)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 21600, 300);
|
(1, 21600, 60, 300);
|
||||||
|
|
||||||
INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agencyFk`, `companyFk`)
|
INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agencyFk`, `companyFk`)
|
||||||
VALUES
|
VALUES
|
||||||
|
|
|
@ -82,7 +82,7 @@ export default class Token {
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
this.renewPeriod = data.renewPeriod;
|
this.renewPeriod = data.renewPeriod;
|
||||||
this.stopRenewer();
|
this.stopRenewer();
|
||||||
this.inservalId = setInterval(() => this.checkValidity(), data.renewInterval * 1000);
|
this.intervalId = setInterval(() => this.checkValidity(), data.renewInterval * 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ export default class Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
stopRenewer() {
|
stopRenewer() {
|
||||||
clearInterval(this.inservalId);
|
clearInterval(this.intervalId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Token.$inject = ['vnInterceptor', '$http', '$rootScope'];
|
Token.$inject = ['vnInterceptor', '$http', '$rootScope'];
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
Date.vnUTC = () => {
|
Date.vnUTC = (env = process.env.NODE_ENV) => {
|
||||||
const env = process.env.NODE_ENV;
|
|
||||||
if (!env || env === 'development')
|
if (!env || env === 'development')
|
||||||
return new Date(Date.UTC(2001, 0, 1, 11));
|
return new Date(Date.UTC(2001, 0, 1, 11));
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
"./middleware/salix-version": {}
|
"./middleware/salix-version": {}
|
||||||
},
|
},
|
||||||
"parse": {
|
"parse": {
|
||||||
"body-parser#json":{}
|
"body-parser#json":{}
|
||||||
},
|
},
|
||||||
"routes": {
|
"routes": {
|
||||||
"loopback#rest": {
|
"loopback#rest": {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module.exports = function(options) {
|
module.exports = function(options) {
|
||||||
return function(req, res, next) {
|
return async function(req, res, next) {
|
||||||
if (!req.accessToken)
|
if (!req.accessToken)
|
||||||
return next();
|
return next();
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ const {models} = require('vn-loopback/server/server');
|
||||||
|
|
||||||
describe('account changePassword()', () => {
|
describe('account changePassword()', () => {
|
||||||
const userId = 70;
|
const userId = 70;
|
||||||
const unauthCtx = {
|
const unAuthCtx = {
|
||||||
req: {
|
req: {
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: {
|
connection: {
|
||||||
|
@ -79,7 +79,7 @@ describe('account changePassword()', () => {
|
||||||
passExpired: yesterday
|
passExpired: yesterday
|
||||||
}
|
}
|
||||||
, options);
|
, options);
|
||||||
await models.VnUser.signIn(unauthCtx, 'trainee', 'nightmare', options);
|
await models.VnUser.signIn(unAuthCtx, 'trainee', 'nightmare', options);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.message != 'Pass expired')
|
if (e.message != 'Pass expired')
|
||||||
throw e;
|
throw e;
|
||||||
|
|
Loading…
Reference in New Issue