Compare commits

...

43 Commits

Author SHA1 Message Date
Javier Segarra c3b91e1719 Merge branch 'dev' into 6434-signIn_issue
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-20 09:25:15 +00:00
Javier Segarra 19bd02f952 Merge pull request '#6264 - renewToken' (!1824) from 6264-renewToken into dev
gitea/salix/pipeline/head This commit looks good Details
Reviewed-on: #1824
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
Reviewed-by: Juan Ferrer <juan@verdnatura.es>
2023-12-20 09:22:25 +00:00
Juan Ferrer 10bfafdd3c Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head This commit looks good Details
2023-12-20 09:05:53 +00:00
David Domenech f33e343acb Merge pull request 'refs #6398' (!1896) from 6398-changes into dev
gitea/salix/pipeline/head This commit looks good Details
Reviewed-on: #1896
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
2023-12-20 08:01:09 +00:00
David Domenech 5936a129da refs #6398
gitea/salix/pipeline/head This commit looks good Details
2023-12-19 12:58:20 +01:00
David Domenech 86f340248f Merge pull request 'fix: refs #6398 Changed alias of views' (!1895) from 6398-fix into dev
gitea/salix/pipeline/head This commit looks good Details
Reviewed-on: #1895
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
2023-12-19 10:04:56 +00:00
Guillermo Bonet 17e177b788 fix: refs #6398 Fix tests back
gitea/salix/pipeline/head This commit looks good Details
2023-12-19 10:12:03 +01:00
David Domenech d60f2a7f88 fix: refs #6398 Changed alias of views
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-19 09:43:09 +01:00
Alex Moreno d47a4a7c16 Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-15 11:12:47 +00:00
Javier Segarra d2461d67c9 refs #6264 test: perf renew-token.spec.js
gitea/salix/pipeline/head This commit looks good Details
2023-12-14 12:25:40 +01:00
Javier Segarra acf192f7e4 Merge branch '6264-renewToken' of https://gitea.verdnatura.es/verdnatura/salix into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-14 12:21:12 +01:00
Javier Segarra 8d29850917 refs #6264 perf: remove UserError exception 2023-12-14 12:21:07 +01:00
Javier Segarra dddc749744 Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-14 11:05:00 +00:00
Javier Segarra 61a323078b refs #6264 fix: remove DEFAULT_COURTESY_TIME
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-14 12:04:05 +01:00
Javier Segarra 485968037b Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head This commit looks good Details
2023-12-14 06:22:17 +00:00
Javier Segarra 1e89ed6cdc Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-13 14:39:10 +00:00
Javier Segarra d364a50ec4 refs #6264 feat: remove validateToken endpoint
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-13 13:39:43 +01:00
Javier Segarra d44cdcbd13 Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-13 06:14:55 +00:00
Javier Segarra 8441877f36 refs #6264 perf: remove field's query
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-12 19:43:32 +01:00
Javier Segarra 0ae75d973f refs #6264 perf: remove async keyword 2023-12-12 19:42:46 +01:00
Javier Segarra a5fb07bf12 refs #6264 perf remove unnecessary code
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-05 13:05:26 +01:00
Javier Segarra a8b0a7a5b3 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-12-04 14:46:21 +01:00
Javier Segarra 5656ed7a2b refs #6264 perf: use functions extracted previously 2023-12-04 14:46:05 +01:00
Javier Segarra b9671c0b67 refs #6264 perf: remove files related to token 2023-12-04 14:44:50 +01:00
Javier Segarra d4cd23853f refs #6264 perf: try to remove jasmine.clock()
gitea/salix/pipeline/head There was a failure building this commit Details
2023-11-30 07:32:16 +01:00
Javier Segarra 97d7f8356f Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 6264-renewToken 2023-11-30 07:08:23 +01:00
Javier Segarra 9da5fb9a14 refs #6264 other: rename camel-case variable
gitea/salix/pipeline/head There was a failure building this commit Details
2023-11-27 10:24:25 +01:00
Javier Segarra 901a44cc62 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-11-27 09:48:31 +01:00
Javier Segarra 81be3b18f7 refs #6264 test: validate-token and renew-token 2023-11-27 09:48:15 +01:00
Javier Segarra 72a0932e35 refs #6264 other: rename camel-case variable 2023-11-27 09:46:27 +01:00
Alex Moreno 7951b874a6 Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head There was a failure building this commit Details
2023-11-20 06:39:50 +00:00
Javier Segarra bcccd1894c refs #6264 test: init test
gitea/salix/pipeline/head There was a failure building this commit Details
2023-11-10 13:13:17 +01:00
Javier Segarra 784f5bb7f9 refs #6264 perf: replace now with vnNew
gitea/salix/pipeline/head There was a failure building this commit Details
2023-11-07 23:23:02 +01:00
Javier Segarra 0c2b2b25b7 refs #6264 fix: remove unnecessary file
gitea/salix/pipeline/head This commit looks good Details
2023-11-04 18:07:37 +01:00
Javier Segarra 5601ce5dac refs #6264 fix: rename variable
gitea/salix/pipeline/head This commit looks good Details
2023-11-04 17:25:30 +01:00
Javier Segarra 43366d1ba8 refs #6264 feat: update fixture.sql 2023-11-04 17:24:04 +01:00
Javier Segarra 21028e3b79 refs #6264 feat: db changes 2023-11-04 17:23:44 +01:00
Javier Segarra 67faf076d2 refs '#6264' feat: memoization accessTokenConfig 2023-11-04 16:51:25 +01:00
Javier Segarra b02e1f000e handle expired token while exists in BD
gitea/salix/pipeline/head This commit looks good Details
2023-11-04 14:39:59 +01:00
Javier Segarra 8c6eab23e5 handle expired token and return new token 2023-11-04 14:39:26 +01:00
Javier Segarra 82ee4f6e5b remove auth:before middleware 2023-11-04 14:38:21 +01:00
Javier Segarra 38d1e2b14f renew-token middleware definition 2023-11-03 16:11:35 +01:00
Javier Segarra 98f237b4bd new middleware for auth:before 2023-11-03 16:10:46 +01:00
14 changed files with 99 additions and 58 deletions

View File

@ -1,5 +1,14 @@
const UserError = require('vn-loopback/util/user-error');
const {models} = require('vn-loopback/server/server');
const handlePromiseLogout = (Self, {id}, courtesyTime) => {
new Promise(res => {
setTimeout(() => {
res(Self.logout(id));
}
, courtesyTime * 1000);
});
};
module.exports = Self => {
Self.remoteMethodCtx('renewToken', {
description: 'Checks if the token has more than renewPeriod seconds to live and if so, renews it',
@ -16,23 +25,32 @@ module.exports = Self => {
});
Self.renewToken = async function(ctx) {
const models = Self.app.models;
const token = ctx.req.accessToken;
const {accessToken: token} = ctx.req;
const now = new Date();
const differenceMilliseconds = now - token.created;
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
// Check if current token is valid
const isValid = await validateToken(token);
if (isValid)
return token;
const fields = ['renewPeriod', 'courtesyTime'];
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields});
const {courtesyTime} = await models.AccessTokenConfig.findOne({fields: ['courtesyTime']});
if (differenceSeconds < accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime)
throw new UserError(`The renew period has not been exceeded`, 'periodNotExceeded');
// Schedule to remove current token
handlePromiseLogout(Self, token, courtesyTime);
await Self.logout(token.id);
// Create new accessToken
const user = await Self.findById(token.userId);
const accessToken = await user.createAccessToken();
return {id: accessToken.id, ttl: accessToken.ttl};
};
async function validateToken(token) {
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;
}
};

View File

@ -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;
let response;
try {
response = await models.VnUser.renewToken(ctx);
} catch (e) {
error = e;
}
expect(error).toBeUndefined();
expect(response.id).toEqual(ctx.req.accessToken.id);
});
});

View File

@ -1,17 +0,0 @@
module.exports = Self => {
Self.remoteMethod('validateToken', {
description: 'Validates the current logged user token',
returns: {
type: 'Boolean',
root: true
},
http: {
path: `/validateToken`,
verb: 'GET'
}
});
Self.validateToken = async function() {
return true;
};
};

View File

@ -10,7 +10,6 @@ module.exports = function(Self) {
require('../methods/vn-user/sign-in')(Self);
require('../methods/vn-user/acl')(Self);
require('../methods/vn-user/recover-password')(Self);
require('../methods/vn-user/validate-token')(Self);
require('../methods/vn-user/privileges')(Self);
require('../methods/vn-user/validate-auth')(Self);
require('../methods/vn-user/renew-token')(Self);

View File

@ -104,13 +104,6 @@
"permission": "ALLOW"
},
{
"property": "validateToken",
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"property": "validateAuth",
"accessType": "EXECUTE",
"principalType": "ROLE",

View File

@ -0,0 +1,4 @@
-- Auto-generated SQL script #202311061003
UPDATE salix.accessTokenConfig
SET courtesyTime=60
WHERE id=1;

View File

@ -3,7 +3,7 @@ CREATE OR REPLACE DEFINER=`root`@`localhost`
VIEW `vn`.`ticketState`
AS SELECT `tt`.`created` AS `updated`,
`tt`.`stateFk` AS `stateFk`,
`tt`.`userFk` AS `userFk`,
`tt`.`userFk` AS `workerFk`,
`tls`.`ticketFk` AS `ticketFk`,
`s`.`id` AS `state`,
`s`.`order` AS `productionOrder`,
@ -40,7 +40,7 @@ SELECT
`ts`.`state` AS `state`,
`ts`.`productionOrder` AS `productionOrder`,
`ts`.`alertLevel` AS `alertLevel`,
`ts`.`userFk` AS `userFk`,
`ts`.`worker` AS `worker`,
`ts`.`code` AS `code`,
`ts`.`updated` AS `updated`,
`ts`.`isPicked` AS `isPicked`

View File

@ -2967,9 +2967,9 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`)
(2, 1, 50, 2),
(3, 1, 0, 3);
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`)
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `courtesyTime`, `renewInterval`)
VALUES
(1, 21600, 300);
(1, 21600, 60, 300);
INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agencyFk`, `companyFk`)
VALUES

View File

@ -82,7 +82,7 @@ export default class Token {
if (!data) return;
this.renewPeriod = data.renewPeriod;
this.stopRenewer();
this.inservalId = setInterval(() => this.checkValidity(), data.renewInterval * 1000);
this.intervalId = setInterval(() => this.checkValidity(), data.renewInterval * 1000);
});
}
@ -103,17 +103,13 @@ export default class Token {
const token = res.data;
this.set(token.id, now, token.ttl, this.remember);
})
.catch(res => {
if (res.data?.error?.code !== 'periodNotExceeded')
throw res;
})
.finally(() => {
this.checking = false;
});
}
stopRenewer() {
clearInterval(this.inservalId);
clearInterval(this.intervalId);
}
}
Token.$inject = ['vnInterceptor', '$http', '$rootScope'];

View File

@ -1,6 +1,5 @@
module.exports = () => {
Date.vnUTC = () => {
const env = process.env.NODE_ENV;
Date.vnUTC = (env = process.env.NODE_ENV) => {
if (!env || env === 'development')
return new Date(Date.UTC(2001, 0, 1, 11));

View File

@ -39,7 +39,7 @@
"./middleware/salix-version": {}
},
"parse": {
"body-parser#json":{}
"body-parser#json":{}
},
"routes": {
"loopback#rest": {

View File

@ -2,7 +2,7 @@ const {models} = require('vn-loopback/server/server');
describe('account changePassword()', () => {
const userId = 70;
const unauthCtx = {
const unAuthCtx = {
req: {
headers: {},
connection: {
@ -79,7 +79,7 @@ describe('account changePassword()', () => {
passExpired: yesterday
}
, options);
await models.VnUser.signIn(unauthCtx, 'trainee', 'nightmare', options);
await models.VnUser.signIn(unAuthCtx, 'trainee', 'nightmare', options);
} catch (e) {
if (e.message != 'Pass expired')
throw e;

View File

@ -34,9 +34,9 @@
"foreignKey": "stateFk"
},
"user": {
"type": "belongsTo",
"model": "VnUser",
"foreignKey": "userFk"
}
"type": "belongsTo",
"model": "VnUser",
"foreignKey": "workerFk"
}
}
}

View File

@ -37,9 +37,9 @@
"foreignKey": "stateFk"
},
"user": {
"type": "belongsTo",
"model": "VnUser",
"foreignKey": "userFk"
}
"type": "belongsTo",
"model": "VnUser",
"foreignKey": "userFk"
}
}
}