auth-server-helper/lib/Blacklist.ts
Timo Hocker b58af27719
All checks were successful
continuous-integration/drone/push Build is passing
add debug logging
2022-01-05 12:32:04 +01:00

73 lines
1.8 KiB
TypeScript

/*
* 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>, December 2020
*/
import { debug } from './debug';
const logger = debug ('blacklist');
interface Signature {
hash: string;
iat: number;
}
class Blacklist {
private _signatures: Signature[];
public constructor () {
this._signatures = [];
}
public clear (before: number = Number.POSITIVE_INFINITY): void {
logger ('clearing blacklist');
for (let i = this._signatures.length - 1; i >= 0; i--) {
if (this._signatures[i].iat < before)
this._signatures.splice (i, 1);
}
}
public add_signature (hash: string): void {
logger ('blacklisting signature %s', hash);
this._signatures.push ({ iat: Date.now (), hash });
}
public remove_signature (hash: string): void {
logger ('removing signature from blacklist %s', hash);
for (let i = this._signatures.length - 1; i >= 0; i--) {
if (this._signatures[i].hash === hash)
this._signatures.splice (i, 1);
}
}
public is_valid (hash: string): boolean {
logger ('checking signature for blacklist entry %s', hash);
for (const sig of this._signatures) {
if (sig.hash === hash) {
logger ('found matching blacklist entry');
return false;
}
}
logger ('signature is not blacklisted');
return true;
}
public export_blacklist (): Signature[] {
logger ('exporting blacklist');
return this._signatures;
}
public import_blacklist (data: Signature[]): void {
logger ('importing %d blacklist entries', data.length);
this._signatures.push (...data);
}
}
const bl = (new Blacklist);
export { Blacklist };
export default bl;