const KEY_FILE = "keys.json";
const len = 32;
const keys = {
"k1":crypto.randomBytes(len).toString('hex'),
"k2":crypto.randomBytes(len).toString('hex'),
"k3":crypto.randomBytes(len).toString('hex'),
};
fs.writeFileSync(KEY_FILE, JSON.stringify(keys));
const createKeyHolder = function() {
let keyHolder = utils.createObject("JWT_KEY_HOLDER");
keyHolder.loadKeyFile = function(keyFile) {
const keyfileContents = fs.readFileSync(keyFile);
const keys = JSON.parse(keyfileContents);
for(kid in keys) {
this[kid] = keys[kid];
this.logger.log(`Loaded key with id: ${kid}`);
}
}
keyHolder.getKey = function(kid) {
this.logger.log(`Attempting to retrieve key with key id ${kid}`);
if(kid in this) {
this.logger.log(`Key[${kid}] found.`);
return this[kid];
}
this.logger.log(`Key[${kid}] not found!`);
return null;
}
return keyHolder;
}