Advait
Advait

Reputation: 599

How to do authentication over web sockets?

Why am I using websockets?

I'm working on routing all my HTTPS requests via a WebSocket, because my app has a chat feature and I need to keep the WebSocket open when the app is running, so why not just route all the requests through it.

My problem.

This turned out to be easier said that done. Should I use the same Access token & refresh token to verify client authentication. Or should I just verify it when the connection opens and then trust it for as long as it's open. So here are my questions:

  1. Is wss(Web socket secure) enough to stop man in the middle attacks?
  2. Should I generate a ticket sort of mechanism for every WebSocket connection, that lasts 2 - 10 minutes and then disconnect and ask the client to reconnect?
  3. Or should I have a Access Token with every request from the client.
  4. How to I make sure that when the server sends the data it is going to the right client.
  5. Should I just end to end encrypt all the payloads to avoid a lot of problems?

Upvotes: 1

Views: 947

Answers (1)

freakish
freakish

Reputation: 56557

Or should I just verify it when the connection opens and then trust it for as long as it's open.

That is fine as long as the connection is over a trusted channel, e.g. ssl/tls.

  1. Is wss(Web socket secure) enough to stop man in the middle attacks?

Yes. Wss is simply ws over ssl/tls.

  1. Should I generate a ticket sort of mechanism for every WebSocket connection, that lasts 2 - 10 minutes and then disconnect and ask the client to reconnect?

I'm not sure why would you do that. On the contrary, with chat-like app you want to keep the connection open as long as possible. Although I advice implementing ping calls on the client side and timeouts on the server side. With such approach you can require action on the client side every say 30s.

  1. Or should I have a Access Token with every request from the client.

Not necessary. With ssl/tls you can authenticate the entire connection once and just remember on the server side that is authenticated. Tokens are used with the classical HTTP because it is easier to scale horizontally such app, e.g. it doesn't matter which server the connection goes to, you can even switch servers between calls and that won't affect auth. But with chat-like app (or any app that requires bidirectional communication) the connection has to be persistent to begin with, and thus tokens introduce unnecessary overhead.

  1. How to I make sure that when the server sends the data it is going to the right client.

I'm not sure what you mean by that. That's pretty much what tcp + ssl/tls guarantees anyway. It is the same for any other protocol over secure tcp. Or do you mean at the app level? Well you have to match a user with a corresponding connection(s) once authenticated. The server has to track this.

  1. Should I just end to end encrypt all the payloads to avoid a lot of problems?

What problems? E2E encryption serves very different purpose: it guarantees that you, a.k.a. the server, is unable to read messages. It guarantess high level of privacy, so that even the server cannot read messages, only peers. And so this is a business decision, not technical or security decision. Do you want to have full control over conversations? Then obviously you can't go with E2E. If on the other hand you want to give the highest level of privacy to your users then it is a good (if not mandatory) approach. Note that full featured E2E is inherently more difficult to implement than non-E2E.

I need to keep the WebSocket open when the app is running, so why not just route all the requests through it.

That is an interesting approach. I myself am thinking about doing that (and most likely will try it out). The advantage is that entire communication goes through a single protocol which is easier to debug. Another advantage is that with a proper protocol you can achieve higher performance. The disadvantage is that the classical HTTP is well understood, there are lots of tools and subprotocols (e.g. REST) covering it. Security, binary streaming (e.g. file serving), etc. are often managed out of the box. So it feels a bit like reinventing the wheel. Either way, I wish you good luck with that, hopefuly you can come back to us and tell us how it went.

Upvotes: 2

Related Questions