improved handling of some cases for expiration timestamps

This commit is contained in:
tigerbot 2017-07-24 16:50:41 -06:00
parent ed0aca5783
commit e49299670e
1 changed files with 24 additions and 4 deletions

28
rest.js
View File

@ -11,11 +11,23 @@ function makeB64UrlSafe(b64) {
function timespan(duration, max) {
var timestamp = Math.floor(Date.now() / 1000);
if (!duration) {
return;
}
if (typeof duration === 'string') {
duration = Math.floor(require('ms')(duration) / 1000);
duration = Math.floor(require('ms')(duration) / 1000) || 0;
}
if (typeof duration !== 'number') {
return timestamp;
return 0;
}
// Handle the case where the user gave us a timestamp instead of duration for the expiration.
// Also make the maximum explicitly defined expiration as one year.
if (duration > 31557600) {
if (duration > timestamp) {
return duration - timestamp;
} else {
return 31557600;
}
}
if (max && timestamp+duration > max) {
@ -461,8 +473,16 @@ module.exports.create = function (bigconf, deps, app) {
kid: jwk.kid
}
};
var accessOpts = {expiresIn: timespan(req.body.exp || '1d', token_info.exp)};
var refreshOpts = {expiresIn: timespan(req.body.refresh_exp, token_info.exp)};
var accessOpts = {};
// We set `expiresIn` like this to make it possible to send `null` and `exp` to have
// no expiration while still having a default of 1 day.
if (req.body.hasOwnProperty('exp')) {
accessOpts.expiresIn = timespan(req.body.exp, token_info.exp);
} else {
accessOpts.expiresIn = timespan('1d', token_info.exp);
}
var refreshOpts = {};
refreshOpts.expiresIn = timespan(req.body.refresh_exp, token_info.exp);
var jwt = require('jsonwebtoken');
var result = {};