This commit is contained in:
@ -42,30 +42,32 @@ async function create_key (valid_for: number) {
|
||||
};
|
||||
}
|
||||
|
||||
function garbage_collect (set: KeyStoreData): void {
|
||||
const time = (new Date)
|
||||
.getTime ();
|
||||
for (const index of Object.keys (set)) {
|
||||
const entry = set[index];
|
||||
if (typeof entry.private_key !== 'undefined'
|
||||
&& entry.private_key.valid_until < time
|
||||
)
|
||||
delete entry.private_key;
|
||||
if (entry.public_key.valid_until < time)
|
||||
delete set[index];
|
||||
}
|
||||
}
|
||||
|
||||
class KeyStore {
|
||||
private _keys: KeyStoreData = {};
|
||||
private _interval: NodeJS.Timeout;
|
||||
|
||||
public constructor () {
|
||||
this._interval = setInterval (() => {
|
||||
garbage_collect (this._keys);
|
||||
this.garbage_collect ();
|
||||
}, renew_interval);
|
||||
}
|
||||
|
||||
private garbage_collect (set: KeyStoreData = this._keys): void {
|
||||
const time = (new Date)
|
||||
.getTime ();
|
||||
const keys = Object.keys (set);
|
||||
for (const index of keys) {
|
||||
const entry = set[index];
|
||||
if (typeof entry.private_key !== 'undefined'
|
||||
&& entry.private_key.valid_until < time
|
||||
)
|
||||
delete entry.private_key;
|
||||
|
||||
if (entry.public_key.valid_until < time)
|
||||
delete set[index];
|
||||
}
|
||||
}
|
||||
|
||||
public async get_sign_key (iat: number, valid_for: number): Promise<string> {
|
||||
if (valid_for <= 0)
|
||||
throw new Error ('cannot create infinitely valid key');
|
||||
@ -87,7 +89,7 @@ class KeyStore {
|
||||
return key.private_key?.key as string;
|
||||
}
|
||||
|
||||
this._keys[index] = await create_key (valid_until);
|
||||
this._keys[index] = await create_key (valid_for);
|
||||
return this._keys[index].private_key?.key as string;
|
||||
}
|
||||
|
||||
@ -102,7 +104,7 @@ class KeyStore {
|
||||
}
|
||||
|
||||
public export_verification_data (): KeyStoreData {
|
||||
garbage_collect (this._keys);
|
||||
this.garbage_collect ();
|
||||
const out: KeyStoreData = {};
|
||||
for (const index of Object.keys (this._keys))
|
||||
out[index] = { public_key: this._keys[index].public_key };
|
||||
@ -112,7 +114,7 @@ class KeyStore {
|
||||
|
||||
public import_verification_data (data: KeyStoreData): void {
|
||||
const import_set = { ...data };
|
||||
garbage_collect (import_set);
|
||||
this.garbage_collect (import_set);
|
||||
|
||||
// TODO: import
|
||||
}
|
||||
|
Reference in New Issue
Block a user