Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export interface ComfyDesktop2AuthBridge {
* is scoped to; null when signed out, disabled, or the scope is missing,
* malformed or different. The refresh token never leaves Desktop. */
getWorkspaceToken(workspaceId: string): Promise<string | null>
getPartnerNodeToken?(workspaceId: string): Promise<string | null>
/** Runs Desktop's browser sign-in and resolves with the resulting state. */
requestSignIn(): Promise<ComfyDesktop2AuthState>
/** Signs Desktop out of its account (every view follows) and resolves with
Expand Down
2 changes: 1 addition & 1 deletion packages/comfyui-desktop-bridge-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comfyorg/comfyui-desktop-bridge-types",
"version": "0.5.0",
"version": "0.6.0",
"description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge",
"type": "module",
"main": "./index.js",
Expand Down
1 change: 1 addition & 0 deletions src/main/auth/desktopLoginCode/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class DesktopLoginCodeError extends Error {
}

export interface CreateDesktopLoginCodeRequest {
session_type?: 'account'
/** Telemetry machine hash; included only when consent is granted. */
installation_id?: string
platform: string
Expand Down
194 changes: 194 additions & 0 deletions src/main/cloud/accountSession.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

const mocks = vi.hoisted(() => ({ fetch: vi.fn(), open: vi.fn() }))
vi.mock('electron', () => ({
app: { getVersion: () => '1.0.0' },
net: { fetch: mocks.fetch },
shell: { openExternal: mocks.open }
}))

import { mintWorkspaceToken, revokeAccountSession, signInAccount } from './accountSession'

const ROOT = 'comfy-session-secret'
const CODE = `dls_${'a'.repeat(43)}`
const EXPIRES = '2030-01-01T00:00:00.000Z'

function respond(data: unknown, status = 200): Response {
return Response.json(data, { status })
}

beforeEach(() => {
vi.useFakeTimers()
mocks.fetch.mockReset()
mocks.open.mockReset().mockResolvedValue(undefined)
})

afterEach(() => {
vi.useRealTimers()
vi.restoreAllMocks()
})

describe('native account login', () => {
it('hands off a proof-bound code and keeps the account credential out of the browser URL', async () => {
mocks.fetch
.mockResolvedValueOnce(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
.mockResolvedValueOnce(respond({ status: 'pending' }))
.mockResolvedValueOnce(respond({ status: 'complete', session_token: ROOT }))
.mockResolvedValueOnce(respond({ token: 'workspace-token', expires_at: EXPIRES }))
const onUrl = vi.fn()
const login = signInAccount(onUrl, new AbortController().signal)
await vi.advanceTimersByTimeAsync(2000)
await expect(login).resolves.toEqual({
accessToken: 'workspace-token',
sessionToken: ROOT,
expiresAt: Date.parse(EXPIRES)
})
const url = new URL(onUrl.mock.calls[0]![0])
expect(url.searchParams.get('desktop_login_code')).toBe(CODE)
expect(url.href).not.toContain(ROOT)
const createBody = JSON.parse(mocks.fetch.mock.calls[0]![1].body)
expect(createBody).toMatchObject({ session_type: 'account' })
const exchangeBody = JSON.parse(mocks.fetch.mock.calls[1]![1].body)
expect(exchangeBody.code_verifier).not.toBe(createBody.code_challenge)
expect(exchangeBody.code).toBe(CODE)
expect(mocks.fetch.mock.calls[3]![1]).toMatchObject({
credentials: 'omit',
redirect: 'error',
headers: { Authorization: `Bearer ${ROOT}` },
body: '{}'
})
})

it('completes login when the browser launch promise never settles', async () => {
mocks.open.mockReturnValue(new Promise(() => {}))
mocks.fetch
.mockResolvedValueOnce(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
.mockResolvedValueOnce(respond({ status: 'complete', session_token: ROOT }))
.mockResolvedValueOnce(respond({ token: 'workspace-token', expires_at: EXPIRES }))
const login = signInAccount(vi.fn(), new AbortController().signal)
await vi.advanceTimersByTimeAsync(1000)
await expect(login).resolves.toMatchObject({ sessionToken: ROOT })
})

it('stops polling when opening the browser fails', async () => {
mocks.open.mockRejectedValue(new Error('browser handler failed'))
mocks.fetch.mockResolvedValue(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
await expect(signInAccount(vi.fn(), new AbortController().signal)).rejects.toMatchObject({
reason: 'browser_unavailable'
})
await vi.advanceTimersByTimeAsync(2000)
expect(mocks.fetch).toHaveBeenCalledOnce()
})

it('refuses an older server before opening a Firebase-only handoff', async () => {
mocks.fetch.mockResolvedValue(
respond({ code: 'dlc_old', expires_in: 300, poll_interval: 1 }, 201)
)
await expect(signInAccount(vi.fn(), new AbortController().signal)).rejects.toMatchObject({
reason: 'server_error'
})
expect(mocks.open).not.toHaveBeenCalled()
})

it('cancels even when the system browser never reports that it opened', async () => {
mocks.fetch.mockResolvedValue(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
mocks.open.mockReturnValue(new Promise(() => {}))
const abort = new AbortController()
const login = signInAccount(vi.fn(), abort.signal)
const result = expect(login).rejects.toMatchObject({ name: 'AbortError' })
await vi.advanceTimersByTimeAsync(0)
abort.abort()
await result
})

it('reports expiry while awaiting the browser without changing caller cancellation', async () => {
const expiry = new AbortController()
vi.spyOn(AbortSignal, 'timeout').mockReturnValue(expiry.signal)
mocks.fetch.mockResolvedValue(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
mocks.open.mockReturnValue(new Promise(() => {}))
const result = expect(
signInAccount(vi.fn(), new AbortController().signal)
).rejects.toMatchObject({ reason: 'timeout' })
await vi.advanceTimersByTimeAsync(0)
expiry.abort(new DOMException('expired', 'TimeoutError'))
await result
})

it('preserves a mint refusal when cleanup crosses the sign-in deadline', async () => {
const expiry = new AbortController()
vi.spyOn(AbortSignal, 'timeout').mockReturnValue(expiry.signal)
mocks.fetch
.mockResolvedValueOnce(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
.mockResolvedValueOnce(respond({ status: 'complete', session_token: ROOT }))
.mockResolvedValueOnce(respond({ code: 'FORBIDDEN' }, 403))
.mockImplementationOnce(async () => {
expiry.abort(new DOMException('expired', 'TimeoutError'))
return respond({ success: true })
})
const result = expect(
signInAccount(vi.fn(), new AbortController().signal)
).rejects.toMatchObject({ reason: 'server_error', httpStatus: 403 })
await vi.advanceTimersByTimeAsync(1000)
await result
})

it('uses one server request to revoke the account and its partner sessions', async () => {
mocks.fetch.mockResolvedValue(respond({ success: true }))
await revokeAccountSession(ROOT)
expect(mocks.fetch).toHaveBeenCalledOnce()
expect(mocks.fetch.mock.calls[0]![0]).toMatch(/\/auth\/session$/)
expect(mocks.fetch.mock.calls[0]![1].method).toBe('DELETE')
})

it('revokes a handed-off session if the first workspace request fails', async () => {
mocks.fetch
.mockResolvedValueOnce(respond({ code: CODE, expires_in: 300, poll_interval: 1 }, 201))
.mockResolvedValueOnce(respond({ status: 'complete', session_token: ROOT }))
.mockResolvedValueOnce(respond({ code: 'FORBIDDEN' }, 403))
.mockResolvedValueOnce(new Response(null, { status: 204 }))
.mockResolvedValueOnce(respond({ success: true }))
const result = expect(
signInAccount(vi.fn(), new AbortController().signal)
).rejects.toMatchObject({ httpStatus: 403 })
await vi.advanceTimersByTimeAsync(1000)
await result
expect(mocks.fetch.mock.calls[3]![0]).toMatch(/\/auth\/session$/)
expect(mocks.fetch.mock.calls[3]![1]).toMatchObject({
method: 'DELETE',
headers: { Authorization: `Bearer ${ROOT}` }
})
})
})

describe('workspace mint', () => {
it('asks the backend for the selected workspace using the account credential', async () => {
mocks.fetch.mockResolvedValue(respond({ token: 'scoped-token', expires_at: EXPIRES }))
await expect(mintWorkspaceToken(ROOT, 'w-team')).resolves.toMatchObject({
accessToken: 'scoped-token',
sessionToken: ROOT
})
expect(mocks.fetch.mock.calls[0]![1]).toMatchObject({
body: JSON.stringify({ workspace_id: 'w-team' }),
credentials: 'omit'
})
})

it.each([
() => Promise.reject(new TypeError('sensitive transport detail')),
() => Promise.resolve(new Response('sensitive non-JSON response'))
])('sanitizes transport and JSON failures', async (response) => {
mocks.fetch.mockImplementation(response)
await expect(mintWorkspaceToken(ROOT)).rejects.toMatchObject({
reason: 'server_error',
message: 'Account session request failed'
})
})

it.each([{}, { token: 'token', expires_at: 'invalid' }])(
'rejects an invalid workspace token response',
async (body) => {
mocks.fetch.mockResolvedValue(respond(body))
await expect(mintWorkspaceToken(ROOT)).rejects.toMatchObject({ reason: 'server_error' })
}
)
})
Loading
Loading