Created
August 8, 2019 17:11
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!