Skip to content

Commit

Permalink
Significant progress on separating out cookieParsing/session mware fo…
Browse files Browse the repository at this point in the history
…r socket requests.
  • Loading branch information
mikermcneil committed Dec 15, 2014
1 parent a074834 commit 0a3cf5b
Show file tree
Hide file tree
Showing 4 changed files with 590 additions and 140 deletions.
91 changes: 26 additions & 65 deletions lib/hooks/session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Module dependencies.
*/

var util = require('sails-util'),
uuid = require('node-uuid'),
path = require('path'),
generateSecret = require('./generateSecret'),
cookie = require('express/node_modules/cookie'),
parseSignedCookie = require('cookie-parser').signedCookie,
ConnectSession = require('express/node_modules/connect').middleware.session.Session;
var util = require('sails-util');
var _ = require('lodash');
var uuid = require('node-uuid');
var path = require('path');
var generateSecret = require('./generateSecret');
var cookie = require('express/node_modules/cookie');
var parseSignedCookie = require('cookie-parser').signedCookie;
var ConnectSession = require('express/node_modules/connect').middleware.session.SessionHook;


module.exports = function(sails) {
Expand All @@ -25,49 +26,8 @@ module.exports = function(sails) {



/**
* Prototype for the connect session store wrapper used by the sockets hook.
* Includes a save() method to persist the session data.
*/
function SocketIOSession(options) {
var sid = options.sid,
data = options.data;


this.save = function(cb) {

if (!sid) {
sails.log.error('Trying to save session, but could not determine session ID.');
sails.log.error('This probably means a requesting socket did not send a cookie.');
sails.log.error('Usually, this happens when a socket from an old browser tab ' +
' tries to reconnect.');
sails.log.error('(this can also occur when trying to connect a cross-origin socket.)');
if (cb) cb('Could not save session.');
return;
}

// Merge data directly into instance to allow easy access on `req.session` later
util.defaults(this, data);

// Persist session
Session.set(sid, sails.util.cloneDeep(this), function(err) {

if (err) {
sails.log.error('Could not save session:');
sails.log.error(err);
}
if (cb) cb(err);
});
};

// Set the data on this object, since it will be used as req.session
util.extend(this, options.data);
}



// Session hook
var Session = {
// `session` hook definition
var SessionHook = {


defaults: {
Expand All @@ -77,6 +37,7 @@ module.exports = function(sails) {
}
},


/**
* Normalize and validate configuration for this hook.
* Then fold any modifications back into `sails.config`
Expand All @@ -86,7 +47,7 @@ module.exports = function(sails) {
// Validate config
// Ensure that secret is specified if a custom session store is used
if (sails.config.session) {
if (!util.isObject(sails.config.session)) {
if (!_.isObject(sails.config.session)) {
throw new Error('Invalid custom session store configuration!\n' +
'\n' +
'Basic usage ::\n' +
Expand All @@ -101,10 +62,10 @@ module.exports = function(sails) {
// If session config is set, but secret is undefined, set a secure, one-time use secret
if (!sails.config.session || !sails.config.session.secret) {

sails.log.verbose('Session secret not defined-- automatically generating one for now...');
sails.log.verbose('SessionHook secret not defined-- automatically generating one for now...');

if (sails.config.environment === 'production') {
sails.log.warn('Session secret must be identified!');
sails.log.warn('SessionHook secret must be identified!');
sails.log.warn('Automatically generating one for now...');

sails.log.error('This generated session secret is NOT OK for production!');
Expand Down Expand Up @@ -212,7 +173,7 @@ module.exports = function(sails) {
}

// Save reference in `sails.session`
sails.session = Session;
sails.session = SessionHook;

return cb();
},
Expand Down Expand Up @@ -243,7 +204,7 @@ module.exports = function(sails) {
});

// Next, persist the new session
Session.set(session.id, session, function(err) {
SessionHook.set(session.id, session, function(err) {
if (err) return cb(err);
sails.log.verbose('Generated new session (', session.id, ') for socket....');

Expand All @@ -262,7 +223,7 @@ module.exports = function(sails) {
*/
get: function(sessionId, cb) {
if (!util.isFunction(cb)) {
throw new Error('Invalid usage :: `Session.get(sessionId, cb)`');
throw new Error('Invalid usage :: `SessionHook.get(sessionId, cb)`');
}
return sails.config.session.store.get(sessionId, cb);
},
Expand Down Expand Up @@ -317,12 +278,12 @@ module.exports = function(sails) {
socket.handshake.sessionID = parseSignedCookie(socket.handshake.cookie[sails.config.session.key], sails.config.session.secret);

// Generate and persist a new session in the store
Session.generate(socket.handshake, function(err, sessionData) {
SessionHook.generate(socket.handshake, function(err, sessionData) {
if (err) return cb(err);
sails.log.silly('socket.handshake.sessionID is now :: ', socket.handshake.sessionID);

// Provide access to adapter-agnostic `.save()`
return cb(null, new SocketIOSession({
return cb(null, new RawSession({
sid: sessionData.id,
data: sessionData
}));
Expand Down Expand Up @@ -350,11 +311,11 @@ module.exports = function(sails) {
// If sid DOES exist, it's easy to look up in the socket
var sid = socket.handshake.sessionID;

// Cache the handshake in case it gets wiped out during the call to Session.get
// Cache the handshake in case it gets wiped out during the call to SessionHook.get
var handshake = socket.handshake;

// Retrieve session data from store
Session.get(sid, function(err, sessionData) {
SessionHook.get(sid, function(err, sessionData) {

if (err) {
sails.log.error('Error retrieving session from socket.');
Expand All @@ -370,11 +331,11 @@ module.exports = function(sails) {
sails.log.verbose('A socket (' + socket.id + ') is trying to connect with an invalid or expired session ID (' + sid + ').');
sails.log.verbose('Regnerating empty session...');

Session.generate(handshake, function(err, sessionData) {
SessionHook.generate(handshake, function(err, sessionData) {
if (err) return cb(err);

// Provide access to adapter-agnostic `.save()`
return cb(null, new SocketIOSession({
return cb(null, new RawSession({
sid: sessionData.id,
data: sessionData
}));
Expand All @@ -383,9 +344,9 @@ module.exports = function(sails) {

// Otherwise session exists and everything is ok.

// Instantiate SocketIOSession (provides .save() method)
// Instantiate RawSession (provides .save() method)
// And extend it with session data
else return cb(null, new SocketIOSession({
else return cb(null, new RawSession({
data: sessionData,
sid: sid
}));
Expand All @@ -394,5 +355,5 @@ module.exports = function(sails) {
};


return Session;
return SessionHook;
};
Loading

0 comments on commit 0a3cf5b

Please sign in to comment.