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
61
lib/index.js
61
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user