From 53d307165483ab8e0bf997e858c4ae810dc2a2ca Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:14:01 +0000 Subject: [PATCH 1/2] feat(auth,web): auto-seed founderInboxOwner from authenticated session founder --- apps/server/src/auth/CloudflareAccessAuth.ts | 10 +++++- apps/server/src/auth/EnvironmentAuth.ts | 3 ++ apps/server/src/auth/SessionStore.ts | 3 ++ apps/server/src/auth/http.ts | 1 + apps/web/src/routes/__root.tsx | 2 ++ apps/web/src/state/founderSessionSeed.tsx | 38 ++++++++++++++++++++ packages/contracts/src/auth.ts | 2 ++ 7 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 apps/web/src/state/founderSessionSeed.tsx diff --git a/apps/server/src/auth/CloudflareAccessAuth.ts b/apps/server/src/auth/CloudflareAccessAuth.ts index 4ac4808910b8c..862c05615f246 100644 --- a/apps/server/src/auth/CloudflareAccessAuth.ts +++ b/apps/server/src/auth/CloudflareAccessAuth.ts @@ -137,13 +137,17 @@ export const verifyCloudflareAccessJwt = Effect.fn("CloudflareAccessAuth.verifyJ return yield* denied(); }); +export interface CloudflareAccessMintedSession extends SessionStore.IssuedSession { + readonly founder: Founder; +} + export class CloudflareAccessAuth extends Context.Service< CloudflareAccessAuth, { readonly config: CloudflareAccessConfig | null; readonly tryAutoMintBrowserSession: ( request: HttpServerRequest.HttpServerRequest, - ) => Effect.Effect; + ) => Effect.Effect; } >()("t3/auth/CloudflareAccessAuth") {} @@ -194,6 +198,10 @@ const make = Effect.fn("CloudflareAccessAuth.make")(function* (options?: { scopes: AuthStandardClientScopes, }) .pipe( + Effect.map((issued): CloudflareAccessMintedSession => ({ + ...issued, + founder: identity.founder, + })), Effect.catch((cause) => Effect.logWarning("Failed to issue T3 browser session from Cloudflare Access JWT.", { cause, diff --git a/apps/server/src/auth/EnvironmentAuth.ts b/apps/server/src/auth/EnvironmentAuth.ts index 5e09be1195d29..0f5b36f4ee531 100644 --- a/apps/server/src/auth/EnvironmentAuth.ts +++ b/apps/server/src/auth/EnvironmentAuth.ts @@ -126,6 +126,7 @@ export interface AuthenticatedSession { readonly subject: string; readonly method: ServerAuthSessionMethod; readonly scopes: ReadonlyArray; + readonly founder?: Founder; readonly proofKeyThumbprint?: string; readonly expiresAt?: DateTime.DateTime; } @@ -1003,6 +1004,7 @@ export const make = Effect.gen(function* () { subject: session.subject, method: session.method, scopes: session.scopes, + ...(session.founder ? { founder: session.founder } : {}), ...(session.proofKeyThumbprint ? { proofKeyThumbprint: session.proofKeyThumbprint } : {}), ...(session.expiresAt ? { expiresAt: session.expiresAt } : {}), })), @@ -1097,6 +1099,7 @@ export const make = Effect.gen(function* () { ...authScopeResponse(session.scopes), sessionMethod: session.method, sessionId: session.sessionId, + ...(session.founder ? { founder: session.founder } : {}), ...(session.expiresAt ? { expiresAt: DateTime.toUtc(session.expiresAt) } : {}), }) satisfies AuthSessionState, ), diff --git a/apps/server/src/auth/SessionStore.ts b/apps/server/src/auth/SessionStore.ts index 51d3bed4c3151..5cb5e80400500 100644 --- a/apps/server/src/auth/SessionStore.ts +++ b/apps/server/src/auth/SessionStore.ts @@ -62,6 +62,7 @@ export interface VerifiedSession { readonly expiresAt?: DateTime.DateTime; readonly subject: string; readonly scopes: ReadonlyArray; + readonly founder?: Founder; readonly proofKeyThumbprint?: string; readonly runtimeModeCeiling?: RuntimeMode; /** An MCP client that is another T3 Code environment's peer link. */ @@ -963,6 +964,7 @@ const make = Effect.gen(function* () { expiresAt: row.value.expiresAt, subject: row.value.subject, scopes: row.value.scopes, + ...(row.value.founder ? { founder: row.value.founder } : {}), } satisfies VerifiedSession; } const [encodedPayload, signature] = token.split("."); @@ -1032,6 +1034,7 @@ const make = Effect.gen(function* () { expiresAt: rowHoldsDeadline ? row.value.expiresAt : expiresAt.value, subject: claims.sub, scopes: row.value.scopes, + ...(row.value.founder ? { founder: row.value.founder } : {}), ...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}), ...(claims.rtc ? { runtimeModeCeiling: claims.rtc } : {}), ...(claims.plk === 1 ? { peerLink: true as const } : {}), diff --git a/apps/server/src/auth/http.ts b/apps/server/src/auth/http.ts index 481c9a4863e6b..a368c313ffb84 100644 --- a/apps/server/src/auth/http.ts +++ b/apps/server/src/auth/http.ts @@ -339,6 +339,7 @@ export const layer = HttpApiBuilder.group( ...authScopeResponse(minted.scopes), sessionMethod: minted.method, sessionId: minted.sessionId, + founder: minted.founder, expiresAt: DateTime.toUtc(minted.expiresAt), } satisfies AuthSessionState; } diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index 109d1c24c34be..971f54f68e3cd 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -66,6 +66,7 @@ import { shouldResumeSnapShotSetupOnStartup } from "../lib/snapShotSetupResume"; import { AppConnectionReporter, LinkOnConnectCoordinator } from "../state/peerLinks"; import { AutoAddLinkedEnvironments } from "../state/knownEnvironments"; import { LinkedSidebarFromHosts } from "../state/linkedSidebarHost"; +import { AutoSeedFounderInboxOwner } from "../state/founderSessionSeed"; export const Route = createRootRoute({ beforeLoad: async ({ location }) => { @@ -214,6 +215,7 @@ function RootRouteBranch() { + {import.meta.env.DEV && primaryEnvironmentAuthenticated ? : null} diff --git a/apps/web/src/state/founderSessionSeed.tsx b/apps/web/src/state/founderSessionSeed.tsx new file mode 100644 index 0000000000000..46ec615fa6226 --- /dev/null +++ b/apps/web/src/state/founderSessionSeed.tsx @@ -0,0 +1,38 @@ +import { useAtomValue } from "@effect/atom-react"; +import type { Founder } from "@t3tools/contracts"; +import { AsyncResult } from "effect/reactivity"; +import { useEffect } from "react"; + +import { updateClientSettings, useClientSettings } from "../hooks/useSettings"; +import { environmentIdsKey } from "./environmentAtomList"; +import { environmentSummaries } from "./presentation"; +import { environmentSessionStatesAtom } from "./session"; + +/** + * Seeds `founderInboxOwner` once on fresh clients (such as a mobile browser authenticated via + * Cloudflare Access) from the authenticated session's `founder`. Never overwrites an explicit + * setting already chosen by the user. + */ +export function AutoSeedFounderInboxOwner() { + const founderInboxOwner = useClientSettings((settings) => settings.founderInboxOwner); + const identities = useAtomValue(environmentSummaries.identitiesAtom); + const states = useAtomValue( + environmentSessionStatesAtom( + environmentIdsKey(identities.map((identity) => identity.environmentId)), + ), + ); + let sessionFounder: Founder | undefined; + for (const state of states) { + const session = AsyncResult.getOrElse(state, () => null); + if (session?.authenticated && session.founder !== undefined) { + sessionFounder = session.founder; + break; + } + } + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- seeds founderInboxOwner once when unset from the authenticated session + useEffect(() => { + if (founderInboxOwner !== undefined || sessionFounder === undefined) return; + void updateClientSettings({ founderInboxOwner: sessionFounder }); + }, [founderInboxOwner, sessionFounder]); + return null; +} diff --git a/packages/contracts/src/auth.ts b/packages/contracts/src/auth.ts index 281c0440b09a7..736a9e5f64c91 100644 --- a/packages/contracts/src/auth.ts +++ b/packages/contracts/src/auth.ts @@ -559,6 +559,8 @@ export const AuthSessionState = Schema.Struct({ sessionMethod: Schema.optionalKey(ServerAuthSessionMethod), /** This client's own session, so it can tell what it did from what another client did. */ sessionId: Schema.optionalKey(AuthSessionId), + /** Founder bound to this session (e.g. Cloudflare Access or founder-paired client), when known. */ + founder: Schema.optionalKey(Founder), expiresAt: Schema.optionalKey(Schema.DateTimeUtc), }); export type AuthSessionState = typeof AuthSessionState.Type; From e6c38ca8449dd09a2b76ec8b6504a50536f9e1b9 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:45:25 +0000 Subject: [PATCH 2/2] fix(auth,web): gate founderInboxOwner auto-seed on settings hydration, prefer primary env, assert founder in auth test --- .../src/auth/CloudflareAccessAuth.test.ts | 4 +++ apps/web/src/state/founderSessionSeed.tsx | 27 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/apps/server/src/auth/CloudflareAccessAuth.test.ts b/apps/server/src/auth/CloudflareAccessAuth.test.ts index 712d293aacff8..c1f1d1f813fc9 100644 --- a/apps/server/src/auth/CloudflareAccessAuth.test.ts +++ b/apps/server/src/auth/CloudflareAccessAuth.test.ts @@ -281,9 +281,11 @@ it.live( authenticated: boolean; sessionMethod?: string; sessionId?: string; + founder?: string; }; expect(firstBody.authenticated).toBe(true); expect(firstBody.sessionMethod).toBe("browser-session-cookie"); + expect(firstBody.founder).toBe("kameron"); const setCookie = first.headers.get("set-cookie"); expect(setCookie).toBeTruthy(); expect(setCookie).toContain("HttpOnly"); @@ -308,9 +310,11 @@ it.live( const secondBody = (await second.json()) as { authenticated: boolean; sessionId?: string; + founder?: string; }; expect(secondBody.authenticated).toBe(true); expect(secondBody.sessionId).toBe(firstBody.sessionId); + expect(secondBody.founder).toBe("kameron"); expect(second.headers.get("set-cookie")).toBeNull(); expect(jwksFetches).toBe(1); diff --git a/apps/web/src/state/founderSessionSeed.tsx b/apps/web/src/state/founderSessionSeed.tsx index 46ec615fa6226..c06de63d46250 100644 --- a/apps/web/src/state/founderSessionSeed.tsx +++ b/apps/web/src/state/founderSessionSeed.tsx @@ -1,19 +1,24 @@ import { useAtomValue } from "@effect/atom-react"; -import type { Founder } from "@t3tools/contracts"; +import { PRIMARY_LOCAL_ENVIRONMENT_ID, type Founder } from "@t3tools/contracts"; import { AsyncResult } from "effect/reactivity"; import { useEffect } from "react"; -import { updateClientSettings, useClientSettings } from "../hooks/useSettings"; +import { + updateClientSettings, + useClientSettings, + useClientSettingsHydrated, +} from "../hooks/useSettings"; import { environmentIdsKey } from "./environmentAtomList"; import { environmentSummaries } from "./presentation"; import { environmentSessionStatesAtom } from "./session"; /** * Seeds `founderInboxOwner` once on fresh clients (such as a mobile browser authenticated via - * Cloudflare Access) from the authenticated session's `founder`. Never overwrites an explicit - * setting already chosen by the user. + * Cloudflare Access) from the authenticated session's `founder`. Waits for client settings to + * hydrate first so an explicit saved choice is never overwritten. */ export function AutoSeedFounderInboxOwner() { + const hydrated = useClientSettingsHydrated(); const founderInboxOwner = useClientSettings((settings) => settings.founderInboxOwner); const identities = useAtomValue(environmentSummaries.identitiesAtom); const states = useAtomValue( @@ -22,17 +27,19 @@ export function AutoSeedFounderInboxOwner() { ), ); let sessionFounder: Founder | undefined; - for (const state of states) { - const session = AsyncResult.getOrElse(state, () => null); - if (session?.authenticated && session.founder !== undefined) { + for (let i = 0; i < states.length; i++) { + const session = AsyncResult.getOrElse(states[i]!, () => null); + if (!session?.authenticated || session.founder === undefined) continue; + if (identities[i]?.environmentId === PRIMARY_LOCAL_ENVIRONMENT_ID) { sessionFounder = session.founder; break; } + sessionFounder ??= session.founder; } - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- seeds founderInboxOwner once when unset from the authenticated session + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- seeds founderInboxOwner once when unset after settings hydrate useEffect(() => { - if (founderInboxOwner !== undefined || sessionFounder === undefined) return; + if (!hydrated || founderInboxOwner !== undefined || sessionFounder === undefined) return; void updateClientSettings({ founderInboxOwner: sessionFounder }); - }, [founderInboxOwner, sessionFounder]); + }, [hydrated, founderInboxOwner, sessionFounder]); return null; }