I am trying to load an external .pem file to sign with JWT in node.js, but jwt.sign does not return anything. It seams that the .pem file is not loading correct.
Hoping that someone can help.
Here is the code:
function(properties, context) {
const jwt = require('jsonwebtoken');
const https = require('https');
https.get(properties.privateKeyName, res => {
// Initialise an array
const bufs = [];
// Add the data to the buffer collection
res.on('data', function (chunk) {
bufs.push(chunk)
});
// This signifies the end of a request
res.on('end', function () {
// We can join all of the 'chunks' of the file together
const privateKey = Buffer.concat(bufs);
const issuer = properties.issuer;
const client_id = properties.client_id;
const aud = 'https://revolut.com'; // Constant
const payload = {
"iss": issuer,
"sub": client_id,
"aud": aud
};
const token = jwt.sign(payload, privateKey, { algorithm: 'RS256', expiresIn: 60 * 60});
return {
'jwt_token': token,
'jwt_test': 'test',
}
});
})
}