socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

how to set status code for socket io Authentication error

Open lonso opened this issue 11 years ago • 11 comments

i use socket.io to Authentication. if error, i throw next(new Error('Authentication error')); but response status code is 200 not 403. how to set it to 403?

thks.

lonso avatar Dec 18 '14 07:12 lonso

There is no way to do that for now. Why do you need that?

nkzawa avatar Dec 23 '14 11:12 nkzawa

@nkzawa i want to auth on connection. if i response status code of 200, the client will be not know which is wrong so i need to response error status code

lonso avatar Dec 24 '14 10:12 lonso

You can listen error event to know the socket is unauthorized instead of http status code. Isn't it enough for you?

// server
io.use(function(socket, next) {
  next(new Error('Authentication error'));
});

// client
socket.on('error', function(err) {
  console.log(err);
});

nkzawa avatar Dec 24 '14 14:12 nkzawa

@nkzawa I know this. There is no way to set response status code ?

lonso avatar Dec 24 '14 15:12 lonso

No, there is no way. And I think we can't support that because it may happen in websocket connection. Could you explain why you have to know it as http status code?

nkzawa avatar Dec 24 '14 15:12 nkzawa

Something like this. If someone client by 'Authentication error',I don't want it create connection, I want it can break out,so I don't want it create a connection and then listen error event and then destroy connection.

lonso avatar Dec 24 '14 15:12 lonso

@lonso I'm pretty sure once your socket.io middleware rejects the authentication then the connection doesn't stay open. Are you finding that it does stay open?

yads avatar Dec 24 '14 16:12 yads

@lonso @yads as far as I tested, engine.io connection keeps open, but you can disconnect it too by disconnect method.

io.use(function(socket, next) {
  socket.disconnect();
  next(new Error());
});

nkzawa avatar Dec 24 '14 16:12 nkzawa

@nkzawa ok,thks.

lonso avatar Dec 25 '14 01:12 lonso

Not sure if anyone would see this, but I would love this feature. We are trying reject clients per certain authorization logic.

  socket.disconnect();
  next(new Error());

works for us, but the problem is it causes our reverse proxy that points to our websocket app to log a 502 because the disconnect just drops the connection. This is not a valid 502 and is causing false positives in the logs/monitoring. We'd like to return with a 400 or 401 to avoid this "false" 502 upstream from the node server. Thanks.

Right now FYI to get this it looks like i'm going to have to respond by intercepting all http requests that are bad with a 400 before we get to socket.io, which is messier then using this middleware.

purpleD17 avatar Mar 21 '16 15:03 purpleD17

Bump.

@purpleD17 You could send a special termination message like next(new Error('authentication_failed')) and then do setTimeout(()=>{ socket.disconnect(true); }, 3000); to avoid packet overlap which might be causing your connections to drop.

On the client side, have your client disconnect as you get the error message. The reason you close the connection on the server as well is to guarantee that the connection does really get disconnected as soon as possible.

SharkFourSix avatar Nov 27 '19 23:11 SharkFourSix