socketio-jwt
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -30,25 +30,35 @@ var xtend = require('xtend'); | ||
} | ||
jwt.verify(data.token, options.secret, options, function(err, decoded) { | ||
var onError = function(){ | ||
return socket.disconnect('unauthorized'); | ||
// error handler | ||
var onError = function(err, code) { | ||
if (err) { | ||
code = code || 'unknown'; | ||
var error = new UnauthorizedError(code, { | ||
message: (Object.prototype.toString.call(err) === '[object Object]' && err.message) ? err.message : err | ||
}); | ||
socket.emit('unauthorized', error, function() { | ||
socket.disconnect('unauthorized'); | ||
}); | ||
return; // stop logic, socket will be close on next tick | ||
} | ||
}; | ||
if (err) { | ||
onError(); | ||
return onError(err, 'invalid_token'); | ||
} | ||
// success handler | ||
var onSuccess = function(){ | ||
socket.decoded_token = decoded; | ||
socket.emit('authenticated'); | ||
if (server.$emit) { | ||
server.$emit('authenticated', socket); | ||
} else { | ||
server.server.sockets.emit('authenticated', socket); | ||
} | ||
socket.decoded_token = decoded; | ||
socket.emit('authenticated'); | ||
if (server.$emit) { | ||
server.$emit('authenticated', socket); | ||
} else { | ||
server.server.sockets.emit('authenticated', socket); | ||
} | ||
}; | ||
if(options.additional_auth){ | ||
options.additional_auth(decoded, onSuccess, onError); | ||
}else{ | ||
if(options.additional_auth && typeof options.additional_auth === 'function') { | ||
options.options.additional_auth(decoded, onSuccess, onError); | ||
} else { | ||
onSuccess(); | ||
@@ -55,0 +65,0 @@ } |
{ | ||
"name": "socketio-jwt", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "authenticate socket.io connections using JWTs", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/auth0/socketio-jwt.svg)](https://travis-ci.org/auth0/socketio-jwt) | ||
Authenticate socket.io incoming connections with JWTs. This is useful if you are build a single page application and you are not using cookies as explained in this blog post: [Cookies vs Tokens. Getting auth right with Angular.JS](http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/). | ||
@@ -2,0 +4,0 @@ |
@@ -58,7 +58,8 @@ var fixture = require('./fixture'); | ||
socket.on('connect', function(){ | ||
console.log('connected'); | ||
socket.on('echo-response', done) | ||
.on('authenticated', function () { | ||
socket.emit('echo'); | ||
}).emit('authenticate', { token: token }); | ||
socket.on('echo-response', function () { | ||
socket.close(); | ||
done(); | ||
}).on('authenticated', function () { | ||
socket.emit('echo'); | ||
}).emit('authenticate', { token: token }); | ||
}); | ||
@@ -65,0 +66,0 @@ }); |
@@ -21,2 +21,3 @@ var fixture = require('./fixture'); | ||
err.code.should.eql("invalid_token"); | ||
socket.close(); | ||
done(); | ||
@@ -47,2 +48,3 @@ }); | ||
socket.on('connect', function(){ | ||
socket.close(); | ||
done(); | ||
@@ -64,4 +66,6 @@ }).on('error', done); | ||
socket.on('connect', function () { | ||
socket.close(); | ||
done(new Error('this shouldnt happen')); | ||
}).on('error', function (err) { | ||
socket.close(); | ||
err.message.should.eql("jwt signature is required"); | ||
@@ -68,0 +72,0 @@ done(); |
@@ -11,3 +11,3 @@ var express = require('express'); | ||
var server; | ||
var server, sio; | ||
@@ -50,3 +50,3 @@ exports.start = function (options, callback) { | ||
var sio = socketIo.listen(server); | ||
sio = socketIo.listen(server); | ||
@@ -57,3 +57,2 @@ if (options.handshake) { | ||
} | ||
sio.set('log level', 0); | ||
@@ -74,2 +73,6 @@ if (options.handshake) { | ||
server.__sockets = []; | ||
server.on('connection', function (c) { | ||
server.__sockets.push(c); | ||
}); | ||
server.listen(9000, callback); | ||
@@ -79,4 +82,4 @@ }; | ||
exports.stop = function (callback) { | ||
server.close(); | ||
sio.close(); | ||
callback(); | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21489
14
353
119