Skip to content

Commit

Permalink
auth: Run removeInvalidTokens 5s after startup
Browse files Browse the repository at this point in the history
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 <philip.molares@udo.edu>
  • Loading branch information
DerMolly committed Jan 25, 2021
1 parent 4a5351d commit 3e8ce26
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/auth/auth.service.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,16 +58,16 @@ export class AuthService {
}

async randomString(length: number): Promise<Buffer> {
// 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;
}
return randomBytes(length);
}

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('+', '-')
Expand Down Expand Up @@ -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.`,
);
}
}

0 comments on commit 3e8ce26

Please sign in to comment.