Parsing a JWT token using javascript/python

Hi,

I need to parse a jwt token to extract the attributes within. How can I do that using javascript or python ? I first need to decode base64 string and then extract the attributes.

Thanks

0 1 1,758
1 REPLY 1

Hi,

You can refer the following javascript code snippet. This snippet expects the JWT payload to have "exp", "iss", "sub" and "aud" mandatorily & "nbf" as optional. The jwt token is passed as JSON request for a POST operation. There is no need to decode the header and payload separately. The jws-3.2.js library can do it for you.

// validateJwt.js
// ------------------------------------------------------------------
//
// Verify / validate a JWT using the jws-3.2.js library from Kenji Urushima.
//
// created: Wed May 27 09:52:15 2015
// last saved: <2015-May-27 12:12:26>


// KJUR.jws.JWS is defined in jws-3.2.js
var jws = new KJUR.jws.JWS();
var acceptAlgs = ['RS256','RS384','RS512', 'ES256', 'ES384'];
try{
  jwt = JSON.parse(context.getVariable('request.content')).jwt;
  jws.parseJWS(jwt);
  var payload = JSON.parse(jws.parsedJWS.payloadS);
  var isValid = KJUR.jws.JWS.verify(jwt, password, acceptAlgs);
  //extract all payload attributes explicitly to verify them. A
}
catch(ex){
  context.setVariable('jwt-error', 'contentError');
  throw(ex);
}