mirror of
https://github.com/hanzoai/node-jsonwebtoken.git
synced 2026-09-24 17:00:48 +00:00
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:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user