clearing instances
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2021-01-15 14:45:05 +01:00
parent d286548850
commit a3f021fdd2
6 changed files with 62 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# auth-server-helper # auth-server-helper
version: 2.0.0 version: 2.0.x
customizable and simple authentication customizable and simple authentication
@ -128,6 +128,24 @@ const export = blacklist.export_blacklist();
blacklist.import_blacklist(export); 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 ## License
MIT © Timo Hocker <timo@scode.ovh> MIT © Timo Hocker <timo@scode.ovh>

View File

@ -7,7 +7,7 @@
interface Signature { interface Signature {
hash: string; hash: string;
iat: Date; iat: number;
} }
class Blacklist { class Blacklist {
@ -17,15 +17,15 @@ class Blacklist {
this._signatures = []; 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--) { 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); this._signatures.splice (i, 1);
} }
} }
public add_signature (hash: string):void { 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 { public remove_signature (hash:string):void {

View File

@ -144,6 +144,11 @@ class KeyStore {
} }
this.garbage_collect (); this.garbage_collect ();
} }
public reset_instance (): void {
this._instance = to_b58 (random_hex (16), 'hex');
this._keys = {};
}
} }
const ks: KeyStore = (new KeyStore); const ks: KeyStore = (new KeyStore);

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/auth-server-helper", "name": "@sapphirecode/auth-server-helper",
"version": "2.0.0", "version": "2.0.1",
"main": "dist/index.js", "main": "dist/index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",

View File

@ -52,7 +52,7 @@ describe ('blacklist', () => {
jasmine.clock () jasmine.clock ()
.tick (5000); .tick (5000);
blacklist.add_signature ('baz'); blacklist.add_signature ('baz');
blacklist.clear_before (new Date (Date.now () - 100)); blacklist.clear (Date.now () - 100);
expect (blacklist.is_valid ('foo')) expect (blacklist.is_valid ('foo'))
.toBeTrue (); .toBeTrue ();
expect (blacklist.is_valid ('bar')) expect (blacklist.is_valid ('bar'))
@ -61,7 +61,27 @@ describe ('blacklist', () => {
.toBeFalse (); .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', () => { it ('should export and import data', () => {
blacklist.add_signature ('baz');
const exp = blacklist.export_blacklist (); const exp = blacklist.export_blacklist ();
// eslint-disable-next-line dot-notation // eslint-disable-next-line dot-notation
expect (blacklist['_signatures']) expect (blacklist['_signatures'])

View File

@ -179,4 +179,16 @@ describe ('key store', () => {
expect (() => ks.import_verification_data (exp)) expect (() => ks.import_verification_data (exp))
.toThrowError ('cannot import to the same instance'); .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);
});
}); });