Skip to content

Commit

Permalink
deprecate expireInMinutes and expireInSeconds - in favor of expiresIn
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Oct 2, 2015
1 parent 4b70ae3 commit 39ecc6f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
4 changes: 2 additions & 2 deletions 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` or `expiresInSeconds`
* `expiresIn`: expressed in seconds or an string describing a time span [rauchg/ms](https://github.com/rauchg/ms.js). Eg: `60`, `"2 days"`, `"10h"`, `"7d"`
* `audience`
* `subject`
* `issuer`
Expand All @@ -35,7 +35,7 @@ encoded private key for RSA and ECDSA.
If `payload` is not a buffer or a string, it will be coerced into a string
using `JSON.stringify`.

If any `expiresInMinutes`, `audience`, `subject`, `issuer` are not provided, there is no default. The jwt generated won't include those properties in the payload.
If any `expiresIn`, `audience`, `subject`, `issuer` are not provided, there is no default. The jwt generated won't include those properties in the payload.

Additional headers can be provided via the `headers` object.

Expand Down
31 changes: 27 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var jws = require('jws');
var ms = require('ms');

var JWT = module.exports;

Expand Down Expand Up @@ -57,12 +58,34 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
payload.iat = payload.iat || timestamp;
}

var expiresInSeconds = options.expiresInMinutes ?
options.expiresInMinutes * 60 :
options.expiresInSeconds;
if (options.expiresInSeconds || options.expiresInMinutes) {
var deprecated_line;
try {
deprecated_line = /.*\((.*)\).*/.exec((new Error()).stack.split('\n')[2])[1];
} catch(err) {
deprecated_line = '';
}

console.warn('jsonwebtoken: expiresInMinutes and expiresInSeconds is deprecated. (' + deprecated_line + ')\n' +
'Use "expiresIn" expressed in seconds.');

var expiresInSeconds = options.expiresInMinutes ?
options.expiresInMinutes * 60 :
options.expiresInSeconds;

if (expiresInSeconds) {
payload.exp = timestamp + expiresInSeconds;
} else if (options.expiresIn) {
if (typeof options.expiresIn === 'string') {
var milliseconds = ms(options.expiresIn);
if (typeof milliseconds === 'undefined') {
throw new Error('bad "expiresIn" format: ' + options.expiresIn);
}
payload.exp = timestamp + milliseconds / 1000;
} else if (typeof options.expiresIn === 'number' ) {
payload.exp = timestamp + options.expiresIn;
} else {
throw new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60');
}
}

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

describe('expires option', function() {

it('should work with a number of seconds', function () {
var token = jwt.sign({foo: 123}, '123', { expiresIn: 10 });
var result = jwt.verify(token, '123');
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + 10, 0.2);
});

it('should work with a string', function () {
var token = jwt.sign({foo: 123}, '123', { expiresIn: '2d' });
var result = jwt.verify(token, '123');
var two_days_in_secs = 2 * 24 * 60 * 60;
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + two_days_in_secs, 0.2);
});

it('should work with a string second example', function () {
var token = jwt.sign({foo: 123}, '123', { expiresIn: '36h' });
var result = jwt.verify(token, '123');
var day_and_a_half_in_secs = 1.5 * 24 * 60 * 60;
expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + day_and_a_half_in_secs, 0.2);
});


it('should throw if expires has a bad string format', function () {
expect(function () {
jwt.sign({foo: 123}, '123', { expiresIn: '1 monkey' });
}).to.throw(/bad "expiresIn" format: 1 monkey/);
});

it('should throw if expires is not an string or number', function () {
expect(function () {
jwt.sign({foo: 123}, '123', { expiresIn: { crazy : 213 } });
}).to.throw(/"expiresIn" should be a number of seconds or string representing a timespan/);
});

});

0 comments on commit 39ecc6f

Please sign in to comment.