diff --git a/apps/billing-web/src/auth/useSsoRequiredRefusal.ts b/apps/billing-web/src/auth/useSsoRequiredRefusal.ts index 05c27d83584..abd2481de5e 100644 --- a/apps/billing-web/src/auth/useSsoRequiredRefusal.ts +++ b/apps/billing-web/src/auth/useSsoRequiredRefusal.ts @@ -12,6 +12,10 @@ import type { Ref } from 'vue' import { CLOUD_BASE_URL } from '@/config/env' import { resolveBillingWebIdentity } from '@/config/firebase' import { readBillingWebSsoEnabled } from '@/config/ssoEnabled' +import { + reportSsoContinueClicked, + reportSsoRequiredShown +} from '@/telemetry/ssoTelemetry' interface SsoRequiredRefusal { readonly code: Readonly> @@ -64,12 +68,18 @@ export function useSsoRequiredRefusal(refusal: SsoRequiredRefusal) { : undefined }) + const offered = computed(() => destination.value !== undefined) + watch(offered, (shown) => { + if (shown) reportSsoRequiredShown() + }) + const leaving = ref(false) const signOutFailed = ref(false) async function continueWithSso(): Promise { const target = destination.value if (target === undefined || leaving.value) return + reportSsoContinueClicked() leaving.value = true signOutFailed.value = false if (!(await signOutOfFirebase())) { @@ -81,7 +91,7 @@ export function useSsoRequiredRefusal(refusal: SsoRequiredRefusal) { } return { - offered: computed(() => destination.value !== undefined), + offered, email, leaving, signOutFailed, diff --git a/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts b/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts index e950d76c595..f1d5a57f9b9 100644 --- a/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts +++ b/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts @@ -428,6 +428,35 @@ describe('trackWebSessionEvent', () => { }) }) +describe('trackSsoEvent', () => { + it('sends an SSO event to RUM and PostHog as is', async () => { + vi.mocked(datadogRum.getInitConfiguration).mockReturnValue({ + clientToken: 'pub', + applicationId: 'app' + }) + const telemetry = createBillingWebTelemetry() + await telemetry.startPostHog({ + config: Promise.resolve(CONFIGURED), + identity: ref({ kind: 'unknown' }) + }) + const event = { + name: 'app:sso_continue_clicked', + properties: { surface: 'billing_web', flow_id: 'flow-1' } + } as const + + telemetry.trackSsoEvent(event) + + expect(datadogRum.addAction).toHaveBeenCalledExactlyOnceWith( + event.name, + event.properties + ) + expect(posthog.capture).toHaveBeenCalledExactlyOnceWith( + event.name, + event.properties + ) + }) +}) + describe('startPostHog', () => { it('turns off every PostHog capture that would carry page text or a full URL', async () => { const telemetry = createBillingWebTelemetry() diff --git a/apps/billing-web/src/telemetry/billingWebTelemetry.ts b/apps/billing-web/src/telemetry/billingWebTelemetry.ts index 0efdbf5a5be..0cddbdad35c 100644 --- a/apps/billing-web/src/telemetry/billingWebTelemetry.ts +++ b/apps/billing-web/src/telemetry/billingWebTelemetry.ts @@ -15,7 +15,10 @@ import { getCheckoutJourneyTelemetryEventPayload } from '@comfyorg/account-core/billing' import type { CloudTelemetryConfig } from '@comfyorg/account-core/firebase' -import type { WebSessionTelemetryEvent } from '@comfyorg/account-core/telemetry' +import type { + SsoTelemetryEvent, + WebSessionTelemetryEvent +} from '@comfyorg/account-core/telemetry' import { createPostHogBeforeSend, createPostHogUrlQueryScrub @@ -53,6 +56,7 @@ interface BillingEvent { | BillingTelemetryEventName | CheckoutJourneyTelemetryEventName | WebSessionTelemetryEvent['name'] + | SsoTelemetryEvent['name'] readonly properties: Readonly> /** Sent while the page goes away, after PostHog has drained its queue on `pagehide`. */ readonly onPageExit?: true @@ -262,6 +266,12 @@ export function createBillingWebTelemetry() { ) } + function trackSsoEvent(event: SsoTelemetryEvent): void { + attempt(() => + send({ name: event.name, properties: { ...event.properties } }) + ) + } + /** The RUM user is the opaque id of the signed-in user, nothing else. */ function startRumUser(identity: WatchSource): void { syncIdentity(RUM_USER, identity) @@ -272,6 +282,7 @@ export function createBillingWebTelemetry() { startRumUser, trackBillingEvent, trackCheckoutJourneyEvent, + trackSsoEvent, trackWebSessionEvent } } diff --git a/apps/billing-web/src/telemetry/ssoTelemetry.ts b/apps/billing-web/src/telemetry/ssoTelemetry.ts new file mode 100644 index 00000000000..13688e2ea59 --- /dev/null +++ b/apps/billing-web/src/telemetry/ssoTelemetry.ts @@ -0,0 +1,31 @@ +import type { SsoFlow } from '@comfyorg/account-core/telemetry' +import { + SSO_TELEMETRY_EVENT, + ssoFlowStore +} from '@comfyorg/account-core/telemetry' + +import { billingWebTelemetry } from '@/telemetry/billingWebTelemetry' + +const flowProperties = ({ flowId, surface }: SsoFlow) => ({ + surface, + flow_id: flowId +}) + +/** Billing web's only SSO refusal is the session's `SSO_REQUIRED`. */ +export function reportSsoRequiredShown(): void { + billingWebTelemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.requiredShown, + properties: { + ...flowProperties(ssoFlowStore.start('billing_web')), + trigger: 'session_refused', + presentation: 'notice' + } + }) +} + +export function reportSsoContinueClicked(): void { + billingWebTelemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.continueClicked, + properties: flowProperties(ssoFlowStore.markContinued('billing_web')) + }) +} diff --git a/apps/billing-web/src/views/SignInView.test.ts b/apps/billing-web/src/views/SignInView.test.ts index 2c11966c0bc..65149709073 100644 --- a/apps/billing-web/src/views/SignInView.test.ts +++ b/apps/billing-web/src/views/SignInView.test.ts @@ -8,6 +8,7 @@ import type { SignInState } from '@/auth/signInState' import { recordBillingEntry } from '@/entry/billingEntry' import { createBillingI18n } from '@/i18n' import { createBillingRouter } from '@/router' +import { billingWebTelemetry } from '@/telemetry/billingWebTelemetry' import { trackedBillingEvents } from '@/test/trackedBillingEvents' import SignInView from '@/views/SignInView.vue' @@ -433,6 +434,37 @@ describe('SignInView', () => { } ) + it('reports the notice once and the click as the same attempt', async () => { + const trackSso = vi + .spyOn(billingWebTelemetry, 'trackSsoEvent') + .mockImplementation(() => undefined) + vi.spyOn(window.location, 'assign').mockImplementation(() => {}) + await renderSignIn(REFUSED_ENTRY) + + await userEvent.click( + await screen.findByRole('button', { name: 'Continue with SSO' }) + ) + + const [[shown], [continued], ...rest] = trackSso.mock.calls + expect(shown).toEqual({ + name: 'app:sso_required_shown', + properties: { + surface: 'billing_web', + trigger: 'session_refused', + presentation: 'notice', + flow_id: expect.any(String) + } + }) + expect(continued).toEqual({ + name: 'app:sso_continue_clicked', + properties: { + surface: 'billing_web', + flow_id: shown.properties.flow_id + } + }) + expect(rest).toEqual([]) + }) + it('offers nothing it cannot start when neither an organization nor an email is known', async () => { h.accountEmail = null await renderSignIn(REFUSED_ENTRY) @@ -470,6 +502,7 @@ describe('SignInView', () => { }) it('keeps the workspace refusal for SSO_REQUIRED while sso_enabled is off', async () => { + const trackSso = vi.spyOn(billingWebTelemetry, 'trackSsoEvent') h.sessionFailureCode = 'SSO_REQUIRED' h.ssoOrganizationId = 'org_acme' h.initialState = { @@ -486,5 +519,6 @@ describe('SignInView', () => { expect( screen.queryByRole('button', { name: 'Continue with SSO' }) ).toBeNull() + expect(trackSso).not.toHaveBeenCalled() }) }) diff --git a/browser_tests/tests/cloudSsoSignIn.spec.ts b/browser_tests/tests/cloudSsoSignIn.spec.ts index c431672d9a8..f8c335406fa 100644 --- a/browser_tests/tests/cloudSsoSignIn.spec.ts +++ b/browser_tests/tests/cloudSsoSignIn.spec.ts @@ -289,15 +289,14 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => { expect(ssoStarts).toEqual([]) }) - test('an sso_error ingest sends back shows its message on the login page', async ({ + test('an sso_error ingest sends back shows its message on the login page, then leaves the URL', async ({ page }) => { await page.goto(`${APP_URL}/?sso_error=SSO_ORG_DISABLED`) - await expect(page).toHaveURL( - /\/cloud\/login\?.*sso_error=SSO_ORG_DISABLED/ - ) await expect(page.getByText(SSO_COPY.errors.orgDisabled)).toBeVisible() + await expect(page).toHaveURL(/\/cloud\/login/) + await expect(page).not.toHaveURL(/sso_error=/) }) test('a customer record refused with sso_required shows the SSO notice on the page, not a failure toast', async ({ diff --git a/packages/account-core/src/telemetry.test.ts b/packages/account-core/src/telemetry.test.ts index a43332df88a..c976657396c 100644 --- a/packages/account-core/src/telemetry.test.ts +++ b/packages/account-core/src/telemetry.test.ts @@ -1,8 +1,16 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { createWebSessionIdentity } from './core/webSessionIdentity.js' -import type { WebSessionTelemetryEvent } from './telemetry.js' -import { webSessionTelemetryHooks } from './telemetry.js' +import type { SsoErrorCode } from './sso.js' +import type { + SsoSignInFailureReason, + WebSessionTelemetryEvent +} from './telemetry.js' +import { + createSsoFlowStore, + ssoFailureReason, + webSessionTelemetryHooks +} from './telemetry.js' import type { FakeWebSessionState } from './testing.js' import { createFakeWebSessionEndpoint, fakeWebSessionUser } from './testing.js' @@ -60,3 +68,121 @@ describe('webSessionTelemetryHooks', () => { ]) }) }) + +function memoryStorage(): Storage { + const items = new Map() + return { + get length() { + return items.size + }, + clear: () => items.clear(), + getItem: (key) => items.get(key) ?? null, + key: (index) => [...items.keys()][index] ?? null, + removeItem: (key) => { + items.delete(key) + }, + setItem: (key, value) => { + items.set(key, value) + } + } +} + +function brokenStorage(): Storage { + const fail = () => { + throw new DOMException('denied', 'SecurityError') + } + return { + length: 0, + clear: fail, + getItem: fail, + key: fail, + removeItem: fail, + setItem: fail + } +} + +describe('createSsoFlowStore', () => { + it('carries an attempt across the redirect until it finishes', () => { + const storage = memoryStorage() + const started = createSsoFlowStore(() => storage).start('cloud_login') + + const afterRedirect = createSsoFlowStore(() => storage) + + expect(afterRedirect.current()).toEqual(started) + expect(afterRedirect.finish()).toEqual(started) + expect(createSsoFlowStore(() => storage).current()).toBeUndefined() + }) + + it('gives every attempt its own flow id', () => { + const flows = createSsoFlowStore(memoryStorage) + + expect(flows.start('cloud_app').flowId).not.toBe( + flows.start('cloud_app').flowId + ) + }) + + it('keeps the attempt for this page when storage throws', () => { + const flows = createSsoFlowStore(brokenStorage) + + const started = flows.start('billing_web') + + expect(flows.current()).toEqual(started) + expect(flows.finish()).toEqual(started) + expect(flows.current()).toBeUndefined() + }) + + it('carries a continued attempt across the redirect, keeping its flow id', () => { + const storage = memoryStorage() + const shown = createSsoFlowStore(() => storage).start('cloud_app') + + createSsoFlowStore(() => storage).markContinued('cloud_login') + + expect(createSsoFlowStore(() => storage).current()).toEqual({ + ...shown, + continued: true + }) + }) + + it.for<{ name: string; continued: boolean; kept: boolean }>([ + { name: 'drops an attempt never continued', continued: false, kept: false }, + { name: 'keeps a continued attempt', continued: true, kept: true } + ])('abandoning $name', ({ continued, kept }) => { + const flows = createSsoFlowStore(memoryStorage) + flows.start('cloud_app', continued) + + flows.abandon() + + expect(flows.current() !== undefined).toBe(kept) + }) + + it('still starts an attempt where crypto is unavailable', () => { + vi.stubGlobal('crypto', undefined) + try { + expect( + createSsoFlowStore(memoryStorage).start('cloud_app').flowId + ).toMatch(/^[0-9a-f]{32}$/) + } finally { + vi.unstubAllGlobals() + } + }) + + it('ignores a stored value it cannot read as a flow', () => { + const storage = memoryStorage() + storage.setItem('Comfy.Sso.TelemetryFlow', '{"flowId":"x","surface":"?"}') + + expect(createSsoFlowStore(() => storage).current()).toBeUndefined() + }) +}) + +describe('ssoFailureReason', () => { + it.for<{ code: SsoErrorCode; reason: SsoSignInFailureReason }>([ + { code: 'SSO_INVALID_STATE', reason: 'state_mismatch' }, + { code: 'SSO_CONFIRM_EXPIRED', reason: 'state_mismatch' }, + { code: 'SSO_ORG_NOT_ATTACHED', reason: 'org_not_attached' }, + { code: 'SSO_ORG_MISMATCH', reason: 'org_not_attached' }, + { code: 'SSO_IDP_ERROR', reason: 'server_error' }, + { code: 'INTERNAL_ERROR', reason: 'server_error' } + ])('reports $code as $reason', ({ code, reason }) => { + expect(ssoFailureReason(code)).toBe(reason) + }) +}) diff --git a/packages/account-core/src/telemetry.ts b/packages/account-core/src/telemetry.ts index 71d4e934dd8..5765792454f 100644 --- a/packages/account-core/src/telemetry.ts +++ b/packages/account-core/src/telemetry.ts @@ -4,10 +4,13 @@ * every host. The package never calls a telemetry API itself; call sites * stay host-specific. */ +import { z } from 'zod' + import type { WebSessionBootstrapEvent, WebSessionIdentityOptions } from './core/webSessionIdentity.js' +import type { SsoErrorCode } from './sso.js' export const SESSION_TELEMETRY_EVENT = { refreshSucceeded: 'auth.unified.refresh.succeeded', @@ -98,3 +101,195 @@ export interface AuthErrorMetadata { error_code: string auth_action: AuthFlowAction } + +export const SSO_TELEMETRY_EVENT = { + requiredShown: 'app:sso_required_shown', + continueClicked: 'app:sso_continue_clicked', + signInFailed: 'app:sso_sign_in_failed', + signInCompleted: 'app:sso_sign_in_completed', + workspaceLanded: 'app:sso_workspace_landed' +} as const + +const SSO_SURFACES = [ + 'cloud_login', + 'cloud_signup', + 'cloud_customer_create', + 'cloud_app', + 'billing_web', + 'platform', + 'desktop', + 'local_web' +] as const + +/** Where an SSO attempt started; `cloud_app` is the in-app dialog. */ +export type SsoSurface = (typeof SSO_SURFACES)[number] + +/** The refusal that showed the SSO-required screen. */ +export type SsoRequiredTrigger = + | 'firebase_sign_in' + | 'api_key' + | 'customer_create' + | 'session_refused' + | 'session_expired' + +/** + * `notice` waits for the person to continue; `redirect` sends them straight + * to SSO, so no `sso_continue_clicked` follows. + */ +export type SsoRequiredPresentation = 'notice' | 'redirect' + +export type SsoSignInFailureReason = + | 'cancelled' + | 'state_mismatch' + | 'org_not_attached' + | 'server_error' + | 'network' + +interface SsoFlowProperties { + readonly surface: SsoSurface + readonly flow_id: string +} + +/** IDs and enums only: never an email, a name or an organization name. */ +export type SsoTelemetryEvent = + | { + readonly name: typeof SSO_TELEMETRY_EVENT.requiredShown + readonly properties: SsoFlowProperties & { + readonly trigger: SsoRequiredTrigger + readonly presentation: SsoRequiredPresentation + } + } + | { + readonly name: typeof SSO_TELEMETRY_EVENT.continueClicked + readonly properties: SsoFlowProperties + } + | { + readonly name: typeof SSO_TELEMETRY_EVENT.signInFailed + readonly properties: SsoFlowProperties & { + readonly reason: SsoSignInFailureReason + readonly http_status?: number + } + } + | { + readonly name: typeof SSO_TELEMETRY_EVENT.signInCompleted + readonly properties: SsoFlowProperties + } + | { + readonly name: typeof SSO_TELEMETRY_EVENT.workspaceLanded + readonly properties: SsoFlowProperties & { + readonly landed_in_default_workspace: boolean + } + } + +const SSO_FAILURE_REASON: Partial< + Readonly> +> = { + SSO_INVALID_STATE: 'state_mismatch', + SSO_CONFIRM_EXPIRED: 'state_mismatch', + SSO_ORG_NOT_ATTACHED: 'org_not_attached', + SSO_ORG_MISMATCH: 'org_not_attached' +} + +/** Reduces an `?sso_error=` code to the reason telemetry reports. */ +export function ssoFailureReason(code: SsoErrorCode): SsoSignInFailureReason { + return SSO_FAILURE_REASON[code] ?? 'server_error' +} + +/** One SSO attempt; `flowId` joins its steps across the IdP redirect. */ +export interface SsoFlow { + readonly flowId: string + readonly surface: SsoSurface + /** Set once the person chose to continue to SSO. */ + readonly continued?: boolean +} + +const SSO_FLOW_STORAGE_KEY = 'Comfy.Sso.TelemetryFlow' + +const zSsoFlow = z.object({ + flowId: z.string().min(1), + surface: z.enum(SSO_SURFACES), + continued: z.boolean().optional() +}) + +function randomBytes(): Uint8Array { + const bytes = new Uint8Array(16) + try { + return crypto.getRandomValues(bytes) + } catch { + return bytes.map(() => Math.floor(Math.random() * 256)) + } +} + +/** `getRandomValues`, unlike `randomUUID`, also works outside a secure context. */ +function newFlowId(): string { + return Array.from(randomBytes(), (byte) => + byte.toString(16).padStart(2, '0') + ).join('') +} + +export function createSsoFlow(surface: SsoSurface): SsoFlow { + return { flowId: newFlowId(), surface } +} + +/** + * Keeps the current attempt in session storage across the redirect, and in + * memory for this page when storage fails, so telemetry never fails a sign-in. + */ +export function createSsoFlowStore(storage: () => Storage) { + let inMemory: SsoFlow | undefined + + function readStored(): SsoFlow | undefined { + try { + const raw = storage().getItem(SSO_FLOW_STORAGE_KEY) + if (raw === null) return undefined + const parsed = zSsoFlow.safeParse(JSON.parse(raw)) + return parsed.success ? parsed.data : undefined + } catch { + return undefined + } + } + + function save(flow: SsoFlow): SsoFlow { + inMemory = flow + try { + storage().setItem(SSO_FLOW_STORAGE_KEY, JSON.stringify(flow)) + } catch { + return flow + } + return flow + } + + function start(surface: SsoSurface, continued = false): SsoFlow { + return save({ ...createSsoFlow(surface), ...(continued && { continued }) }) + } + + function current(): SsoFlow | undefined { + return inMemory ?? readStored() + } + + /** Marks the current attempt, or a new one from `surface`, as continued. */ + function markContinued(surface: SsoSurface): SsoFlow { + return save({ ...(current() ?? createSsoFlow(surface)), continued: true }) + } + + function finish(): SsoFlow | undefined { + const flow = current() + inMemory = undefined + try { + storage().removeItem(SSO_FLOW_STORAGE_KEY) + } catch { + return flow + } + return flow + } + + /** Drops an attempt the person never continued. */ + function abandon(): void { + if (current()?.continued) return + finish() + } + + return { start, current, markContinued, finish, abandon } +} + +export const ssoFlowStore = createSsoFlowStore(() => globalThis.sessionStorage) diff --git a/src/composables/auth/useAuthActions.test.ts b/src/composables/auth/useAuthActions.test.ts index 69c90731006..3a4fd1efa40 100644 --- a/src/composables/auth/useAuthActions.test.ts +++ b/src/composables/auth/useAuthActions.test.ts @@ -445,6 +445,7 @@ describe('useAuthActions auth flow error telemetry', () => { await expect(signInWithGoogle()).resolves.toBeUndefined() expect(presentSsoRequired).toHaveBeenCalledWith( + 'customer_create', expect.objectContaining({ organizationId: 'org_1' }) ) expect(mockAuthStore.logout).toHaveBeenCalledOnce() diff --git a/src/composables/auth/useAuthActions.ts b/src/composables/auth/useAuthActions.ts index 8f52c32f40b..6dab86be6b9 100644 --- a/src/composables/auth/useAuthActions.ts +++ b/src/composables/auth/useAuthActions.ts @@ -76,7 +76,7 @@ export const useAuthActions = () => { /** Sends an account its SSO organization refused to SSO instead of a failure toast. */ const presentSsoRefusal = (error: SsoRequiredAuthError): boolean => { - const presented = presentSsoRequired({ + const presented = presentSsoRequired('customer_create', { email: authStore.userEmail ?? undefined, organizationId: error.organizationId }) diff --git a/src/platform/auth/session/cloudWebSessionStore.ts b/src/platform/auth/session/cloudWebSessionStore.ts index 84e62384998..c5f57794596 100644 --- a/src/platform/auth/session/cloudWebSessionStore.ts +++ b/src/platform/auth/session/cloudWebSessionStore.ts @@ -42,6 +42,10 @@ import { t } from '@/i18n' import { isCloud } from '@/platform/distribution/types' import { firebaseIdentity } from '@/platform/auth/firebaseIdentity' import { presentSsoRequired } from '@/platform/auth/sso/ssoRequired' +import { + endSsoFlow, + trackSsoSignInCompleted +} from '@/platform/auth/sso/ssoTelemetry' import { webSessionUser } from '@/platform/auth/session/webSessionUser' import { clearInteractiveSignIn, @@ -50,6 +54,7 @@ import { } from '@/platform/auth/session/interactiveSignInMarker' import { forgetSsoHint, + isSsoSession, rememberSignedInSession } from '@/platform/auth/session/ssoReentryStorage' import type { WebSessionRequestScope } from '@/platform/auth/session/webSessionFetch' @@ -342,7 +347,7 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { }) } if (failure.code === 'SSO_REQUIRED') { - presentSsoRequired({ email: session.user.email }) + presentSsoRequired('session_refused', { email: session.user.email }) } throw new WebSessionTokenError( error, @@ -363,6 +368,9 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { signedOutHere = false if (useFeatureFlags().flags.ssoEnabled) { rememberSignedInSession(next.session.user) + if (isSsoSession(next.session.user)) { + trackSsoSignInCompleted() + } } } else if (next.phase === 'signed_out' && next.outcome === 'revoked') { forgetSsoHint() @@ -379,6 +387,7 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { /** Only after an interactive sign-in; a token refresh never calls this. */ function signedInInteractively(user: User): void { const getProof = () => user.getIdToken() + endSsoFlow() if (isCloud) markInteractiveSignIn(user.uid) if (identity) creating = createSession(identity, getProof) else if (!decided) pendingSignIn = { uid: user.uid, getProof } @@ -482,7 +491,7 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { workspaceDenied: (workspaceId) => useWorkspaceAuthStore().dropDeniedWorkspace(workspaceId), ssoRequired: ({ session }) => - presentSsoRequired({ email: session.user.email }) + presentSsoRequired('session_refused', { email: session.user.email }) }) } diff --git a/src/platform/auth/session/ssoReentryStorage.ts b/src/platform/auth/session/ssoReentryStorage.ts index 7afd0b857a1..08735023f11 100644 --- a/src/platform/auth/session/ssoReentryStorage.ts +++ b/src/platform/auth/session/ssoReentryStorage.ts @@ -33,9 +33,13 @@ export function forgetSsoHint(): void { } } +export function isSsoSession(user: WebSession['user']): boolean { + return SSO_SIGN_IN_PROVIDERS.has(user.signInProvider ?? '') +} + /** Keyed on the provider the server reports, so only an SSO session leaves a hint. */ export function rememberSignedInSession(user: WebSession['user']): void { - if (!SSO_SIGN_IN_PROVIDERS.has(user.signInProvider ?? '')) { + if (!isSsoSession(user)) { forgetSsoHint() return } diff --git a/src/platform/auth/sso/SsoRequiredDialogContent.test.ts b/src/platform/auth/sso/SsoRequiredDialogContent.test.ts index dc12dc13aca..b6460cd02de 100644 --- a/src/platform/auth/sso/SsoRequiredDialogContent.test.ts +++ b/src/platform/auth/sso/SsoRequiredDialogContent.test.ts @@ -3,14 +3,19 @@ import userEvent from '@testing-library/user-event' import { fromPartial } from '@total-typescript/shoehorn' import type { User } from 'firebase/auth' import type { Mock } from 'vitest' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createI18n } from 'vue-i18n' import { createMemoryHistory, createRouter } from 'vue-router' +import { ssoFlowStore } from '@comfyorg/account-core/telemetry' + import SsoRequiredDialogContent from '@/platform/auth/sso/SsoRequiredDialogContent.vue' +import { trackSsoRequiredShown } from '@/platform/auth/sso/ssoTelemetry' +import { useTelemetry } from '@/platform/telemetry' import { useAuthStore } from '@/stores/authStore' vi.mock(import('firebase/auth')) +vi.mock(import('@/platform/telemetry')) async function renderDialog( props: { @@ -53,6 +58,10 @@ describe('SsoRequiredDialogContent', () => { vi.spyOn(window.location, 'assign').mockImplementation(assign) }) + afterEach(() => { + ssoFlowStore.finish() + }) + it('explains the refusal and starts SSO for the known email', async () => { await renderDialog({ email: 'ada@acme.com', returnTo: '/cloud/user-check' }) @@ -70,6 +79,22 @@ describe('SsoRequiredDialogContent', () => { expect(useAuthStore().logout).not.toHaveBeenCalled() }) + it('reports the click as the shown attempt continuing to SSO', async () => { + trackSsoRequiredShown('cloud_app', 'session_refused', 'notice') + await renderDialog({ email: 'ada@acme.com' }) + + await userEvent.click( + screen.getByRole('button', { name: 'auth.sso.continueWithSso' }) + ) + + const [[shown], [continued]] = vi.mocked(useTelemetry()!.trackSsoEvent).mock + .calls + expect(continued).toEqual({ + name: 'app:sso_continue_clicked', + properties: { surface: 'cloud_app', flow_id: shown.properties.flow_id } + }) + }) + it.for<{ name: string; email?: string }>([ { name: 'with the email as a hint', email: 'alice@comfy.org' }, { name: 'when no email is known' } @@ -122,6 +147,22 @@ describe('SsoRequiredDialogContent', () => { ) }) + it('reports no continue while signing the account out fails', async () => { + trackSsoRequiredShown('cloud_app', 'session_refused', 'notice') + useAuthStore().currentUser = fromPartial({ email: 'ada@acme.com' }) + vi.mocked(useAuthStore().logout).mockRejectedValueOnce(new Error('network')) + await renderDialog({ email: 'ada@acme.com' }) + const continueButton = screen.getByRole('button', { + name: 'auth.sso.continueWithSso' + }) + + await userEvent.click(continueButton) + await waitFor(() => expect(continueButton).toBeEnabled()) + + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledOnce() + expect(ssoFlowStore.current()?.continued).toBeUndefined() + }) + it('lets the person retry when signing the account out fails', async () => { useAuthStore().currentUser = fromPartial({ email: 'ada@acme.com' }) vi.mocked(useAuthStore().logout) @@ -153,4 +194,19 @@ describe('SsoRequiredDialogContent', () => { expect(target.pathname).toBe('/cloud/login') expect(target.searchParams.get('sso')).toBe('open') }) + + it('leaves the continue to the login page it hands the attempt to', async () => { + trackSsoRequiredShown('cloud_app', 'session_refused', 'notice') + await renderDialog({}) + + await userEvent.click( + screen.getByRole('button', { name: 'auth.sso.continueWithSso' }) + ) + + await waitFor(() => expect(assign).toHaveBeenCalledOnce()) + const [[shown], ...rest] = vi.mocked(useTelemetry()!.trackSsoEvent).mock + .calls + expect(rest).toEqual([]) + expect(ssoFlowStore.current()?.flowId).toBe(shown.properties.flow_id) + }) }) diff --git a/src/platform/auth/sso/ssoRequired.test.ts b/src/platform/auth/sso/ssoRequired.test.ts index 269c4dc7a09..29d1af281ba 100644 --- a/src/platform/auth/sso/ssoRequired.test.ts +++ b/src/platform/auth/sso/ssoRequired.test.ts @@ -1,4 +1,6 @@ -import { beforeAll, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest' + +import { ssoFlowStore } from '@comfyorg/account-core/telemetry' import { useFeatureFlags } from '@/composables/useFeatureFlags' import { @@ -7,9 +9,11 @@ import { presentSsoRequired } from '@/platform/auth/sso/ssoRequired' import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey' +import { useTelemetry } from '@/platform/telemetry' import { useDialogStore } from '@/stores/dialogStore' vi.mock(import('@/composables/useFeatureFlags')) +vi.mock(import('@/platform/telemetry')) const SSO_REFUSAL = { code: 'sso_required', @@ -25,21 +29,31 @@ async function shownDialog() { describe('the SSO-required screen', () => { beforeAll(() => import('@/platform/auth/sso/SsoRequiredDialogContent.vue')) + afterEach(() => { + ssoFlowStore.finish() + }) it('stays hidden with the flag off, so the caller keeps its handling', async () => { - expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(false) + expect( + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + ).toBe(false) expect(await shownDialog()).toBeUndefined() }) it('opens once with the flag on, keeping what any caller knew', async () => { vi.mocked(useFeatureFlags().flags).ssoEnabled = true - expect(presentSsoRequired()).toBe(true) + expect(presentSsoRequired('session_refused')).toBe(true) await shownDialog() - expect(presentSsoRequired({ email: 'ada@acme.com', returnTo: '/x' })).toBe( + expect( + presentSsoRequired('session_refused', { + email: 'ada@acme.com', + returnTo: '/x' + }) + ).toBe(true) + expect(presentSsoRequired('session_refused', { email: undefined })).toBe( true ) - expect(presentSsoRequired({ email: undefined })).toBe(true) const dialog = await shownDialog() expect(dialog?.contentProps).toEqual({ @@ -47,6 +61,54 @@ describe('the SSO-required screen', () => { returnTo: '/x' }) expect(useDialogStore().dialogStack).toHaveLength(1) + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ + name: 'app:sso_required_shown', + properties: { + surface: 'cloud_app', + trigger: 'session_refused', + presentation: 'notice', + flow_id: expect.any(String) + } + }) + }) + + async function closeShownDialog(beforeClose: () => void) { + vi.mocked(useFeatureFlags().flags).ssoEnabled = true + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + await shownDialog() + beforeClose() + useDialogStore().closeDialog({ key: SSO_REQUIRED_DIALOG_KEY }) + } + + it('closing the dialog drops an attempt never continued', async () => { + await closeShownDialog(() => {}) + + expect(ssoFlowStore.current()).toBeUndefined() + }) + + it('closing the dialog keeps an attempt already continued', async () => { + await closeShownDialog(() => ssoFlowStore.markContinued('cloud_app')) + + expect(ssoFlowStore.current()?.continued).toBe(true) + }) + + it('reports a refused API request as the api_key trigger', async () => { + vi.mocked(useFeatureFlags().flags).ssoEnabled = true + + presentForRefusal(403, SSO_REFUSAL) + await shownDialog() + + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ + name: 'app:sso_required_shown', + properties: expect.objectContaining({ trigger: 'api_key' }) + }) + }) + + it('reports nothing with the flag off', async () => { + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + await shownDialog() + + expect(useTelemetry()?.trackSsoEvent).not.toHaveBeenCalled() }) it.for<{ name: string; status: number; body: unknown; shown: boolean }>([ diff --git a/src/platform/auth/sso/ssoRequired.ts b/src/platform/auth/sso/ssoRequired.ts index 5359b8f1cac..a3a625a6266 100644 --- a/src/platform/auth/sso/ssoRequired.ts +++ b/src/platform/auth/sso/ssoRequired.ts @@ -1,5 +1,6 @@ import { omitBy } from 'es-toolkit' +import type { SsoRequiredTrigger } from '@comfyorg/account-core/telemetry' import { isSsoRequiredRefusal, ssoRequiredOrganizationId @@ -10,6 +11,10 @@ import { useFeatureFlags } from '@/composables/useFeatureFlags' import { t } from '@/i18n' import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey' import { presentInline } from '@/platform/auth/sso/ssoRequiredInline' +import { + abandonSsoFlow, + trackSsoRequiredShown +} from '@/platform/auth/sso/ssoTelemetry' import { reportError } from '@/platform/telemetry/reportError' import { useToast } from '@/components/ui/toast/toastStore' import { useDialogStore } from '@/stores/dialogStore' @@ -29,7 +34,10 @@ export interface SsoRequiredContext { * never erases the email the sign-in page passed. A refusal always answers the * organization, so one that names none clears an earlier one. */ -export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { +export function presentSsoRequired( + trigger: SsoRequiredTrigger, + context: SsoRequiredContext = {} +): boolean { if (!useFeatureFlags().flags.ssoEnabled) return false const dialogStore = useDialogStore() const known = { @@ -38,7 +46,7 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { organizationId: context.organizationId }) } - if (presentInline(known)) return true + if (presentInline(known, trigger)) return true void import('@/platform/auth/sso/SsoRequiredDialogContent.vue') .then(({ default: component }) => { if (dialogStore.isDialogOpen(SSO_REQUIRED_DIALOG_KEY)) { @@ -48,6 +56,7 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { }) return } + trackSsoRequiredShown('cloud_app', trigger, 'notice') dialogStore.showDialog({ key: SSO_REQUIRED_DIALOG_KEY, component, @@ -55,7 +64,8 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { dialogComponentProps: { renderer: 'reka', headless: true, - contentClass: SELF_STYLED_PANEL_CONTENT_CLASS + contentClass: SELF_STYLED_PANEL_CONTENT_CLASS, + onClose: abandonSsoFlow } }) }) @@ -71,10 +81,13 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { return true } +/** A Cloud API request whose credential the organization refused. */ export function presentForRefusal(status: number, body: unknown): boolean { return ( isSsoRequiredRefusal(status, body) && - presentSsoRequired({ organizationId: ssoRequiredOrganizationId(body) }) + presentSsoRequired('api_key', { + organizationId: ssoRequiredOrganizationId(body) + }) ) } diff --git a/src/platform/auth/sso/ssoRequiredChunkFailure.test.ts b/src/platform/auth/sso/ssoRequiredChunkFailure.test.ts index 4d659e0e894..f31fd420b0d 100644 --- a/src/platform/auth/sso/ssoRequiredChunkFailure.test.ts +++ b/src/platform/auth/sso/ssoRequiredChunkFailure.test.ts @@ -15,7 +15,7 @@ describe('the SSO-required screen when its chunk fails to load', () => { it('reports the failure and still tells the person to use SSO', async () => { vi.mocked(useFeatureFlags().flags).ssoEnabled = true - expect(presentSsoRequired()).toBe(true) + expect(presentSsoRequired('session_refused')).toBe(true) await vi.waitFor(() => expect(useToast().toasts).toEqual([ expect.objectContaining({ diff --git a/src/platform/auth/sso/ssoRequiredInline.test.ts b/src/platform/auth/sso/ssoRequiredInline.test.ts index 70fc7df0de4..2414741feac 100644 --- a/src/platform/auth/sso/ssoRequiredInline.test.ts +++ b/src/platform/auth/sso/ssoRequiredInline.test.ts @@ -2,17 +2,21 @@ import { render, screen } from '@testing-library/vue' import { afterEach, describe, expect, it, vi } from 'vitest' import { defineComponent, h } from 'vue' +import type { SsoRequiredTrigger } from '@comfyorg/account-core/telemetry' + import { useFeatureFlags } from '@/composables/useFeatureFlags' import { presentSsoRequired } from '@/platform/auth/sso/ssoRequired' import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey' import { useSsoRequiredInlineHost } from '@/platform/auth/sso/ssoRequiredInline' +import { useTelemetry } from '@/platform/telemetry' import { useDialogStore } from '@/stores/dialogStore' vi.mock(import('@/composables/useFeatureFlags')) +vi.mock(import('@/platform/telemetry')) const AuthPage = defineComponent({ setup() { - const { notice } = useSsoRequiredInlineHost() + const { notice } = useSsoRequiredInlineHost('cloud_login') return () => h('pre', JSON.stringify(notice.value ?? null)) } }) @@ -33,8 +37,12 @@ describe('the SSO-required notice on an auth page', () => { vi.mocked(useFeatureFlags().flags).ssoEnabled = true render(AuthPage) - expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(true) - expect(presentSsoRequired({ organizationId: 'org_meta' })).toBe(true) + expect( + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + ).toBe(true) + expect( + presentSsoRequired('session_refused', { organizationId: 'org_meta' }) + ).toBe(true) expect( await screen.findByText( @@ -44,12 +52,41 @@ describe('the SSO-required notice on an auth page', () => { expect(await dialogShown()).toBe(false) }) + it.for<{ + trigger: SsoRequiredTrigger + surface: string + }>([ + { trigger: 'firebase_sign_in', surface: 'cloud_login' }, + { trigger: 'customer_create', surface: 'cloud_customer_create' } + ])( + 'reports a $trigger refusal once, from $surface', + ({ trigger, surface }) => { + vi.mocked(useFeatureFlags().flags).ssoEnabled = true + render(AuthPage) + + presentSsoRequired(trigger, { email: 'ada@acme.com' }) + presentSsoRequired('session_refused', { organizationId: 'org_meta' }) + + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ + name: 'app:sso_required_shown', + properties: { + surface, + trigger, + presentation: 'notice', + flow_id: expect.any(String) + } + }) + } + ) + it('falls back to the dialog once the page is gone', async () => { vi.mocked(useFeatureFlags().flags).ssoEnabled = true const page = render(AuthPage) page.unmount() - expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(true) + expect( + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + ).toBe(true) expect(await dialogShown()).toBe(true) }) @@ -57,7 +94,9 @@ describe('the SSO-required notice on an auth page', () => { vi.mocked(useFeatureFlags().flags).ssoEnabled = false render(AuthPage) - expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(false) + expect( + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + ).toBe(false) expect(screen.getByText('null')).toBeInTheDocument() }) }) diff --git a/src/platform/auth/sso/ssoRequiredInline.ts b/src/platform/auth/sso/ssoRequiredInline.ts index 8cef361d763..edcb5917b6f 100644 --- a/src/platform/auth/sso/ssoRequiredInline.ts +++ b/src/platform/auth/sso/ssoRequiredInline.ts @@ -1,25 +1,40 @@ import { onUnmounted, readonly, shallowRef } from 'vue' +import type { + SsoRequiredTrigger, + SsoSurface +} from '@comfyorg/account-core/telemetry' + import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired' +import { + abandonSsoFlow, + trackSsoRequiredShown +} from '@/platform/auth/sso/ssoTelemetry' -let hosts = 0 +const hostSurfaces: SsoSurface[] = [] const notice = shallowRef() /** * Shows the refusal on the auth page that hosts it, in that page's brand * design system, instead of the app-styled shared dialog. False without one. */ -export function presentInline(context: SsoRequiredContext): boolean { - if (hosts === 0) return false +export function presentInline( + context: SsoRequiredContext, + trigger: SsoRequiredTrigger +): boolean { + const surface = hostSurfaces.at(-1) + if (!surface) return false + if (!notice.value) trackSsoRequiredShown(surface, trigger, 'notice') notice.value = { ...notice.value, ...context } return true } /** Makes the calling auth page the place an SSO refusal is shown. */ -export function useSsoRequiredInlineHost() { - hosts++ +export function useSsoRequiredInlineHost(surface: SsoSurface) { + hostSurfaces.push(surface) onUnmounted(() => { - hosts-- + hostSurfaces.splice(hostSurfaces.lastIndexOf(surface), 1) + if (notice.value) abandonSsoFlow() notice.value = undefined }) return { notice: readonly(notice) } diff --git a/src/platform/auth/sso/ssoTelemetry.ts b/src/platform/auth/sso/ssoTelemetry.ts new file mode 100644 index 00000000000..e03e03863d2 --- /dev/null +++ b/src/platform/auth/sso/ssoTelemetry.ts @@ -0,0 +1,101 @@ +import type { + SsoFlow, + SsoRequiredPresentation, + SsoRequiredTrigger, + SsoSignInFailureReason, + SsoSurface +} from '@comfyorg/account-core/telemetry' +import { + createSsoFlow, + SSO_TELEMETRY_EVENT, + ssoFlowStore +} from '@comfyorg/account-core/telemetry' + +import { useTelemetry } from '@/platform/telemetry' + +/** The attempt that just signed in, until its first workspace opens. */ +let signedInFlow: SsoFlow | undefined + +function flowProperties({ flowId, surface }: SsoFlow) { + return { surface, flow_id: flowId } +} + +/** + * Starts a new attempt each time SSO is required. A `redirect` leaves for SSO + * without a screen, so its attempt is already continued. + */ +export function trackSsoRequiredShown( + surface: SsoSurface, + trigger: SsoRequiredTrigger, + presentation: SsoRequiredPresentation +): void { + const telemetry = useTelemetry() + if (!telemetry) return + const flow = ssoFlowStore.start( + trigger === 'customer_create' ? 'cloud_customer_create' : surface, + presentation === 'redirect' + ) + telemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.requiredShown, + properties: { ...flowProperties(flow), trigger, presentation } + }) +} + +export function trackSsoContinueClicked(surface: SsoSurface): void { + const telemetry = useTelemetry() + if (!telemetry) return + const flow = ssoFlowStore.markContinued(surface) + telemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.continueClicked, + properties: flowProperties(flow) + }) +} + +export function trackSsoSignInFailed( + reason: SsoSignInFailureReason, + surface: SsoSurface +): void { + const telemetry = useTelemetry() + if (!telemetry) return + const flow = ssoFlowStore.finish() ?? createSsoFlow(surface) + telemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.signInFailed, + properties: { ...flowProperties(flow), reason } + }) +} + +/** An SSO session after an attempt this tab continued is that attempt's sign-in. */ +export function trackSsoSignInCompleted(): void { + const flow = ssoFlowStore.finish() + if (!flow?.continued) return + signedInFlow = flow + useTelemetry()?.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.signInCompleted, + properties: flowProperties(flow) + }) +} + +/** The SSO-required screen closed without the person continuing. */ +export function abandonSsoFlow(): void { + ssoFlowStore.abandon() +} + +/** Another sign-in method completed, so no SSO attempt is in flight. */ +export function endSsoFlow(): void { + ssoFlowStore.finish() +} + +export function trackSsoWorkspaceLanded( + landedInDefaultWorkspace: boolean +): void { + const flow = signedInFlow + signedInFlow = undefined + if (!flow) return + useTelemetry()?.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.workspaceLanded, + properties: { + ...flowProperties(flow), + landed_in_default_workspace: landedInDefaultWorkspace + } + }) +} diff --git a/src/platform/auth/sso/useContinueWithSso.ts b/src/platform/auth/sso/useContinueWithSso.ts index 3bbb8ddeb49..84494f93357 100644 --- a/src/platform/auth/sso/useContinueWithSso.ts +++ b/src/platform/auth/sso/useContinueWithSso.ts @@ -6,6 +6,7 @@ import { ssoStartUrl } from '@comfyorg/account-core/sso' import { useErrorHandling } from '@/composables/useErrorHandling' import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired' import { toSsoReturnPath } from '@/platform/auth/sso/ssoReturnPath' +import { trackSsoContinueClicked } from '@/platform/auth/sso/ssoTelemetry' import { SSO_ENTRY_OPEN_QUERY } from '@/platform/cloud/onboarding/sso/ssoEntryQuery' import { useAuthStore } from '@/stores/authStore' @@ -17,7 +18,8 @@ export function useContinueWithSso(context: () => SsoRequiredContext) { const knownEmail = computed(() => context().email ?? authStore.userEmail) const leaving = ref(false) - function destination(): string { + /** The SSO start URL, or undefined when the sign-in page must ask for an email. */ + function ssoDestination(): string | undefined { const { returnTo, organizationId } = context() const back = { returnTo: toSsoReturnPath(returnTo ?? router.currentRoute.value.fullPath), @@ -30,18 +32,16 @@ export function useContinueWithSso(context: () => SsoRequiredContext) { ...back }) } - if (!knownEmail.value) { - return router.resolve({ - name: 'cloud-login', - query: SSO_ENTRY_OPEN_QUERY - }).href - } + if (!knownEmail.value) return undefined return ssoStartUrl({ email: knownEmail.value, ...back }) } async function continueWithSso() { leaving.value = true - const target = destination() + const ssoTarget = ssoDestination() + const target = + ssoTarget ?? + router.resolve({ name: 'cloud-login', query: SSO_ENTRY_OPEN_QUERY }).href try { if (authStore.currentUser) await authStore.logout() } catch (error) { @@ -49,6 +49,7 @@ export function useContinueWithSso(context: () => SsoRequiredContext) { toastErrorHandler(error) return } + if (ssoTarget) trackSsoContinueClicked('cloud_app') window.location.assign(target) } diff --git a/src/platform/auth/webSessionFlagOn.test.ts b/src/platform/auth/webSessionFlagOn.test.ts index 3776490a578..3906aa711b2 100644 --- a/src/platform/auth/webSessionFlagOn.test.ts +++ b/src/platform/auth/webSessionFlagOn.test.ts @@ -26,6 +26,7 @@ import { OPERATION_POLL_TIMING } from '@comfyorg/account-core/billing' import { MISSING_CUSTOMER_MESSAGE } from '@comfyorg/account-core/customerRecovery' import { COMFY_CLIENT } from '@comfyorg/account-core/requestAuth' import { SessionTokenError } from '@comfyorg/account-core/sessionTokenMint' +import { ssoFlowStore } from '@comfyorg/account-core/telemetry' import { clearPreservedQuery, @@ -3201,6 +3202,93 @@ describe('a lapsed SSO session signs in again through SSO (sso_enabled)', () => return landings } + describe('the sign-in telemetry', () => { + afterEach(() => { + ssoFlowStore.finish() + }) + + const ssoSignIns = () => + vi + .mocked(useTelemetry()!.trackSsoEvent) + .mock.calls.filter( + ([event]) => event.name === 'app:sso_sign_in_completed' + ) + + it('reports an SSO session after an attempt this tab continued, by id only', async () => { + const attempt = ssoFlowStore.markContinued('cloud_login') + + await loadPage(SSO_SESSION) + + expect(ssoSignIns()).toEqual([ + [ + { + name: 'app:sso_sign_in_completed', + properties: { surface: 'cloud_login', flow_id: attempt.flowId } + } + ] + ]) + expect(ssoFlowStore.current()).toBeUndefined() + expect(useTelemetry()?.trackAuth).not.toHaveBeenCalled() + }) + + it('reports a lapse sent straight to SSO, then the SSO session it brings back', async () => { + await inAppThenLapsed(true) + await vi.waitFor(() => expect(assign).toHaveBeenCalledOnce()) + const [[shown]] = vi.mocked(useTelemetry()!.trackSsoEvent).mock.calls + expect(shown).toEqual({ + name: 'app:sso_required_shown', + properties: { + surface: 'cloud_app', + trigger: 'session_expired', + presentation: 'redirect', + flow_id: expect.any(String) + } + }) + + await loadPage(SSO_SESSION) + + expect(ssoSignIns()).toEqual([ + [ + { + name: 'app:sso_sign_in_completed', + properties: { + surface: 'cloud_app', + flow_id: shown.properties.flow_id + } + } + ] + ]) + }) + + it.for<{ + name: string + arrange: () => void + session: ServerSession + }>([ + { + name: 'an SSO session with no attempt', + arrange: () => {}, + session: SSO_SESSION + }, + { + name: 'an SSO session after an attempt only shown, never continued', + arrange: () => ssoFlowStore.start('cloud_login'), + session: SSO_SESSION + }, + { + name: 'a Google session during an attempt', + arrange: () => ssoFlowStore.markContinued('cloud_login'), + session: { userId: 'user-a' } + } + ])('reports nothing for $name', async ({ arrange, session }) => { + arrange() + + await loadPage(session) + + expect(ssoSignIns()).toEqual([]) + }) + }) + describe('the hint', () => { it.for<{ name: string diff --git a/src/platform/cloud/onboarding/CloudLoginView.test.ts b/src/platform/cloud/onboarding/CloudLoginView.test.ts index 0c7c5150be3..90e48d50772 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.test.ts +++ b/src/platform/cloud/onboarding/CloudLoginView.test.ts @@ -9,6 +9,8 @@ import { defineComponent, ref } from 'vue' import { createI18n } from 'vue-i18n' import { createMemoryHistory, createRouter } from 'vue-router' +import { ssoFlowStore } from '@comfyorg/account-core/telemetry' + import { useAuthActions } from '@/composables/auth/useAuthActions' import { useFeatureFlags } from '@/composables/useFeatureFlags' import enMessages from '@/locales/en/main.json' with { type: 'json' } @@ -20,11 +22,13 @@ import type { useSessionCookie } from '@/platform/auth/session/useSessionCookie' import { presentSsoRequired } from '@/platform/auth/sso/ssoRequired' import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey' import { useAuthStore } from '@/stores/authStore' +import { useTelemetry } from '@/platform/telemetry' import { useDialogStore } from '@/stores/dialogStore' vi.mock(import('@/composables/auth/useAuthActions')) vi.mock(import('@/composables/useFeatureFlags')) vi.mock(import('firebase/auth')) +vi.mock(import('@/platform/telemetry')) const redirectAfterAuth = vi.hoisted(() => vi.fn(async () => undefined)) vi.mock( @@ -103,7 +107,7 @@ async function renderLoginView( }) await router.push(url) await router.isReady() - return render(CloudLoginView, { + const rendered = render(CloudLoginView, { global: { plugins: [ router, @@ -112,6 +116,7 @@ async function renderLoginView( stubs: { CloudSignInForm: SignInFormStub } } }) + return { ...rendered, router } } afterEach(() => { @@ -332,6 +337,108 @@ describe('CloudLoginView SSO', () => { }) }) + it('reports the SSO email as the attempt continuing to SSO', async () => { + discoverReplies({ sso: true }) + await renderLoginView() + + await continueWithSso('ada@acme.com') + + await waitFor(() => expect(assign).toHaveBeenCalledOnce()) + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ + name: 'app:sso_continue_clicked', + properties: { surface: 'cloud_login', flow_id: expect.any(String) } + }) + }) + + it('still leaves for SSO when session storage refuses the attempt', async () => { + vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { + throw new DOMException('full', 'QuotaExceededError') + }) + discoverReplies({ sso: true }) + await renderLoginView() + + await continueWithSso('ada@acme.com') + + await waitFor(() => expect(assign).toHaveBeenCalledOnce()) + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith( + expect.objectContaining({ name: 'app:sso_continue_clicked' }) + ) + }) + + it('reports nothing for an email without SSO', async () => { + discoverReplies({ sso: false }) + await renderLoginView() + + await continueWithSso('ada@example.com') + + expect(await screen.findByText('auth.sso.notSso')).toBeInTheDocument() + expect(useTelemetry()?.trackSsoEvent).not.toHaveBeenCalled() + }) + + it('reports a return from the identity provider without signing in as cancelled', async () => { + discoverReplies({ sso: true }) + await renderLoginView() + await continueWithSso('ada@acme.com') + await waitFor(() => expect(assign).toHaveBeenCalledOnce()) + + window.dispatchEvent( + Object.assign(new Event('pageshow'), { persisted: true }) + ) + + const [[continued], [failed]] = vi.mocked(useTelemetry()!.trackSsoEvent) + .mock.calls + expect(failed).toEqual({ + name: 'app:sso_sign_in_failed', + properties: { + surface: 'cloud_login', + reason: 'cancelled', + flow_id: continued.properties.flow_id + } + }) + }) + + it.for<{ code: string; reason: string }>([ + { code: 'SSO_INVALID_STATE', reason: 'state_mismatch' }, + { code: 'SSO_ORG_NOT_ATTACHED', reason: 'org_not_attached' }, + { code: 'SSO_IDP_ERROR', reason: 'server_error' } + ])( + 'reports ?sso_error=$code as a $reason failure of the attempt that left', + async ({ code, reason }) => { + const attempt = ssoFlowStore.start('cloud_app') + + await renderLoginView(`/cloud/login?sso_error=${code}`) + + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ + name: 'app:sso_sign_in_failed', + properties: { surface: 'cloud_app', reason, flow_id: attempt.flowId } + }) + expect(ssoFlowStore.current()).toBeUndefined() + } + ) + + it('reports an ?sso_error once, dropping it from the URL but keeping its message', async () => { + ssoFlowStore.start('cloud_app') + const { router, unmount } = await renderLoginView( + '/cloud/login?sso_error=SSO_IDP_ERROR&previousFullPath=%2Fworkflows' + ) + + await waitFor(() => + expect(router.currentRoute.value.query).toEqual({ + previousFullPath: '/workflows' + }) + ) + expect(screen.getByText('auth.sso.errors.idpError')).toBeInTheDocument() + unmount() + render(CloudLoginView, { + global: { + plugins: [router, createI18n({ legacy: false, locale: 'en' })], + stubs: { CloudSignInForm: SignInFormStub } + } + }) + + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledOnce() + }) + it('returns to the page the visitor came from', async () => { discoverReplies({ sso: true }) await renderLoginView( @@ -583,7 +690,7 @@ describe('CloudLoginView Firebase sign-in refused for SSO', () => { await signInWithFirebase() await waitFor(() => - expect(presentSsoRequired).toHaveBeenCalledWith({ + expect(presentSsoRequired).toHaveBeenCalledWith('firebase_sign_in', { email: 'ada@acme.com', returnTo }) diff --git a/src/platform/cloud/onboarding/CloudLoginView.vue b/src/platform/cloud/onboarding/CloudLoginView.vue index add692d0a11..463e2dfef22 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.vue +++ b/src/platform/cloud/onboarding/CloudLoginView.vue @@ -91,16 +91,19 @@