remove connect and cookie dependency

This commit is contained in:
José F. Romaniello 2013-06-05 08:38:33 -03:00
parent 9fea831500
commit 04ea36fdd3
4 changed files with 39 additions and 22 deletions

View File

@ -26,11 +26,12 @@ Usage
//same attribute than the session middleware http://www.senchalabs.org/connect/middleware-session.html //same attribute than the session middleware http://www.senchalabs.org/connect/middleware-session.html
sio.set("authorization", passportSocketIo.authorize({ sio.set("authorization", passportSocketIo.authorize({
key: 'express.sid', //the cookie where express (or connect) stores its session id. cookieParser: express.cookieParser, //or connect.cookieParser
secret: 'my session secret', //the session secret to parse the cookie key: 'express.sid', //the cookie where express (or connect) stores its session id.
store: mySessionStore, //the session store that express uses secret: 'my session secret', //the session secret to parse the cookie
fail: function(data, accept) { // *optional* callbacks on success or fail store: mySessionStore, //the session store that express uses
accept(null, false); // second param takes boolean on whether or not to allow handshake 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) { success: function(data, accept) {
accept(null, true); accept(null, true);

View File

@ -1,29 +1,44 @@
var connectUtils = require('connect').utils, var xtend = require('xtend');
cookie = require('cookie'),
xtend = require('xtend'); function parseCookie(auth, cookieHeader) {
var cookieParser = auth.cookieParser(auth.secret);
var req = {
headers:{
cookie: cookieHeader
}
};
var result;
cookieParser(req, {}, function (err) {
if (err) throw err;
result = req.signedCookies;
});
return result;
}
function authorize(options) { function authorize(options) {
var defaults = { var defaults = {
passport: require('passport'), passport: require('passport'),
key: 'connect.sid', key: 'connect.sid',
secret: null, secret: null,
store: null, store: null,
success: null, success: null,
fail: null fail: null
}; };
var auth = xtend({}, defaults, options ); var auth = xtend({}, defaults, options );
auth.userProperty = auth.passport._userProperty || 'user'; auth.userProperty = auth.passport._userProperty || 'user';
if (typeof auth.cookieParser === 'undefined' || !auth.cookieParser) {
throw new Error('cookieParser is required use connect.cookieParser or express.cookieParser');
}
return function(data, accept){ return function(data, accept){
if (!data.headers.cookie) { if (!data.headers.cookie) {
return accept(null, false); return accept(null, false);
} }
var parsedCookie = cookie.parse(data.headers.cookie); data.cookie = parseCookie(auth, data.headers.cookie);
data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.secret);
data.sessionID = data.cookie[ auth.key ]; data.sessionID = data.cookie[ auth.key ];

View File

@ -22,8 +22,6 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"passport": "~0.1.16", "passport": "~0.1.16",
"connect": "2.7.5",
"cookie": "0.0.5",
"request": "~2.19.0", "request": "~2.19.0",
"xtend": "~2.0.3" "xtend": "~2.0.3"
}, },
@ -34,6 +32,7 @@
"socket.io": "~0.9.14", "socket.io": "~0.9.14",
"passport-local": "~0.1.6", "passport-local": "~0.1.6",
"xmlhttprequest": "~1.5.0", "xmlhttprequest": "~1.5.0",
"socket.io-client": "git+https://github.com/jfromaniello/socket.io-client.git" "socket.io-client": "git+https://github.com/jfromaniello/socket.io-client.git",
"connect": "~2.7.11"
} }
} }

View File

@ -24,8 +24,10 @@ exports.start = function (options, callback) {
if(typeof options == 'function'){ if(typeof options == 'function'){
callback = options; callback = options;
options = {}; options = {
};
} }
options.cookieParser = express.cookieParser;
var app = express(); var app = express();
app.configure(function(){ app.configure(function(){