diff --git a/lib/index.js b/lib/index.js index e2a23c3..7e014c7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,14 @@ function noQsMethod(options) { return function (socket) { var server = this; + if (!server.$emit) { + //then is socket.io 1.0 + var Namespace = Object.getPrototypeOf(server.server.sockets).constructor; + if (!~Namespace.events.indexOf('authenticated')) { + Namespace.events.push('authenticated'); + } + } + var auth_timeout = setTimeout(function () { socket.disconnect('unauthorized'); }, options.timeout || 5000); @@ -19,7 +27,11 @@ function noQsMethod(options) { socket.decoded_token = decoded; socket.emit('authenticated'); - server.$emit('authenticated', socket); + if (server.$emit) { + server.$emit('authenticated', socket); + } else { + server.server.sockets.emit('authenticated', socket); + } }); }); @@ -37,7 +49,7 @@ function authorize(options, onConnection) { }, fail: function(error, data, accept){ if (data.request) { - accept(); + accept(error); } else { accept(null, false); } @@ -52,8 +64,8 @@ function authorize(options, onConnection) { return function(data, accept){ var token, error; - - var authorization_header = ((data.request || data).headers || {}).authorization; + var req = data.request || data; + var authorization_header = (req.headers || {}).authorization; if (authorization_header) { var parts = authorization_header.split(' '); @@ -73,7 +85,9 @@ function authorize(options, onConnection) { } //get the token from query string - token = (data.request || data).query.token; + if (req._query && req._query.token) { + token = req._query.token; + } if (!token) { error = new UnauthorizedError('credentials_required', { diff --git a/package.json b/package.json index ef1a199..a9344bb 100644 --- a/package.json +++ b/package.json @@ -26,14 +26,13 @@ "xtend": "~2.1.2" }, "devDependencies": { + "connect": "~2.7.11", + "express": "~3.1.2", + "mocha": "~1.17.0", + "passport-local": "~0.1.6", "request": "~2.19.0", "should": "~1.2.2", - "mocha": "~1.17.0", - "express": "~3.1.2", - "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", - "connect": "~2.7.11" + "socket.io": "^1.0.4", + "socket.io-client": "^1.0.4" } } diff --git a/test/authorizer.test.js b/test/authorizer.test.js index 2f12819..8890dfb 100644 --- a/test/authorizer.test.js +++ b/test/authorizer.test.js @@ -11,13 +11,12 @@ describe('authorizer', function () { describe('when the user is not logged in', function () { it('should emit error with unauthorized handshake', function (done){ - var socket = io.connect('http://localhost:9000', { - 'query': 'token=Booooooooooooooooooooo', - 'force new connection': true + var socket = io.connect('http://localhost:9000?token=boooooo', { + 'forceNew': true }); socket.on('error', function(err){ - err.should.eql('handshake unauthorized'); + err.should.eql("Invalid token: no header in signature 'boooooo'"); done(); }); }); @@ -39,7 +38,7 @@ describe('authorizer', function () { it('should do the handshake and connect', function (done){ var socket = io.connect('http://localhost:9000', { - 'force new connection':true, + 'forceNew':true, 'query': 'token=' + this.token }); socket.on('connect', function(){ diff --git a/test/authorizer_noqs.test.js b/test/authorizer_noqs.test.js index d6f8ef4..5ad39fe 100644 --- a/test/authorizer_noqs.test.js +++ b/test/authorizer_noqs.test.js @@ -24,7 +24,7 @@ describe('authorizer without querystring', function () { it('should not respond echo', function (done){ var socket = io.connect('http://localhost:9000', { - 'force new connection':true, + 'forceNew':true, }); socket.on('echo-response', function () { @@ -51,7 +51,7 @@ describe('authorizer without querystring', function () { it('should do the handshake and connect', function (done){ var socket = io.connect('http://localhost:9000', { - 'force new connection':true, + 'forceNew':true, }); var token = this.token; socket.on('connect', function(){ diff --git a/test/fixture/index.js b/test/fixture/index.js index e431341..070219a 100644 --- a/test/fixture/index.js +++ b/test/fixture/index.js @@ -48,12 +48,11 @@ exports.start = function (options, callback) { var sio = socketIo.listen(server); - sio.configure(function(){ - if (options.handshake) { - this.set('authorization', socketio_jwt.authorize(options)); - } - this.set('log level', 0); - }); + if (options.handshake) { + // this.set('authorization', socketio_jwt.authorize(options)); + sio.use(socketio_jwt.authorize(options)); + } + sio.set('log level', 0); if (options.handshake) { sio.sockets.on('echo', function (m) {