I wondered if there is a way to get an User (implementing UserInterface) by its JWT token?
what you need to do is to use the function getUser() in controller.
It's not in a controller but in a Ratchet WS server (acting like a service). The user is sending a WS message for authentication containing a token field. With this token I need to get the user object.
If you want to get User object in a service, just inject Security class from Symfony\Component\Security\Core\Security and use getUser() to obtain User object.
Documentation: https://symfony.com/doc/current/security.html#b-fetching-the-user-from-a-service
The thing is (as I said before), I am running a Websocket server using Ratchet (this server is always running in the background with a supervisor, and this is a separate part of my main app). The server is started using a Symfony command.
To authenticate, the user sends a WS message with a JWT token field. I need in my server to get this token, and I a service (for example UserService) to get the user authenticated with this JWT token and store it in memory with its connection ID.
@Cypaubr If you have access to Symfony services from the server, you can just inject TokenStorageInterface or the Security service and get user from there.
If you don't have access to the Symfony services, you have to parse the data manually, see https://jwt.io for the description of the structure.
Exact same case here. My Ratchet server receive a JWT token in WebSocket, and I would like to know which user sent this token.
I manage to get it with the method decode from Lexik\Bundle\JWTAuthenticationBundle\Encoder\LcobucciJWTEncoder