feat(auth): clean expired revoked tokens

- Remove expired token revocations at startup and on the configured cron.
- Verify expiry boundaries and disabled-auth behavior.
This commit is contained in:
agent committed 2026-11-05 02:48:53 -05:00
1 parent c3e8670515
commit 2976a08aef
3 files changed
+194

No files matched your search

+2
View File
@@ -6,6 +6,7 @@ import { AuthGuard } from "./auth.guard";
import { AuthService } from "./auth.service";
import { PasswordService } from "./password.service";
import { RevokedTokenEntity } from "./revoked-token.entity";
import { RevokedTokenCleanupService } from "./revoked-token-cleanup.service";
import { UserEntity } from "./user.entity";
@Module({
@@ -14,6 +15,7 @@ import { UserEntity } from "./user.entity";
providers: [
AuthService,
PasswordService,
RevokedTokenCleanupService,
{ provide: APP_GUARD, useClass: AuthGuard },
],
exports: [AuthService, PasswordService],
@@ -0,0 +1,76 @@
import { Injectable, OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { SchedulerRegistry } from "@nestjs/schedule";
import { InjectRepository } from "@nestjs/typeorm";
import { CronJob } from "cron";
import { LessThanOrEqual, Repository } from "typeorm";
import { TraceLogger } from "../common/trace-logger";
import { AppConfig } from "../config/app.config";
import { RevokedTokenEntity } from "./revoked-token.entity";
export const AUTH_REVOKED_TOKEN_CLEANUP_JOB = "auth-revoked-token-cleanup";
@Injectable()
export class RevokedTokenCleanupService implements OnModuleInit {
private readonly logger = new TraceLogger(RevokedTokenCleanupService.name);
private readonly enabled: boolean;
private readonly cleanupCron: string;
constructor(
@InjectRepository(RevokedTokenEntity)
private readonly revokedTokens: Repository<RevokedTokenEntity>,
config: ConfigService<AppConfig, true>,
private readonly schedulerRegistry: SchedulerRegistry,
) {
this.enabled = config.get("AUTH_ENABLED");
this.cleanupCron = config.get("AUTH_REVOKED_TOKEN_CLEANUP_CRON");
}
async onModuleInit(): Promise<void> {
if (!this.enabled) return;
const job = CronJob.from({
cronTime: this.cleanupCron,
start: false,
waitForCompletion: true,
onTick: async () => {
try {
await this.cleanupExpiredTokens();
} catch (error) {
this.logger.error(
`Failed to clean up expired revoked tokens: ${this.errorMessage(error)}`,
);
}
},
});
this.schedulerRegistry.addCronJob(AUTH_REVOKED_TOKEN_CLEANUP_JOB, job);
job.start();
this.logger.debug(
`Revoked-token cleanup cron registered: ${this.cleanupCron}`,
);
try {
await this.cleanupExpiredTokens();
} catch (error) {
this.logger.error(
`Failed to clean up expired revoked tokens at startup: ${this.errorMessage(error)}`,
);
}
}
async cleanupExpiredTokens(): Promise<void> {
if (!this.enabled) return;
const now = new Date();
const result = await this.revokedTokens.delete({
expiresAt: LessThanOrEqual(now),
});
if (result.affected) {
this.logger.debug(`Cleaned up ${result.affected} expired revoked tokens`);
}
}
private errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
}