Merge pull request #2 from bradleyolson/master
Success and Fail Callbacks
This commit is contained in:
commit
3675d24adf
@ -24,7 +24,13 @@ Usage
|
||||
sio.set("authorization", passportSocketIo.authorize({
|
||||
sessionKey: 'express.sid', //the cookie where express (or connect) stores its session id.
|
||||
sessionStore: mySessionStore, //the session store that express uses
|
||||
sessionSecret: "my session secret" //the session secret to parse the cookie
|
||||
sessionSecret: "my session secret", //the session secret to parse the cookie
|
||||
fail: function(data, accept) { // *optional* callbacks on success or fail
|
||||
accept(null, false); // second param takes boolean on whether or not to allow handshake
|
||||
},
|
||||
success: function(data, accept) {
|
||||
accept(null, true);
|
||||
}
|
||||
}));
|
||||
|
||||
sio.sockets.on("connection", function(socket){
|
||||
|
63
lib/index.js
63
lib/index.js
@ -1,13 +1,37 @@
|
||||
var connectUtils = require('connect').utils,
|
||||
cookie = require('cookie');
|
||||
|
||||
function authorize(options){
|
||||
var passport = options.passport || require('passport'),
|
||||
sessionKey = options.sessionKey || 'express.sid',
|
||||
sessionSecret = options.sessionSecret,
|
||||
sessionStore = options.sessionStore;
|
||||
var overwrite = function(overwritten) {
|
||||
return (function() {
|
||||
if( arguments.length > 1 ) {
|
||||
for( objects in arguments ) {
|
||||
overwrite( this, arguments[objects] );
|
||||
}
|
||||
}
|
||||
|
||||
var userProperty = passport._userProperty || 'user';
|
||||
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
|
||||
};
|
||||
|
||||
overwrite( auth, options );
|
||||
|
||||
auth.userProperty = auth.passport._userProperty || 'user';
|
||||
|
||||
return function(data, accept){
|
||||
if (!data.headers.cookie) {
|
||||
@ -16,30 +40,37 @@ function authorize(options){
|
||||
|
||||
var parsedCookie = cookie.parse(data.headers.cookie);
|
||||
|
||||
data.cookie = connectUtils.parseSignedCookies(parsedCookie, sessionSecret);
|
||||
data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.sessionSecret);
|
||||
|
||||
data.sessionID = data.cookie[sessionKey];
|
||||
data.sessionID = data.cookie[ auth.sessionKey ];
|
||||
|
||||
sessionStore.get(data.sessionID, function(err, session){
|
||||
|
||||
auth.sessionStore.get(data.sessionID, function(err, session){
|
||||
if (err) {
|
||||
return accept('Error in session store.', false);
|
||||
} else if (!session) {
|
||||
return accept('Session not found.', false);
|
||||
}
|
||||
|
||||
if(!session[passport._key]){
|
||||
if( !session[ auth.passport._key ] ){
|
||||
return accept('passport was not initialized', false);
|
||||
}
|
||||
|
||||
var userKey = session[passport._key][userProperty];
|
||||
var userKey = session[ auth.passport._key ][ auth.userProperty ];
|
||||
|
||||
if(!userKey){
|
||||
if( !userKey && auth.fail ) {
|
||||
return auth.fail( data, accept );
|
||||
} else if( !userKey ) {
|
||||
return accept('not yet authenticated', false);
|
||||
}
|
||||
|
||||
passport.deserializeUser(userKey, function(err, user) {
|
||||
data[userProperty] = user;
|
||||
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;
|
||||
return accept(null, true);
|
||||
});
|
||||
|
||||
@ -59,4 +90,4 @@ function filterSocketsByUser(socketIo, filter){
|
||||
}
|
||||
|
||||
exports.authorize = authorize;
|
||||
exports.filterSocketsByUser = filterSocketsByUser;
|
||||
exports.filterSocketsByUser = filterSocketsByUser;
|
||||
|
Reference in New Issue
Block a user