Skip to content

Root nsp middleware always called before sub nsp middleware authorization  #1888

@peteruithoven

Description

@peteruithoven

It's now possible to define middleware per namespace, but when this is used for authorization there is an issue. The issue is that the root namespace authorization is always called first, when this fails the namespace specific authorization isn't called. This makes it "impossible" to send the client an error from the namespace it was connecting to.
Not sure what would be the best solution here...

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketClient = require('socket.io-client');
var debug = require('debug')('nspAuthorization');
var port = 5000;

// SERVER
http.listen(port, function(){
  debug('server listening on *:'+port);
});
var rootNSP = io.of('/');
var subNSP = io.of('/namespace');
rootNSP.use(authorization); // when enabled, the user won't get a error
subNSP.use(authorization);
function authorization(socket, next) {
  debug('authorization: to nsp: ',socket.nsp.name);
  var query = socket.handshake.query;
  if (query.secret === 'abc') {
    debug("  valid secret");
    next();
  } else{
    debug("  invalid secret");
    next(new Error('invalid secret'));
  }
}

// CLIENT
var url = 'http://localhost:'+port;
debug('namespace: connecting');
var socket = socketClient.connect(url+'/namespace?secret=fake');
socket.on('error',function(err) { // not received when root authorization middleware is enabled
  debug('namespace: error: ',arguments);
});
socket.on('connection',function() { 
  debug('namespace: connected');
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions