Fix JWT expire time going crazy. Now set to 1day

This commit is contained in:
Jamie Curnow 2020-02-19 13:01:34 +10:00
parent e09b427081
commit 0ec4f63926
2 changed files with 140 additions and 152 deletions

View file

@ -20,7 +20,7 @@ module.exports = {
let Token = new TokenModel(); let Token = new TokenModel();
data.scope = data.scope || 'user'; data.scope = data.scope || 'user';
data.expiry = data.expiry || '30d'; data.expiry = data.expiry || '1d';
return userModel return userModel
.query() .query()
@ -59,9 +59,8 @@ module.exports = {
attrs: { attrs: {
id: user.id id: user.id
}, },
scope: [data.scope] scope: [data.scope],
}, { expiresIn: data.expiry
expires: expiry.unix()
}) })
.then(signed => { .then(signed => {
return { return {
@ -94,7 +93,7 @@ module.exports = {
let Token = new TokenModel(); let Token = new TokenModel();
data = data || {}; data = data || {};
data.expiry = data.expiry || '30d'; data.expiry = data.expiry || '1d';
if (access && access.token.getUserId(0)) { if (access && access.token.getUserId(0)) {
@ -121,9 +120,8 @@ module.exports = {
return Token.create({ return Token.create({
iss: 'api', iss: 'api',
scope: scope, scope: scope,
attrs: token_attrs attrs: token_attrs,
}, { expiresIn: data.expiry
expiresIn: expiry.unix()
}) })
.then(signed => { .then(signed => {
return { return {
@ -140,18 +138,18 @@ module.exports = {
* @param {Object} user * @param {Object} user
* @returns {Promise} * @returns {Promise}
*/ */
getTokenFromUser: user => { getTokenFromUser: (user) => {
let Token = new TokenModel(); const expire = '1d';
let expiry = helpers.parseDatePeriod('1d'); const Token = new TokenModel();
const expiry = helpers.parseDatePeriod(expire);
return Token.create({ return Token.create({
iss: 'api', iss: 'api',
attrs: { attrs: {
id: user.id id: user.id
}, },
scope: ['user'] scope: ['user'],
}, { expiresIn: expire
expiresIn: expiry.unix()
}) })
.then(signed => { .then(signed => {
return { return {

View file

@ -19,23 +19,15 @@ module.exports = function () {
let self = { let self = {
/** /**
* @param {Object} payload * @param {Object} payload
* @param {Object} [user_options]
* @param {Integer} [user_options.expires]
* @returns {Promise} * @returns {Promise}
*/ */
create: (payload, user_options) => { create: (payload) => {
user_options = user_options || {};
// sign with RSA SHA256 // sign with RSA SHA256
let options = { let options = {
algorithm: ALGO algorithm: ALGO,
expiresIn: payload.expiresIn || '1d'
}; };
if (typeof user_options.expires !== 'undefined' && user_options.expires) {
options.expiresIn = user_options.expires;
}
payload.jti = crypto.randomBytes(12) payload.jti = crypto.randomBytes(12)
.toString('base64') .toString('base64')
.substr(-8); .substr(-8);
@ -51,10 +43,8 @@ module.exports = function () {
payload: payload payload: payload
}); });
} }
}); });
}); });
}, },
/** /**