Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/billing-web/src/auth/useSsoRequiredRefusal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ref<SessionErrorCode | undefined>>
Expand Down Expand Up @@ -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<void> {
const target = destination.value
if (target === undefined || leaving.value) return
reportSsoContinueClicked()
leaving.value = true
signOutFailed.value = false
if (!(await signOutOfFirebase())) {
Expand All @@ -81,7 +91,7 @@ export function useSsoRequiredRefusal(refusal: SsoRequiredRefusal) {
}

return {
offered: computed(() => destination.value !== undefined),
offered,
email,
leaving,
signOutFailed,
Expand Down
29 changes: 29 additions & 0 deletions apps/billing-web/src/telemetry/billingWebTelemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionIdentity>({ 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()
Expand Down
13 changes: 12 additions & 1 deletion apps/billing-web/src/telemetry/billingWebTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +56,7 @@ interface BillingEvent {
| BillingTelemetryEventName
| CheckoutJourneyTelemetryEventName
| WebSessionTelemetryEvent['name']
| SsoTelemetryEvent['name']
readonly properties: Readonly<Record<string, unknown>>
/** Sent while the page goes away, after PostHog has drained its queue on `pagehide`. */
readonly onPageExit?: true
Expand Down Expand Up @@ -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<SessionIdentity>): void {
syncIdentity(RUM_USER, identity)
Expand All @@ -272,6 +282,7 @@ export function createBillingWebTelemetry() {
startRumUser,
trackBillingEvent,
trackCheckoutJourneyEvent,
trackSsoEvent,
trackWebSessionEvent
}
}
Expand Down
31 changes: 31 additions & 0 deletions apps/billing-web/src/telemetry/ssoTelemetry.ts
Original file line number Diff line number Diff line change
@@ -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'))
})
}
34 changes: 34 additions & 0 deletions apps/billing-web/src/views/SignInView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand All @@ -486,5 +519,6 @@ describe('SignInView', () => {
expect(
screen.queryByRole('button', { name: 'Continue with SSO' })
).toBeNull()
expect(trackSso).not.toHaveBeenCalled()
})
})
7 changes: 3 additions & 4 deletions browser_tests/tests/cloudSsoSignIn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,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 ({
Expand Down
132 changes: 129 additions & 3 deletions packages/account-core/src/telemetry.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -60,3 +68,121 @@ describe('webSessionTelemetryHooks', () => {
])
})
})

function memoryStorage(): Storage {
const items = new Map<string, string>()
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)
})
})
Loading
Loading