auth-server-helper/lib/Blacklist.ts

73 lines
1.8 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-01-05 12:32:04 +01:00
logger ('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)
2020-12-12 15:53:47 +01:00
this._signatures.splice (i, 1);
}
}
2021-05-10 12:41:00 +02:00
public add_signature (hash: string): void {
2022-01-05 12:32:04 +01:00
logger ('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
}
2021-05-10 12:41:00 +02:00
public remove_signature (hash: string): void {
2022-01-05 12:32:04 +01:00
logger ('removing signature from blacklist %s', hash);
2020-12-12 15:53:47 +01:00
for (let i = this._signatures.length - 1; i >= 0; i--) {
if (this._signatures[i].hash === hash)
this._signatures.splice (i, 1);
}
}
2021-05-10 12:41:00 +02:00
public is_valid (hash: string): boolean {
2022-01-05 12:32:04 +01:00
logger ('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) {
logger ('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-01-05 12:32:04 +01:00
logger ('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-01-05 12:32:04 +01:00
logger ('exporting blacklist');
2021-01-09 12:20:14 +01:00
return this._signatures;
}
public import_blacklist (data: Signature[]): void {
2022-01-05 12:32:04 +01:00
logger ('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;