Skip to content

Commit

Permalink
Merge branch 'ignoreExpiration' of https://github.com/ben-bradley/nod…
Browse files Browse the repository at this point in the history
…e-jsonwebtoken into ben-bradley-ignoreExpiration

Conflicts:
	README.md
	index.js
	test/jwt.hs.tests.js
  • Loading branch information
jfromaniello committed Mar 6, 2015
2 parents 6448ce3 + 002cce1 commit 8d4da27
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Additional headers can be provided via the `headers` object.

Generated jwts will include an `iat` claim by default unless `noTimestamp` is specified.

Setting `ignoreExpiration` to `true` will prevent expired tokens from generating an error.

Example

```js
Expand All @@ -55,6 +57,13 @@ var token = jwt.sign({ foo: 'bar' }, cert, { algorithm: 'RS256'});

### jwt.verify(token, secretOrPublicKey, [options, callback])

`options`:

* `ignoreExpiration`
* `audience`
* `issuer`


(Asynchronous) If a callback is supplied, function acts asynchronously. Callback passed the payload decoded if the signature (and optionally expiration, audience, issuer) are valid. If not, it will be passed the error.

(Synchronous) If a callback is not supplied, function acts synchronously. Returns the payload decoded if the signature (and optionally expiration, audience, issuer) are valid. If not, it will throw the error.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports.verify = function(jwtString, secretOrPublicKey, options, callback
return done(err);
}

if (typeof payload.exp !== 'undefined') {
if (typeof payload.exp !== 'undefined' && !options.ignoreExpiration) {
if (typeof payload.exp !== 'number') {
return done(new JsonWebTokenError('invalid exp value'));
}
Expand Down
30 changes: 29 additions & 1 deletion test/jwt.hs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('HS256', function() {

it('should without options', function(done) {
var callback = function(err, decoded) {
assert.ok(decoded.foo);
assert.ok(decoded.foo);
assert.equal('bar', decoded.foo);
done();
};
Expand Down Expand Up @@ -59,5 +59,33 @@ describe('HS256', function() {
});
});

it('should throw when the payload is not json', function(done) {
var token = jwt.sign('bar', 'secret', { algorithm: 'HS256' });
jwt.verify(token, 'secret', function(err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
done();
});
});

it('should return an error when the token is expired', function(done) {
var token = jwt.sign({ exp: 1 }, secret, { algorithm: 'HS256' });
jwt.verify(token, secret, { algorithm: 'HS256' }, function(err, decoded) {
assert.isUndefined(decoded);
assert.isNotNull(err);
done();
});
});

it('should NOT return an error when the token is expired with "ignoreExpiration"', function(done) {
var token = jwt.sign({ exp: 1, foo: 'bar' }, secret, { algorithm: 'HS256' });
jwt.verify(token, secret, { algorithm: 'HS256', ignoreExpiration: true }, function(err, decoded) {
assert.ok(decoded.foo);
assert.equal('bar', decoded.foo);
assert.isNull(err);
done();
});
});

});
});
11 changes: 11 additions & 0 deletions test/jwt.rs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ describe('RS256', function() {
done();
});
});

it('should NOT be invalid', function(done) {
// expired token
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: 'RS256', expiresInMinutes: -10 });

jwt.verify(token, pub, { ignoreExpiration: true }, function(err, decoded) {
assert.ok(decoded.foo);
assert.equal('bar', decoded.foo);
done();
});
});
});

describe('when signing a token with audience', function() {
Expand Down

0 comments on commit 8d4da27

Please sign in to comment.