diff --git a/client/src/pages/nativeHandoff/useNativeHandoff.test.tsx b/client/src/pages/nativeHandoff/useNativeHandoff.test.tsx index 7e9e0f4f1..4181fca5b 100644 --- a/client/src/pages/nativeHandoff/useNativeHandoff.test.tsx +++ b/client/src/pages/nativeHandoff/useNativeHandoff.test.tsx @@ -1,4 +1,4 @@ -// FE-NATIVE-HANDOFF-001 to FE-NATIVE-HANDOFF-011 +// FE-NATIVE-HANDOFF-001 to FE-NATIVE-HANDOFF-012 import React from 'react'; import { MemoryRouter } from 'react-router'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; @@ -93,6 +93,16 @@ describe('useNativeHandoff', () => { expect(rememberHandoffReturn).toHaveBeenCalledWith(`/native-handoff?challenge=${challenge}`); }); + it('FE-NATIVE-HANDOFF-012: goes through the identity provider for SSO even when this browser is still signed in', () => { + // A session left from an earlier sign-in skipped the provider, and with it the + // re-authentication it demands (#3014). + setSearch(`?challenge=${challenge}&start=oidc`); + const { result } = renderHandoff(); + expect(window.location.replace).toHaveBeenCalledWith('/api/auth/oidc/login?remember=1'); + expect(result.current.ready).toBe(false); + expect(mockNavigate).not.toHaveBeenCalled(); + }); + it('FE-NATIVE-HANDOFF-005: waits while the session is still being checked', () => { useAuthStore.setState({ isAuthenticated: false, isLoading: true }); const { result } = renderHandoff(); diff --git a/client/src/pages/nativeHandoff/useNativeHandoff.ts b/client/src/pages/nativeHandoff/useNativeHandoff.ts index 7300bbcfe..412bb5e65 100644 --- a/client/src/pages/nativeHandoff/useNativeHandoff.ts +++ b/client/src/pages/nativeHandoff/useNativeHandoff.ts @@ -35,17 +35,22 @@ export function useNativeHandoff() { // Back here once signed in, without `start`, so an SSO round trip cannot loop. const returnPath = `${HANDOFF_PATH}?challenge=${challenge}`; + // The app's SSO button always goes through the identity provider, even when this + // browser still holds a TREK session from an earlier sign-in. That session would + // otherwise skip the provider, and with it a re-authentication it demands. + const startOidc = valid && params.get('start') === 'oidc'; useEffect(() => { - if (!valid || isLoading || isAuthenticated || passkeyStep) return; - if (params.get('start') === 'oidc') { + if (!valid || isLoading || passkeyStep) return; + if (startOidc) { sessionStorage.setItem('oidc_redirect', returnPath); rememberHandoffReturn(returnPath); window.location.replace('/api/auth/oidc/login?remember=1'); return; } + if (isAuthenticated) return; void navigate(`/login?redirect=${encodeURIComponent(returnPath)}`, { replace: true }); - }, [valid, isLoading, isAuthenticated, passkeyStep, params, returnPath, navigate]); + }, [valid, isLoading, isAuthenticated, passkeyStep, startOidc, returnPath, navigate]); const openApp = async (): Promise => { setStatus('sending'); @@ -90,7 +95,7 @@ export function useNativeHandoff() { passkeyStep: valid && !isLoading && !isAuthenticated && passkeyStep, continueWithPasskey, otherSignIn, - ready: valid && !isLoading && isAuthenticated, + ready: valid && !isLoading && isAuthenticated && !startOidc, userName, status, openApp,