Skip to content

Commit 69db3ac

Browse files
committed
decode invalid jwt token returns null
1 parent 342b86c commit 69db3ac

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var jws = require('jws');
22

33
module.exports.decode = function (jwt) {
4-
return jws.decode(jwt).payload;
4+
var decoded = jws.decode(jwt);
5+
return decoded && decoded.payload;
56
};
67

78
module.exports.sign = function(payload, secretOrPrivateKey, options) {

test/jwt.rs.tests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,21 @@ describe('RS256', function() {
135135
});
136136
});
137137

138+
describe('when decoding a invalid jwt token', function() {
139+
it('should return null', function(done) {
140+
var payload = jwt.decode('whatever.token');
141+
assert.isNull(payload);
142+
done();
143+
});
144+
});
138145

146+
describe('when decoding a valid jwt token', function() {
147+
it('should return the payload', function(done) {
148+
var obj = { foo: 'bar' };
149+
var token = jwt.sign(obj, priv, { algorithm: 'RS256' });
150+
var payload = jwt.decode(token);
151+
assert.deepEqual(payload, obj);
152+
done();
153+
});
154+
});
139155
});

0 commit comments

Comments
 (0)