added option of success or fail callbacks. Needed to still allow users access to sockets even if they weren't logged in, but needed specific data if they were.
This commit is contained in:
parent
a5cb696579
commit
f62a93c9d2
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