Created
April 4, 2014 18:49
-
-
Save metadaddy/9980861 to your computer and use it in GitHub Desktop.
Decode a JSON Web Token (JWT), verifying against a bare RSA key
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
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