mirror of
https://github.com/hanzoai/node-jsonwebtoken.git
synced 2026-09-24 17:00:48 +00:00
throw error on invalid options when the payload is not an object
This commit is contained in:
@@ -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') {
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user