Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: auth0/node-jsonwebtoken
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.5.0
Choose a base ref
...
head repository: auth0/node-jsonwebtoken
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.5.1
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 4, 2016

  1. Copy the full SHA
    786d37b View commit details
  2. 5.5.1

    jfromaniello committed Jan 4, 2016
    Copy the full SHA
    42145bc View commit details
Showing with 51 additions and 6 deletions.
  1. +3 −4 index.js
  2. +2 −2 package.json
  3. +14 −0 test/jwt.rs.tests.js
  4. +32 −0 test/util/fakeDate.js
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
@@ -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');
@@ -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)));
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "jsonwebtoken",
"version": "5.5.0",
"version": "5.5.1",
"description": "JSON Web Token implementation (symmetric and asymmetric)",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha --require test/util/fakeDate"
},
"repository": {
"type": "git",
14 changes: 14 additions & 0 deletions test/jwt.rs.tests.js
Original file line number Diff line number Diff line change
@@ -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 });
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;
};

};