2022-08-15 17:33:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
|
|
* This file is part of Auth-Server-Helper which is released under MIT.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, August 2022
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { debug } from '../debug';
|
2022-08-27 16:39:07 +02:00
|
|
|
import { Redis } from '../Redis';
|
2022-08-15 17:33:25 +02:00
|
|
|
|
|
|
|
const logger = debug ('RedisBlacklistStore');
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
export class RedisBlacklistStore extends Redis {
|
2022-09-09 15:49:53 +02:00
|
|
|
public async add (key: string, valid_until: Date): Promise<void> {
|
2022-08-15 17:33:25 +02:00
|
|
|
const log = logger.extend ('set');
|
|
|
|
log ('trying to add key %s to redis blacklist', key);
|
2022-08-27 16:39:07 +02:00
|
|
|
if (!this.is_active) {
|
2022-08-15 17:33:25 +02:00
|
|
|
log ('redis is inactive, skipping');
|
|
|
|
return;
|
|
|
|
}
|
2022-09-09 15:49:53 +02:00
|
|
|
await this.redis.setex (
|
|
|
|
`blacklist_${key}`,
|
2022-09-09 16:02:25 +02:00
|
|
|
Math.floor ((valid_until.getTime () - Date.now ()) / 1000),
|
2022-09-09 15:49:53 +02:00
|
|
|
1
|
|
|
|
);
|
2022-08-15 17:33:25 +02:00
|
|
|
log ('saved key');
|
|
|
|
}
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
public async remove (key: string): Promise<void> {
|
|
|
|
const log = logger.extend ('remove');
|
|
|
|
log ('removing key %s from redis', key);
|
|
|
|
if (!this.is_active) {
|
|
|
|
log ('redis is inactive, skipping');
|
|
|
|
return;
|
|
|
|
}
|
2022-09-09 15:49:53 +02:00
|
|
|
await this.redis.del (`blacklist_${key}`);
|
2022-08-27 16:39:07 +02:00
|
|
|
log ('removed key');
|
|
|
|
}
|
|
|
|
|
2022-08-15 17:33:25 +02:00
|
|
|
public async get (key: string): Promise<boolean> {
|
|
|
|
const log = logger.extend ('get');
|
|
|
|
log ('trying to find key %s in redis blacklist', key);
|
2022-08-27 16:39:07 +02:00
|
|
|
if (!this.is_active) {
|
2022-08-15 17:33:25 +02:00
|
|
|
log ('redis is inactive, skipping');
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-09 15:49:53 +02:00
|
|
|
const res = await this.redis.exists (`blacklist_${key}`) === 1;
|
2022-08-15 17:33:25 +02:00
|
|
|
log ('found key %s', res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 16:39:07 +02:00
|
|
|
|
|
|
|
export const redis_blacklist_store = new RedisBlacklistStore;
|