throw error on invalid options when the payload is not an object

This commit is contained in:
José F. Romaniello
2016-04-27 12:49:54 -03:00
parent 5835f552e2
commit 304f1b3307
2 changed files with 20 additions and 2 deletions
+18
View File
@@ -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 || {};
@@ -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';
});
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') {
+2 -2
View File
@@ -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/);
@@ -30,4 +30,4 @@ describe('non_object_values values', function() {
expect(result).to.equal('123');
});
});
});