/* * 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 , August 2022 */ import { debug } from '../debug'; import { Redis } from '../Redis'; const logger = debug ('RedisBlacklistStore'); export class RedisBlacklistStore extends Redis { public async add (key: string, valid_until: Date): Promise { const log = logger.extend ('set'); log ('trying to add key %s to redis blacklist', key); if (!this.is_active) { log ('redis is inactive, skipping'); return; } await this.redis.setex ( `blacklist_${key}`, Math.floor ((valid_until.getTime () - Date.now ()) / 1000), 1 ); log ('saved key'); } public async remove (key: string): Promise { const log = logger.extend ('remove'); log ('removing key %s from redis', key); if (!this.is_active) { log ('redis is inactive, skipping'); return; } await this.redis.del (`blacklist_${key}`); log ('removed key'); } public async get (key: string): Promise { const log = logger.extend ('get'); log ('trying to find key %s in redis blacklist', key); if (!this.is_active) { log ('redis is inactive, skipping'); return false; } const res = await this.redis.exists (`blacklist_${key}`) === 1; log ('found key %s', res); return res; } } export const redis_blacklist_store = new RedisBlacklistStore;