Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket server: how to authorize clients?

Tags:

I am currently building a webpage with a game using html5 canvas and websockets. I have a websocket server written in perl running on server:3000. On the server I have also installed apache, and made a website that the user can visit: http://server/. javascript on this page creates a websocket to ws://server:3000

Connection works and I can send messages from my browser and get the responses, everything shown in a div for now. The messages that I send are for now only "message:...", so the server knows that it is a message and outputs '...' to all users currently connected to the "game".

Now the problem is that anyone could write their own client that would connect to ws://server:3000 and send/receive appropriate messages. This way they would use my backend, but not my client (I would like them to play only from http://server/).

How can I ensure that only my client is used? The main concern is cheating. You could write a bot that connect to a port and plays for you by sending messages and intepreting the ones that it receives...How to overcome this issue?

like image 499
Michal Avatar asked Feb 23 '11 10:02

Michal


People also ask

How do I authenticate a WebSocket user?

Authentication FlowThe server generates a temporary external authentication token, stores it in the Authentication Cache, and returns it to the client. The client makes a WebSocket handshake request with the external authentication token passed as a query-string parameter in the handshake endpoint URL.

How do websockets connect to clients?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

Can WebSocket handle multiple clients?

A server can open WebSocket connections with multiple clients—even multiple connections with the same client. It can then message one, some, or all of these clients. Practically, this means multiple people can connect to our chat app, and we can message some of them at a time.

How do I make my WebSocket server secure?

Use wss:// instead of ws://. This adds a security layer over your communication. Use a server like Nginx for reverse proxying websockets and enable SSL over them.


1 Answers

I'm using a Jetty web server.

I am using Basic authorisation. I used this at first simply because it was easy to implement. However, after discovering the authorisation issue presented by websockets, I think basic authorisation is a good mechanism to use.

The upgrade request header has authorisation data hashed in it:

Accept-Encoding:gzip, deflate, sdch Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 Authorization:Basic YWRtaW46cm9vdA== Cache-Control:no-cache Connection:Upgrade 

In my websocket server, with each OnMessage event I call:

session.getUpgradeRequest().getUserPrincipal() 

I can then use this information to decide whether the data is authorised to be processed. Also, I check the origin of the socket in the OnConnect event:

 session.getUpgradeRequest().getOrigin() 
like image 105
Christian Lacdael Avatar answered Oct 14 '22 13:10

Christian Lacdael