Merge pull request #62 from auth0/update_jws_utf8

this is a major change, it changes the default encoding from binary to utf8
This commit is contained in:
José F. Romaniello
2015-03-06 11:37:17 -03:00
4 changed files with 51 additions and 3 deletions
+20
View File
@@ -0,0 +1,20 @@
{
"evil": true,
"regexdash": true,
"browser": true,
"wsh": true,
"trailing": true,
"sub": true,
"unused": true,
"undef": true,
"laxcomma": true,
"node": true,
"browser": false,
"globals": {
"describe": true,
"it": true,
"require": true,
"atob": false,
"escape": true
}
}
+6 -1
View File
@@ -43,7 +43,12 @@ module.exports.sign = function(payload, secretOrPrivateKey, options) {
if (options.subject)
payload.sub = options.subject;
var signed = jws.sign({header: header, payload: payload, secret: secretOrPrivateKey});
var encoding = 'utf8';
if (options.encoding) {
encoding = options.encoding;
}
var signed = jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding});
return signed;
};
+1 -1
View File
@@ -19,7 +19,7 @@
"url": "https://github.com/auth0/node-jsonwebtoken/issues"
},
"dependencies": {
"jws": "~1.0.1"
"jws": "~2.0.0"
},
"devDependencies": {
"atob": "~1.1.2",
+24 -1
View File
@@ -4,11 +4,34 @@ var atob = require('atob');
describe('encoding', function() {
it('should properly encode the token', function () {
function b64_to_utf8 (str) {
return decodeURIComponent(escape(atob( str )));
}
it('should properly encode the token (utf8)', function () {
var expected = 'José';
var token = jwt.sign({ name: expected }, 'shhhhh');
var decoded_name = JSON.parse(b64_to_utf8(token.split('.')[1])).name;
expect(decoded_name).to.equal(expected);
});
it('should properly encode the token (binary)', function () {
var expected = 'José';
var token = jwt.sign({ name: expected }, 'shhhhh', { encoding: 'binary' });
var decoded_name = JSON.parse(atob(token.split('.')[1])).name;
expect(decoded_name).to.equal(expected);
});
it('should return the same result when decoding', function () {
var username = '測試';
var token = jwt.sign({
username: username
}, 'test');
var payload = jwt.verify(token, 'test');
expect(payload.username).to.equal(username);
});
});