mirror of
https://github.com/hanzoai/node-jsonwebtoken.git
synced 2026-09-24 17:00:48 +00:00
Merge pull request #16 from tdantas/master
decode invalid jwt token returns null
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
var jws = require('jws');
|
||||
|
||||
module.exports.decode = function (jwt) {
|
||||
return jws.decode(jwt).payload;
|
||||
var decoded = jws.decode(jwt);
|
||||
return decoded && decoded.payload;
|
||||
};
|
||||
|
||||
module.exports.sign = function(payload, secretOrPrivateKey, options) {
|
||||
|
||||
@@ -135,5 +135,21 @@ describe('RS256', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when decoding a invalid jwt token', function() {
|
||||
it('should return null', function(done) {
|
||||
var payload = jwt.decode('whatever.token');
|
||||
assert.isNull(payload);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when decoding a valid jwt token', function() {
|
||||
it('should return the payload', function(done) {
|
||||
var obj = { foo: 'bar' };
|
||||
var token = jwt.sign(obj, priv, { algorithm: 'RS256' });
|
||||
var payload = jwt.decode(token);
|
||||
assert.deepEqual(payload, obj);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user