SocketIOClient-Unreal icon indicating copy to clipboard operation
SocketIOClient-Unreal copied to clipboard

Additional headers on connect

Open oandreassen opened this issue 9 years ago • 11 comments

It would be great if you could pass additional HTTP headers when connecting - for example, passing OAUTH-tokens to authenticate on the server.

oandreassen avatar Apr 07 '17 13:04 oandreassen

If it's something supported by https://socket.io/docs/client-api/ and https://socket.io/docs/server-api/ then give me an example of expected behavior for what you mean from both sides (server and client)

getnamo avatar Apr 07 '17 13:04 getnamo

From our Socket.IO Server:

public use(server: SocketIO.Server): void {
        let self = this;
        this._namespace = server.use(this.onRequest.bind(this));
    // ...
    }

private onRequest(socket: SocketIO.Socket, next: (error: Error) => void): void {
        let message = socket.request as IncomingMessage;

        if (message != null && message.headers['authentication']) {
            let auth = message.headers['authentication'] as string;

            if (auth == null || typeof (auth) != 'string') {
                done(new RequestError(400, 'Authentication header value was null or not a string.'));
            } else {
               // .. parse header
        }
    }

@getnamo

oandreassen avatar Apr 07 '17 13:04 oandreassen

That looks like javascript, but I've never seen public/private and type specifiers on it, is it TypeScript?

getnamo avatar Apr 07 '17 14:04 getnamo

Yes sir! But the point is, socket.io have a server.use method which can be used to authenticate incoming calls. I would like to refer you to the socket.io documentation but its sooo limited. There are however a number of authentication npm-packages for socket.io which uses this approach as well.

oandreassen avatar Apr 07 '17 14:04 oandreassen

It looks like the client api has a callback on connection https://socket.io/docs/client-api/#manager-open-callback where you could then call the server with the auth information and then let the server accept/reject, and I'm assuming the OAuth token/etc would be a json object we'd pass in at the right point somewhere. But that doesn't seem ideal because the connection can still be made before the auth happens.

I guess what I'm saying is, we'd need an example on the client side of how this is handled in e.g. the authentication packages.

getnamo avatar Apr 07 '17 14:04 getnamo

We dont actually use any callback here, its just a additional header that is passed upon connect. If the authentication fails the client is disconnected - there is no additional work on the client side.

oandreassen avatar Apr 07 '17 14:04 oandreassen

Ok so we might be in luck, maybe. While https://socket.io/docs/client-api/ doesn't have anything about headers, there is this overload on connect https://github.com/socketio/socket.io-client-cpp/blob/master/src/sio_client.h#L61 which was pulled into socket.io-cpp master "recently" https://github.com/socketio/socket.io-client-cpp/commit/cda7b205dcc6d5db6f421d0eb57b4893ea6ad9f6 and with 0.5.0 I did update the underlying library to sync with master which added that API https://github.com/getnamo/socketio-client-ue4/commit/1af67eb2dc7e4de4d9ee6ac6846c634ccffdc071#diff-bbdab768c8bada5381ddc3001a2bb126R61.

To try the method, we would need to expose that connect method as an overload and try it out. The headers seem to be structured as a string:string keyed map. For UE4 api I'm thinking we could expose a blueprint connect function taking a SIOJson object with the understanding that it should be String:String keyed.

Thoughts? Is this what you're looking for?

getnamo avatar Apr 08 '17 01:04 getnamo

@getnamo thats exactly what I'm looking for

oandreassen avatar Apr 10 '17 06:04 oandreassen

latest merge https://github.com/getnamo/socketio-client-ue4/commit/f84af8c3a0bc02a957a05700c7c2cf7b51418cf9 (0.5.4) exposes the query and headers field as optional settings for Connect.

They expect an empty field by default (nullptr in c++) or a SIOJsonObject with purely string fields.

Here's an example of a connect call with a single header X-Forwarded-Host: qnova.io (remember to toggle auto-connect off in the component) connectwithheader

I can inspect this header being passed by using the socket from io.on('connection') via socket.conn and then digging down the tree in the object being passed

image

which resulted in

image

from the server console, so it should be working. Have a go with 0.5.4 and let me know if it's working as intended; though you'll have to recompile from source right now as I'm going to wait until I've addressed the new connection issues before making a new release.

getnamo avatar Apr 23 '17 01:04 getnamo

documentation added under: https://github.com/getnamo/socketio-client-ue4#complex-connect

getnamo avatar Apr 23 '17 01:04 getnamo

Thank you very much! Keep up the awesome work!11!

oandreassen avatar Apr 23 '17 17:04 oandreassen