Skip to content

Instantly share code, notes, and snippets.

@deviantlycan
Created April 17, 2019 21:09
Show Gist options
  • Save deviantlycan/226b0fd4b6a29f26a933f89994980c56 to your computer and use it in GitHub Desktop.
Save deviantlycan/226b0fd4b6a29f26a933f89994980c56 to your computer and use it in GitHub Desktop.
/**
* This snippet can be used in postman to read the cotnents of a
* JWT and set a postman variable to the value of one of the claims
*/
let jsonData = pm.response.json();
// use whatever key in the response contains the jwt you want to look into. This example is using access_token
let jwtContents = jwt_decode(jsonData.access_token);
// Now you can set a postman variable with the value of a claim in the JWT
pm.variable.set("someClaim", jwtContents.payload.someClaim);
function jwt_decode(jwt) {
var parts = jwt.split('.'); // header, payload, signature
let tokenContents={};
tokenContents.header = JSON.parse(atob(parts[0]));
tokenContents.payload = JSON.parse(atob(parts[1]));
tokenContents.signature = atob(parts[2]);
// this just lets you see the jwt contents in the postman console.
console.log("Token Contents:\n" + JSON.stringify(tokenContents, null, 2));
return tokenContents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment