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({
|
sio.set("authorization", passportSocketIo.authorize({
|
||||||
sessionKey: 'express.sid', //the cookie where express (or connect) stores its session id.
|
sessionKey: 'express.sid', //the cookie where express (or connect) stores its session id.
|
||||||
sessionStore: mySessionStore, //the session store that express uses
|
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){
|
sio.sockets.on("connection", function(socket){
|
||||||
|
63
lib/index.js
63
lib/index.js
@ -1,13 +1,37 @@
|
|||||||
var connectUtils = require('connect').utils,
|
var connectUtils = require('connect').utils,
|
||||||
cookie = require('cookie');
|
cookie = require('cookie');
|
||||||
|
|
||||||
function authorize(options){
|
var overwrite = function(overwritten) {
|
||||||
var passport = options.passport || require('passport'),
|
return (function() {
|
||||||
sessionKey = options.sessionKey || 'express.sid',
|
if( arguments.length > 1 ) {
|
||||||
sessionSecret = options.sessionSecret,
|
for( objects in arguments ) {
|
||||||
sessionStore = options.sessionStore;
|
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){
|
return function(data, accept){
|
||||||
if (!data.headers.cookie) {
|
if (!data.headers.cookie) {
|
||||||
@ -16,30 +40,37 @@ function authorize(options){
|
|||||||
|
|
||||||
var parsedCookie = cookie.parse(data.headers.cookie);
|
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) {
|
if (err) {
|
||||||
return accept('Error in session store.', false);
|
return accept('Error in session store.', false);
|
||||||
} else if (!session) {
|
} else if (!session) {
|
||||||
return accept('Session not found.', false);
|
return accept('Session not found.', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!session[passport._key]){
|
if( !session[ auth.passport._key ] ){
|
||||||
return accept('passport was not initialized', false);
|
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);
|
return accept('not yet authenticated', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
passport.deserializeUser(userKey, function(err, user) {
|
if( auth.success ) {
|
||||||
data[userProperty] = user;
|
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);
|
return accept(null, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -59,4 +90,4 @@ function filterSocketsByUser(socketIo, filter){
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.authorize = authorize;
|
exports.authorize = authorize;
|
||||||
exports.filterSocketsByUser = filterSocketsByUser;
|
exports.filterSocketsByUser = filterSocketsByUser;
|
||||||
|
Reference in New Issue
Block a user