initial
This commit is contained in:
commit
678d4ccd6c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules/*
|
50
README.md
Normal file
50
README.md
Normal file
@ -0,0 +1,50 @@
|
||||
Access [Passport.js](http://passportjs.org) user information from socket.io(http://socket.io) connection.
|
||||
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
```
|
||||
npm install passport.socketio
|
||||
```
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
|
||||
```javascript
|
||||
|
||||
//configure passport and express
|
||||
|
||||
var socketIo = require("socket.io"),
|
||||
passportSocketIo = require("passport.socketio");
|
||||
|
||||
var sio = socketIo.listen(webServer);
|
||||
|
||||
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
|
||||
}));
|
||||
|
||||
sio.sockets.on("connection", function(socket){
|
||||
console.log("user connected: ", socket.handshake.user.name);
|
||||
|
||||
//filter sockets by user...
|
||||
var userGender = socket.handshake.user.gender,
|
||||
opposite = userGender === "male" ? "female" : "male";
|
||||
|
||||
passportSocketIo.filterSocketsByUser(sio, function (user) {
|
||||
return user.gender === opposite;
|
||||
}).forEach(function(s){
|
||||
s.send("a " + userGender + " has arrived!");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
License
|
||||
========
|
||||
|
||||
MIT - José F. Romaniello 2012.
|
62
lib/index.js
Normal file
62
lib/index.js
Normal file
@ -0,0 +1,62 @@
|
||||
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 userProperty = passport._userProperty || 'user';
|
||||
|
||||
return function(data, accept){
|
||||
if (!data.headers.cookie) {
|
||||
return accept('Session cookie required.', false);
|
||||
}
|
||||
|
||||
var parsedCookie = cookie.parse(data.headers.cookie);
|
||||
|
||||
data.cookie = connectUtils.parseSignedCookies(parsedCookie, sessionSecret);
|
||||
|
||||
data.sessionID = data.cookie['express.sid'];
|
||||
|
||||
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]){
|
||||
return accept('passport was not initialized', false);
|
||||
}
|
||||
|
||||
var userKey = session[passport._key][userProperty];
|
||||
|
||||
if(!userKey){
|
||||
return accept('not yet authenticated', false);
|
||||
}
|
||||
|
||||
passport.deserializeUser(userKey, function(err, user) {
|
||||
data[userProperty] = user;
|
||||
return accept(null, true);
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function filterSocketsByUser(socketIo, filter){
|
||||
var handshaken = socketIo.sockets.manager.handshaken;
|
||||
return Object.keys(handshaken || {})
|
||||
.filter(function(skey){
|
||||
return filter(handshaken[skey].user);
|
||||
})
|
||||
.map(function(skey){
|
||||
return socketIo.sockets.manager.sockets.sockets[skey];
|
||||
});
|
||||
}
|
||||
|
||||
exports.authorize = authorize;
|
||||
exports.filterSocketsByUser = filterSocketsByUser;
|
26
package.json
Normal file
26
package.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "passport.socketio",
|
||||
"version": "0.0.1",
|
||||
"description": "access passport.js authenticated user information from socket.io",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"socket",
|
||||
"socket.io",
|
||||
"passport"
|
||||
],
|
||||
"author": {
|
||||
"name": "José F. Romaniello",
|
||||
"email": "jfromaniello@gmail.com"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/jfromaniello/passport.socketio.git"
|
||||
},
|
||||
"scripts": {},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"passport": "~0.1.12",
|
||||
"connect": "~2.4.5",
|
||||
"cookie": "0.0.4"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user