auth-server-helper/lib/Blacklist.ts

85 lines
2.2 KiB
TypeScript
Raw Normal View History

/*
* 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
*/
2022-01-05 12:32:04 +01:00
import { debug } from './debug';
const logger = debug ('blacklist');
2020-12-12 15:53:47 +01:00
interface Signature {
hash: string;
2021-01-15 14:45:05 +01:00
iat: number;
2020-12-12 15:53:47 +01:00
}
class Blacklist {
private _signatures: Signature[];
public constructor () {
this._signatures = [];
}
2021-05-10 12:41:00 +02:00
public clear (before: number = Number.POSITIVE_INFINITY): void {
2022-08-15 17:33:25 +02:00
logger.extend ('clear') ('clearing blacklist');
2020-12-12 15:53:47 +01:00
for (let i = this._signatures.length - 1; i >= 0; i--) {
2021-01-15 14:45:05 +01:00
if (this._signatures[i].iat < before)
2022-08-15 17:33:25 +02:00
this.remove_signature (i);
2020-12-12 15:53:47 +01:00
}
}
2021-05-10 12:41:00 +02:00
public add_signature (hash: string): void {
2022-08-15 17:33:25 +02:00
logger.extend ('add_signature') ('blacklisting signature %s', hash);
2021-01-15 14:45:05 +01:00
this._signatures.push ({ iat: Date.now (), hash });
2020-12-12 15:53:47 +01:00
}
2022-08-15 17:33:25 +02:00
public remove_signature (signature: number | string): void {
const log = logger.extend ('remove_signature');
log ('removing signature from blacklist %s', signature);
if (typeof signature === 'string') {
log ('received string, searching through signatures');
for (let i = this._signatures.length - 1; i >= 0; i--) {
if (this._signatures[i].hash === signature)
this._signatures.splice (i, 1);
}
}
else {
log ('received index, removing at index');
this._signatures.splice (signature, 1);
2020-12-12 15:53:47 +01:00
}
}
2021-05-10 12:41:00 +02:00
public is_valid (hash: string): boolean {
2022-08-15 17:33:25 +02:00
const log = logger.extend ('is_valid');
log ('checking signature for blacklist entry %s', hash);
2020-12-12 15:53:47 +01:00
for (const sig of this._signatures) {
2022-01-05 12:32:04 +01:00
if (sig.hash === hash) {
2022-08-15 17:33:25 +02:00
log ('found matching blacklist entry');
2020-12-12 15:53:47 +01:00
return false;
2022-01-05 12:32:04 +01:00
}
2020-12-12 15:53:47 +01:00
}
2022-08-15 17:33:25 +02:00
log ('signature is not blacklisted');
2020-12-12 15:53:47 +01:00
return true;
}
2021-01-09 12:20:14 +01:00
public export_blacklist (): Signature[] {
2022-08-15 17:33:25 +02:00
logger.extend ('export_blacklist') ('exporting blacklist');
2021-01-09 12:20:14 +01:00
return this._signatures;
}
public import_blacklist (data: Signature[]): void {
2022-08-15 17:33:25 +02:00
logger.extend ('import_blacklist') (
'importing %d blacklist entries',
data.length
);
2021-01-09 12:20:14 +01:00
this._signatures.push (...data);
}
2020-12-12 15:53:47 +01:00
}
const bl = (new Blacklist);
2021-01-05 17:06:35 +01:00
export { Blacklist };
2020-12-12 15:53:47 +01:00
export default bl;