Skip to content

Instantly share code, notes, and snippets.

@felickz
Created August 8, 2019 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felickz/14d7af4fc47d8b02e51e08031ec6f5f2 to your computer and use it in GitHub Desktop.
Save felickz/14d7af4fc47d8b02e51e08031ec6f5f2 to your computer and use it in GitHub Desktop.
Postman Test syntax to decode and validate an Application JWT token from Azure AD with Scopes(roles)
function jwtDecode(t) {
let token = {};
token.raw = t;
token.header = JSON.parse(atob(t.toString().split('.')[0]));
token.payload = JSON.parse(atob(t.toString().split('.')[1]));
return (token);
}
console.log("JWT Decode");
var jwt = jwtDecode(pm.response.json().access_token);
console.log(jwt);
pm.test("Access token to needs to have application roles assigned and granted", function () {
pm.expect(jwt.payload.roles).to.not.equal(undefined);
});
pm.test("Access token must have 1 role", function () {
pm.expect(jwt.payload.roles.length).to.equal(1);
});
pm.test("Access token must have XYZ role", function () {
pm.expect(jwt.payload.roles.includes("XYZ")).to.equal(true);
});
if(jwt.payload.roles !== undefined)
{
pm.test("Access token - JWT Roles: " + jwt.payload.roles.join(), function(){});
}
@felickz
Copy link
Author

felickz commented Aug 8, 2019

No idea where i kludged this together from but it took me some time to get the postman js library references right ... make sure to open the console in postman to see the full JWT!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment