diff --git a/README.md b/README.md index d826a8b9..7878356c 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,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 2241d974..23a4b8fd 100644 --- a/index.js +++ b/index.js @@ -76,6 +76,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] @@ -113,6 +114,8 @@ function session(options) { // get the cookie signing secret var secret = opts.secret + var getManualSessionId = options.sessionid || function(req) {}; + if (typeof generateId !== 'function') { throw new TypeError('genid option must be a function'); } @@ -155,7 +158,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(cookieOptions); @@ -213,7 +216,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(){