Skip to content

Instantly share code, notes, and snippets.

@aroemen
Created September 22, 2021 20:43
Show Gist options
  • Save aroemen/91d4368cf5714f5d32ea5bdb53d221f9 to your computer and use it in GitHub Desktop.
Save aroemen/91d4368cf5714f5d32ea5bdb53d221f9 to your computer and use it in GitHub Desktop.
Postman RS256 JWT Signing
var jwtPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`;
// Set headers for JWT
var header = {
'alg': 'RS256',
'typ': 'JWT'
};
// Prepare timestamp in seconds
var currentTimestamp = Math.floor(Date.now() / 1000);
var payload = {
'iat': currentTimestamp,
'exp': currentTimestamp + 60 * 5
};
function generateJwt() {
eval(pm.globals.get('jsrsasign-js')); // import javascript jsrsasign
var sHeader = JSON.stringify(header);
var sPayload = JSON.stringify(payload);
var signedToken = KJUR.jws.JWS.sign(header.alg, sHeader, sPayload, jwtPrivateKey);
pm.environment.set('assertionToken', signedToken);
// console.log('jwt', signedToken);
}
var navigator = {}; // fake a navigator object for the lib
var window = {}; // fake a window object for the lib
if (pm.globals.has('jsrsasign-js')) generateJwt();
else pm.sendRequest(
'https://kjur.github.io/jsrsasign/jsrsasign-all-min.js',
function (err, res) {
if (err) {
console.log(err);
} else {
pm.globals.set('jsrsasign-js', res.text());
generateJwt();
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment