adds activeDuration, default 5 minutes

if a user has an active session, such that they make a request, and the
expiration of their session is within the activeDuration value, their
session will be extended by the same value.

fixes #2
This commit is contained in:
Sean McArthur 2013-10-23 18:26:11 -07:00
Родитель d8aebba854
Коммит b8c53bfa64
4 изменённых файлов: 70 добавлений и 3 удалений

Просмотреть файл

@ -15,6 +15,7 @@ Basic usage:
cookieName: 'mySession', // cookie name dictates the key name added to the request object
secret: 'blargadeeblargblarg', // should be a large unguessable string
duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
}));
app.use(function(req, res, next) {

Просмотреть файл

@ -8,6 +8,7 @@ var Handler = require("./ProxyHandler.js");
var crypto = require("crypto");
const COOKIE_NAME_SEP = '=';
const ACTIVE_DURATION = 1000 * 60 * 5;
function base64urlencode(arg) {
var s = arg.toString('base64');
@ -192,6 +193,7 @@ function Session(req, res, cookies, opts) {
// via reset() or unbox()
this.createdAt = null;
this.duration = opts.duration;
this.activeDuration = opts.activeDuration;
// support for maxAge
if (opts.cookie.maxAge) {
@ -295,9 +297,17 @@ Session.prototype = {
if (cookie) {
this.unbox(cookie);
var expiresAt = this.createdAt + this.duration;
var now = Date.now();
// should we reset this session?
if ((this.createdAt + this.duration) < new Date().getTime())
if (expiresAt < now)
this.reset();
// if expiration is soon, push back a few minutes to not interrupt user
else if (expiresAt - now < this.activeDuration) {
this.createdAt += this.activeDuration;
this.dirty = true;
this.updateDefaultExpires();
}
} else {
if (force_reset) {
this.reset();
@ -374,6 +384,7 @@ var cookieSession = function(opts) {
// defaults
opts.cookieName = opts.cookieName || "session_state";
opts.duration = opts.duration || 24*60*60*1000;
opts.activeDuration = 'activeDuration' in opts ? opts.activeDuration : ACTIVE_DURATION;
// set up cookie defaults
opts.cookie = opts.cookie || {};

Просмотреть файл

@ -13,7 +13,7 @@
"node-proxy": "0.6.0"
},
"devDependencies": {
"vows": "0.5.13",
"vows": "0.7.0",
"express": "2.5.0",
"tobi": "https://github.com/Cowboy-coder/tobi/tarball/fd733a3",
"zombie": "1.4.1"

Просмотреть файл

@ -16,6 +16,7 @@ function create_app() {
var middleware = cookieSessions({
cookieName: 'session',
secret: 'yo',
activeDuration: 0,
cookie: {
maxAge: 5000
}
@ -28,6 +29,7 @@ function create_app() {
var secureStoreMiddleware = cookieSessions({
cookieName: 'securestore',
secret: 'yo',
activeDuration: 0,
cookie: {
maxAge: 5000
}
@ -329,6 +331,7 @@ function create_app_with_duration() {
app.use(cookieSessions({
cookieName: 'session',
secret: 'yo',
activeDuration: 0,
duration: 500 // 0.5 seconds
}));
@ -496,6 +499,7 @@ function create_app_with_duration_modification() {
app.use(cookieSessions({
cookieName: 'session',
secret: 'yobaby',
activeDuration: 0,
duration: 5000 // 5.0 seconds
}));
@ -679,6 +683,7 @@ function create_app_with_secure(firstMiddleware) {
var middleware = cookieSessions({
cookieName: 'session',
secret: 'yo',
activeDuration: 0,
cookie: {
maxAge: 5000,
secure: true
@ -834,12 +839,13 @@ suite.addBatch({
var app = express.createServer();
app.use(cookieSessions({
cookieName: 'ooga_booga_momma',
activeDuration: 0,
requestKey: 'ses',
secret: 'yo'
}));
app.get('/foo', function(req, res) {
self.callback(null, req)
self.callback(null, req);
});
var browser = tobi.createBrowser(app);
@ -913,6 +919,7 @@ suite.addBatch({
app.use(cookieSessions({
cookieName: 'session',
duration: 50000,
activeDuration: 0,
secret: 'yo'
}));
@ -944,6 +951,7 @@ suite.addBatch({
app.use(cookieSessions({
cookieName: 'session',
duration: 500,
activeDuration: 0,
secret: 'yo'
}));
@ -972,6 +980,52 @@ suite.addBatch({
var cookieDuration = expiryDate.getTime() - Date.now();
assert(Math.abs(cookieDuration - 5000) < 1000, "expiry is pretty far from the specified duration");
}
},
"active user with session close to expiration": {
topic: function() {
var app = express.createServer();
var self = this;
app.use(cookieSessions({
cookieName: 'session',
duration: 300,
activeDuration: 500,
secret: 'yo'
}));
app.get("/foo", function(req, res) {
req.session.foo = 'foobar';
res.send("hello");
});
app.get("/bar", function(req, res) {
req.session.bar = 'baz';
res.send('hi');
});
app.get("/baz", function(req, res) {
res.json({ "msg": req.session.foo + req.session.bar });
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function() {
browser.get("/bar", function() {
setTimeout(function () {
browser.get("/baz", function(res, first) {
setTimeout(function() {
browser.get('/baz', function(res, second) {
self.callback(null, first, second);
});
}, 1000);
});
}, 400);
});
});
},
"extends session duration": function(err, extended, tooLate) {
assert.equal(extended.msg, 'foobarbaz');
assert.equal(tooLate.msg, null);
}
}
});
@ -1031,6 +1085,7 @@ suite.addBatch({
app.use(cookieSessions({
cookieName: 'session',
duration: 50000,
activeDuration: 0,
secret: 'yo',
cookie: {
ephemeral: true