LCOV - code coverage report
Current view: top level - src/platform/auth/session - ssoReentryStorage.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 13.6 % 44 6
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { z } from 'zod'
       2              : 
       3              : import type { WebSession } from '@comfyorg/account-core/webSession'
       4              : 
       5         2356 : const HINT_KEY = 'Comfy.WebSession.SsoHint'
       6         2356 : const ATTEMPT_KEY = 'Comfy.WebSession.SsoReentryAt'
       7         2356 : const ATTEMPT_WINDOW_MS = 5 * 60_000
       8         2356 : const SSO_SIGN_IN_PROVIDERS: ReadonlySet<string> = new Set([
       9              :   'saml.workos',
      10              :   'oidc.workos'
      11              : ])
      12              : 
      13         2356 : const zHint = z.object({ email: z.string().min(1) })
      14         2356 : const zAttemptAt = z.number()
      15              : 
      16              : /** A convenience hint, never proof of identity: the session decides that. */
      17              : export type SsoHint = z.infer<typeof zHint>
      18              : 
      19            0 : function parseJson(raw: string | null): unknown {
      20            0 :   if (raw === null) return undefined
      21            0 :   try {
      22            0 :     return JSON.parse(raw)
      23              :   } catch {
      24            0 :     return undefined
      25              :   }
      26              : }
      27              : 
      28            0 : export function forgetSsoHint(): void {
      29            0 :   try {
      30            0 :     localStorage.removeItem(HINT_KEY)
      31              :   } catch {
      32            0 :     return
      33              :   }
      34              : }
      35              : 
      36            0 : export function isSsoSession(user: WebSession['user']): boolean {
      37            0 :   return SSO_SIGN_IN_PROVIDERS.has(user.signInProvider ?? '')
      38              : }
      39              : 
      40              : /** Keyed on the provider the server reports, so only an SSO session leaves a hint. */
      41            0 : export function rememberSignedInSession(user: WebSession['user']): void {
      42            0 :   if (!isSsoSession(user)) {
      43            0 :     forgetSsoHint()
      44            0 :     return
      45              :   }
      46            0 :   const hint: SsoHint = { email: user.email }
      47            0 :   try {
      48            0 :     localStorage.setItem(HINT_KEY, JSON.stringify(hint))
      49              :   } catch {
      50            0 :     return
      51              :   }
      52              : }
      53              : 
      54            0 : export function readSsoHint(): SsoHint | null {
      55            0 :   let raw: string | null
      56            0 :   try {
      57            0 :     raw = localStorage.getItem(HINT_KEY)
      58              :   } catch {
      59            0 :     return null
      60              :   }
      61            0 :   const hint = zHint.safeParse(parseJson(raw))
      62            0 :   return hint.success ? hint.data : null
      63              : }
      64              : 
      65              : /** Unreadable storage counts as recent, so a tab that cannot remember never loops. */
      66            0 : export function hasRecentSsoReentry(now = Date.now()): boolean {
      67            0 :   let raw: string | null
      68            0 :   try {
      69            0 :     raw = sessionStorage.getItem(ATTEMPT_KEY)
      70              :   } catch {
      71            0 :     return true
      72              :   }
      73            0 :   const at = zAttemptAt.safeParse(parseJson(raw))
      74            0 :   return at.success && now >= at.data && now - at.data < ATTEMPT_WINDOW_MS
      75              : }
      76              : 
      77              : /** False when the attempt could not be recorded; the caller must not redirect. */
      78            0 : export function markSsoReentry(now = Date.now()): boolean {
      79            0 :   try {
      80            0 :     sessionStorage.setItem(ATTEMPT_KEY, JSON.stringify(now))
      81            0 :     return true
      82              :   } catch {
      83            0 :     return false
      84              :   }
      85              : }
        

Generated by: LCOV version 2.0-1