Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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