caesarjs
Version:
A microservice framework for node.js implementing client-server mutual certificate authentication
35 lines (26 loc) • 965 B
JavaScript
const perfy = require('perfy');
const reqDataDecrypt = (encryptor) => (req, res, next) => {
const data = req.get('encryptor-data');
const clientId = req.get('encryptor-client-id');
// decrypt data
const encryptorDecryptedData = encryptor.decrypt(data);
const encryptorDecryptedClientId = encryptor.decrypt(clientId);
if(encryptorDecryptedData == null)
{
// encryptor key is wrong - bad decryption
res.status(500).json({error: 'encryptor key is wrong!'});
return;
}
// pass to controller's req object
req.data = encryptorDecryptedData;
req.clientId = encryptorDecryptedClientId;
// emit event received data
req.caesar.events.emitReceiveData(req.data, encryptorDecryptedClientId);
// add stats
req.statsDb.addStat(req.path, encryptorDecryptedClientId);
// start metrics by path
perfy.start(req.path);
next();
};
module.exports = reqDataDecrypt;
;