Skip to content

Commit df67307

Browse files
Deprecated: payload must be valid JSON object
https://www.rfc-editor.org/rfc/rfc7519#section-7.2
1 parent bc28861 commit df67307

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

sign.js

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const options_for_objects = [
8484
];
8585

8686
module.exports = function (payload, secretOrPrivateKey, options, callback) {
87+
if (typeof payload !== 'object') {
88+
throw Error('Payload must be valid JSON object')
89+
}
90+
8791
if (typeof options === 'function') {
8892
callback = options;
8993
options = {};

test/non_object_values.tests.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,34 @@ var expect = require('chai').expect;
33

44
describe('non_object_values values', function() {
55

6-
it('should work with string', function () {
7-
var token = jwt.sign('hello', '123');
8-
var result = jwt.verify(token, '123');
9-
expect(result).to.equal('hello');
6+
it('should does not work with string', function () {
7+
expect(function () {
8+
jwt.sign('hello', '123');
9+
}).to.throw('Payload must be valid JSON object');
1010
});
1111

12-
it('should work with number', function () {
13-
var token = jwt.sign(123, '123');
14-
var result = jwt.verify(token, '123');
15-
expect(result).to.equal('123');
12+
it('should does not work with number', function () {
13+
expect(function () {
14+
jwt.sign(123, '123');
15+
}).to.throw('Payload must be valid JSON object');
1616
});
1717

18+
it('should does not work with function', function () {
19+
expect(function () {
20+
jwt.sign(function () { return 0 }, '123')
21+
}).to.throw('Payload must be valid JSON object')
22+
})
23+
24+
it('should does not work with boolean', function () {
25+
expect(function () {
26+
jwt.sign(true, '123')
27+
}).to.throw('Payload must be valid JSON object')
28+
})
29+
30+
it('should does not work with undefined', function () {
31+
expect(function () {
32+
jwt.sign(undefined, '123')
33+
}).to.throw('Payload must be valid JSON object')
34+
})
35+
1836
});

0 commit comments

Comments
 (0)