Skip to content

Commit

Permalink
fix issue with buffer payload. closes #216
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Jul 15, 2016
1 parent 184f28d commit 6b50ff3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ var options_for_objects = [
module.exports = function (payload, secretOrPrivateKey, options, callback) {
options = options || {};

var isObjectPayload = typeof payload === 'object' &&
!Buffer.isBuffer(payload);

var header = xtend({
alg: options.algorithm || 'HS256',
typ: typeof payload === 'object' ? 'JWT' : undefined
typ: isObjectPayload ? 'JWT' : undefined
}, options.header);

function failure(err) {
Expand All @@ -56,17 +59,18 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
throw err;
}


if (typeof payload === 'undefined') {
return failure(new Error('payload is required'));
} else if (typeof payload === 'object') {
} else if (isObjectPayload) {
var payload_validation_result = registered_claims_schema.validate(payload);

if (payload_validation_result.error) {
return failure(payload_validation_result.error);
}

payload = xtend(payload);
} else if (typeof payload !== 'object') {
} else {
var invalid_options = options_for_objects.filter(function (opt) {
return typeof options[opt] !== 'undefined';
});
Expand Down
10 changes: 10 additions & 0 deletions test/buffer.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var jwt = require("../.");
var assert = require('chai').assert;

describe('buffer payload', function () {
it('should work', function () {
var payload = new Buffer('TkJyotZe8NFpgdfnmgINqg==', 'base64');
var token = jwt.sign(payload, "signing key");
assert.equal(jwt.decode(token), payload.toString());
});
});

0 comments on commit 6b50ff3

Please sign in to comment.