Skip to content

Commit

Permalink
allow passing encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Mar 6, 2015
1 parent 92d33bd commit 1fc385e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
9 changes: 8 additions & 1 deletion test/encoding.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '測試';

Expand Down

0 comments on commit 1fc385e

Please sign in to comment.