From a3f021fdd28511abe53d436ac8bdbe584fae2f58 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 15 Jan 2021 14:45:05 +0100 Subject: [PATCH] clearing instances --- README.md | 20 +++++++++++++++++++- lib/Blacklist.ts | 8 ++++---- lib/KeyStore.ts | 5 +++++ package.json | 2 +- test/spec/Blacklist.ts | 22 +++++++++++++++++++++- test/spec/KeyStore.ts | 12 ++++++++++++ 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9034fee..e02b674 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # auth-server-helper -version: 2.0.0 +version: 2.0.x customizable and simple authentication @@ -128,6 +128,24 @@ const export = blacklist.export_blacklist(); blacklist.import_blacklist(export); ``` +### Clearing Keystore and Blacklist + +Resetting the Keystore instance generates a new instance id and deletes all +imported or generated keys. + +```js +const {keystore, blacklist} = require('@sapphirecode/auth-server-helper'); + +// clear keystore +keystore.reset_instance(); + +// clear blacklist +blacklist.clear(); + +// clear blacklist items older than 10 seconds +blacklist.clear(Date.now() - 10000); +``` + ## License MIT © Timo Hocker diff --git a/lib/Blacklist.ts b/lib/Blacklist.ts index 2d3d73b..4d5ee61 100644 --- a/lib/Blacklist.ts +++ b/lib/Blacklist.ts @@ -7,7 +7,7 @@ interface Signature { hash: string; - iat: Date; + iat: number; } class Blacklist { @@ -17,15 +17,15 @@ class Blacklist { this._signatures = []; } - public clear_before (date: Date):void { + public clear (before: number = Number.POSITIVE_INFINITY):void { for (let i = this._signatures.length - 1; i >= 0; i--) { - if (this._signatures[i].iat < date) + if (this._signatures[i].iat < before) this._signatures.splice (i, 1); } } public add_signature (hash: string):void { - this._signatures.push ({ iat: (new Date), hash }); + this._signatures.push ({ iat: Date.now (), hash }); } public remove_signature (hash:string):void { diff --git a/lib/KeyStore.ts b/lib/KeyStore.ts index fa2a474..635fb20 100644 --- a/lib/KeyStore.ts +++ b/lib/KeyStore.ts @@ -144,6 +144,11 @@ class KeyStore { } this.garbage_collect (); } + + public reset_instance (): void { + this._instance = to_b58 (random_hex (16), 'hex'); + this._keys = {}; + } } const ks: KeyStore = (new KeyStore); diff --git a/package.json b/package.json index c65e37a..46219f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sapphirecode/auth-server-helper", - "version": "2.0.0", + "version": "2.0.1", "main": "dist/index.js", "author": { "name": "Timo Hocker", diff --git a/test/spec/Blacklist.ts b/test/spec/Blacklist.ts index 3edc32a..d2f359b 100644 --- a/test/spec/Blacklist.ts +++ b/test/spec/Blacklist.ts @@ -52,7 +52,7 @@ describe ('blacklist', () => { jasmine.clock () .tick (5000); blacklist.add_signature ('baz'); - blacklist.clear_before (new Date (Date.now () - 100)); + blacklist.clear (Date.now () - 100); expect (blacklist.is_valid ('foo')) .toBeTrue (); expect (blacklist.is_valid ('bar')) @@ -61,7 +61,27 @@ describe ('blacklist', () => { .toBeFalse (); }); + it ('should clear all', () => { + blacklist.add_signature ('foo'); + blacklist.add_signature ('bar'); + blacklist.add_signature ('baz'); + expect (blacklist.is_valid ('foo')) + .toBeFalse (); + expect (blacklist.is_valid ('bar')) + .toBeFalse (); + expect (blacklist.is_valid ('baz')) + .toBeFalse (); + blacklist.clear (); + expect (blacklist.is_valid ('foo')) + .toBeTrue (); + expect (blacklist.is_valid ('bar')) + .toBeTrue (); + expect (blacklist.is_valid ('baz')) + .toBeTrue (); + }); + it ('should export and import data', () => { + blacklist.add_signature ('baz'); const exp = blacklist.export_blacklist (); // eslint-disable-next-line dot-notation expect (blacklist['_signatures']) diff --git a/test/spec/KeyStore.ts b/test/spec/KeyStore.ts index 175b6f7..fdf4627 100644 --- a/test/spec/KeyStore.ts +++ b/test/spec/KeyStore.ts @@ -179,4 +179,16 @@ describe ('key store', () => { expect (() => ks.import_verification_data (exp)) .toThrowError ('cannot import to the same instance'); }); + + it ('should clear all', () => { + // eslint-disable-next-line dot-notation + expect (Object.keys (ks['_keys']).length) + .toBeGreaterThan (0); + const instance = ks.instance_id; + ks.reset_instance (); + // eslint-disable-next-line dot-notation + expect (Object.keys (ks['_keys']).length) + .toEqual (0); + expect (instance).not.toEqual (ks.instance_id); + }); });