I'm creating a JWT token for an API in Postman and when I did this process manually, it works without issue. However, when I take the call and put it into Postman Runner, I get an error stating 'ReferenceError: KEYUTIL is not defined'.
I'm wondering if it's because the library that I'm loading isn't loading before the pre-request script needs it to?
I'm making a POST call to https://account-d.docusign.com/oauth/token
Header: Authorization: Basic {encodedKeys} Body: x-www-form-urlencoded grant_type: urn:ietf:params:oauth:grant-type:jwt-bearer assertion: {JWT_assertion}
pre-request script:
// Loading the jsrsasign library into Postman Sandbox
var navigator = {}; // Fake a navigator object for the lib
var window = {}; // Fake a window object for the lib
eval(pm.environment.get("jsrsasign")); // Import JavaScript jsrsasign
var timestamp = new Date(); // The current time in milliseconds
var issuedAtTimeSeconds = timestamp/1000;
var expirationTimeSeconds = timestamp/1000 + 3600;
// Create header and payload objects
var header = {
"typ": "JWT",
"alg": "RS256"
};
var payload = {
"iss": "{DS Integration Key}",
"sub": "{DS User}",
"iat" : Math.ceil(issuedAtTimeSeconds),
"exp" : Math.ceil(expirationTimeSeconds),
"aud": "account-d.docusign.com",
"scope": "signature impersonation organization_read"
};
// Prep the objects for a JWT
var sHeader = JSON.stringify(header);
var sPayload = JSON.stringify(payload);
var jwk = pm.environment.get("jwk");
var prvKey = KEYUTIL.getKey(jwk);
var sJWT = KJUR.jws.JWS.sign(header.alg, sHeader, sPayload, prvKey);
pm.environment.set("JWT_assertion", sJWT);
Tests:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("JWT_access_token", jsonData.access_token);
I have the jsrsasign library saved in an environment variable. Here's a link to the library.
I'm following this example from DocuSign which works, but they don't use Postman Runner in their example.
I set up a scheduled run using Postman runner and it failed, as does a manual run from Postman runner, however if I run the call normally, it works without issue.