2012-09-05 20:14:36 +02:00
|
|
|
var connectUtils = require('connect').utils,
|
|
|
|
cookie = require('cookie');
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
var overwrite = function(overwritten) {
|
|
|
|
return (function() {
|
|
|
|
if( arguments.length > 1 ) {
|
|
|
|
for( objects in arguments ) {
|
|
|
|
overwrite( this, arguments[objects] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( var key in arguments[0] ) {
|
|
|
|
if( arguments[0].hasOwnProperty(key) ) {
|
|
|
|
this[key] = arguments[0][key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}).apply(overwritten, Array.prototype.slice.call(arguments, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
function authorize(options) {
|
|
|
|
var auth = {
|
|
|
|
passport: require('passport'),
|
|
|
|
sessionKey: 'express.sid',
|
|
|
|
sessionSecret: null,
|
|
|
|
sessionStore: null,
|
|
|
|
success: null,
|
|
|
|
fail: null
|
|
|
|
};
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
overwrite( auth, options );
|
|
|
|
|
|
|
|
auth.userProperty = auth.passport._userProperty || 'user';
|
2012-09-05 20:14:36 +02:00
|
|
|
|
|
|
|
return function(data, accept){
|
|
|
|
if (!data.headers.cookie) {
|
|
|
|
return accept('Session cookie required.', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsedCookie = cookie.parse(data.headers.cookie);
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.sessionSecret);
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
data.sessionID = data.cookie[ auth.sessionKey ];
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
auth.sessionStore.get(data.sessionID, function(err, session){
|
2012-09-05 20:14:36 +02:00
|
|
|
if (err) {
|
|
|
|
return accept('Error in session store.', false);
|
|
|
|
} else if (!session) {
|
|
|
|
return accept('Session not found.', false);
|
|
|
|
}
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
if( !session[ auth.passport._key ] ){
|
2012-09-05 20:14:36 +02:00
|
|
|
return accept('passport was not initialized', false);
|
|
|
|
}
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
var userKey = session[ auth.passport._key ][ auth.userProperty ];
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
if( !userKey && auth.fail ) {
|
|
|
|
return auth.fail( data, accept );
|
|
|
|
} else if( !userKey ) {
|
2012-09-05 20:14:36 +02:00
|
|
|
return accept('not yet authenticated', false);
|
|
|
|
}
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
if( auth.success ) {
|
|
|
|
auth.passport.deserializeUser(userKey, function(err, user) {
|
|
|
|
data[ auth.userProperty ] = user;
|
|
|
|
return auth.success( data, accept );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
auth.passport.deserializeUser(userKey, function(err, user) {
|
|
|
|
data[ auth.userProperty ] = user;
|
2012-09-05 20:14:36 +02:00
|
|
|
return accept(null, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterSocketsByUser(socketIo, filter){
|
|
|
|
var handshaken = socketIo.sockets.manager.handshaken;
|
|
|
|
return Object.keys(handshaken || {})
|
|
|
|
.filter(function(skey){
|
|
|
|
return filter(handshaken[skey].user);
|
|
|
|
})
|
|
|
|
.map(function(skey){
|
|
|
|
return socketIo.sockets.manager.sockets.sockets[skey];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.authorize = authorize;
|
2012-10-26 18:13:28 +02:00
|
|
|
exports.filterSocketsByUser = filterSocketsByUser;
|