diff --git a/README.md b/README.md index e7a2ad9..03ab039 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,14 @@ Usage var sio = socketIo.listen(webServer); + + //except for the optional fail and success the parameter object has the + //same attribute than the session middleware http://www.senchalabs.org/connect/middleware-session.html + sio.set("authorization", passportSocketIo.authorize({ - sessionKey: 'express.sid', //the cookie where express (or connect) stores its session id. - sessionStore: mySessionStore, //the session store that express uses - sessionSecret: "my session secret", //the session secret to parse the cookie + 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 }, diff --git a/lib/index.js b/lib/index.js index a4bdf8c..4434353 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,17 +3,17 @@ var connectUtils = require('connect').utils, xtend = require('xtend'); function authorize(options) { - var auth = { + var defaults = { passport: require('passport'), - sessionKey: 'express.sid', - sessionSecret: null, - sessionStore: null, + key: 'express.sid', + secret: null, + store: null, success: null, fail: null }; - xtend( auth, options ); - + var auth = xtend({}, defaults, options ); + auth.userProperty = auth.passport._userProperty || 'user'; return function(data, accept){ @@ -23,11 +23,11 @@ function authorize(options) { var parsedCookie = cookie.parse(data.headers.cookie); - data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.sessionSecret); + data.cookie = connectUtils.parseSignedCookies(parsedCookie, auth.secret); - data.sessionID = data.cookie[ auth.sessionKey ]; + data.sessionID = data.cookie[ auth.key ]; - auth.sessionStore.get(data.sessionID, function(err, session){ + auth.store.get(data.sessionID, function(err, session){ if (err) { return accept('Error in session store.', false); } else if (!session) { diff --git a/package.json b/package.json index 2dc605e..8cdd7d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "passport.socketio", - "version": "0.0.4", + "version": "0.0.5", "description": "access passport.js authenticated user information from socket.io", "main": "lib/index.js", "keywords": [ diff --git a/test/fixture/index.js b/test/fixture/index.js index 0b4b952..aa5b9e8 100644 --- a/test/fixture/index.js +++ b/test/fixture/index.js @@ -8,8 +8,13 @@ var socketIo = require('socket.io'), passportSocketIo = require('../../lib'); var sessionStore = new connect.session.MemoryStore(), - cookieSecret = 'asdasdsdas1312312', - sessionKey = 'test-session-key'; + sessionSecret = 'asdasdsdas1312312', + sessionKey = 'test-session-key', + sessionOptions = { + store: sessionStore, + key: sessionKey, + secret: sessionSecret + }; var server; @@ -24,15 +29,12 @@ exports.start = function (options, callback) { var app = express(); app.configure(function(){ - app.use(express.cookieParser(cookieSecret)); + app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(express.methodOverride()); - app.use(express.session({ - store: sessionStore, - key: sessionKey - })); + app.use(express.session(sessionOptions)); app.use(passport.initialize()); app.use(passport.session()); @@ -55,11 +57,7 @@ exports.start = function (options, callback) { var sio = socketIo.listen(server); sio.configure(function(){ - this.set('authorization', passportSocketIo.authorize(xtend({ - sessionKey: sessionKey, - sessionStore: sessionStore, - sessionSecret: cookieSecret - }, options))); + this.set('authorization', passportSocketIo.authorize(xtend(sessionOptions, options))); this.set('log level', 0);