diff --git a/index.js b/index.js index 7a146e3..60e9be6 100644 --- a/index.js +++ b/index.js @@ -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; }; diff --git a/test/encoding.tests.js b/test/encoding.tests.js index 6c013a8..7a8b54a 100644 --- a/test/encoding.tests.js +++ b/test/encoding.tests.js @@ -8,13 +8,20 @@ describe('encoding', function() { return decodeURIComponent(escape(atob( str ))); } - it('should properly encode the token', function () { + 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 = '測試';