diff --git a/src/platform/auth/session/cloudWebSessionStore.ts b/src/platform/auth/session/cloudWebSessionStore.ts index ebb4f8fdb23..6726062addb 100644 --- a/src/platform/auth/session/cloudWebSessionStore.ts +++ b/src/platform/auth/session/cloudWebSessionStore.ts @@ -24,7 +24,6 @@ import { createSessionTokenMint, SessionTokenError } from '@comfyorg/account-core/sessionTokenMint' -import { readWebSession } from '@comfyorg/account-core/webSession' import { createWebSessionIdentity } from '@comfyorg/account-core/webSessionIdentity' import { createWebCrossTabRefreshPort, @@ -168,7 +167,6 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { let decidedForRequests: Promise = Promise.resolve() let reconnectTimer: ReturnType | undefined let pendingSignIn: InteractiveSignIn | null = null - let reread: WebSession | null = null let releaseRequests = () => {} const state = shallowRef({ phase: 'idle' }) const signedInUser = computed(() => @@ -244,7 +242,6 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { const session = createCloudIdentity() identity = session session.subscribe((next) => { - reread = null state.value = next }) const mint = createSessionTokenMint({ @@ -319,8 +316,7 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { function currentSession(): WebSession | undefined { const state = identity?.getState() - if (state?.phase !== 'signed_in') return undefined - return reread?.user.id === state.session.user.id ? reread : state.session + return state?.phase === 'signed_in' ? state.session : undefined } /** The epoch moves on every account change, so it pins the scope's user. */ @@ -370,15 +366,13 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { } } - async function rereadFor( + async function refreshFor( scope: WebSessionRequestScope ): Promise { - const result = await readWebSession(sessionOptions(), { - expectedUserId: scope.session.user.id - }) - if (result.status !== 'ok' || identity?.getEpoch() !== scope.epoch) return - reread = result.session - return { ...scope, session: result.session } + const next = await identity?.refresh(scope.session.user.id) + if (next?.phase !== 'signed_in' || next.session === scope.session) return + if (identity?.getEpoch() !== scope.epoch) return + return { ...scope, session: next.session } } function send( @@ -389,7 +383,7 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { ): Promise { return fetchOnWebSession(url, init, scope, { authorize, - reread: rereadFor, + refresh: refreshFor, workspaceDenied: (workspaceId) => useWorkspaceAuthStore().dropDeniedWorkspace(workspaceId) }) diff --git a/src/platform/auth/session/webSessionFetch.ts b/src/platform/auth/session/webSessionFetch.ts index 84f9e0f5d16..2ca68a6ce24 100644 --- a/src/platform/auth/session/webSessionFetch.ts +++ b/src/platform/auth/session/webSessionFetch.ts @@ -26,7 +26,7 @@ export interface WebSessionRequestScope { export interface WebSessionFetchPorts { /** A fresh session for the same user and epoch, or undefined to abandon. */ - readonly reread: ( + readonly refresh: ( scope: WebSessionRequestScope ) => Promise readonly workspaceDenied: (workspaceId: string) => void @@ -76,7 +76,7 @@ export async function fetchOnWebSession( if (code !== 'csrf_invalid' || init.body instanceof ReadableStream) { return response } - const fresh = await ports.reread(scope) + const fresh = await ports.refresh(scope) return fresh ? send(fresh) : response } diff --git a/src/platform/auth/webSessionFlagOn.test.ts b/src/platform/auth/webSessionFlagOn.test.ts index b404265b77f..bded9e570f5 100644 --- a/src/platform/auth/webSessionFlagOn.test.ts +++ b/src/platform/auth/webSessionFlagOn.test.ts @@ -544,6 +544,7 @@ function installIngest(features: Record = {}) { const ingest = { userId: 'user-a', csrfToken: 'csrf-1', + sessionDown: false, refusals: [] as string[], mintRefusal: undefined as (() => Response) | undefined, mintGate: undefined as Promise | undefined, @@ -611,14 +612,17 @@ function installIngest(features: Record = {}) { ) } + const answerSession = (): Response => + ingest.sessionDown + ? jsonResponse({ code: 'unavailable', message: 'down' }, 503) + : jsonResponse({ + ...sessionBody(ingest.userId), + csrf_token: ingest.csrfToken + }) + const respond = (request: ApiRequest, body: unknown): Response => { const { path, headers } = request - if (path === '/api/auth/session') { - return jsonResponse({ - ...sessionBody(ingest.userId), - csrf_token: ingest.csrfToken - }) - } + if (path === '/api/auth/session') return answerSession() if (path === '/api/auth/token') return mint(body) if (path === '/api/workspaces/current') { if (ingest.currentWorkspaceDown) return ingest.currentWorkspaceDown() @@ -757,31 +761,46 @@ describe('cloud API requests on the shared web session', () => { { name: 'the same user is re-read once and retried once with the fresh token', sessionUser: 'user-a', + sessionDown: false, status: 200, tokens: ['csrf-1', 'session', 'csrf-2', 'csrf-2'] }, { - name: 'a changed user abandons the request', + name: 'a changed user abandons the request and the tab follows the new account', sessionUser: 'user-b', + sessionDown: false, + status: 403, + tokens: ['csrf-1', 'session', 'csrf-2'] + }, + { + name: 'a failed re-read abandons the request', + sessionUser: 'user-a', + sessionDown: true, status: 403, tokens: ['csrf-1', 'session', 'csrf-1'] } - ])('csrf_invalid: $name', async ({ sessionUser, status, tokens }) => { - const ingest = await bootOnSession() - ingest.refusals.push('csrf_invalid') - ingest.userId = sessionUser - ingest.csrfToken = 'csrf-2' + ])( + 'csrf_invalid: $name', + async ({ sessionUser, sessionDown, status, tokens }) => { + const ingest = await bootOnSession() + ingest.refusals.push('csrf_invalid') + ingest.userId = sessionUser + ingest.sessionDown = sessionDown + ingest.csrfToken = 'csrf-2' - const response = await postPrompt() - await postPrompt() + const response = await postPrompt() + const signedInAfterRefusal = useCloudWebSessionStore().signedInUser?.id + await postPrompt() - expect(response.status).toBe(status) - expect( - ingest.requests.map(({ path, headers }) => - path === '/api/auth/session' ? 'session' : headers['x-csrf-token'] - ) - ).toEqual(tokens) - }) + expect(response.status).toBe(status) + expect(signedInAfterRefusal).toBe(sessionUser) + expect( + ingest.requests.map(({ path, headers }) => + path === '/api/auth/session' ? 'session' : headers['x-csrf-token'] + ) + ).toEqual(tokens) + } + ) it('workspace_access_denied drops the selection and is never replayed', async () => { const ingest = await bootOnSession()