security: AuthGuard cache max size — eviction FIFO 1000 entries
This commit is contained in:
@@ -12,6 +12,7 @@ interface CacheEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const TOKEN_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
const TOKEN_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||||
|
const MAX_CACHE_SIZE = 1000;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuard implements CanActivate {
|
export class AuthGuard implements CanActivate {
|
||||||
@@ -49,6 +50,10 @@ export class AuthGuard implements CanActivate {
|
|||||||
|
|
||||||
const user = await this.introspect(token);
|
const user = await this.introspect(token);
|
||||||
if (user) {
|
if (user) {
|
||||||
|
if (this.cache.size >= MAX_CACHE_SIZE) {
|
||||||
|
const oldest = this.cache.keys().next().value;
|
||||||
|
if (oldest) this.cache.delete(oldest);
|
||||||
|
}
|
||||||
this.cache.set(token, {
|
this.cache.set(token, {
|
||||||
user,
|
user,
|
||||||
expiresAt: Date.now() + TOKEN_CACHE_TTL_MS,
|
expiresAt: Date.now() + TOKEN_CACHE_TTL_MS,
|
||||||
|
|||||||
Reference in New Issue
Block a user