2012-09-05 20:14:36 +02:00
|
|
|
var connectUtils = require('connect').utils,
|
2013-01-07 14:40:52 +01:00
|
|
|
cookie = require('cookie'),
|
|
|
|
xtend = require('xtend');
|
2012-10-26 18:13:28 +02:00
|
|
|
|
|
|
|
function authorize(options) {
|
2013-02-05 23:15:04 +01:00
|
|
|
var defaults = {
|
2012-10-26 18:13:28 +02:00
|
|
|
passport: require('passport'),
|
2013-02-15 11:49:56 +01:00
|
|
|
key: 'connect.sid',
|
2013-02-05 23:15:04 +01:00
|
|
|
secret: null,
|
|
|
|
store: null,
|
2012-10-26 18:13:28 +02:00
|
|
|
success: null,
|
|
|
|
fail: null
|
|
|
|
};
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2013-02-05 23:15:04 +01:00
|
|
|
var auth = xtend({}, defaults, options );
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
auth.userProperty = auth.passport._userProperty || 'user';
|
2012-09-05 20:14:36 +02:00
|
|
|
|
|
|
|
return function(data, accept){
|
|
|
|
if (!data.headers.cookie) {
|
2012-11-16 09:58:33 +01:00
|
|
|
return accept(null, false);
|
2012-09-05 20:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var parsedCookie = cookie.parse(data.headers.cookie);
|
|
|
|
|
2013-02-05 23:15:04 +01:00
|
|
|
data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.secret);
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2013-02-05 23:15:04 +01:00
|
|
|
data.sessionID = data.cookie[ auth.key ];
|
2012-09-05 20:14:36 +02:00
|
|
|
|
2013-02-05 23:15:04 +01:00
|
|
|
auth.store.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) {
|
2012-11-16 09:58:33 +01:00
|
|
|
return accept(null, false);
|
2012-09-05 20:14:36 +02:00
|
|
|
}
|
|
|
|
|
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-11-16 09:58:33 +01:00
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
if( !userKey && auth.fail ) {
|
|
|
|
return auth.fail( data, accept );
|
|
|
|
} else if( !userKey ) {
|
2012-11-16 09:58:33 +01:00
|
|
|
return accept(null, false);
|
2012-09-05 20:14:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-26 18:13:28 +02:00
|
|
|
auth.passport.deserializeUser(userKey, function(err, user) {
|
|
|
|
data[ auth.userProperty ] = user;
|
2013-01-07 15:30:15 +01:00
|
|
|
if( auth.success ) {
|
|
|
|
return auth.success( data, accept );
|
|
|
|
}
|
|
|
|
accept(null, true);
|
2012-09-05 20:14:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|