From 75fe442e73f49d5b202419df9832d12fcb9e196b Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Tue, 25 Apr 2023 15:42:30 +0800 Subject: [PATCH] feat: add maxTokenLength config --- src/lib/token/token-utils.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/token/token-utils.js b/src/lib/token/token-utils.js index 6ac4c29..8e46111 100644 --- a/src/lib/token/token-utils.js +++ b/src/lib/token/token-utils.js @@ -41,9 +41,12 @@ export default class TokenUtils { tokenExpiresIn, tokenExpiresThreshold } = this.config - if (tokenExpiresThreshold > tokenExpiresIn) { + if (tokenExpiresThreshold >= tokenExpiresIn) { throw new Error('Config error, tokenExpiresThreshold should be less than tokenExpiresIn') } + if (tokenExpiresThreshold > tokenExpiresIn / 2) { + console.warn(`Please check whether the tokenExpiresThreshold configuration is set too large, tokenExpiresThreshold: ${tokenExpiresThreshold}, tokenExpiresIn: ${tokenExpiresIn}`) + } } get customToken () { @@ -193,7 +196,8 @@ export default class TokenUtils { const now = Date.now() const { tokenSecret, - tokenExpiresIn + tokenExpiresIn, + maxTokenLength = 10 } = this.config const token = jwt.sign({ ...signContent, @@ -219,6 +223,10 @@ export default class TokenUtils { tokenList.push(token) + if (tokenList.length > maxTokenLength) { + tokenList.splice(0, tokenList.length - 10) + } + await this.updateUserRecord({ last_login_ip: this.clientInfo.clientIP, last_login_date: now, -- GitLab