2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00

fixed all broken tests with socket.io 1.0, close #10

This commit is contained in:
José F. Romaniello 2014-06-05 15:45:41 -03:00
parent 130357ff80
commit 3484a429ed
5 changed files with 36 additions and 25 deletions

View File

@ -6,6 +6,14 @@ function noQsMethod(options) {
return function (socket) { return function (socket) {
var server = this; 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 () { var auth_timeout = setTimeout(function () {
socket.disconnect('unauthorized'); socket.disconnect('unauthorized');
}, options.timeout || 5000); }, options.timeout || 5000);
@ -19,7 +27,11 @@ function noQsMethod(options) {
socket.decoded_token = decoded; socket.decoded_token = decoded;
socket.emit('authenticated'); 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){ fail: function(error, data, accept){
if (data.request) { if (data.request) {
accept(); accept(error);
} else { } else {
accept(null, false); accept(null, false);
} }
@ -52,8 +64,8 @@ function authorize(options, onConnection) {
return function(data, accept){ return function(data, accept){
var token, error; var token, error;
var req = data.request || data;
var authorization_header = ((data.request || data).headers || {}).authorization; var authorization_header = (req.headers || {}).authorization;
if (authorization_header) { if (authorization_header) {
var parts = authorization_header.split(' '); var parts = authorization_header.split(' ');
@ -73,7 +85,9 @@ function authorize(options, onConnection) {
} }
//get the token from query string //get the token from query string
token = (data.request || data).query.token; if (req._query && req._query.token) {
token = req._query.token;
}
if (!token) { if (!token) {
error = new UnauthorizedError('credentials_required', { error = new UnauthorizedError('credentials_required', {

View File

@ -26,14 +26,13 @@
"xtend": "~2.1.2" "xtend": "~2.1.2"
}, },
"devDependencies": { "devDependencies": {
"connect": "~2.7.11",
"express": "~3.1.2",
"mocha": "~1.17.0",
"passport-local": "~0.1.6",
"request": "~2.19.0", "request": "~2.19.0",
"should": "~1.2.2", "should": "~1.2.2",
"mocha": "~1.17.0", "socket.io": "^1.0.4",
"express": "~3.1.2", "socket.io-client": "^1.0.4"
"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"
} }
} }

View File

@ -11,13 +11,12 @@ describe('authorizer', function () {
describe('when the user is not logged in', function () { describe('when the user is not logged in', function () {
it('should emit error with unauthorized handshake', function (done){ it('should emit error with unauthorized handshake', function (done){
var socket = io.connect('http://localhost:9000', { var socket = io.connect('http://localhost:9000?token=boooooo', {
'query': 'token=Booooooooooooooooooooo', 'forceNew': true
'force new connection': true
}); });
socket.on('error', function(err){ socket.on('error', function(err){
err.should.eql('handshake unauthorized'); err.should.eql("Invalid token: no header in signature 'boooooo'");
done(); done();
}); });
}); });
@ -39,7 +38,7 @@ describe('authorizer', function () {
it('should do the handshake and connect', function (done){ it('should do the handshake and connect', function (done){
var socket = io.connect('http://localhost:9000', { var socket = io.connect('http://localhost:9000', {
'force new connection':true, 'forceNew':true,
'query': 'token=' + this.token 'query': 'token=' + this.token
}); });
socket.on('connect', function(){ socket.on('connect', function(){

View File

@ -24,7 +24,7 @@ describe('authorizer without querystring', function () {
it('should not respond echo', function (done){ it('should not respond echo', function (done){
var socket = io.connect('http://localhost:9000', { var socket = io.connect('http://localhost:9000', {
'force new connection':true, 'forceNew':true,
}); });
socket.on('echo-response', function () { socket.on('echo-response', function () {
@ -51,7 +51,7 @@ describe('authorizer without querystring', function () {
it('should do the handshake and connect', function (done){ it('should do the handshake and connect', function (done){
var socket = io.connect('http://localhost:9000', { var socket = io.connect('http://localhost:9000', {
'force new connection':true, 'forceNew':true,
}); });
var token = this.token; var token = this.token;
socket.on('connect', function(){ socket.on('connect', function(){

View File

@ -48,12 +48,11 @@ exports.start = function (options, callback) {
var sio = socketIo.listen(server); var sio = socketIo.listen(server);
sio.configure(function(){ if (options.handshake) {
if (options.handshake) { // this.set('authorization', socketio_jwt.authorize(options));
this.set('authorization', socketio_jwt.authorize(options)); sio.use(socketio_jwt.authorize(options));
} }
this.set('log level', 0); sio.set('log level', 0);
});
if (options.handshake) { if (options.handshake) {
sio.sockets.on('echo', function (m) { sio.sockets.on('echo', function (m) {