socketio-jwt
Advanced tools
| var fixture = require('./fixture/namespace'); | ||
| var request = require('request'); | ||
| var io = require('socket.io-client'); | ||
| describe('authorizer with namespaces', function () { | ||
| //start and stop the server | ||
| before(fixture.start); | ||
| after(fixture.stop); | ||
| describe('when the user is not logged in', function () { | ||
| it('should be able to connect to the default namespace', function (done){ | ||
| var socket = io.connect('http://localhost:9000'); | ||
| socket.once('hi', done); | ||
| }); | ||
| it('should not be able to connect to the admin namespace', function (done){ | ||
| var socket = io.connect('http://localhost:9000/admin'); | ||
| socket.once('disconnect', function () { | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| describe('when the user is logged in', function() { | ||
| beforeEach(function (done) { | ||
| request.post({ | ||
| url: 'http://localhost:9000/login', | ||
| form: { username: 'jose', password: 'Pa123' }, | ||
| json: true | ||
| }, function (err, resp, body) { | ||
| this.token = body.token; | ||
| done(); | ||
| }.bind(this)); | ||
| }); | ||
| it('should do the handshake and connect', function (done){ | ||
| var socket = io.connect('http://localhost:9000/admin', { | ||
| 'forceNew': true, | ||
| }); | ||
| var token = this.token; | ||
| socket.on('connect', function(){ | ||
| socket.on('authenticated', function () { | ||
| done(); | ||
| }).emit('authenticate', { token: token }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
| var express = require('express'); | ||
| var http = require('http'); | ||
| var socketIo = require('socket.io'); | ||
| var socketio_jwt = require('../../lib'); | ||
| var jwt = require('jsonwebtoken'); | ||
| var xtend = require('xtend'); | ||
| var server, sio; | ||
| var enableDestroy = require('server-destroy'); | ||
| /** | ||
| * This is an example server that shows how to do namespace authentication. | ||
| * | ||
| * The /admin namespace is protected by JWTs while the global namespace is public. | ||
| */ | ||
| exports.start = function (callback) { | ||
| options = { | ||
| secret: 'aaafoo super sercret', | ||
| timeout: 1000, | ||
| handshake: false | ||
| }; | ||
| var app = express(); | ||
| app.configure(function(){ | ||
| this.use(express.json()); | ||
| this.use(express.urlencoded()); | ||
| }); | ||
| app.post('/login', function (req, res) { | ||
| var profile = { | ||
| first_name: 'John', | ||
| last_name: 'Doe', | ||
| email: 'john@doe.com', | ||
| id: 123 | ||
| }; | ||
| // We are sending the profile inside the token | ||
| var token = jwt.sign(profile, options.secret, { expiresInMinutes: 60*5 }); | ||
| res.json({token: token}); | ||
| }); | ||
| server = http.createServer(app); | ||
| sio = socketIo.listen(server); | ||
| sio.on('connection', function (socket) { | ||
| socket.emit('hi'); | ||
| }); | ||
| var admin_nsp = sio.of('/admin'); | ||
| admin_nsp.on('connection', socketio_jwt.authorize(options)) | ||
| .on('authenticated', function (socket) { | ||
| socket.emit('hi admin'); | ||
| }); | ||
| server.listen(9000, callback); | ||
| enableDestroy(server); | ||
| }; | ||
| exports.stop = function (callback) { | ||
| sio.close(); | ||
| server.destroy(); | ||
| callback(); | ||
| }; |
+9
-3
@@ -10,7 +10,7 @@ var xtend = require('xtend'); | ||
| return function (socket) { | ||
| var server = this; | ||
| var server = this.server || socket.server; | ||
| if (!server.$emit) { | ||
| //then is socket.io 1.0 | ||
| var Namespace = Object.getPrototypeOf(server.server.sockets).constructor; | ||
| var Namespace = Object.getPrototypeOf(server.sockets).constructor; | ||
| if (!~Namespace.events.indexOf('authenticated')) { | ||
@@ -57,3 +57,9 @@ Namespace.events.push('authenticated'); | ||
| } else { | ||
| server.server.sockets.emit('authenticated', socket); | ||
| //try getting the current namespace otherwise fallback to all sockets. | ||
| var namespace = (server.nsps && socket.nsp && | ||
| server.nsps[socket.nsp.name]) || | ||
| server.sockets; | ||
| // explicit namespace | ||
| namespace.emit('authenticated', socket); | ||
| } | ||
@@ -60,0 +66,0 @@ }; |
+2
-1
| { | ||
| "name": "socketio-jwt", | ||
| "version": "4.2.1", | ||
| "version": "4.3.0", | ||
| "description": "authenticate socket.io connections using JWTs", | ||
@@ -34,2 +34,3 @@ "main": "lib/index.js", | ||
| "request": "~2.19.0", | ||
| "server-destroy": "~1.0.1", | ||
| "should": "~1.2.2", | ||
@@ -36,0 +37,0 @@ "socket.io": "^1.0.4", |
@@ -19,4 +19,6 @@ var fixture = require('./fixture'); | ||
| it('should close the connection after a timeout if no auth message is received', function (done){ | ||
| var socket = io.connect('http://localhost:9000'); | ||
| socket.on('disconnect', function () { | ||
| var socket = io.connect('http://localhost:9000', { | ||
| forceNew: true | ||
| }); | ||
| socket.once('disconnect', function () { | ||
| done(); | ||
@@ -23,0 +25,0 @@ }); |
@@ -12,2 +12,3 @@ var express = require('express'); | ||
| var server, sio; | ||
| var enableDestroy = require('server-destroy'); | ||
@@ -53,7 +54,4 @@ exports.start = function (options, callback) { | ||
| if (options.handshake) { | ||
| // this.set('authorization', socketio_jwt.authorize(options)); | ||
| sio.use(socketio_jwt.authorize(options)); | ||
| } | ||
| if (options.handshake) { | ||
| sio.sockets.on('echo', function (m) { | ||
@@ -77,2 +75,3 @@ sio.sockets.emit('echo-response', m); | ||
| server.listen(9000, callback); | ||
| enableDestroy(server); | ||
| }; | ||
@@ -82,3 +81,4 @@ | ||
| sio.close(); | ||
| server.destroy(); | ||
| callback(); | ||
| }; |
24804
15.47%16
14.29%454
28.61%9
12.5%3
50%