Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Created April 4, 2014 18:49
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 metadaddy/9980861 to your computer and use it in GitHub Desktop.
Save metadaddy/9980861 to your computer and use it in GitHub Desktop.
Decode a JSON Web Token (JWT), verifying against a bare RSA key
var jwt = require('jwt-simple');
var getpem = require('rsa-pem-from-mod-exp');
// Decode JWT token, verify with the relevant key from the supplied
// array
function decodeIdToken(idtoken, keys, code) {
var header = JSON.parse(new Buffer(idtoken.split('.')[0], 'base64').toString('utf8'));
for (var i = 0; i < keys.length; i++) {
if (keys[i].kid === header.kid) {
return jwt.decode(idtoken, getpem(keys[i].n, keys[i].e));;
} catch (e) {
console.log(e);
return null;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment