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
78 changes: 78 additions & 0 deletions browser_tests/tests/cloudSsoSignIn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Page, Request } from '@playwright/test'
import type { ErrorResponse, SsoDiscoverResponse } from '@comfyorg/ingest-types'

import enMessages from '@/locales/en/main.json' with { type: 'json' }
import type { OAuthConsentChallenge } from '@/platform/cloud/oauth/oauthApi'
import type { operations } from '@/types/comfyRegistryTypes'

import {
Expand All @@ -28,6 +29,24 @@ const APP_ROOT = new RegExp(
`^${APP_URL.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/?(\\?.*)?$`
)
const SSO_COPY = enMessages.auth.sso
const OAUTH_REQUEST_ID = '550e8400-e29b-41d4-a716-446655440000'
const CONSENT_CHALLENGE: OAuthConsentChallenge = {
oauth_request_id: OAUTH_REQUEST_ID,
csrf_token: 'csrf-token',
client_display_name: 'Comfy Desktop',
resource_display_name: 'Comfy Cloud',
redirect_uri: 'http://127.0.0.1:50632/callback',
client_application_type: 'native',
scopes: ['comfy-cloud:user:read'],
workspaces: [
{
id: 'personal-workspace',
name: 'Personal',
type: 'personal',
role: 'owner'
}
]
}
type CreateCustomerResponse =
operations['createCustomer']['responses']['201']['content']['application/json']

Expand Down Expand Up @@ -177,6 +196,65 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => {
expect(ssoStarts).toHaveLength(1)
})

test('an SSO sign-in for a pending OAuth request returns to a consent page the server serves', async ({
page
}) => {
await answerDiscover(page, 200, SSO_DISCOVERED)
await page.route('**/api/auth/sso/start**', (route) => {
const returnTo = new URL(route.request().url()).searchParams.get(
'return_to'
)
// Playwright does not route requests that follow a redirect, so the
// IdP round trip ends in a fresh navigation to return_to instead.
const target = new URL(returnTo ?? '/', APP_URL).toString()
return route.fulfill({
status: 200,
contentType: 'text/html',
body: `<script>location.replace(${JSON.stringify(target)})</script>`
})
})
// The server serves the SPA as a page only at /cloud/oauth/consent and
// answers a document load of /oauth/consent with a JSON 404.
await page.route('**/oauth/consent**', async (route) => {
const request = route.request()
if (request.resourceType() !== 'document') return route.fallback()
const path = new URL(request.url()).pathname.replace(/\/+$/, '')
if (path === '/cloud/oauth/consent') {
const app = await route.fetch({ url: `${APP_URL}/` })
return route.fulfill({ response: app })
}
return route.fulfill({
status: 404,
json: { code: 'NOT_FOUND', message: 'Not Found' }
})
})
await page.route('**/oauth/authorize?**', (route) =>
route.request().resourceType() === 'document'
? route.fallback()
: route.fulfill({ status: 200, json: CONSENT_CHALLENGE })
)

await page.goto(`${APP_URL}/?oauth_request_id=${OAUTH_REQUEST_ID}`)
await expect(page).toHaveURL(/\/cloud\/login/)
await page.getByRole('button', { name: SSO_COPY.continueWithSso }).click()
await page.locator('#cloud-sso-email').fill(SSO_EMAIL)
await page
.getByRole('button', { name: SSO_COPY.submit, exact: true })
.click()

await expect(
page.getByRole('heading', {
name: enMessages.oauth.consent.title.replace(
'{client}',
CONSENT_CHALLENGE.client_display_name
)
})
).toBeVisible()
expect(new URL(page.url()).searchParams.get('oauth_request_id')).toBe(
OAUTH_REQUEST_ID
)
})

test('an email that does not use SSO stays on the login page with the not-SSO message', async ({
page,
ssoStarts
Expand Down
35 changes: 35 additions & 0 deletions src/platform/auth/sso/ssoReturnPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest'

import { toSsoReturnPath } from '@/platform/auth/sso/ssoReturnPath'

describe('toSsoReturnPath', () => {
it.for([
{
name: 'the SPA-only consent path',
input: '/oauth/consent?oauth_request_id=req-1',
expected: '/cloud/oauth/consent?oauth_request_id=req-1'
},
{
name: 'the consent path with a trailing slash',
input: '/oauth/consent/?oauth_request_id=req-1',
expected: '/cloud/oauth/consent?oauth_request_id=req-1'
},
{
name: 'the served consent path',
input: '/cloud/oauth/consent?oauth_request_id=req-1',
expected: '/cloud/oauth/consent?oauth_request_id=req-1'
},
{
name: 'any other app path',
input: '/workflows?id=7#top',
expected: '/workflows?id=7#top'
},
{
name: 'a path that only starts like the consent path',
input: '/oauth/consent-help',
expected: '/oauth/consent-help'
}
])('returns $expected for $name', ({ input, expected }) => {
expect(toSsoReturnPath(input)).toBe(expected)
})
})
7 changes: 5 additions & 2 deletions src/platform/auth/sso/ssoReturnPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ const SERVED_CONSENT_PATH = '/cloud/oauth/consent'
/**
* The path SSO's callback can land on with a full page load. The server
* serves the consent page only at `/cloud/oauth/consent`, which the SPA then
* redirects; a direct `/oauth/consent` load gets a 404.
* redirects; a direct `/oauth/consent` load gets a 404. Remove together with
* that redirect route once BE-4146 serves `/oauth/consent`.
*/
export function toSsoReturnPath(fullPath: string): string {
const url = new URL(fullPath, 'http://sso.invalid')
if (url.pathname !== SPA_ONLY_CONSENT_PATH) return fullPath
if (url.pathname.replace(/\/+$/, '') !== SPA_ONLY_CONSENT_PATH) {
return fullPath
}
return `${SERVED_CONSENT_PATH}${url.search}${url.hash}`
}
Loading