From ad4727714997ccad17e95dfc0ce444a8bf02ae0e Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 23 Sep 2022 12:15:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/user.ts | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/back/services/user.ts b/back/services/user.ts index 5d912b75..be055a99 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -24,7 +24,7 @@ export default class UserService { @Inject('logger') private logger: winston.Logger, private scheduleService: ScheduleService, private sockService: SockService, - ) {} + ) { } public async login( payloads: { @@ -65,15 +65,13 @@ export default class UserService { return this.initAuthInfo(); } - if (retries > 2 && Date.now() - lastlogon < Math.pow(3, retries) * 1000) { + const retriesTime = Math.pow(3, retries) * 1000; + if (retries > 2 && timestamp - lastlogon < retriesTime) { + const waitTime = Math.ceil((retriesTime - (timestamp - lastlogon)) / 1000); return { code: 410, - message: `失败次数过多,请${Math.round( - (Math.pow(3, retries) * 1000 - Date.now() + lastlogon) / 1000, - )}秒后重试`, - data: Math.round( - (Math.pow(3, retries) * 1000 - Date.now() + lastlogon) / 1000, - ), + message: `失败次数过多,请${waitTime}秒后重试`, + data: waitTime, }; } @@ -90,7 +88,7 @@ export default class UserService { } const data = createRandomString(50, 100); - const expiration = twoFactorActivated ? 30 : 3; + const expiration = twoFactorActivated ? 60 : 20; let token = jwt.sign({ data }, config.secret as any, { expiresIn: 60 * 60 * 24 * expiration, algorithm: 'HS384', @@ -111,8 +109,7 @@ export default class UserService { }); await this.notificationService.notify( '登录通知', - `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}在 ${address} ${ - req.platform + `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}在 ${address} ${req.platform }端 登录成功,ip地址 ${ip}`, ); await this.getLoginLog(); @@ -140,8 +137,7 @@ export default class UserService { }); await this.notificationService.notify( '登录通知', - `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}在 ${address} ${ - req.platform + `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}在 ${address} ${req.platform }端 登录失败,ip地址 ${ip}`, ); await this.getLoginLog(); @@ -155,7 +151,16 @@ export default class UserService { status: LoginStatus.fail, }, }); - return { code: 400, message: config.authError }; + if (retries > 2) { + const waitTime = Math.round(Math.pow(3, retries + 1)); + return { + code: 410, + message: `失败次数过多,请${waitTime}秒后重试`, + data: waitTime, + }; + } else { + return { code: 400, message: config.authError }; + } } } else { return this.initAuthInfo();