simplify export data
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Timo Hocker 2021-01-14 21:31:21 +01:00
parent 9ec97d8aa2
commit ce58c0d204
3 changed files with 32 additions and 22 deletions

View File

@ -15,12 +15,17 @@ interface Key {
valid_until: number; valid_until: number;
} }
interface LabelledKey extends Key {
index: string;
}
interface KeyPair { interface KeyPair {
private_key?: Key; private_key?: Key;
public_key: Key; public_key: Key;
} }
type KeyStoreData = Record<string, KeyPair>; type KeyStoreData = Record<string, KeyPair>;
type KeyStoreExport = LabelledKey[];
async function create_key (valid_for: number) { async function create_key (valid_for: number) {
const time = (new Date) const time = (new Date)
@ -59,19 +64,19 @@ class KeyStore {
.toFixed (0); .toFixed (0);
} }
private garbage_collect (set: KeyStoreData = this._keys): void { private garbage_collect (): void {
const time = (new Date) const time = (new Date)
.getTime (); .getTime ();
const keys = Object.keys (set); const keys = Object.keys (this._keys);
for (const index of keys) { for (const index of keys) {
const entry = set[index]; const entry = this._keys[index];
if (typeof entry.private_key !== 'undefined' if (typeof entry.private_key !== 'undefined'
&& entry.private_key.valid_until < time && entry.private_key.valid_until < time
) )
delete entry.private_key; delete entry.private_key;
if (entry.public_key.valid_until < time) if (entry.public_key.valid_until < time)
delete set[index]; delete this._keys[index];
} }
} }
@ -117,22 +122,25 @@ class KeyStore {
return key.public_key.key; return key.public_key.key;
} }
public export_verification_data (): KeyStoreData { public export_verification_data (): KeyStoreExport {
this.garbage_collect (); this.garbage_collect ();
const out: KeyStoreData = {}; const out: KeyStoreExport = [];
for (const index of Object.keys (this._keys)) for (const index of Object.keys (this._keys))
out[index] = { public_key: this._keys[index].public_key }; out.push ({ ...this._keys[index].public_key, index });
return out; return out;
} }
public import_verification_data (data: KeyStoreData): void { public import_verification_data (data: KeyStoreExport): void {
const import_set = { ...data }; for (const key of data) {
this.garbage_collect (import_set); if (typeof this._keys[key.index] !== 'undefined')
for (const key of Object.keys (import_set)) {
if (typeof this._keys[key] !== 'undefined')
throw new Error ('cannot import to the same instance'); throw new Error ('cannot import to the same instance');
this._keys[key] = import_set[key]; this._keys[key.index] = {
public_key: {
key: key.key,
valid_until: key.valid_until
}
};
} }
this.garbage_collect (); this.garbage_collect ();
} }
@ -140,4 +148,4 @@ class KeyStore {
const ks: KeyStore = (new KeyStore); const ks: KeyStore = (new KeyStore);
export default ks; export default ks;
export { KeyStore, KeyStoreData, Key, KeyPair }; export { KeyStore, Key, LabelledKey, KeyStoreExport };

View File

@ -28,7 +28,10 @@ import create_gateway, {
Gateway, Gateway,
AnyFunc AnyFunc
} from './Gateway'; } from './Gateway';
import keystore, { KeyStore, KeyStoreData } from './KeyStore'; import keystore, {
KeyStore, KeyStoreExport,
LabelledKey, Key
} from './KeyStore';
export { export {
create_gateway, create_gateway,
@ -54,5 +57,7 @@ export {
Gateway, Gateway,
AnyFunc, AnyFunc,
KeyStore, KeyStore,
KeyStoreData KeyStoreExport,
LabelledKey,
Key
}; };

View File

@ -155,17 +155,14 @@ describe ('key store', () => {
const exp = ks.export_verification_data (); const exp = ks.export_verification_data ();
// eslint-disable-next-line dot-notation // eslint-disable-next-line dot-notation
expect (Object.keys (ks['_keys'])) expect (Object.keys (ks['_keys']))
.toEqual (Object.keys (exp)); .toEqual (exp.map ((v) => v.index));
expect (Object.keys (exp)
.filter ((v) => typeof exp[v].private_key !== 'undefined').length)
.toEqual (0);
const ks2 = (new KeyStore); const ks2 = (new KeyStore);
expect (ks2.instance_id).not.toEqual (ks.instance_id); expect (ks2.instance_id).not.toEqual (ks.instance_id);
ks2.import_verification_data (exp); ks2.import_verification_data (exp);
// eslint-disable-next-line dot-notation // eslint-disable-next-line dot-notation
expect (ks2['_keys']) expect (Object.keys (ks2['_keys']))
.toEqual (exp); .toEqual (exp.map ((v) => v.index));
const sign2 = await ks2.get_sign_key (iat, frame); const sign2 = await ks2.get_sign_key (iat, frame);
const ver2 = ks2.get_key (iat); const ver2 = ks2.get_key (iat);