Skip to content

Commit 786d37b

Browse files
committed
fix nbf verification. fix #152
1 parent f1fb176 commit 786d37b

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
5959
payload.iat = payload.iat || timestamp;
6060
}
6161

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

8484
payload.exp = timestamp + expiresInSeconds;
85-
} else if (options.expiresIn) {
85+
} else if (typeof options.expiresIn !== 'undefined') {
8686
payload.exp = timespan(options.expiresIn);
8787
if (typeof payload.exp === 'undefined') {
8888
throw new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60');
@@ -209,8 +209,7 @@ JWT.verify = function(jwtString, secretOrPublicKey, options, callback) {
209209
if (typeof payload.nbf !== 'number') {
210210
return done(new JsonWebTokenError('invalid nbf value'));
211211
}
212-
if (payload.nbf >= Math.floor(Date.now() / 1000)) {
213-
console.log(payload.nbf, '>=', Math.floor(Date.now() / 1000));
212+
if (payload.nbf > Math.floor(Date.now() / 1000)) {
214213
return done(new NotBeforeError('jwt not active', new Date(payload.nbf * 1000)));
215214
}
216215
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "JSON Web Token implementation (symmetric and asymmetric)",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha"
7+
"test": "mocha --require test/util/fakeDate"
88
},
99
"repository": {
1010
"type": "git",

test/jwt.rs.tests.js

+14
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ describe('RS256', function() {
115115
});
116116
});
117117

118+
119+
it('should valid when date are equals', function(done) {
120+
Date.fix(1451908031);
121+
122+
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: 'RS256', notBefore: 0 });
123+
124+
jwt.verify(token, pub, function(err, decoded) {
125+
assert.isNull(err);
126+
assert.isNotNull(decoded);
127+
Date.unfix();
128+
done();
129+
});
130+
});
131+
118132
it('should NOT be invalid', function(done) {
119133
// not active token
120134
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: 'RS256', notBeforeMinutes: 10 });

test/util/fakeDate.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var oldDate = global.Date;
2+
3+
/*
4+
* fix new Date() to a fixed unix timestamp.
5+
*/
6+
global.Date.fix = function (timestamp) {
7+
var time = timestamp * 1000;
8+
9+
if (global.Date.unfake) {
10+
global.Date.unfake();
11+
}
12+
13+
global.Date = function (ts) {
14+
return new oldDate(ts || time);
15+
};
16+
17+
global.Date.prototype = Object.create(oldDate.prototype);
18+
global.Date.prototype.constructor = global.Date;
19+
20+
global.Date.prototype.now = function () {
21+
return time;
22+
};
23+
24+
global.Date.now = function () {
25+
return time;
26+
};
27+
28+
global.Date.unfix = function () {
29+
global.Date = oldDate;
30+
};
31+
32+
};

0 commit comments

Comments
 (0)