Skip to content

Instantly share code, notes, and snippets.

@harinduravin
Created January 19, 2021 08:32
Show Gist options
  • Save harinduravin/ed1c61675c5f42a66b2f5b5466266fa3 to your computer and use it in GitHub Desktop.
Save harinduravin/ed1c61675c5f42a66b2f5b5466266fa3 to your computer and use it in GitHub Desktop.
Pre request script for the Post request
// Sketchy loading of 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.globals.get("jsrsasign-js")); //import javascript jsrsasign
var currentTime = +new Date(); // the current time in milliseconds
var issuedAtTimeSeconds = currentTime/1000;
var expirationTimeSeconds = currentTime/1000 + 2592000;
// Generate random string for "jti" claim - needed if client has Replay Prevention enabled
var newJti="";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 12; i++ ) {
newJti += charset.charAt(Math.floor(Math.random() * charset.length));
}
pm.variables.set("jti", newJti);
// Create Header and Payload objects
var header = {
"typ": "JWT",
"kid": "DwMKdWMmj7PWinvoqfQyXVzyZ6Q",
"alg": "PS256"
};
var payload = {
"iss": "9b5usDpbNtmxDcTzs7GzKp",
"exp" : Math.ceil(expirationTimeSeconds),
"iat" : Math.ceil(issuedAtTimeSeconds),
"jti": "1601982046",
"aud": "https://localhost:8243/token",
"scope": "accounts payments",
"token_endpoint_auth_method": "private_key_jwt",
"grant_types": [
"authorization_code",
"refresh_token"
],
"response_types": [
"code id_token"
],
"id_token_signed_response_alg": "PS256",
"request_object_signing_alg": "PS256",
"software_id": "9b5usDpbNtmxDcTzs7GzKp",
"application_type": "web",
"redirect_uris": [
"https://wso2.com"
],
"token_endpoint_auth_signing_alg": "PS256",
"software_statement": pm.environment.get("software_statement"),
"backchannel_token_delivery_mode": "ping",
"backchannel_client_notification_endpoint": "https://docs.wso2.com/display/OB200/Dynamic+Client+Registration+v3.2#c97c86b28d9d4688992e0eab0bd4bf1b",
"backchannel_authentication_request_signing_alg": "PS256",
"backchannel_user_code_parameter_supported": false
};
// Prep the objects for a JWT
var sHeader = JSON.stringify(header);
var sPayload = JSON.stringify(payload);
var prvKey = pm.globals.get("private_key");
var sJWT = KJUR.jws.JWS.sign(header.alg, sHeader, sPayload, prvKey);
pm.environment.set("post_body", sJWT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment