From c8da989f258a9c21da006942ed9cb6578e89f974 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 25 Jan 2021 16:29:09 +0100 Subject: [PATCH] auth: Run removeInvalidTokens 5s after startup This should prevent problem with the AuthToken purge on Sundays, as the service is either running on sunday or will be restarted there after. Also move base64url comment to right function Signed-off-by: Philip Molares --- src/auth/auth.service.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index fc2dcfcfc..3d5751e89 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -21,7 +21,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { ConsoleLoggerService } from '../logger/console-logger.service'; import { TimestampMillis } from '../utils/timestamp'; -import { Cron } from '@nestjs/schedule'; +import { Cron, Timeout } from '@nestjs/schedule'; @Injectable() export class AuthService { @@ -58,9 +58,6 @@ export class AuthService { } async randomString(length: number): Promise { - // This is necessary as the is no base64url encoding in the toString method - // but as can be seen on https://tools.ietf.org/html/rfc4648#page-7 - // base64url is quite easy buildable from base64 if (length <= 0) { return null; } @@ -68,6 +65,9 @@ export class AuthService { } BufferToBase64Url(text: Buffer): string { + // This is necessary as the is no base64url encoding in the toString method + // but as can be seen on https://tools.ietf.org/html/rfc4648#page-7 + // base64url is quite easy buildable from base64 return text .toString('base64') .replace('+', '-') @@ -205,12 +205,28 @@ export class AuthService { // Delete all non valid tokens every sunday on 3:00 AM @Cron('0 0 3 * * 0') async handleCron() { + return this.removeInvalidTokens(); + } + + // Delete all non valid tokens 5 sec after startup + @Timeout(5000) + async handleTimeout() { + return this.removeInvalidTokens(); + } + + async removeInvalidTokens() { const currentTime = new Date().getTime(); const tokens: AuthToken[] = await this.authTokenRepository.find(); + let removedTokens = 0; for (const token of tokens) { if (token.validUntil && token.validUntil.getTime() <= currentTime) { + this.logger.debug(`AuthToken '${token.keyId}' was removed`); await this.authTokenRepository.remove(token); + removedTokens++; } } + this.logger.log( + `${removedTokens} invalid AuthTokens were purged from the DB.`, + ); } }