diff --git a/README.md b/README.md index 03ab039..a3ddc2c 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,12 @@ Usage //same attribute than the session middleware http://www.senchalabs.org/connect/middleware-session.html sio.set("authorization", passportSocketIo.authorize({ - key: 'express.sid', //the cookie where express (or connect) stores its session id. - secret: 'my session secret', //the session secret to parse the cookie - store: mySessionStore, //the session store that express uses - fail: function(data, accept) { // *optional* callbacks on success or fail - accept(null, false); // second param takes boolean on whether or not to allow handshake + cookieParser: express.cookieParser, //or connect.cookieParser + key: 'express.sid', //the cookie where express (or connect) stores its session id. + secret: 'my session secret', //the session secret to parse the cookie + store: mySessionStore, //the session store that express uses + 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); diff --git a/lib/index.js b/lib/index.js index f6b33bf..21954ec 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,29 +1,44 @@ -var connectUtils = require('connect').utils, - cookie = require('cookie'), - xtend = require('xtend'); +var 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) { var defaults = { - passport: require('passport'), - key: 'connect.sid', - secret: null, - store: null, - success: null, - fail: null + passport: require('passport'), + key: 'connect.sid', + secret: null, + store: null, + success: null, + fail: null }; var auth = xtend({}, defaults, options ); 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){ if (!data.headers.cookie) { return accept(null, false); } - var parsedCookie = cookie.parse(data.headers.cookie); - - data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.secret); + data.cookie = parseCookie(auth, data.headers.cookie); data.sessionID = data.cookie[ auth.key ]; diff --git a/package.json b/package.json index b39671b..519bcee 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,6 @@ "license": "MIT", "dependencies": { "passport": "~0.1.16", - "connect": "2.7.5", - "cookie": "0.0.5", "request": "~2.19.0", "xtend": "~2.0.3" }, @@ -34,6 +32,7 @@ "socket.io": "~0.9.14", "passport-local": "~0.1.6", "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" } } diff --git a/test/fixture/index.js b/test/fixture/index.js index aa5b9e8..50b92cf 100644 --- a/test/fixture/index.js +++ b/test/fixture/index.js @@ -24,8 +24,10 @@ exports.start = function (options, callback) { if(typeof options == 'function'){ callback = options; - options = {}; - } + options = { + }; + } + options.cookieParser = express.cookieParser; var app = express(); app.configure(function(){