Skip to content

Commit

Permalink
throw error on invalid options when the payload is not an object
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Apr 27, 2016
1 parent 5835f55 commit 304f1b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ var options_to_payload = {
'jwtid': 'jti'
};

var options_for_objects = [
'expiresIn',
'notBefore',
'noTimestamp',
'audience',
'issuer',
'subject',
'jwtid',
];

module.exports = function(payload, secretOrPrivateKey, options, callback) {
options = options || {};

Expand All @@ -35,6 +45,14 @@ module.exports = function(payload, secretOrPrivateKey, options, callback) {
throw new Error('payload is required');
} else if (typeof payload === 'object') {
payload = xtend(payload);
} else if (typeof payload !== 'object') {
var invalid_options = options_for_objects.filter(function (opt) {
return typeof options[opt] !== 'undefined';

This comment has been minimized.

Copy link
@twinssbc

twinssbc May 1, 2016

Does that mean if the payload is passed as a string, any defined option will be treated as invalid option?

});

if (invalid_options.length > 0) {
throw new Error('invalid ' + invalid_options.join(',') + ' option for ' + (typeof payload ) + ' payload' );
}
}

if (typeof payload.exp !== 'undefined' && typeof options.expiresIn !== 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions test/non_object_values.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('non_object_values values', function() {
});

//v6 version will throw in this case:
it.skip('should throw with expiresIn', function () {
it('should throw with expiresIn', function () {
expect(function () {
jwt.sign('hello', '123', { expiresIn: '12h' });
}).to.throw(/invalid expiresIn option for string payload/);
Expand All @@ -30,4 +30,4 @@ describe('non_object_values values', function() {
expect(result).to.equal('123');
});

});
});

0 comments on commit 304f1b3

Please sign in to comment.