/*
 * 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
 */

interface Signature {
  hash: string;
  iat: Date;
}

class Blacklist {
  private _signatures: Signature[];

  public constructor () {
    this._signatures = [];
  }

  public clear_before (date: Date) {
    for (let i = this._signatures.length - 1; i >= 0; i--) {
      if (this._signatures[i].iat < date)
        this._signatures.splice (i, 1);
    }
  }

  public add_signature (hash: string) {
    this._signatures.push ({ iat: (new Date), hash });
  }

  public remove_signature (hash:string) {
    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) {
    for (const sig of this._signatures) {
      if (sig.hash === hash)
        return false;
    }

    return true;
  }
}

const bl = (new Blacklist);
export default bl;