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
29 changes: 18 additions & 11 deletions browser_tests/tests/cloudSsoSignIn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => {
await expect(page.getByText(SSO_COPY.errors.orgDisabled)).toBeVisible()
})

test('a customer record refused with sso_required opens the SSO dialog, not a failure toast', async ({
test('a customer record refused with sso_required shows the SSO notice on the page, not a failure toast', async ({
page,
cloudAuth
}) => {
Expand All @@ -315,15 +315,19 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => {
await signInWithEmail(page)

await expect(
page
.getByRole('dialog')
.getByRole('heading', { name: SSO_COPY.required.title })
page.getByRole('alert').filter({
hasText: SSO_COPY.required.bodyWithEmail.replace(
'{email}',
CLOUD_SELF_EMAIL
)
})
).toBeVisible()
await expect(page.getByRole('dialog')).toHaveCount(0)
await expect(page.getByText(/Failed to create customer/)).toHaveCount(0)
await expect(page).toHaveURL(/\/cloud\/login/)
})

test('a Firebase sign-in ingest refuses with sso_required opens the SSO dialog', async ({
test('a Firebase sign-in ingest refuses with sso_required shows the SSO notice on the page', async ({
page,
cloudAuth
}) => {
Expand All @@ -337,15 +341,18 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => {

await signInWithEmail(page)

const dialog = page.getByRole('dialog')
await expect(
dialog.getByRole('heading', { name: SSO_COPY.required.title })
page.getByRole('alert').filter({
hasText: SSO_COPY.required.bodyWithEmail.replace(
'{email}',
CLOUD_SELF_EMAIL
)
})
).toBeVisible()
await expect(
dialog.getByText(
SSO_COPY.required.bodyWithEmail.replace('{email}', CLOUD_SELF_EMAIL)
)
).toBeVisible()
page.getByRole('button', { name: SSO_COPY.continueWithSso })
).toHaveCount(1)
await expect(page.getByRole('dialog')).toHaveCount(0)
await expect(page).toHaveURL(/\/cloud\/login/)
})
})
Expand Down
51 changes: 6 additions & 45 deletions src/platform/auth/sso/SsoRequiredDialogContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,11 @@
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'

import { ssoStartUrl } from '@comfyorg/account-core/sso'

import Button from '@/components/ui/button/Button.vue'
import { useErrorHandling } from '@/composables/useErrorHandling'
import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey'
import { toSsoReturnPath } from '@/platform/auth/sso/ssoReturnPath'
import { SSO_ENTRY_OPEN_QUERY } from '@/platform/cloud/onboarding/sso/ssoEntryQuery'
import { useAuthStore } from '@/stores/authStore'
import { useContinueWithSso } from '@/platform/auth/sso/useContinueWithSso'
import { useDialogStore } from '@/stores/dialogStore'

const { email, returnTo, organizationId } = defineProps<{
Expand All @@ -65,46 +58,14 @@ const { email, returnTo, organizationId } = defineProps<{
}>()

const { t } = useI18n()
const router = useRouter()
const authStore = useAuthStore()
const dialogStore = useDialogStore()
const { toastErrorHandler } = useErrorHandling()
const knownEmail = computed(() => email ?? authStore.userEmail)
const leaving = ref(false)
const { knownEmail, leaving, continueWithSso } = useContinueWithSso(() => ({
email,
returnTo,
organizationId
}))

function dismiss() {
dialogStore.closeDialog({ key: SSO_REQUIRED_DIALOG_KEY })
}

function destination(): string {
const back = {
returnTo: toSsoReturnPath(returnTo ?? router.currentRoute.value.fullPath),
origin: window.location.origin
}
if (organizationId) {
return ssoStartUrl({
organizationId,
email: knownEmail.value ?? undefined,
...back
})
}
if (!knownEmail.value) {
return router.resolve({ name: 'cloud-login', query: SSO_ENTRY_OPEN_QUERY })
.href
}
return ssoStartUrl({ email: knownEmail.value, ...back })
}

async function continueWithSso() {
leaving.value = true
const target = destination()
try {
if (authStore.currentUser) await authStore.logout()
} catch (error) {
leaving.value = false
toastErrorHandler(error)
return
}
window.location.assign(target)
}
</script>
2 changes: 2 additions & 0 deletions src/platform/auth/sso/ssoRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SELF_STYLED_PANEL_CONTENT_CLASS } from '@/components/ui/dialog/dialog.v
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 { reportError } from '@/platform/telemetry/reportError'
import { useToastStore } from '@/platform/updates/common/toastStore'
import { useDialogStore } from '@/stores/dialogStore'
Expand Down Expand Up @@ -37,6 +38,7 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean {
organizationId: context.organizationId
})
}
if (presentInline(known)) return true
void import('@/platform/auth/sso/SsoRequiredDialogContent.vue')
.then(({ default: component }) => {
if (dialogStore.isDialogOpen(SSO_REQUIRED_DIALOG_KEY)) {
Expand Down
63 changes: 63 additions & 0 deletions src/platform/auth/sso/ssoRequiredInline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { render, screen } from '@testing-library/vue'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, h } from 'vue'

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 { useDialogStore } from '@/stores/dialogStore'

vi.mock(import('@/composables/useFeatureFlags'))

const AuthPage = defineComponent({
setup() {
const { notice } = useSsoRequiredInlineHost()
return () => h('pre', JSON.stringify(notice.value ?? null))
}
})

async function dialogShown() {
await vi.dynamicImportSettled()
return useDialogStore().dialogStack.some(
(dialog) => dialog.key === SSO_REQUIRED_DIALOG_KEY
)
}

describe('the SSO-required notice on an auth page', () => {
afterEach(() => {
useDialogStore().closeDialog({ key: SSO_REQUIRED_DIALOG_KEY })
})

it('shows on the page instead of the shared dialog, keeping what each caller knew', async () => {
vi.mocked(useFeatureFlags().flags).ssoEnabled = true
render(AuthPage)

expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(true)
expect(presentSsoRequired({ organizationId: 'org_meta' })).toBe(true)

expect(
await screen.findByText(
JSON.stringify({ email: 'ada@acme.com', organizationId: 'org_meta' })
)
).toBeInTheDocument()
expect(await dialogShown()).toBe(false)
})

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(await dialogShown()).toBe(true)
})

it('stays hidden with the flag off', async () => {
vi.mocked(useFeatureFlags().flags).ssoEnabled = false
render(AuthPage)

expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(false)
expect(screen.getByText('null')).toBeInTheDocument()
})
})
26 changes: 26 additions & 0 deletions src/platform/auth/sso/ssoRequiredInline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { onUnmounted, readonly, shallowRef } from 'vue'

import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired'

let hosts = 0
const notice = shallowRef<SsoRequiredContext>()

/**
* 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
notice.value = { ...notice.value, ...context }
return true
}

/** Makes the calling auth page the place an SSO refusal is shown. */
export function useSsoRequiredInlineHost() {
hosts++
onUnmounted(() => {
hosts--
notice.value = undefined
})
return { notice: readonly(notice) }
}
56 changes: 56 additions & 0 deletions src/platform/auth/sso/useContinueWithSso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'

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 { SSO_ENTRY_OPEN_QUERY } from '@/platform/cloud/onboarding/sso/ssoEntryQuery'
import { useAuthStore } from '@/stores/authStore'

/** Signs out of Firebase and leaves for the refusing organization's SSO. */
export function useContinueWithSso(context: () => SsoRequiredContext) {
const router = useRouter()
const authStore = useAuthStore()
const { toastErrorHandler } = useErrorHandling()
const knownEmail = computed(() => context().email ?? authStore.userEmail)
const leaving = ref(false)

function destination(): string {
const { returnTo, organizationId } = context()
const back = {
returnTo: toSsoReturnPath(returnTo ?? router.currentRoute.value.fullPath),
origin: window.location.origin
}
if (organizationId) {
return ssoStartUrl({
organizationId,
email: knownEmail.value ?? undefined,
...back
})
}
if (!knownEmail.value) {
return router.resolve({
name: 'cloud-login',
query: SSO_ENTRY_OPEN_QUERY
}).href
}
return ssoStartUrl({ email: knownEmail.value, ...back })
}

async function continueWithSso() {
leaving.value = true
const target = destination()
try {
if (authStore.currentUser) await authStore.logout()
} catch (error) {
leaving.value = false
toastErrorHandler(error)
return
}
window.location.assign(target)
}

return { knownEmail, leaving, continueWithSso }
}
13 changes: 9 additions & 4 deletions src/platform/cloud/onboarding/CloudLoginView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ describe('CloudLoginView Firebase sign-in refused for SSO', () => {

it.for([
{
name: 'signs the account out and shows the SSO screen',
name: 'signs the account out and shows the SSO notice on the page',
url: '/cloud/login',
returnTo: '/cloud/user-check'
},
Expand Down Expand Up @@ -604,9 +604,14 @@ describe('CloudLoginView Firebase sign-in refused for SSO', () => {
vi.mocked(presentSsoRequired).mock.results[0].value,
'shown before the sign-out drops the remote config that carries sso_enabled'
).toBe(true)
await waitFor(() =>
expect(useDialogStore().isDialogOpen(SSO_REQUIRED_DIALOG_KEY)).toBe(true)
)
expect(
await screen.findByText('auth.sso.required.bodyWithEmail')
).toBeInTheDocument()
expect(
screen.getAllByRole('button', { name: 'auth.sso.continueWithSso' }),
'the notice replaces the SSO entry, so there is one way on'
).toHaveLength(1)
expect(useDialogStore().isDialogOpen(SSO_REQUIRED_DIALOG_KEY)).toBe(false)
expect(useAuthStore().logout).toHaveBeenCalledOnce()
expect(redirectAfterAuth).not.toHaveBeenCalled()
})
Expand Down
10 changes: 9 additions & 1 deletion src/platform/cloud/onboarding/CloudLoginView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
{{ t(ssoErrorKey) }}
</Message>

<CloudSsoRequiredNotice
v-if="ssoRequiredNotice"
v-bind="ssoRequiredNotice"
class="mt-4"
/>

<div class="mt-12 flex flex-col gap-4 xl:gap-6">
<template v-if="authMode !== 'email'">
<CloudSocialAuthButtons
Expand All @@ -37,7 +43,7 @@
@github="signInWithGithub"
/>

<template v-if="flags.ssoEnabled">
<template v-if="flags.ssoEnabled && !ssoRequiredNotice">
<CloudSsoSignIn
v-if="authMode === 'sso'"
:state="ssoState"
Expand Down Expand Up @@ -97,6 +103,7 @@ import { useAuthActions } from '@/composables/auth/useAuthActions'
import { useFeatureFlags } from '@/composables/useFeatureFlags'
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'
import CloudSsoSignIn from '@/platform/cloud/onboarding/components/CloudSsoSignIn.vue'
import { useCloudAuthPage } from '@/platform/cloud/onboarding/composables/useCloudAuthPage'
import { useSsoSignIn } from '@/platform/cloud/onboarding/composables/useSsoSignIn'
Expand All @@ -123,6 +130,7 @@ const freeRunsSuffix = computed(() => {
const {
authError,
authMode,
ssoRequiredNotice,
onAuthSuccess,
isSecureContext,
showGoogleSsoInAppBrowserNotice,
Expand Down
8 changes: 8 additions & 0 deletions src/platform/cloud/onboarding/CloudSignupView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
{{ t('auth.login.insecureContextWarning') }}
</Message>

<CloudSsoRequiredNotice
v-if="ssoRequiredNotice"
v-bind="ssoRequiredNotice"
class="mt-4"
/>

<div class="mt-12 flex flex-col gap-4 xl:gap-6">
<template v-if="authMode !== 'email'">
<CloudSocialAuthButtons
Expand Down Expand Up @@ -98,6 +104,7 @@ import Skeleton from '@/components/ui/skeleton/Skeleton.vue'
import { useAuthActions } from '@/composables/auth/useAuthActions'
import { useFeatureFlags } from '@/composables/useFeatureFlags'
import CloudSocialAuthButtons from '@/platform/cloud/onboarding/components/CloudSocialAuthButtons.vue'
import CloudSsoRequiredNotice from '@/platform/cloud/onboarding/components/CloudSsoRequiredNotice.vue'
import { useCloudAuthPage } from '@/platform/cloud/onboarding/composables/useCloudAuthPage'
import { useFreeTierOnboarding } from '@/platform/cloud/onboarding/composables/useFreeTierOnboarding'
import { useSsoSignIn } from '@/platform/cloud/onboarding/composables/useSsoSignIn'
Expand All @@ -121,6 +128,7 @@ const { isFreeTierEnabled } = useFreeTierOnboarding()
const {
authError,
authMode,
ssoRequiredNotice,
onAuthSuccess,
isSecureContext,
showGoogleSsoInAppBrowserNotice,
Expand Down
Loading
Loading