diff --git a/src/sw-media-auth-recovery.test.ts b/src/sw-media-auth-recovery.test.ts index 7d5720958..1b27b2a9b 100644 --- a/src/sw-media-auth-recovery.test.ts +++ b/src/sw-media-auth-recovery.test.ts @@ -82,6 +82,50 @@ describe('service worker media auth recovery', () => { expect(fetch).not.toHaveBeenCalled(); }); + it('resolves a push without user_id against the only known account', async () => { + await swTestHooks.setSession( + 'alice-window', + 'alice-token', + 'https://matrix.example.org', + '@alice:example.org' + ); + + await expect(swTestHooks.getSessionForPush(undefined)).resolves.toMatchObject({ + accessToken: 'alice-token', + }); + }); + + it('resolves a push without user_id from cache after an sw restart', async () => { + persistedSessions = JSON.stringify({ + '@alice:example.org': { + accessToken: 'alice-token', + baseUrl: 'https://matrix.example.org', + userId: '@alice:example.org', + }, + }); + + await expect(swTestHooks.getSessionForPush(undefined)).resolves.toMatchObject({ + accessToken: 'alice-token', + }); + }); + + it('refuses to guess an account for a push without user_id when several are signed in', async () => { + await swTestHooks.setSession( + 'alice-window', + 'alice-token', + 'https://matrix.example.org', + '@alice:example.org' + ); + await swTestHooks.setSession( + 'bob-window', + 'bob-token', + 'https://matrix.example.org', + '@bob:example.org' + ); + + await expect(swTestHooks.getSessionForPush(undefined)).resolves.toBeUndefined(); + }); + it('removes a stale persisted account when a client switches users', async () => { await swTestHooks.setSession( 'client-a', diff --git a/src/sw.ts b/src/sw.ts index c11abfc17..2f23eeb47 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -382,9 +382,17 @@ function mxcToNotificationUrl(mxcUrl: string, baseUrl: string): string | undefin return `${baseUrl}/_matrix/media/v3/thumbnail/${encodeURIComponent(server)}/${encodeURIComponent(mediaId)}?width=96&height=96&method=crop`; } -async function getSessionForPush(userId: string): Promise { - const liveSession = [...sessions.values()].find((session) => session.userId === userId); - return liveSession ?? loadPersistedSession(userId); +async function getSessionForPush(userId?: string): Promise { + if (userId) { + const liveSession = [...sessions.values()].find((session) => session.userId === userId); + return liveSession ?? loadPersistedSession(userId); + } + + const liveAccounts = new Set([...sessions.values()].map((session) => session.userId)); + if (liveAccounts.size === 1) return sessions.values().next().value; + + const persisted = Object.values(await loadPersistedSessions()); + return persisted.length === 1 ? persisted[0] : undefined; } /** @@ -450,7 +458,7 @@ async function handleMinimalPushPayload( userId: string | undefined, windowClients: readonly Client[] ): Promise { - const session = userId ? await getSessionForPush(userId) : undefined; + const session = await getSessionForPush(userId); if (!session) { // No session anywhere — app was never opened since install, or the user logged out. @@ -859,6 +867,7 @@ function unavailableAuthenticatedMediaResponse(): Response { } export const swTestHooks = { + getSessionForPush, requestSessionWithTimeout, respondWithMediaAuthRecovery, setSession,