GeniusesOfSymfony / WebSocketBundle

:part_alternation_mark: Websocket server for Symfony applications (powered by Ratchet), includes a Autobahn.JS based JavaScript client
MIT License
609 stars 140 forks source link

Session Management & User authentication #411

Open tejadong opened 4 years ago

tejadong commented 4 years ago

Hello, I have an API (Symfony 3) that works with the following repository: https://gist.github.com/diegonobre/341eb7b793fc841c0bba3f2b865b8d66

It works using tokens and it works quite well. The jWe part of the GosWebSocketBundle client has been integrated into a test project in Angular 9 and the websocket is working correctly (it connects and sends messages between the client and the server). As for the authentication, if I do it using the Symfony form, I can detect the user's data from the websocket server without problems, but if I do it from angular I can't because the authentication works through the path /oauth/v2/token and this returns me the token I have to use on each next request so the server knows who I am and allows me to perform this task ... is there any way to get GosWebSocketBundle to detect login via path before mentioned in order to obtain the user's data in the websocket server in the same way as if I log in from the symfony login form?

mbabker commented 4 years ago

I couldn't tell you to be honest if that approach is even possible out-of-the-box.

This bundle and the underlying Ratchet library expect to be able to share a session started in the main Symfony web application using a more conventional request/response workflow, that's being done by dispatching an event when a new websocket connection is established with the most pertinent bit at this part which is reading the data out of the session object created by Ratchet. If you're doing OAuth, then it sounds like you don't have a cookie with the session name in it and that's where Ratchet is having issues making the session functionality work.

Unfortunately the request info isn't available by the time it gets to this bundle's code, so I don't know if there would be a way for you to just replace the inbuilt authentication provider. Looking at https://github.com/ratchetphp/Ratchet/issues/604 it seems one option might be to create your own replacement for Ratchet\Session\SessionProvider that can get the info you need to set up the session correctly, doing that is going to require you replace the gos_web_socket.server.builder service from this bundle with your own Gos\Bundle\WebSocketBundle\Server\App\ServerBuilderInterface implementation (you should be able to just copy the builder from this bundle and change the new SessionProvider() call to your custom class).