Skip to content

Commit

Permalink
fix nbf verification. fix #152
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Jan 4, 2016
1 parent f1fb176 commit 786d37b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
payload.iat = payload.iat || timestamp;
}

if (options.notBefore) {
if (typeof options.notBefore !== 'undefined') {
payload.nbf = timespan(options.notBefore);
if (typeof payload.nbf === 'undefined') {
throw new Error('"notBefore" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60');
Expand All @@ -82,7 +82,7 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
options.expiresInSeconds;

payload.exp = timestamp + expiresInSeconds;
} else if (options.expiresIn) {
} else if (typeof options.expiresIn !== 'undefined') {
payload.exp = timespan(options.expiresIn);
if (typeof payload.exp === 'undefined') {
throw new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60');
Expand Down Expand Up @@ -209,8 +209,7 @@ JWT.verify = function(jwtString, secretOrPublicKey, options, callback) {
if (typeof payload.nbf !== 'number') {
return done(new JsonWebTokenError('invalid nbf value'));
}
if (payload.nbf >= Math.floor(Date.now() / 1000)) {
console.log(payload.nbf, '>=', Math.floor(Date.now() / 1000));
if (payload.nbf > Math.floor(Date.now() / 1000)) {
return done(new NotBeforeError('jwt not active', new Date(payload.nbf * 1000)));
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "JSON Web Token implementation (symmetric and asymmetric)",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha --require test/util/fakeDate"
},
"repository": {
"type": "git",
Expand Down
14 changes: 14 additions & 0 deletions test/jwt.rs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ describe('RS256', function() {
});
});


it('should valid when date are equals', function(done) {
Date.fix(1451908031);

token = jwt.sign({ foo: 'bar' }, priv, { algorithm: 'RS256', notBefore: 0 });

jwt.verify(token, pub, function(err, decoded) {
assert.isNull(err);
assert.isNotNull(decoded);
Date.unfix();
done();
});
});

it('should NOT be invalid', function(done) {
// not active token
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: 'RS256', notBeforeMinutes: 10 });
Expand Down
32 changes: 32 additions & 0 deletions test/util/fakeDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var oldDate = global.Date;

/*
* fix new Date() to a fixed unix timestamp.
*/
global.Date.fix = function (timestamp) {
var time = timestamp * 1000;

if (global.Date.unfake) {
global.Date.unfake();
}

global.Date = function (ts) {
return new oldDate(ts || time);
};

global.Date.prototype = Object.create(oldDate.prototype);
global.Date.prototype.constructor = global.Date;

global.Date.prototype.now = function () {
return time;
};

global.Date.now = function () {
return time;
};

global.Date.unfix = function () {
global.Date = oldDate;
};

};

0 comments on commit 786d37b

Please sign in to comment.