Permalink
Browse files

Better modularity and different auth method by receiving cookies.

  • Loading branch information...
1 parent 2a8ca86 commit 50d6b5e5074629e2ac170d3c236e89a419edd983 Jerome Paradis committed Jan 18, 2012
Showing with 139 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 README.md
  3. +9 −0 index.js
  4. +23 −0 package.json
  5. +103 −0 plugins/django-auth.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,3 @@
+# Introduction
+
+
View
@@ -0,0 +1,9 @@
+
+exports = module.exports = Manager;
+
+function Manager(io, plugin, options) {
+ var SocketAuth = require('./plugins/' + plugin);
+
+ SocketAuth(io, options);
+
+}
View
@@ -0,0 +1,23 @@
+{
+ "name": "socket-auth"
+ , "version": "0.0.1"
+ , "description": "Socket.io authentication"
+ , "homepage": "https://github.com/JeromeParadis/socket-auth"
+ , "keywords": ["websocket", "socket", "authentication", "django"]
+ , "author": "Jerome Paradis <jerome@buyosphere.com>"
+ , "contributors": [
+ ]
+ , "repository":{
+ "type": "git"
+ , "url": "https://JeromeParadis@github.com/JeromeParadis/socket-auth.git"
+ }
+ , "dependencies": {
+ "redis": "0.6.7"
+ , "socket.io": "0.8.7"
+ , "connect": "1.8.5"
+ }
+ , "devDependencies": {
+ }
+ , "main": "index"
+ , "engines": { "node": ">= 0.4.0" }
+}
View
@@ -0,0 +1,103 @@
+/**
+ * Export the wrapper.
+ */
+
+var connect = require('connect')
+ ,redis = require('redis');
+
+exports = module.exports = SocketAuthentication;
+
+function SocketAuthentication(io, options) {
+ this.rc = redis.createClient();
+ this.redis_namespace = (options && options.redis_namespace) ? options.redis_namespace : "sauth.";
+ this.session_timeout = (options && options.session_timeout) ? options.session_timeout * 60 : 20 * 60;
+ var self = this;
+ /* DO THIS IF RUNNING ON SAME DOMAIN?
+ io.configure(function (){
+ io.set('authorization', function (handshakeData, callback) {
+ var cookie = handshakeData.headers.cookie;
+ console.log("Cookie = ", cookie)
+ var sessionid = null;
+ if (cookie) {
+ var parsed_cookies = connect.utils.parseCookie(cookie);
+ sessionid = parsed_cookies['sessionid'] || null;
+ console.log('Session = ' + sessionid)
+ }
+ handshakeData.django_sessionid = sessionid;
+ if (sessionid) {
+ var key = options.redis_namespace + 'sessions:' + sessionid;
+ self.rc.get(key,function(err,obj) {
+ console.log(err,obj)
+ if (obj) {
+ console.log("User connected",obj);
+ // rc.expire(key,20*60); // Reset expiration when user reconnects
+ callback(null, true);
+ }
+ else
+ callback("Session is not longer active.", false);
+ });
+ }
+ else if (!sessionid)
+ callback("Not a valid session.", false);
+ });
+
+ return this;
+ });
+ */
+
+ io.sockets.on('connection', function (socket) {
+ socket.emit('authorize');
+ socket.on('auth', function (data) {
+ console.log("sessionid:",data.sessionid);
+ self.authorize(data.sessionid,function(err,authorized) {
+ console.log("Authorized: ",authorized, data.sessionid)
+ });
+ });
+
+ // Ping event to refresh session of openned browser window
+ // --------------------------------------------
+ socket.on('auth_ping', function (data) {
+ console.log("ping sessionid:",data.sessionid);
+ self.ping(data.sessionid,function(err,authorized) {
+ console.log("Pinged: ",authorized, data.sessionid)
+ });
+ });
+
+ });
+
+ this.get_user = function(id,callback) {
+
+ };
+
+ this.getSessionKey = function(sessionid) {
+ return key = options.redis_namespace + 'sessions:' + sessionid;
+ };
+
+ this.authorize = function(sessionid, callback) {
+ if (sessionid) {
+ var key = self.getSessionKey(sessionid);
+ self.rc.get(key,function(err,obj) {
+ console.log(err,obj)
+ if (obj) {
+ console.log("User connected",obj);
+ rc.expire(key,self.session_timeout); // Reset expiration when user reconnects
+ callback && callback(null, true);
+ }
+ else
+ callback && callback("Session is not longer active.", false);
+ });
+ }
+ else if (!sessionid)
+ callback && callback("Not a valid session.", false);
+ };
+
+ this.ping = function(sessionid, callback) {
+ console.log("pinging session ", sessionid)
+ self.authorize(sessionid,callback);
+ }
+
+ this.get_session = function(sessionid) {
+
+ };
+
+}

0 comments on commit 50d6b5e

Please sign in to comment.