From 67faf076d267af7a88a7b93ae48bee7f7f9b5c8c Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Sat, 4 Nov 2023 16:51:25 +0100 Subject: [PATCH] refs '#6264' feat: memoization accessTokenConfig --- back/methods/vn-user/is-token-valid.js | 5 ++++- back/methods/vn-user/renew-token.js | 11 ++++++----- back/methods/vn-user/token-config.js | 9 +++++++++ back/methods/vn-user/validate-token.js | 4 +--- 4 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 back/methods/vn-user/token-config.js diff --git a/back/methods/vn-user/is-token-valid.js b/back/methods/vn-user/is-token-valid.js index 1104b55e2..e40c2765a 100644 --- a/back/methods/vn-user/is-token-valid.js +++ b/back/methods/vn-user/is-token-valid.js @@ -1,5 +1,8 @@ +const tokenConfig = require('./token-config'); + +module.exports = async token => { + const accessTokenConfig = await tokenConfig(); -module.exports = async(token, accessTokenConfig) => { const now = new Date(); const differenceMilliseconds = now - token.created; const differenceSeconds = Math.floor(differenceMilliseconds / 1000); diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 1206e537f..4226886fc 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -1,5 +1,7 @@ const UserError = require('vn-loopback/util/user-error'); -const handlePromiseLogout = (Self, {id}, courtesyTime = 60) => { +const tokenConfig = require('./token-config'); +const DEFAULT_COURTESY_TIME = 60; +const handlePromiseLogout = (Self, {id}, courtesyTime = DEFAULT_COURTESY_TIME) => { new Promise(res => { setTimeout(() => { res(Self.logout(id)); @@ -23,14 +25,13 @@ module.exports = Self => { }); Self.renewToken = async function(ctx) { - const models = Self.app.models; - const token = ctx.req.accessToken; + const {accessToken: token} = ctx.req; // Check if current token is valid const isValid = await Self.validateToken(token); if (!isValid) throw new UserError(`The renew period has not been exceeded`, 'periodNotExceeded'); - const fields = ['courtesyTime']; - const {courtesyTime} = await models.AccessTokenConfig.findOne({fields}); + + const {courtesyTime} = await tokenConfig(); // Schedule to remove current token handlePromiseLogout(Self, token, courtesyTime); diff --git a/back/methods/vn-user/token-config.js b/back/methods/vn-user/token-config.js new file mode 100644 index 000000000..0936e0b89 --- /dev/null +++ b/back/methods/vn-user/token-config.js @@ -0,0 +1,9 @@ +const DEFAULT_FIELDS = ['renewPeriod', 'courtesyTime']; +const {models} = require('vn-loopback/server/server'); +let currentAccessTokenConfig = null; +module.exports = async(fields = DEFAULT_FIELDS) => { + if (currentAccessTokenConfig) return currentAccessTokenConfig; + const accessTokenConfig = await models.AccessTokenConfig.findOne({fields}); + if (!accessTokenConfig) currentAccessTokenConfig = accessTokenConfig; + return accessTokenConfig; +}; diff --git a/back/methods/vn-user/validate-token.js b/back/methods/vn-user/validate-token.js index 5cbbbf0e8..fadaed43b 100644 --- a/back/methods/vn-user/validate-token.js +++ b/back/methods/vn-user/validate-token.js @@ -14,9 +14,7 @@ module.exports = Self => { }); Self.validateToken = async function(token) { - const fields = ['renewPeriod', 'courtesyTime']; - const accessTokenConfig = await Self.app.models.AccessTokenConfig.findOne({fields}); - const isValid = await isTokenValid(token, accessTokenConfig); + const isValid = await isTokenValid(token); return isValid; }; };