LCOV - code coverage report
Current view: top level - src/platform/auth/localWeb - localWebSession.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 58.3 % 24 14
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
       2              : import { getComfyCloudBaseUrl } from '@/config/comfyApi'
       3              : import { startDesktopHostSession } from '@/platform/auth/desktopHost/desktopHostSession'
       4              : import type {
       5              :   FailedSignIn,
       6              :   LocalWebAuthEnvironment
       7              : } from '@/platform/auth/localWeb/localWebAuthBridge'
       8              : import { createLocalWebAuthBridge } from '@/platform/auth/localWeb/localWebAuthBridge'
       9              : import { createStorageTokenStore } from '@/platform/auth/localWeb/localWebTokenStore'
      10              : import { reportError } from '@comfyorg/telemetry/reportError'
      11              : 
      12              : /**
      13              :  * Hosts whose origin Cloud allows to read `/oauth/token` responses. `[::1]` is
      14              :  * a registered redirect but has no CORS origin, so it cannot finish a sign-in.
      15              :  */
      16         2352 : const LOOPBACK_HOSTNAMES: ReadonlySet<string> = new Set([
      17              :   '127.0.0.1',
      18              :   'localhost'
      19              : ])
      20              : 
      21         2352 : const REFRESH_LOCK_NAME = 'Comfy.LocalWebSignIn.Refresh'
      22         2352 : const POPUP_FEATURES = 'popup,width=520,height=720'
      23              : 
      24            0 : function reportSignInFailure(failure: FailedSignIn): void {
      25            0 :   reportError(new Error(`local web sign-in failed: ${failure.reason}`), {
      26              :     errorType: 'local_web_sign_in_failed',
      27              :     surface: 'auth',
      28              :     context: { reason: failure.reason, oauthError: failure.error }
      29              :   })
      30              : }
      31              : 
      32            0 : function withOriginLock<T>(task: () => Promise<T>): Promise<T> {
      33            0 :   return navigator.locks.request(REFRESH_LOCK_NAME, task)
      34              : }
      35              : 
      36              : interface SignInFailureNotices {
      37              :   onPopupBlocked: () => void
      38              :   onSignInFailed: () => void
      39              : }
      40              : 
      41         2352 : function browserEnvironment({
      42              :   onPopupBlocked,
      43              :   onSignInFailed
      44              : }: SignInFailureNotices): LocalWebAuthEnvironment {
      45         2352 :   return {
      46              :     issuer: getComfyCloudBaseUrl(),
      47              :     pageUrl: () => window.location.href,
      48              :     replaceUrl: (url) =>
      49              :       window.history.replaceState(window.history.state, '', url),
      50              :     sessionStorage: {
      51              :       getItem: (key) => window.sessionStorage.getItem(key),
      52              :       setItem: (key, value) => window.sessionStorage.setItem(key, value),
      53              :       removeItem: (key) => window.sessionStorage.removeItem(key)
      54              :     },
      55              :     tokens: createStorageTokenStore(
      56              :       {
      57              :         getItem: (key) => window.localStorage.getItem(key),
      58              :         setItem: (key, value) => window.localStorage.setItem(key, value),
      59              :         removeItem: (key) => window.localStorage.removeItem(key)
      60              :       },
      61              :       window
      62              :     ),
      63              :     withRefreshLock: withOriginLock,
      64              :     fetchImpl: (input, init) => fetch(input, init),
      65              :     now: () => Date.now(),
      66              :     openPopup: () => window.open('about:blank', '_blank', POPUP_FEATURES),
      67              :     onMessage(listener) {
      68            0 :       const onMessage = (event: MessageEvent) => listener(event)
      69            0 :       window.addEventListener('message', onMessage)
      70            0 :       return () => window.removeEventListener('message', onMessage)
      71              :     },
      72              :     reportSignInFailure(failure) {
      73            0 :       reportSignInFailure(failure)
      74            0 :       if (failure.reason === 'popup-blocked') onPopupBlocked()
      75            0 :       else onSignInFailed()
      76              :     }
      77              :   }
      78              : }
      79              : 
      80              : export type LocalWebSessionStart = 'off' | 'started' | 'sign-in-failed'
      81              : 
      82              : /**
      83              :  * Signs a local ComfyUI in to Cloud from the browser when Desktop does not
      84              :  * host it. Off unless `local_web_sso` is on and the page is on a loopback
      85              :  * host; a sign-in the redirect brought back is finished before the session
      86              :  * starts, so the first state consumers read already includes it.
      87              :  * `sign-in-failed` means the returned sign-in failed for a reason other than
      88              :  * the person declining consent, so the page should say so once it mounts.
      89              :  */
      90         2352 : export async function startLocalWebSession(
      91              :   notices: SignInFailureNotices,
      92              :   env: LocalWebAuthEnvironment = browserEnvironment(notices)
      93              : ): Promise<LocalWebSessionStart> {
      94         2352 :   if (!useFeatureFlags().flags.localWebSsoEnabled) return 'off'
      95           10 :   if (!LOOPBACK_HOSTNAMES.has(new URL(env.pageUrl()).hostname)) return 'off'
      96              : 
      97           10 :   const bridge = createLocalWebAuthBridge(env)
      98           10 :   const outcome = await bridge.completeSignIn()
      99         2352 :   const failed = outcome.kind === 'failed' && outcome.reason !== 'denied'
     100         2352 :   if (failed) reportSignInFailure(outcome)
     101           10 :   await startDesktopHostSession(bridge, { reportsSignInFailures: true })
     102         2352 :   return failed ? 'sign-in-failed' : 'started'
     103              : }
        

Generated by: LCOV version 2.0-1