Line data Source code
1 : import { omitBy } from 'es-toolkit'
2 :
3 : import type { SsoRequiredTrigger } from '@comfyorg/account-core/telemetry'
4 : import {
5 : isAccountOutsideOrganizationRefusal,
6 : isSsoRequiredRefusal,
7 : ssoRequiredOrganizationId
8 : } from '@comfyorg/account-core/sso'
9 :
10 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
11 : import { t } from '@/i18n'
12 : import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequiredContext'
13 : import {
14 : ACCOUNT_OUTSIDE_ORGANIZATION_DIALOG_KEY,
15 : SSO_REQUIRED_DIALOG_KEY
16 : } from '@/platform/auth/sso/ssoRequiredDialogKey'
17 : import { presentInline } from '@/platform/auth/sso/ssoRequiredInline'
18 : import {
19 : abandonSsoFlow,
20 : trackSsoRequiredShown
21 : } from '@/platform/auth/sso/ssoTelemetry'
22 : import { reportError } from '@comfyorg/telemetry/reportError'
23 : import { useToast } from '@/components/ui/toast/toastStore'
24 : import { useDialogStore } from '@/stores/dialogStore'
25 :
26 : /**
27 : * Shows the one SSO-required screen. False while `sso_enabled` is off, so the
28 : * caller keeps the handling it has today. Several refusals can land at once;
29 : * a later caller only adds what it knows, so a request seam without context
30 : * never erases the email the sign-in page passed. A refusal always answers the
31 : * organization, so one that names none clears an earlier one.
32 : */
33 0 : export function presentSsoRequired(
34 : trigger: SsoRequiredTrigger,
35 : context: SsoRequiredContext = {}
36 : ): boolean {
37 0 : if (!useFeatureFlags().flags.ssoEnabled) return false
38 0 : const dialogStore = useDialogStore()
39 0 : const known = {
40 : ...omitBy(context, (value) => value === undefined),
41 : ...('organizationId' in context && {
42 : organizationId: context.organizationId
43 : })
44 : }
45 0 : if (presentInline(known, trigger)) return true
46 0 : void import('@/platform/auth/sso/SsoRequiredDialogContent.vue')
47 0 : .then(({ default: component }) => {
48 0 : if (dialogStore.isDialogOpen(SSO_REQUIRED_DIALOG_KEY)) {
49 0 : dialogStore.updateDialog({
50 : key: SSO_REQUIRED_DIALOG_KEY,
51 : contentProps: known
52 : })
53 0 : return
54 : }
55 0 : trackSsoRequiredShown('cloud_app', trigger, 'notice')
56 0 : dialogStore.showDialog({
57 : key: SSO_REQUIRED_DIALOG_KEY,
58 : component,
59 : props: known,
60 : dialogComponentProps: {
61 : headless: true,
62 : onClose: abandonSsoFlow
63 : }
64 : })
65 : })
66 : .catch((error: unknown) => {
67 0 : reportError(error, {
68 : surface: 'auth',
69 : errorType: 'failure_loading_sso_required_dialog'
70 : })
71 0 : useToast().error(t('auth.sso.required.title'), {
72 : description: t('auth.sso.required.body')
73 : })
74 : })
75 0 : return true
76 : }
77 :
78 : function presentAccountOutsideOrganization(): boolean {
79 : if (!useFeatureFlags().flags.ssoEnabled) return false
80 : const dialogStore = useDialogStore()
81 : if (dialogStore.isDialogOpen(ACCOUNT_OUTSIDE_ORGANIZATION_DIALOG_KEY)) {
82 : return true
83 : }
84 : void import('@/platform/auth/sso/SsoAccountOutsideOrganizationDialogContent.vue')
85 : .then(({ default: component }) => {
86 : dialogStore.showDialog({
87 : key: ACCOUNT_OUTSIDE_ORGANIZATION_DIALOG_KEY,
88 : component,
89 : dialogComponentProps: {
90 : headless: true
91 : }
92 : })
93 : })
94 : .catch((error: unknown) => {
95 : reportError(error, {
96 : surface: 'auth',
97 : errorType: 'failure_loading_sso_outside_organization_dialog'
98 : })
99 : useToast().error(t('auth.sso.outsideOrganization.title'), {
100 : description: t('auth.sso.outsideOrganization.body')
101 : })
102 : })
103 : return true
104 : }
105 :
106 : /** A Cloud API request whose credential the organization refused. */
107 : export function presentForRefusal(status: number, body: unknown): boolean {
108 : if (isAccountOutsideOrganizationRefusal(status, body)) {
109 : return presentAccountOutsideOrganization()
110 : }
111 : return (
112 : isSsoRequiredRefusal(status, body) &&
113 : presentSsoRequired('api_key', {
114 : organizationId: ssoRequiredOrganizationId(body)
115 : })
116 : )
117 : }
118 :
119 : /** Reads a clone, and only of a 403 while `sso_enabled` is on. */
120 : export async function presentForResponse(response: Response): Promise<boolean> {
121 : if (response.status !== 403 || !useFeatureFlags().flags.ssoEnabled) {
122 : return false
123 : }
124 : const body: unknown = await response
125 : .clone()
126 : .json()
127 : .catch(() => undefined)
128 : return presentForRefusal(response.status, body)
129 : }
|