I am trying to generate JWS signature for PSD2 purposes (RFC 7515). I have the following functions:
function signJson(json, privateKey) {
const header = {
alg: 'RS256',
typ: 'JWT'
};
const payload = JSON.stringify(json);
const data = base64UrlEncode(header) + '.' + base64UrlEncode(payload);
const signature = crypto.createSign('RSA-SHA256').update(data).sign(privateKey, 'base64');
return data + '.' + signature;
}
function base64UrlEncode(data) {
return data.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
But the returning string is wrong JWT as it not begins with "ey". Where I am making mistake?
Here is how we did this using the node-jwa library, probably the same can be done with
crypto.createSign
.