Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): rp-008 reject setup while authenticated

Josué0fffe01750e9
diff --git a/desktop-app/src/__tests__/appService.nativeCommit.test.ts b/desktop-app/src/__tests__/appService.nativeCommit.test.ts
index 276be32e9..3331de3b8 100644
--- a/desktop-app/src/__tests__/appService.nativeCommit.test.ts
+++ b/desktop-app/src/__tests__/appService.nativeCommit.test.ts
@@ -8,6 +8,45 @@ import {
afterEach(cleanupNativeCommitTestHarness)
describe('AppService native auth and environment commit ordering', () => {
+ it('rejects desktop setup while preserving the active authenticated environment', async () => {
+ const { service, runtimeConfig, restA, restB } = await createNativeCommitTestHarness()
+ const me = { id: 'user-a', email: '[email protected]', teamId: 'team-a' }
+ service.authClient = {
+ googleLogin: vi.fn().mockResolvedValue({ token: 'synthetic-session-a', me }),
+ } as never
+ const setupRequest = vi.fn().mockResolvedValue({
+ valid: true,
+ email: '[email protected]',
+ externalRestApiBaseUrl: restB,
+ rpcProxyBaseUrl: 'https://rpc-b.example.test',
+ appName: 'Environment B',
+ })
+ const serviceInternals = service as unknown as {
+ memberRegistrationServiceClient: { completeDesktopSetup: typeof setupRequest }
+ completeDesktopSetup: (email: string, authorizationToken: string) => Promise<unknown>
+ getSessionGeneration: () => number
+ }
+ serviceInternals.memberRegistrationServiceClient = { completeDesktopSetup: setupRequest }
+
+ await service.googleLogin('synthetic-google-token')
+ const runtimeBeforeSetup = runtimeConfig.getDesktopRuntimeConfigState()
+ const generationBeforeSetup = serviceInternals.getSessionGeneration()
+ const gfsScopeBeforeSetup = service.gfsScopeIdentity
+
+ await expect(
+ serviceInternals.completeDesktopSetup('[email protected]', 'synthetic-setup-token')
+ ).rejects.toThrow('desktop_setup_requires_signout')
+
+ expect(setupRequest).not.toHaveBeenCalled()
+ expect(runtimeConfig.getDesktopRuntimeConfigState()).toEqual(runtimeBeforeSetup)
+ expect(runtimeConfig.config.externalRestApiBaseUrl).toBe(restA)
+ expect(service.sessionToken).toBe('synthetic-session-a')
+ expect(service.getCachedUserId()).toBe('user-a')
+ await expect(service.getSessionState()).resolves.toMatchObject({ authenticated: true, me })
+ expect(serviceInternals.getSessionGeneration()).toBe(generationBeforeSetup)
+ expect(service.gfsScopeIdentity).toEqual(gfsScopeBeforeSetup)
+ })
+
it('keeps a pending environment selection ahead of a login commit', async () => {
const { service, runtimeConfig, restA, restB, optionB } = await createNativeCommitTestHarness()
const loginStarted = deferred<void>()
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index 6794df453..4fa98f10b 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -2103,9 +2103,12 @@ export class AppService {
if (!normalizedEmail || !authorizationToken.trim()) {
throw new Error('email and authorization token are required')
}
- const setupGeneration = await this.withNativeAuthEnvironmentCommit(
- async () => ++this.sessionGeneration
- )
+ const setupGeneration = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (this.sessionToken && this.me) {
+ throw new Error('desktop_setup_requires_signout')
+ }
+ return ++this.sessionGeneration
+ })
let setupRequest:
| ReturnType<MemberRegistrationServiceClient['completeDesktopSetup']>
| undefined
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx
index 805bb303c..adb187346 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx
@@ -9,6 +9,7 @@ import { useAuthController } from '../useAuthController'
const mocks = vi.hoisted(() => ({
clearQueryCache: vi.fn(),
+ completeDesktopSetup: vi.fn(),
getRuntimeConfigState: vi.fn(),
getSessionGeneration: vi.fn(),
logoutForEnvironmentMismatch: vi.fn(),
@@ -42,10 +43,14 @@ type DesktopEnvironmentSetupPayload = {
externalRestApiBaseUrl: string
appName?: string
}
+type DesktopSetupTokenPayload = { email: string; authorizationToken: string }
let desktopEnvironmentSetupListener:
| ((payload: DesktopEnvironmentSetupPayload) => void | Promise<void>)
| null = null
+let desktopSetupTokenListener:
+ | ((payload: DesktopSetupTokenPayload) => void | Promise<void>)
+ | null = null
let confirmDesktopEnvironmentSetupForTest: (() => Promise<void>) | null = null
let confirmDesktopEnvironmentSwitchForTest: (() => void) | null = null
let cancelDesktopEnvironmentSwitchForTest: (() => void) | null = null
@@ -109,6 +114,7 @@ async function savedTargetOptionId(): Promise<string> {
beforeEach(async () => {
vi.clearAllMocks()
desktopEnvironmentSetupListener = null
+ desktopSetupTokenListener = null
confirmDesktopEnvironmentSetupForTest = null
confirmDesktopEnvironmentSwitchForTest = null
cancelDesktopEnvironmentSwitchForTest = null
@@ -163,7 +169,10 @@ beforeEach(async () => {
nativeSessionGeneration += 1
return nativeSessionGeneration
})
- mocks.onDesktopSetupToken.mockReturnValue(() => undefined)
+ mocks.onDesktopSetupToken.mockImplementation(listener => {
+ desktopSetupTokenListener = listener
+ return () => undefined
+ })
mocks.onExternalLogout.mockReturnValue(() => undefined)
mocks.onDesktopEnvironmentSetup.mockImplementation(
(listener: (payload: DesktopEnvironmentSetupPayload) => void | Promise<void>) => {
@@ -182,6 +191,7 @@ beforeEach(async () => {
selectRuntimeConfigForHandoff: mocks.selectRuntimeConfigForHandoff,
onDesktopEnvironmentSetup: mocks.onDesktopEnvironmentSetup,
onDesktopSetupToken: mocks.onDesktopSetupToken,
+ completeDesktopSetup: mocks.completeDesktopSetup,
onExternalLogout: mocks.onExternalLogout,
},
}
@@ -197,6 +207,26 @@ afterEach(async () => {
})
describe('Desktop environment handoff', () => {
+ it('asks the user to sign out when native desktop setup rejects an active session', async () => {
+ mocks.completeDesktopSetup.mockRejectedValue(new Error('desktop_setup_requires_signout'))
+ render(<Probe />)
+
+ await waitFor(() => expect(desktopSetupTokenListener).toBeTypeOf('function'))
+ await act(async () => {
+ desktopSetupTokenListener?.({
+ email: '[email protected]',
+ authorizationToken: 'synthetic-setup-token',
+ })
+ })
+
+ await waitFor(() =>
+ expect(mocks.setStatus).toHaveBeenCalledWith(
+ 'Sign out before setting up another desktop environment.',
+ 'error'
+ )
+ )
+ })
+
it('switches to a saved environment matching its REST endpoint', async () => {
render(<Probe />)
await waitFor(() => expect(screen.getByTestId('configuration-loaded')).toHaveTextContent('yes'))
diff --git a/desktop-app/ui/src/hooks/domain/useAuthController.ts b/desktop-app/ui/src/hooks/domain/useAuthController.ts
index 51bbcf19f..2fee993fc 100644
--- a/desktop-app/ui/src/hooks/domain/useAuthController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAuthController.ts
@@ -43,6 +43,14 @@ function isUnauthorizedError(error: unknown) {
return /\b401\s+unauthorized\b/.test(message) || /:\s*401\s/.test(message)
}
+function getDesktopSetupErrorMessage(error: unknown) {
+ const message = error instanceof Error ? error.message : String(error)
+ if (message === 'desktop_setup_requires_signout') {
+ return 'Sign out before setting up another desktop environment.'
+ }
+ return 'Desktop setup could not be completed.'
+}
+
export function useAuthController({
setStatus,
onSessionNeedsLoad,
@@ -190,8 +198,8 @@ export function useAuthController({
setBusy(true)
setAuthTransitioning(true)
completeDesktopSetupWith(normalizedEmail, authorizationToken)
- .catch(() => {
- setStatus('Desktop setup could not be completed.', 'error')
+ .catch(error => {
+ setStatus(getDesktopSetupErrorMessage(error), 'error')
})
.finally(() => {
setAuthTransitioning(false)
@@ -294,8 +302,8 @@ export function useAuthController({
setBusy(true)
setAuthTransitioning(true)
await completeDesktopSetupWith(email, desktopSetupAuthorizationToken)
- } catch {
- setStatus('Desktop setup could not be completed.', 'error')
+ } catch (error) {
+ setStatus(getDesktopSetupErrorMessage(error), 'error')
} finally {
setAuthTransitioning(false)
setBusy(false)