Skip to content

Commit

Permalink
add expiresInSeconds option to sign. closes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Mar 6, 2015
1 parent c7b3ab1 commit dd156cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ encoded private key for RSA and ECDSA.
`options`:

* `algorithm` (default: `HS256`)
* `expiresInMinutes`
* `expiresInMinutes` or `expiresInSeconds`
* `audience`
* `subject`
* `issuer`
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ module.exports.sign = function(payload, secretOrPrivateKey, options) {
payload.iat = timestamp;
}

if (options.expiresInMinutes) {
var ms = options.expiresInMinutes * 60;
payload.exp = timestamp + ms;
var expiresInSeconds = options.expiresInMinutes ?
options.expiresInMinutes * 60 :
options.expiresInSeconds;

if (expiresInSeconds) {
payload.exp = timestamp + expiresInSeconds;
}

if (options.audience)
Expand Down
12 changes: 12 additions & 0 deletions test/expiresInSeconds.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var jwt = require('../index');
var expect = require('chai').expect;

describe('noTimestamp', function() {

it('should work with string', function () {
var token = jwt.sign({foo: 123}, '123', { expiresInSeconds: 5 });
var result = jwt.verify(token, '123');
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + 5, 0.5);
});

});

0 comments on commit dd156cc

Please sign in to comment.