express-session-max-duration
v1.0.0
Published
added max duration session parameter to express session library
Downloads
1
Readme
Agregar Duracion maxima a express-session
Estos cambios se han realizado a partir de un pull request de la libreria (testeado y verificado)
Cambios en session/cookie.js
Agregar el parametro createdAt:
get data() {
return {
originalMaxAge: this.originalMaxAge,
partitioned: this.partitioned,
priority: this.priority
, expires: this._expires
, secure: this.secure
, httpOnly: this.httpOnly
, domain: this.domain
, path: this.path
, sameSite: this.sameSite
, createdAt: this.createdAt //Este parametro es el que se ha de agregar
}
},
Cambios en index.js
Agregar @params maxDuration bajo unset
* @param {String} [options.unset]
El siguiente codigo
* @param {Numeber} [options.maxDuration] sets the maximum total age in seconds for a session to minimize replay duration
Agregar comprobacion maxDuration existe bajo el siguiente codigo
if (secret && !Array.isArray(secret)) { secret = [secret]; }
Este condicional
if (opts.maxDuration && typeof opts.maxDuration !== "number") { throw new TypeError("MaxDuration needs to be specified as a number"); }
Agregar comprobacion maxDuration mayor a 0 en la funcion generate
// generates the new session store.generate = function (req) { req.sessionID = generateId(req); req.session = new Session(req); req.session.cookie = new Cookie(cookieOptions); if (cookieOptions.secure === 'auto') { req.session.cookie.secure = issecure(req, trustProxy); } if (opts.maxDuration > 0) { //Agregar este if req.session.cookie.createdAt = new Date(); } };
Agregar function hasReachedMaxDuration dentro de
return function session(req, res, next) {
function hasReachedMaxDuration(sess, opts) { if (!opts || !opts.maxDuration || opts.maxDuration <= 0) { return false } if (!sess || !sess.cookie || !sess.cookie.createdAt) { debug("session should be timed out, but the created at value is not saved"); return true } var createdDate = new Date(sess.cookie.createdAt); var nowDate = new Date(); if ((nowDate.getTime() - createdDate.getTime()) / 1000 < opts.maxDuration) { return false } return true }
Agregar condicional (else if) hasReachedMaxDuration dentro de
try { if (err || !sess) { debug('no session found') generate() }else if(hasReachedMaxDuration(sess, opts)){ debug('session has reached the max duration') generate() } else { debug('session found') inflate(req, sess) } } catch (e) { next(e) return }