From c7e43f45ce67a181a4b2f8398ba2164ac4779fd1 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 10 Oct 2026 14:29:45 -0400 Subject: [PATCH 1/5] feat(telemetry): report the SSO sign-in funnel and failures Adds app:sso_required_shown, app:sso_continue_clicked, app:sso_sign_in_failed and app:sso_workspace_landed to account-core telemetry with a flow_id carried across the IdP redirect, reports SSO sign-ins as user_auth_completed with method sso, and wires the Cloud app (PostHog + Datadog RUM through the registry) and billing-web. --- .../src/auth/useSsoRequiredRefusal.ts | 12 +- .../src/telemetry/billingWebTelemetry.test.ts | 29 ++++ .../src/telemetry/billingWebTelemetry.ts | 13 +- .../billing-web/src/telemetry/ssoTelemetry.ts | 32 ++++ apps/billing-web/src/views/SignInView.test.ts | 33 ++++ packages/account-core/src/telemetry.test.ts | 95 ++++++++++- packages/account-core/src/telemetry.ts | 160 +++++++++++++++++- src/composables/auth/useAuthActions.test.ts | 1 + src/composables/auth/useAuthActions.ts | 2 +- .../auth/session/cloudWebSessionStore.ts | 9 +- .../auth/session/ssoReentryStorage.ts | 6 +- .../auth/sso/SsoRequiredDialogContent.test.ts | 19 +++ src/platform/auth/sso/ssoRequired.test.ts | 44 ++++- src/platform/auth/sso/ssoRequired.ts | 15 +- .../auth/sso/ssoRequiredChunkFailure.test.ts | 2 +- .../auth/sso/ssoRequiredInline.test.ts | 44 ++++- src/platform/auth/sso/ssoRequiredInline.ts | 23 ++- src/platform/auth/sso/ssoTelemetry.ts | 86 ++++++++++ src/platform/auth/sso/useContinueWithSso.ts | 2 + src/platform/auth/webSessionFlagOn.test.ts | 42 +++++ .../cloud/onboarding/CloudLoginView.test.ts | 85 +++++++++- .../cloud/onboarding/CloudLoginView.vue | 19 ++- .../cloud/onboarding/CloudSignupView.vue | 2 +- .../composables/useCloudAuthPage.ts | 6 +- .../onboarding/composables/useSsoSignIn.ts | 14 +- .../telemetry/TelemetryRegistry.test.ts | 16 ++ src/platform/telemetry/TelemetryRegistry.ts | 9 +- .../cloud/DatadogRumTelemetryProvider.test.ts | 27 +++ .../cloud/DatadogRumTelemetryProvider.ts | 9 +- .../cloud/PostHogTelemetryProvider.test.ts | 26 +++ .../cloud/PostHogTelemetryProvider.ts | 9 +- src/platform/telemetry/types.ts | 12 +- .../stores/teamWorkspaceStore.test.ts | 50 ++++++ .../workspace/stores/teamWorkspaceStore.ts | 5 + .../workspace/stores/workspaceAuthStore.ts | 8 +- 35 files changed, 920 insertions(+), 46 deletions(-) create mode 100644 apps/billing-web/src/telemetry/ssoTelemetry.ts create mode 100644 src/platform/auth/sso/ssoTelemetry.ts 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..9ce5d868fbc --- /dev/null +++ b/apps/billing-web/src/telemetry/ssoTelemetry.ts @@ -0,0 +1,32 @@ +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' + } + }) +} + +export function reportSsoContinueClicked(): void { + billingWebTelemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.continueClicked, + properties: flowProperties( + ssoFlowStore.current() ?? ssoFlowStore.start('billing_web') + ) + }) +} diff --git a/apps/billing-web/src/views/SignInView.test.ts b/apps/billing-web/src/views/SignInView.test.ts index 2c11966c0bc..9ee3e65428f 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,36 @@ 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', + 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 +501,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 +518,6 @@ describe('SignInView', () => { expect( screen.queryByRole('button', { name: 'Continue with SSO' }) ).toBeNull() + expect(trackSso).not.toHaveBeenCalled() }) }) diff --git a/packages/account-core/src/telemetry.test.ts b/packages/account-core/src/telemetry.test.ts index a43332df88a..6620a073f92 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 { 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,86 @@ 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('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..00e9956abe0 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', @@ -75,7 +78,7 @@ export const AUTH_TELEMETRY_EVENT = { authFailed: 'app:user_auth_failed' } as const -export type AuthMethod = 'email' | 'google' | 'github' +export type AuthMethod = 'email' | 'google' | 'github' | 'sso' /** What the cloud app reports on every successful credential. */ export interface AuthCompletedMetadata { @@ -83,6 +86,8 @@ export interface AuthCompletedMetadata { is_new_user: boolean user_id: string email?: string + /** The SSO attempt this sign-in completes. */ + flow_id?: string } export type AuthFlowAction = @@ -98,3 +103,156 @@ 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', + 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' + +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 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.workspaceLanded + readonly properties: SsoFlowProperties & { + readonly landed_in_org_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 +} + +const SSO_FLOW_STORAGE_KEY = 'Comfy.Sso.TelemetryFlow' + +const zSsoFlow = z.object({ + flowId: z.string().min(1), + surface: z.enum(SSO_SURFACES) +}) + +/** `getRandomValues`, unlike `randomUUID`, also works outside a secure context. */ +function newFlowId(): string { + return Array.from(crypto.getRandomValues(new Uint8Array(16)), (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 start(surface: SsoSurface): SsoFlow { + const flow = createSsoFlow(surface) + inMemory = flow + try { + storage().setItem(SSO_FLOW_STORAGE_KEY, JSON.stringify(flow)) + } catch { + return flow + } + return flow + } + + function current(): SsoFlow | undefined { + return inMemory ?? readStored() + } + + function finish(): SsoFlow | undefined { + const flow = current() + inMemory = undefined + try { + storage().removeItem(SSO_FLOW_STORAGE_KEY) + } catch { + return flow + } + return flow + } + + return { start, current, finish } +} + +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..c1e5992b9ca 100644 --- a/src/platform/auth/session/cloudWebSessionStore.ts +++ b/src/platform/auth/session/cloudWebSessionStore.ts @@ -42,6 +42,7 @@ import { t } from '@/i18n' import { isCloud } from '@/platform/distribution/types' import { firebaseIdentity } from '@/platform/auth/firebaseIdentity' import { presentSsoRequired } from '@/platform/auth/sso/ssoRequired' +import { trackSsoSignInCompleted } from '@/platform/auth/sso/ssoTelemetry' import { webSessionUser } from '@/platform/auth/session/webSessionUser' import { clearInteractiveSignIn, @@ -50,6 +51,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 +344,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 +365,9 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { signedOutHere = false if (useFeatureFlags().flags.ssoEnabled) { rememberSignedInSession(next.session.user) + if (isSsoSession(next.session.user)) { + trackSsoSignInCompleted(next.session.user.id) + } } } else if (next.phase === 'signed_out' && next.outcome === 'revoked') { forgetSsoHint() @@ -482,7 +487,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..01268f68b4a 100644 --- a/src/platform/auth/sso/SsoRequiredDialogContent.test.ts +++ b/src/platform/auth/sso/SsoRequiredDialogContent.test.ts @@ -8,9 +8,12 @@ import { createI18n } from 'vue-i18n' import { createMemoryHistory, createRouter } from 'vue-router' 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: { @@ -70,6 +73,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') + 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' } diff --git a/src/platform/auth/sso/ssoRequired.test.ts b/src/platform/auth/sso/ssoRequired.test.ts index 269c4dc7a09..e07cefc347b 100644 --- a/src/platform/auth/sso/ssoRequired.test.ts +++ b/src/platform/auth/sso/ssoRequired.test.ts @@ -7,9 +7,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', @@ -27,19 +29,26 @@ describe('the SSO-required screen', () => { beforeAll(() => import('@/platform/auth/sso/SsoRequiredDialogContent.vue')) 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 +56,33 @@ 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', + flow_id: expect.any(String) + } + }) + }) + + 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..8b6b1ce1d21 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,7 @@ 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 { 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 +31,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 +43,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 +53,7 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { }) return } + trackSsoRequiredShown('cloud_app', trigger) dialogStore.showDialog({ key: SSO_REQUIRED_DIALOG_KEY, component, @@ -71,10 +77,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..5925f8b800d 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,36 @@ 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, 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 +89,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..d43b53d2c43 100644 --- a/src/platform/auth/sso/ssoRequiredInline.ts +++ b/src/platform/auth/sso/ssoRequiredInline.ts @@ -1,25 +1,36 @@ import { onUnmounted, readonly, shallowRef } from 'vue' +import type { + SsoRequiredTrigger, + SsoSurface +} from '@comfyorg/account-core/telemetry' + import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired' +import { 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.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) 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..acb18c669c3 --- /dev/null +++ b/src/platform/auth/sso/ssoTelemetry.ts @@ -0,0 +1,86 @@ +import type { + SsoFlow, + 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 the SSO-required screen appears. */ +export function trackSsoRequiredShown( + surface: SsoSurface, + trigger: SsoRequiredTrigger +): void { + const telemetry = useTelemetry() + if (!telemetry) return + const flow = ssoFlowStore.start( + trigger === 'customer_create' ? 'cloud_customer_create' : surface + ) + telemetry.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.requiredShown, + properties: { ...flowProperties(flow), trigger } + }) +} + +export function trackSsoContinueClicked(surface: SsoSurface): void { + const telemetry = useTelemetry() + if (!telemetry) return + const flow = ssoFlowStore.current() ?? ssoFlowStore.start(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 started is that attempt's sign-in. */ +export function trackSsoSignInCompleted(userId: string): void { + const telemetry = useTelemetry() + if (!telemetry) return + const flow = ssoFlowStore.finish() + if (!flow) return + signedInFlow = flow + telemetry.trackAuth({ + method: 'sso', + user_id: userId, + flow_id: flow.flowId + }) +} + +export function trackSsoWorkspaceLanded(landedInOrgWorkspace: boolean): void { + const flow = signedInFlow + signedInFlow = undefined + if (!flow) return + useTelemetry()?.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.workspaceLanded, + properties: { + ...flowProperties(flow), + landed_in_org_workspace: landedInOrgWorkspace + } + }) +} diff --git a/src/platform/auth/sso/useContinueWithSso.ts b/src/platform/auth/sso/useContinueWithSso.ts index 3bbb8ddeb49..d5c22e1b347 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' @@ -40,6 +41,7 @@ export function useContinueWithSso(context: () => SsoRequiredContext) { } async function continueWithSso() { + trackSsoContinueClicked('cloud_app') leaving.value = true const target = destination() try { diff --git a/src/platform/auth/webSessionFlagOn.test.ts b/src/platform/auth/webSessionFlagOn.test.ts index 3776490a578..70b146351ba 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,47 @@ 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()!.trackAuth) + .mock.calls.filter(([metadata]) => metadata.method === 'sso') + + it('reports an SSO session after an attempt this tab started, by id only', async () => { + const attempt = ssoFlowStore.start('cloud_login') + + await loadPage(SSO_SESSION) + + expect(ssoSignIns()).toEqual([ + [{ method: 'sso', user_id: 'user-a', flow_id: attempt.flowId }] + ]) + expect(ssoFlowStore.current()).toBeUndefined() + }) + + it.for<{ name: string; attempt: boolean; session: ServerSession }>([ + { + name: 'an SSO session with no attempt', + attempt: false, + session: SSO_SESSION + }, + { + name: 'a Google session during an attempt', + attempt: true, + session: { userId: 'user-a' } + } + ])('reports nothing for $name', async ({ attempt, session }) => { + if (attempt) ssoFlowStore.start('cloud_login') + + 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..d61d1521db7 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( @@ -332,6 +336,85 @@ 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('returns to the page the visitor came from', async () => { discoverReplies({ sso: true }) await renderLoginView( @@ -583,7 +666,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..2eb82991180 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.vue +++ b/src/platform/cloud/onboarding/CloudLoginView.vue @@ -96,11 +96,13 @@ import { useI18n } from 'vue-i18n' import { RouterLink, useRoute } from 'vue-router' import { readSsoError } from '@comfyorg/account-core/sso' +import { ssoFailureReason } from '@comfyorg/account-core/telemetry' import Button from '@/components/ui/button/Button.vue' import Message from '@/components/ui/message/Message.vue' import { useAuthActions } from '@/composables/auth/useAuthActions' import { useFeatureFlags } from '@/composables/useFeatureFlags' +import { trackSsoSignInFailed } from '@/platform/auth/sso/ssoTelemetry' import CloudSignInForm from '@/platform/cloud/onboarding/components/CloudSignInForm.vue' import CloudSocialAuthButtons from '@/platform/cloud/onboarding/components/CloudSocialAuthButtons.vue' import CloudSsoRequiredNotice from '@/platform/cloud/onboarding/components/CloudSsoRequiredNotice.vue' @@ -116,7 +118,7 @@ const { t } = useI18n() const route = useRoute() const authActions = useAuthActions() const { flags } = useFeatureFlags() -const { state: ssoState, busy: ssoBusy, trySso } = useSsoSignIn() +const { state: ssoState, busy: ssoBusy, trySso } = useSsoSignIn('cloud_login') const freeRunsSuffix = computed(() => { const offer = remoteConfig.value.free_tier_offer @@ -144,11 +146,16 @@ const { defaultRedirect: () => ({ name: 'cloud-user-check' }) }) -const ssoErrorKey = computed(() => { - if (!flags.ssoEnabled) return undefined - const code = readSsoError(route.query.sso_error) - return code && SSO_ERROR_MESSAGE_KEY[code] -}) +const ssoError = computed(() => + flags.ssoEnabled ? readSsoError(route.query.sso_error) : undefined +) +const ssoErrorKey = computed( + () => ssoError.value && SSO_ERROR_MESSAGE_KEY[ssoError.value] +) + +if (ssoError.value) { + trackSsoSignInFailed(ssoFailureReason(ssoError.value), 'cloud_login') +} const signInWithEmail = async (values: SignInData) => { authError.value = '' diff --git a/src/platform/cloud/onboarding/CloudSignupView.vue b/src/platform/cloud/onboarding/CloudSignupView.vue index c32dd1aa8dc..7732af5d047 100644 --- a/src/platform/cloud/onboarding/CloudSignupView.vue +++ b/src/platform/cloud/onboarding/CloudSignupView.vue @@ -120,7 +120,7 @@ const route = useRoute() const authActions = useAuthActions() const telemetry = useTelemetry() const { flags } = useFeatureFlags() -const { trySso } = useSsoSignIn() +const { trySso } = useSsoSignIn('cloud_signup') const { status: regionStatus } = useRegionGate() const { isFreeTierEnabled } = useFreeTierOnboarding() diff --git a/src/platform/cloud/onboarding/composables/useCloudAuthPage.ts b/src/platform/cloud/onboarding/composables/useCloudAuthPage.ts index 80b181750f9..d487ed996ec 100644 --- a/src/platform/cloud/onboarding/composables/useCloudAuthPage.ts +++ b/src/platform/cloud/onboarding/composables/useCloudAuthPage.ts @@ -29,7 +29,9 @@ export function useCloudAuthPage(options: { const route = useRoute() const router = useRouter() const { flags } = useFeatureFlags() - const { notice: ssoRequiredNotice } = useSsoRequiredInlineHost() + const { notice: ssoRequiredNotice } = useSsoRequiredInlineHost( + options.isNewUser ? 'cloud_signup' : 'cloud_login' + ) const authError = ref('') const authMode = ref( flags.ssoEnabled && route.query.sso === SSO_ENTRY_OPEN_QUERY.sso @@ -50,7 +52,7 @@ export function useCloudAuthPage(options: { async function onAuthSuccess() { if (flags.ssoEnabled && (await useSessionCookie().sessionRequiresSso())) { const authStore = useAuthStore() - presentSsoRequired({ + presentSsoRequired('firebase_sign_in', { email: authStore.userEmail ?? undefined, returnTo: resolveSsoReturnTo(route, router) }) diff --git a/src/platform/cloud/onboarding/composables/useSsoSignIn.ts b/src/platform/cloud/onboarding/composables/useSsoSignIn.ts index 7dd53203350..e21da684b44 100644 --- a/src/platform/cloud/onboarding/composables/useSsoSignIn.ts +++ b/src/platform/cloud/onboarding/composables/useSsoSignIn.ts @@ -4,8 +4,13 @@ import type { RouteLocationNormalizedLoaded, Router } from 'vue-router' import { useRoute, useRouter } from 'vue-router' import { discoverSso, ssoStartUrl } from '@comfyorg/account-core/sso' +import type { SsoSurface } from '@comfyorg/account-core/telemetry' import { toSsoReturnPath } from '@/platform/auth/sso/ssoReturnPath' +import { + trackSsoContinueClicked, + trackSsoSignInFailed +} from '@/platform/auth/sso/ssoTelemetry' import { captureOAuthRequestId, @@ -47,7 +52,7 @@ export function resolveSsoReturnTo( } /** SSO sign-in for the cloud auth pages, where ingest is same-origin. */ -export function useSsoSignIn() { +export function useSsoSignIn(surface: SsoSurface) { const route = useRoute() const router = useRouter() const state = ref({ phase: 'idle' }) @@ -62,7 +67,11 @@ export function useSsoSignIn() { // Back from the identity provider restores this page from the bfcache with // the navigation it started still showing as in flight. useEventListener(window, 'pageshow', (event) => { - if (event.persisted) dispatch({ type: 'restored' }) + if (!event.persisted) return + if (state.value.phase === 'redirecting') { + trackSsoSignInFailed('cancelled', surface) + } + dispatch({ type: 'restored' }) }) /** @@ -80,6 +89,7 @@ export function useSsoSignIn() { if (disposal.signal.aborted) return true dispatch({ type: 'discovered', discovery }) if (discovery.kind !== 'sso') return false + trackSsoContinueClicked(surface) window.location.assign( ssoStartUrl({ email, diff --git a/src/platform/telemetry/TelemetryRegistry.test.ts b/src/platform/telemetry/TelemetryRegistry.test.ts index ad886f90264..dc00d16a75f 100644 --- a/src/platform/telemetry/TelemetryRegistry.test.ts +++ b/src/platform/telemetry/TelemetryRegistry.test.ts @@ -105,6 +105,22 @@ describe('TelemetryRegistry', () => { expect(b.trackAuthFailed).toHaveBeenCalledExactlyOnceWith(payload) }) + it('dispatches an SSO event to every provider that records it', () => { + const a: TelemetryProvider = { trackSsoEvent: vi.fn() } + const b: TelemetryProvider = {} + const registry = new TelemetryRegistry() + registry.registerProvider(a) + registry.registerProvider(b) + + const event = { + name: 'app:sso_continue_clicked', + properties: { surface: 'cloud_app', flow_id: 'flow-1' } + } as const + registry.trackSsoEvent(event) + + expect(a.trackSsoEvent).toHaveBeenCalledExactlyOnceWith(event) + }) + it('dispatches unified auth retry outcomes to supporting providers', () => { const trackUnifiedAuthRetry = vi.fn() const registry = new TelemetryRegistry() diff --git a/src/platform/telemetry/TelemetryRegistry.ts b/src/platform/telemetry/TelemetryRegistry.ts index 4516469dfe2..4972a22ccc4 100644 --- a/src/platform/telemetry/TelemetryRegistry.ts +++ b/src/platform/telemetry/TelemetryRegistry.ts @@ -2,7 +2,10 @@ import type { BillingTelemetryEvent, CheckoutJourneyTelemetryEvent } from '@comfyorg/account-core/billing' -import type { WebSessionTelemetryEvent } from '@comfyorg/account-core/telemetry' +import type { + SsoTelemetryEvent, + WebSessionTelemetryEvent +} from '@comfyorg/account-core/telemetry' import type { AddCreditsClickMetadata, AgentAttachButtonClickedMetadata, @@ -142,6 +145,10 @@ export class TelemetryRegistry implements TelemetryDispatcher { this.dispatch((provider) => provider.trackWebSessionEvent?.(event)) } + trackSsoEvent(event: SsoTelemetryEvent): void { + this.dispatch((provider) => provider.trackSsoEvent?.(event)) + } + trackImageLoadFailed(metadata: ImageLoadFailureMetadata): void { this.dispatch((provider) => provider.trackImageLoadFailed?.(metadata)) } diff --git a/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts b/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts index 7edfe014ab4..382b25de53a 100644 --- a/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts +++ b/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts @@ -2,6 +2,7 @@ import type { BillingTelemetryEvent, CheckoutJourneyTelemetryEvent } from '@comfyorg/account-core/billing' +import type { SsoTelemetryEvent } from '@comfyorg/account-core/telemetry' import { computed } from 'vue' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -214,6 +215,32 @@ describe('DatadogRumTelemetryProvider', () => { ) }) + it.for([ + { + name: 'app:sso_required_shown', + properties: { + surface: 'cloud_login', + trigger: 'firebase_sign_in', + flow_id: 'flow-1' + } + }, + { + name: 'app:sso_sign_in_failed', + properties: { + surface: 'cloud_app', + reason: 'state_mismatch', + flow_id: 'flow-1' + } + } + ])('records the SSO event $name as a custom action', (event) => { + new DatadogRumTelemetryProvider().trackSsoEvent(event) + + expect(addAction).toHaveBeenCalledExactlyOnceWith( + event.name, + event.properties + ) + }) + it('records image load failures by source', () => { new DatadogRumTelemetryProvider().trackImageLoadFailed({ source: 'node_image_preview' diff --git a/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.ts b/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.ts index b68eb4098e5..c4422a57c56 100644 --- a/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.ts +++ b/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.ts @@ -8,7 +8,10 @@ import type { BillingTelemetryEvent, CheckoutJourneyTelemetryEvent } from '@comfyorg/account-core/billing' -import type { WebSessionTelemetryEvent } from '@comfyorg/account-core/telemetry' +import type { + SsoTelemetryEvent, + WebSessionTelemetryEvent +} from '@comfyorg/account-core/telemetry' // oxlint-disable-next-line no-restricted-imports -- the telemetry layer owns the sinks that reportError() fans out to import { datadogRum } from '@datadog/browser-rum' @@ -73,6 +76,10 @@ export class DatadogRumTelemetryProvider implements TelemetryProvider { datadogRum.addAction(event.name, event.properties) } + trackSsoEvent(event: SsoTelemetryEvent): void { + datadogRum.addAction(event.name, event.properties) + } + trackImageLoadFailed(metadata: ImageLoadFailureMetadata): void { datadogRum.addAction(TelemetryEvents.IMAGE_LOAD_FAILED, metadata) } diff --git a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts index c45d62cee18..2372056b99e 100644 --- a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts +++ b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts @@ -2,6 +2,7 @@ import type { BillingTelemetryEvent, CheckoutJourneyTelemetryEvent } from '@comfyorg/account-core/billing' +import type { SsoTelemetryEvent } from '@comfyorg/account-core/telemetry' import type { CaptureResult, PostHog } from 'posthog-js' import { afterEach, assert, beforeEach, describe, expect, it, vi } from 'vitest' import type { Ref } from 'vue' @@ -799,6 +800,31 @@ describe('PostHogTelemetryProvider', () => { ) }) + it.for([ + { + name: 'app:sso_continue_clicked', + properties: { surface: 'cloud_signup', flow_id: 'flow-1' } + }, + { + name: 'app:sso_workspace_landed', + properties: { + surface: 'cloud_login', + flow_id: 'flow-1', + landed_in_org_workspace: true + } + } + ])('captures the SSO event $name as is', async (event) => { + const provider = createProvider() + await vi.dynamicImportSettled() + + provider.trackSsoEvent(event) + + expect(hoisted.mockCapture).toHaveBeenCalledWith( + event.name, + event.properties + ) + }) + it('captures image load failures', async () => { const provider = createProvider() await vi.dynamicImportSettled() diff --git a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts index cfae61bab1d..222017f47bb 100644 --- a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts +++ b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts @@ -9,7 +9,10 @@ import type { BillingTelemetryEvent, CheckoutJourneyTelemetryEvent } from '@comfyorg/account-core/billing' -import type { WebSessionTelemetryEvent } from '@comfyorg/account-core/telemetry' +import type { + SsoTelemetryEvent, + WebSessionTelemetryEvent +} from '@comfyorg/account-core/telemetry' import type { CaptureResult, PostHog } from 'posthog-js' import { watch } from 'vue' import type { WatchStopHandle } from 'vue' @@ -467,6 +470,10 @@ export class PostHogTelemetryProvider implements TelemetryProvider { this.trackEvent(event.name, event.properties) } + trackSsoEvent(event: SsoTelemetryEvent): void { + this.trackEvent(event.name, event.properties) + } + trackImageLoadFailed(metadata: ImageLoadFailureMetadata): void { this.trackEvent(TelemetryEvents.IMAGE_LOAD_FAILED, metadata) } diff --git a/src/platform/telemetry/types.ts b/src/platform/telemetry/types.ts index 3439e8f463d..0cc0e7458ab 100644 --- a/src/platform/telemetry/types.ts +++ b/src/platform/telemetry/types.ts @@ -14,7 +14,8 @@ import { AUTH_TELEMETRY_EVENT, - SESSION_TELEMETRY_EVENT + SESSION_TELEMETRY_EVENT, + SSO_TELEMETRY_EVENT } from '@comfyorg/account-core/telemetry' import type { BillingTelemetryEvent, @@ -33,6 +34,7 @@ import type { AuthErrorMetadata, AuthFlowAction, AuthMethod, + SsoTelemetryEvent, WebSessionTelemetryEvent } from '@comfyorg/account-core/telemetry' import type { SessionRefreshOutcome } from '@comfyorg/account-core/session' @@ -53,6 +55,8 @@ export interface AuthMetadata { is_new_user?: boolean user_id?: string email?: string + /** The SSO attempt this sign-in completes. */ + flow_id?: string share_id?: string referrer_url?: string utm_source?: string @@ -1343,6 +1347,7 @@ export interface TelemetryProvider { trackUnifiedAuthRetry?(metadata: UnifiedAuthRetryMetadata): void trackUnifiedAuthRefresh?(metadata: UnifiedAuthRefreshMetadata): void trackWebSessionEvent?(event: WebSessionTelemetryEvent): void + trackSsoEvent?(event: SsoTelemetryEvent): void trackImageLoadFailed?(metadata: ImageLoadFailureMetadata): void trackUserLoggedIn?(): void trackBootstrapComplete?(metadata: BootstrapCompleteMetadata): void @@ -1533,6 +1538,10 @@ export const TelemetryEvents = { UNIFIED_AUTH_RETRY_FAILED: 'auth.unified.request_retry.failed', UNIFIED_AUTH_REFRESH_SUCCEEDED: SESSION_TELEMETRY_EVENT.refreshSucceeded, UNIFIED_AUTH_REFRESH_FAILED: SESSION_TELEMETRY_EVENT.refreshFailed, + SSO_REQUIRED_SHOWN: SSO_TELEMETRY_EVENT.requiredShown, + SSO_CONTINUE_CLICKED: SSO_TELEMETRY_EVENT.continueClicked, + SSO_SIGN_IN_FAILED: SSO_TELEMETRY_EVENT.signInFailed, + SSO_WORKSPACE_LANDED: SSO_TELEMETRY_EVENT.workspaceLanded, IMAGE_LOAD_FAILED: 'app:image_load_failed', BOOTSTRAP_COMPLETE: 'app:bootstrap_complete', @@ -1739,6 +1748,7 @@ export type TelemetryEventProperties = | UnifiedAuthRetryMetadata | UnifiedAuthRefreshMetadata | WebSessionTelemetryEvent['properties'] + | SsoTelemetryEvent['properties'] | ImageLoadFailureMetadata | BootstrapCompleteMetadata | SurveyResponses diff --git a/src/platform/workspace/stores/teamWorkspaceStore.test.ts b/src/platform/workspace/stores/teamWorkspaceStore.test.ts index 3a99f777d6c..42b9899ab37 100644 --- a/src/platform/workspace/stores/teamWorkspaceStore.test.ts +++ b/src/platform/workspace/stores/teamWorkspaceStore.test.ts @@ -19,6 +19,10 @@ import { startDesktopHostSession, stopDesktopHostSession } from '@/platform/auth/desktopHost/desktopHostSession' +import { ssoFlowStore } from '@comfyorg/account-core/telemetry' + +import { trackSsoSignInCompleted } from '@/platform/auth/sso/ssoTelemetry' +import { useTelemetry } from '@/platform/telemetry' import { NoWorkspaceAccessError } from '@/platform/workspace/api/workspaceApiError' import { WORKSPACE_STORAGE_KEYS } from '@/platform/workspace/workspaceConstants' @@ -70,6 +74,7 @@ const mockWorkspaceApi = vi.hoisted(() => ({ })) vi.mock(import('@/platform/telemetry/reportError'), { spy: true }) +vi.mock(import('@/platform/telemetry')) const mockWorkspaceApiError = vi.hoisted( () => @@ -351,6 +356,51 @@ describe('useTeamWorkspaceStore', () => { expect(store.activeWorkspaceId).toBe(expected) }) + it.for([ + { + name: "lands in the organization's workspace the server names", + defaultId: mockTeamWorkspace.id, + landed: true + }, + { + name: 'lands elsewhere without a default', + defaultId: undefined, + landed: false + } + ])( + 'after an SSO sign-in, reports it $name', + async ({ defaultId, landed }) => { + const attempt = ssoFlowStore.start('cloud_login') + trackSsoSignInCompleted('user-1') + mockWorkspaceApi.list.mockResolvedValue({ + workspaces: [mockPersonalWorkspace, mockTeamWorkspace], + ...(defaultId && { default_workspace_id: defaultId }) + }) + + await useTeamWorkspaceStore().initialize() + + expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ + name: 'app:sso_workspace_landed', + properties: { + surface: 'cloud_login', + flow_id: attempt.flowId, + landed_in_org_workspace: landed + } + }) + } + ) + + it('reports no landing for a sign-in that was not SSO', async () => { + mockWorkspaceApi.list.mockResolvedValue({ + workspaces: [mockPersonalWorkspace, mockTeamWorkspace], + default_workspace_id: mockTeamWorkspace.id + }) + + await useTeamWorkspaceStore().initialize() + + expect(useTelemetry()?.trackSsoEvent).not.toHaveBeenCalled() + }) + it('falls back to personal if stored workspace not in list', async () => { mockLocalStorage.getItem.mockReturnValue('non-existent-workspace') diff --git a/src/platform/workspace/stores/teamWorkspaceStore.ts b/src/platform/workspace/stores/teamWorkspaceStore.ts index 80556ac7219..32bf8541029 100644 --- a/src/platform/workspace/stores/teamWorkspaceStore.ts +++ b/src/platform/workspace/stores/teamWorkspaceStore.ts @@ -8,6 +8,7 @@ import { isDesktopHostSignedIn, requestDesktopHostWorkspaceSwitch } from '@/platform/auth/desktopHost/desktopHostSession' +import { trackSsoWorkspaceLanded } from '@/platform/auth/sso/ssoTelemetry' import { isCloud } from '@/platform/distribution/types' import { WORKSPACE_STORAGE_KEYS } from '@/platform/workspace/workspaceConstants' import { clearPreservedQuery } from '@/platform/navigation/preservedQueryManager' @@ -482,6 +483,10 @@ export const useTeamWorkspaceStore = defineStore('teamWorkspace', () => { // 5. Set active workspace mutableActiveWorkspaceId.value = targetWorkspaceId setLastWorkspaceId(targetWorkspaceId) + trackSsoWorkspaceLanded( + !!response.default_workspace_id && + targetWorkspaceId === response.default_workspace_id + ) initState.value = 'ready' isFetchingWorkspaces.value = false diff --git a/src/platform/workspace/stores/workspaceAuthStore.ts b/src/platform/workspace/stores/workspaceAuthStore.ts index 78c852c6c75..4942a921a9c 100644 --- a/src/platform/workspace/stores/workspaceAuthStore.ts +++ b/src/platform/workspace/stores/workspaceAuthStore.ts @@ -94,7 +94,9 @@ function sessionErrorMessageKey( function surfacePermanentAuthError(err: WorkspaceAuthError): void { if ( err.code === 'SSO_REQUIRED' && - presentSsoRequired({ email: useAuthStore().userEmail ?? undefined }) + presentSsoRequired('session_refused', { + email: useAuthStore().userEmail ?? undefined + }) ) { return } @@ -686,7 +688,9 @@ export const useWorkspaceAuthStore = defineStore('workspaceAuth', () => { // A reload would take down the SSO-required screen this refusal opens. if ( refusalCode === 'SSO_REQUIRED' && - presentSsoRequired({ email: useAuthStore().userEmail ?? undefined }) + presentSsoRequired('session_refused', { + email: useAuthStore().userEmail ?? undefined + }) ) { clearWorkspaceContext() cancelWorkflowTransition?.() From 23e8141a6f6c1b31c9dc8d066645e626d2c3301f Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 10 Oct 2026 14:58:29 -0400 Subject: [PATCH 2/5] fix(telemetry): keep SSO telemetry off non-SSO sinks and attribute completion only to continued attempts - report app:sso_sign_in_completed via trackSsoEvent instead of trackAuth - mark a flow continued on continue; complete only a continued flow; drop an uncontinued flow on dismiss and on another sign-in - strip ?sso_error after reporting it; report continue once on the sign-in page hand-off - add presentation (notice/redirect) to sso_required_shown and report the session-expiry redirect - rename landed_in_org_workspace to landed_in_default_workspace - guard flow id generation when crypto is unavailable --- .../billing-web/src/telemetry/ssoTelemetry.ts | 7 +- apps/billing-web/src/views/SignInView.test.ts | 1 + packages/account-core/src/telemetry.test.ts | 37 ++++++++++- packages/account-core/src/telemetry.ts | 55 +++++++++++++--- .../auth/session/cloudWebSessionStore.ts | 8 ++- .../auth/sso/SsoRequiredDialogContent.test.ts | 25 ++++++- src/platform/auth/sso/ssoRequired.test.ts | 22 ++++++- src/platform/auth/sso/ssoRequired.ts | 10 ++- .../auth/sso/ssoRequiredInline.test.ts | 7 +- src/platform/auth/sso/ssoRequiredInline.ts | 8 ++- src/platform/auth/sso/ssoTelemetry.ts | 47 +++++++++----- src/platform/auth/sso/useContinueWithSso.ts | 17 +++-- src/platform/auth/webSessionFlagOn.test.ts | 65 ++++++++++++++++--- .../cloud/onboarding/CloudLoginView.test.ts | 26 +++++++- .../cloud/onboarding/CloudLoginView.vue | 15 +++-- .../cloud/DatadogRumTelemetryProvider.test.ts | 1 + .../cloud/PostHogTelemetryProvider.test.ts | 6 +- src/platform/telemetry/types.ts | 3 +- .../stores/teamWorkspaceStore.test.ts | 10 +-- src/router.ts | 2 + 20 files changed, 297 insertions(+), 75 deletions(-) diff --git a/apps/billing-web/src/telemetry/ssoTelemetry.ts b/apps/billing-web/src/telemetry/ssoTelemetry.ts index 9ce5d868fbc..13688e2ea59 100644 --- a/apps/billing-web/src/telemetry/ssoTelemetry.ts +++ b/apps/billing-web/src/telemetry/ssoTelemetry.ts @@ -17,7 +17,8 @@ export function reportSsoRequiredShown(): void { name: SSO_TELEMETRY_EVENT.requiredShown, properties: { ...flowProperties(ssoFlowStore.start('billing_web')), - trigger: 'session_refused' + trigger: 'session_refused', + presentation: 'notice' } }) } @@ -25,8 +26,6 @@ export function reportSsoRequiredShown(): void { export function reportSsoContinueClicked(): void { billingWebTelemetry.trackSsoEvent({ name: SSO_TELEMETRY_EVENT.continueClicked, - properties: flowProperties( - ssoFlowStore.current() ?? ssoFlowStore.start('billing_web') - ) + 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 9ee3e65428f..65149709073 100644 --- a/apps/billing-web/src/views/SignInView.test.ts +++ b/apps/billing-web/src/views/SignInView.test.ts @@ -451,6 +451,7 @@ describe('SignInView', () => { properties: { surface: 'billing_web', trigger: 'session_refused', + presentation: 'notice', flow_id: expect.any(String) } }) diff --git a/packages/account-core/src/telemetry.test.ts b/packages/account-core/src/telemetry.test.ts index 6620a073f92..c976657396c 100644 --- a/packages/account-core/src/telemetry.test.ts +++ b/packages/account-core/src/telemetry.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { createWebSessionIdentity } from './core/webSessionIdentity.js' import type { SsoErrorCode } from './sso.js' @@ -131,6 +131,41 @@ describe('createSsoFlowStore', () => { 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":"?"}') diff --git a/packages/account-core/src/telemetry.ts b/packages/account-core/src/telemetry.ts index 00e9956abe0..5765792454f 100644 --- a/packages/account-core/src/telemetry.ts +++ b/packages/account-core/src/telemetry.ts @@ -78,7 +78,7 @@ export const AUTH_TELEMETRY_EVENT = { authFailed: 'app:user_auth_failed' } as const -export type AuthMethod = 'email' | 'google' | 'github' | 'sso' +export type AuthMethod = 'email' | 'google' | 'github' /** What the cloud app reports on every successful credential. */ export interface AuthCompletedMetadata { @@ -86,8 +86,6 @@ export interface AuthCompletedMetadata { is_new_user: boolean user_id: string email?: string - /** The SSO attempt this sign-in completes. */ - flow_id?: string } export type AuthFlowAction = @@ -108,6 +106,7 @@ 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 @@ -131,6 +130,13 @@ export type SsoRequiredTrigger = | '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' @@ -150,6 +156,7 @@ export type SsoTelemetryEvent = readonly name: typeof SSO_TELEMETRY_EVENT.requiredShown readonly properties: SsoFlowProperties & { readonly trigger: SsoRequiredTrigger + readonly presentation: SsoRequiredPresentation } } | { @@ -163,10 +170,14 @@ export type SsoTelemetryEvent = 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_org_workspace: boolean + readonly landed_in_default_workspace: boolean } } @@ -188,18 +199,30 @@ export function ssoFailureReason(code: SsoErrorCode): SsoSignInFailureReason { 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) + 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(crypto.getRandomValues(new Uint8Array(16)), (byte) => + return Array.from(randomBytes(), (byte) => byte.toString(16).padStart(2, '0') ).join('') } @@ -226,8 +249,7 @@ export function createSsoFlowStore(storage: () => Storage) { } } - function start(surface: SsoSurface): SsoFlow { - const flow = createSsoFlow(surface) + function save(flow: SsoFlow): SsoFlow { inMemory = flow try { storage().setItem(SSO_FLOW_STORAGE_KEY, JSON.stringify(flow)) @@ -237,10 +259,19 @@ export function createSsoFlowStore(storage: () => Storage) { 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 @@ -252,7 +283,13 @@ export function createSsoFlowStore(storage: () => Storage) { return flow } - return { start, current, finish } + /** 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/platform/auth/session/cloudWebSessionStore.ts b/src/platform/auth/session/cloudWebSessionStore.ts index c1e5992b9ca..c5f57794596 100644 --- a/src/platform/auth/session/cloudWebSessionStore.ts +++ b/src/platform/auth/session/cloudWebSessionStore.ts @@ -42,7 +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 { trackSsoSignInCompleted } from '@/platform/auth/sso/ssoTelemetry' +import { + endSsoFlow, + trackSsoSignInCompleted +} from '@/platform/auth/sso/ssoTelemetry' import { webSessionUser } from '@/platform/auth/session/webSessionUser' import { clearInteractiveSignIn, @@ -366,7 +369,7 @@ export const useCloudWebSessionStore = defineStore('cloudWebSession', () => { if (useFeatureFlags().flags.ssoEnabled) { rememberSignedInSession(next.session.user) if (isSsoSession(next.session.user)) { - trackSsoSignInCompleted(next.session.user.id) + trackSsoSignInCompleted() } } } else if (next.phase === 'signed_out' && next.outcome === 'revoked') { @@ -384,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 } diff --git a/src/platform/auth/sso/SsoRequiredDialogContent.test.ts b/src/platform/auth/sso/SsoRequiredDialogContent.test.ts index 01268f68b4a..a28cd8f8ad3 100644 --- a/src/platform/auth/sso/SsoRequiredDialogContent.test.ts +++ b/src/platform/auth/sso/SsoRequiredDialogContent.test.ts @@ -3,10 +3,12 @@ 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' @@ -56,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' }) @@ -74,7 +80,7 @@ describe('SsoRequiredDialogContent', () => { }) it('reports the click as the shown attempt continuing to SSO', async () => { - trackSsoRequiredShown('cloud_app', 'session_refused') + trackSsoRequiredShown('cloud_app', 'session_refused', 'notice') await renderDialog({ email: 'ada@acme.com' }) await userEvent.click( @@ -172,4 +178,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 e07cefc347b..33b90dded49 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 { @@ -27,6 +29,9 @@ 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( @@ -61,11 +66,26 @@ describe('the SSO-required screen', () => { properties: { surface: 'cloud_app', trigger: 'session_refused', + presentation: 'notice', flow_id: expect.any(String) } }) }) + it.for<{ name: string; continued: boolean; kept: boolean }>([ + { name: 'drops an attempt never continued', continued: false, kept: false }, + { name: 'keeps an attempt already continued', continued: true, kept: true } + ])('closing the dialog $name', async ({ continued, kept }) => { + vi.mocked(useFeatureFlags().flags).ssoEnabled = true + presentSsoRequired('session_refused', { email: 'ada@acme.com' }) + await shownDialog() + if (continued) ssoFlowStore.markContinued('cloud_app') + + useDialogStore().closeDialog({ key: SSO_REQUIRED_DIALOG_KEY }) + + expect(ssoFlowStore.current() !== undefined).toBe(kept) + }) + it('reports a refused API request as the api_key trigger', async () => { vi.mocked(useFeatureFlags().flags).ssoEnabled = true diff --git a/src/platform/auth/sso/ssoRequired.ts b/src/platform/auth/sso/ssoRequired.ts index 8b6b1ce1d21..a3a625a6266 100644 --- a/src/platform/auth/sso/ssoRequired.ts +++ b/src/platform/auth/sso/ssoRequired.ts @@ -11,7 +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 { trackSsoRequiredShown } from '@/platform/auth/sso/ssoTelemetry' +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' @@ -53,7 +56,7 @@ export function presentSsoRequired( }) return } - trackSsoRequiredShown('cloud_app', trigger) + trackSsoRequiredShown('cloud_app', trigger, 'notice') dialogStore.showDialog({ key: SSO_REQUIRED_DIALOG_KEY, component, @@ -61,7 +64,8 @@ export function presentSsoRequired( dialogComponentProps: { renderer: 'reka', headless: true, - contentClass: SELF_STYLED_PANEL_CONTENT_CLASS + contentClass: SELF_STYLED_PANEL_CONTENT_CLASS, + onClose: abandonSsoFlow } }) }) diff --git a/src/platform/auth/sso/ssoRequiredInline.test.ts b/src/platform/auth/sso/ssoRequiredInline.test.ts index 5925f8b800d..2414741feac 100644 --- a/src/platform/auth/sso/ssoRequiredInline.test.ts +++ b/src/platform/auth/sso/ssoRequiredInline.test.ts @@ -69,7 +69,12 @@ describe('the SSO-required notice on an auth page', () => { expect(useTelemetry()?.trackSsoEvent).toHaveBeenCalledExactlyOnceWith({ name: 'app:sso_required_shown', - properties: { surface, trigger, flow_id: expect.any(String) } + properties: { + surface, + trigger, + presentation: 'notice', + flow_id: expect.any(String) + } }) } ) diff --git a/src/platform/auth/sso/ssoRequiredInline.ts b/src/platform/auth/sso/ssoRequiredInline.ts index d43b53d2c43..edcb5917b6f 100644 --- a/src/platform/auth/sso/ssoRequiredInline.ts +++ b/src/platform/auth/sso/ssoRequiredInline.ts @@ -6,7 +6,10 @@ import type { } from '@comfyorg/account-core/telemetry' import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired' -import { trackSsoRequiredShown } from '@/platform/auth/sso/ssoTelemetry' +import { + abandonSsoFlow, + trackSsoRequiredShown +} from '@/platform/auth/sso/ssoTelemetry' const hostSurfaces: SsoSurface[] = [] const notice = shallowRef() @@ -21,7 +24,7 @@ export function presentInline( ): boolean { const surface = hostSurfaces.at(-1) if (!surface) return false - if (!notice.value) trackSsoRequiredShown(surface, trigger) + if (!notice.value) trackSsoRequiredShown(surface, trigger, 'notice') notice.value = { ...notice.value, ...context } return true } @@ -31,6 +34,7 @@ export function useSsoRequiredInlineHost(surface: SsoSurface) { hostSurfaces.push(surface) onUnmounted(() => { 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 index acb18c669c3..e03e03863d2 100644 --- a/src/platform/auth/sso/ssoTelemetry.ts +++ b/src/platform/auth/sso/ssoTelemetry.ts @@ -1,5 +1,6 @@ import type { SsoFlow, + SsoRequiredPresentation, SsoRequiredTrigger, SsoSignInFailureReason, SsoSurface @@ -19,26 +20,31 @@ function flowProperties({ flowId, surface }: SsoFlow) { return { surface, flow_id: flowId } } -/** Starts a new attempt each time the SSO-required screen appears. */ +/** + * 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 + trigger: SsoRequiredTrigger, + presentation: SsoRequiredPresentation ): void { const telemetry = useTelemetry() if (!telemetry) return const flow = ssoFlowStore.start( - trigger === 'customer_create' ? 'cloud_customer_create' : surface + trigger === 'customer_create' ? 'cloud_customer_create' : surface, + presentation === 'redirect' ) telemetry.trackSsoEvent({ name: SSO_TELEMETRY_EVENT.requiredShown, - properties: { ...flowProperties(flow), trigger } + properties: { ...flowProperties(flow), trigger, presentation } }) } export function trackSsoContinueClicked(surface: SsoSurface): void { const telemetry = useTelemetry() if (!telemetry) return - const flow = ssoFlowStore.current() ?? ssoFlowStore.start(surface) + const flow = ssoFlowStore.markContinued(surface) telemetry.trackSsoEvent({ name: SSO_TELEMETRY_EVENT.continueClicked, properties: flowProperties(flow) @@ -58,21 +64,30 @@ export function trackSsoSignInFailed( }) } -/** An SSO session after an attempt this tab started is that attempt's sign-in. */ -export function trackSsoSignInCompleted(userId: string): void { - const telemetry = useTelemetry() - if (!telemetry) return +/** 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) return + if (!flow?.continued) return signedInFlow = flow - telemetry.trackAuth({ - method: 'sso', - user_id: userId, - flow_id: flow.flowId + useTelemetry()?.trackSsoEvent({ + name: SSO_TELEMETRY_EVENT.signInCompleted, + properties: flowProperties(flow) }) } -export function trackSsoWorkspaceLanded(landedInOrgWorkspace: boolean): void { +/** 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 @@ -80,7 +95,7 @@ export function trackSsoWorkspaceLanded(landedInOrgWorkspace: boolean): void { name: SSO_TELEMETRY_EVENT.workspaceLanded, properties: { ...flowProperties(flow), - landed_in_org_workspace: landedInOrgWorkspace + landed_in_default_workspace: landedInDefaultWorkspace } }) } diff --git a/src/platform/auth/sso/useContinueWithSso.ts b/src/platform/auth/sso/useContinueWithSso.ts index d5c22e1b347..350b966e82b 100644 --- a/src/platform/auth/sso/useContinueWithSso.ts +++ b/src/platform/auth/sso/useContinueWithSso.ts @@ -18,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), @@ -31,19 +32,17 @@ 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() { - trackSsoContinueClicked('cloud_app') leaving.value = true - const target = destination() + const ssoTarget = ssoDestination() + if (ssoTarget) trackSsoContinueClicked('cloud_app') + const target = + ssoTarget ?? + router.resolve({ name: 'cloud-login', query: SSO_ENTRY_OPEN_QUERY }).href try { if (authStore.currentUser) await authStore.logout() } catch (error) { diff --git a/src/platform/auth/webSessionFlagOn.test.ts b/src/platform/auth/webSessionFlagOn.test.ts index 70b146351ba..afcb0c4fc4d 100644 --- a/src/platform/auth/webSessionFlagOn.test.ts +++ b/src/platform/auth/webSessionFlagOn.test.ts @@ -3209,33 +3209,80 @@ describe('a lapsed SSO session signs in again through SSO (sso_enabled)', () => const ssoSignIns = () => vi - .mocked(useTelemetry()!.trackAuth) - .mock.calls.filter(([metadata]) => metadata.method === 'sso') + .mocked(useTelemetry()!.trackSsoEvent) + .mock.calls.filter( + ([event]) => event.name === 'app:sso_sign_in_completed' + ) - it('reports an SSO session after an attempt this tab started, by id only', async () => { - const attempt = ssoFlowStore.start('cloud_login') + 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([ - [{ method: 'sso', user_id: 'user-a', flow_id: attempt.flowId }] + [ + { + 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; attempt: boolean; session: ServerSession }>([ + it.for<{ + name: string + attempt: 'none' | 'shown' | 'continued' + session: ServerSession + }>([ { name: 'an SSO session with no attempt', - attempt: false, + attempt: 'none', + session: SSO_SESSION + }, + { + name: 'an SSO session after an attempt only shown, never continued', + attempt: 'shown', session: SSO_SESSION }, { name: 'a Google session during an attempt', - attempt: true, + attempt: 'continued', session: { userId: 'user-a' } } ])('reports nothing for $name', async ({ attempt, session }) => { - if (attempt) ssoFlowStore.start('cloud_login') + if (attempt === 'shown') ssoFlowStore.start('cloud_login') + if (attempt === 'continued') ssoFlowStore.markContinued('cloud_login') await loadPage(session) diff --git a/src/platform/cloud/onboarding/CloudLoginView.test.ts b/src/platform/cloud/onboarding/CloudLoginView.test.ts index d61d1521db7..90e48d50772 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.test.ts +++ b/src/platform/cloud/onboarding/CloudLoginView.test.ts @@ -107,7 +107,7 @@ async function renderLoginView( }) await router.push(url) await router.isReady() - return render(CloudLoginView, { + const rendered = render(CloudLoginView, { global: { plugins: [ router, @@ -116,6 +116,7 @@ async function renderLoginView( stubs: { CloudSignInForm: SignInFormStub } } }) + return { ...rendered, router } } afterEach(() => { @@ -415,6 +416,29 @@ describe('CloudLoginView SSO', () => { } ) + 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( diff --git a/src/platform/cloud/onboarding/CloudLoginView.vue b/src/platform/cloud/onboarding/CloudLoginView.vue index 2eb82991180..463e2dfef22 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.vue +++ b/src/platform/cloud/onboarding/CloudLoginView.vue @@ -91,9 +91,10 @@