auth-server-helper/lib/KeyStore.ts
Timo Hocker 0be180f632
Some checks failed
continuous-integration/drone/push Build is failing
key store
2020-12-06 21:08:42 +01:00

27 lines
622 B
TypeScript

import { create_salt } from '@sapphirecode/crypto-helper';
class KeyStore {
private _keys: Record<string, string> = {};
public get_key (iat: number, valid_for = 0): string {
const key = Math.floor (iat / 60)
.toFixed (0);
if (typeof this._keys[key] === 'string')
return this._keys[key];
if (valid_for !== 0) {
this._keys[key] = create_salt ();
setTimeout (() => {
delete this._keys[key];
}, (valid_for + 5) * 1000);
return this._keys[key];
}
throw new Error ('key could not be found');
}
}
const ks: KeyStore = (new KeyStore);
export default ks;