mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-26 19:53:59 -05:00
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 <philip.molares@udo.edu>
This commit is contained in:
parent
67a5f3c7ec
commit
bfe14dad8d
1 changed files with 20 additions and 4 deletions
|
@ -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<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;
|
||||
}
|
||||
|
@ -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.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue