Skip to content

Instantly share code, notes, and snippets.

@mwillbanks
Created October 1, 2014 15:12
Show Gist options
  • Save mwillbanks/0bcd17b02ac2726bbe33 to your computer and use it in GitHub Desktop.
Save mwillbanks/0bcd17b02ac2726bbe33 to your computer and use it in GitHub Desktop.
Create a token from the psecio/jwt package with encoding, copy over the secrets, encoding, etc and then apply the token.
var jwt = require('jsonwebtoken'),
crypto = require('crypto')
var token = '';
var cypher = 'aes-256-cbc';
var iv = '1234567812345678';
var key = 'my-encryption-key';
var secret = 'example_key';
// it comes over encrypted, so we have to get it all cleaned up
var decode = function(token, callback) {
token = token.split('.');
var claims = token[1];
if (claims.length % 4 != 0) {
claims += ('===').slice(0, 4 - (claims.length % 4));
}
claims = claims .replace(/-/g, '+').replace(/_/g, '/');
// they were double base64 encoded... watch out :)
// this is the psecio jwt library, it ultimately encodes the claims twice
// since the encryption does it first and then the encoder does it a second
// time.
claims = new Buffer(claims, 'base64');
claims = new Buffer(claims.toString('utf8'), 'base64');
var decipher = crypto.createDecipheriv(cypher, new Buffer(key, 'ascii'), new Buffer(iv, 'ascii'));
var decoded = Buffer.concat([
decipher.update(claims),
decipher.final()
]);
console.log(decoded.toString('utf8'));
token[1] = decoded.toString('base64');
token[1] = token[1].replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
token = token.join('.');
callback(token);
};
decode(token, function(token) {
console.log(token);
console.log(jwt.decode(token));
jwt.verify(token, secret, function(err, decoded) {
console.log(err);
console.log(decoded);
});
});
{
"dependencies": {
"jsonwebtoken": "^1.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment