Repository navigation
feat(socket-auth): authenticate the v3 SocketProvider with driver socket tokens - #116
Merged
Merged
Conversation
The v3 realtime client connected anonymously. It now builds the same
in-memory auth session the v2 contexts use (services/socket-auth): the
handshake carries a socket token minted for the signed-in driver from
POST {host}/{namespace}/socket/token, refreshed a minute before expiry
via socket.authenticate, and re-fetched when the server deauthenticates
the socket, kicks it out of a channel or refuses a subscription. The
channels `listen` currently holds are then subscribed again. A signed-out
app and a server without the route (404) connect anonymously as before.
V3App passes the API host and driver bearer to SocketProvider, plus a
session key (AuthContext's sessionEpoch, driver and organisation) so the
socket is rebuilt on login, logout, organisation switch and instance
reconfiguration. The handshake identifies the client as
navigator/<version>; the token never reaches storage or the URL.
A listener releasing a channel another listener still holds no longer
closes it under the other.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Authenticates the driver's realtime socket in both client trees:
SocketClusterContext.native.tsx/.web.tsx,AuthContext.tsx,OrderScreen.tsx) andsrc/services/socket-auth.ts: already onrelease/v3.0.0through feat(socket-auth): authenticate realtime socket with driver socket tokens #112. This PR does not change them.SocketProvider(src/v3/realtime/SocketProvider.tsx): new in this PR. It uses the same in-memory auth session,src/services/socket-auth.ts.How
POST {host}/{namespace}/socket/token, sent with the driver bearer. It goes through a custom in-memoryauthEngine:loadTokenfetches when there is no token or it is within 60 s of expiry.expires_in - 60swithsocket.authenticate.deauthenticate,kickOutorsubscribeFail, the app fetches a fresh token, re-authenticates, and resubscribes every channellistenstill holds. Channels are reference-counted, so a channel two listeners share is not closed while the second one still needs it. Handler and loop errors still never end the connection.V3Apppasses the API host, the driver bearer (userToken) and a session key toSocketProvider. The session key combines AuthContext'ssessionEpoch, the driver and the organisation. The socket is rebuilt on login, logout, organisation switch and instance reconfiguration (API host or socket config).navigator/<version>.Old servers
If the server has no socket token route (it answers 404), the socket connects anonymously for that session, the same as before this change. It also connects anonymously while no driver is signed in.
Why a new PR
This PR was meant to replace #112, whose branch was cut from main and carried the v2.0.11 release commits into v3. #112 has since been merged into
feature/v3-redesignand from there intorelease/v3.0.0(#109), so its v2 socket-auth commit and those v2.0.11 commits are already in this base. This branch isrelease/v3.0.0plus one commit, the v3SocketProviderwork.Tests
src/v3/realtime/__tests__/SocketProvider.test.tsxcovers:deauthenticate,kickOutandsubscribeFail__tests__/SocketAuth-test.jsis unchanged.The repo's CI workflow only runs on PRs to
mainand does not runyarn test, so these suites need a manualyarn jest src/v3/realtime __tests__/SocketAuth-test.jsrun before merge.Related PRs