diff --git a/README.md b/README.md index 239d49b7..e7b83428 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,24 @@ app.use(session({ })) ``` +##### sessionid + +Function to manually set the sessionID. This is most helpful for environments where +cookies are not possible. When the expression returns no value, the regular sessionID +will be used or generated. + +**NOTE** be careful with unsecured connections. When you don't use SSL, a man in the +middle could intercept a sessionID and pick up a session from there. + +```js +app.use(session({ + sessionid: function(req) { + return req.query.sessionID; + }, + secret: 'keyboard cat' +})) +``` + ##### name The name of the session ID cookie to set in the response (and read from in the diff --git a/index.js b/index.js index 6f6d1b52..b506def5 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,7 @@ var defer = typeof setImmediate === 'function' * @param {Boolean} [options.resave] Resave unmodified sessions back to the store * @param {Boolean} [options.rolling] Enable/disable rolling session expiration * @param {Boolean} [options.saveUninitialized] Save uninitialized sessions to the store + * @param {Function} [options.sessionid] Manually set session ID * @param {String|Array} [options.secret] Secret for signing session ID * @param {Object} [options.store=MemoryStore] Session store * @param {String} [options.unset] @@ -96,6 +97,8 @@ function session(options){ var generateId = options.genid || generateSessionId; + var getManualSessionId = options.sessionid || function(req) {}; + if (typeof generateId !== 'function') { throw new TypeError('genid option must be a function'); } @@ -137,7 +140,7 @@ function session(options){ // generates the new session store.generate = function(req){ - req.sessionID = generateId(req); + req.sessionID = getManualSessionId(req) ? getManualSessionId(req) : generateId(req); req.session = new Session(req); req.session.cookie = new Cookie(cookie); }; @@ -176,7 +179,7 @@ function session(options){ req.sessionStore = store; // get the session ID from the cookie - var cookieId = req.sessionID = getcookie(req, name, secrets); + var cookieId = req.sessionID = getManualSessionId(req) ? getManualSessionId(req) : getcookie(req, name, secrets); // set-cookie onHeaders(res, function(){