diff --git a/apps/server/src/auth/Layers/BootstrapCredentialService.test.ts b/apps/server/src/auth/Layers/BootstrapCredentialService.test.ts index 110dae8437..3b6ce51045 100644 --- a/apps/server/src/auth/Layers/BootstrapCredentialService.test.ts +++ b/apps/server/src/auth/Layers/BootstrapCredentialService.test.ts @@ -87,17 +87,18 @@ it.layer(NodeServices.layer)("BootstrapCredentialServiceLive", (it) => { }).pipe(Effect.provide(makeBootstrapCredentialLayer())), ); - it.effect("seeds the desktop bootstrap credential as a one-time grant", () => + it.effect("seeds the desktop bootstrap credential as a reusable grant", () => Effect.gen(function* () { const bootstrapCredentials = yield* BootstrapCredentialService; const first = yield* bootstrapCredentials.consume("desktop-bootstrap-token"); - const second = yield* Effect.flip(bootstrapCredentials.consume("desktop-bootstrap-token")); + const second = yield* bootstrapCredentials.consume("desktop-bootstrap-token"); + const third = yield* bootstrapCredentials.consume("desktop-bootstrap-token"); expect(first.method).toBe("desktop-bootstrap"); expect(first.role).toBe("owner"); expect(first.subject).toBe("desktop-bootstrap"); - expect(second._tag).toBe("BootstrapCredentialError"); - expect(second.status).toBe(401); + expect(second.method).toBe("desktop-bootstrap"); + expect(third.method).toBe("desktop-bootstrap"); }).pipe( Effect.provide( makeBootstrapCredentialLayer({ @@ -111,7 +112,13 @@ it.layer(NodeServices.layer)("BootstrapCredentialServiceLive", (it) => { Effect.gen(function* () { const bootstrapCredentials = yield* BootstrapCredentialService; - yield* TestClock.adjust(Duration.minutes(6)); + // The desktop-bootstrap grant lives for 24h. Within that window + // it stays reusable. + yield* TestClock.adjust(Duration.hours(12)); + const stillValid = yield* bootstrapCredentials.consume("desktop-bootstrap-token"); + expect(stillValid.method).toBe("desktop-bootstrap"); + + yield* TestClock.adjust(Duration.hours(13)); const expired = yield* Effect.flip(bootstrapCredentials.consume("desktop-bootstrap-token")); expect(expired._tag).toBe("BootstrapCredentialError"); diff --git a/apps/server/src/auth/Layers/BootstrapCredentialService.ts b/apps/server/src/auth/Layers/BootstrapCredentialService.ts index 77d865eca6..60e8c3498c 100644 --- a/apps/server/src/auth/Layers/BootstrapCredentialService.ts +++ b/apps/server/src/auth/Layers/BootstrapCredentialService.ts @@ -36,6 +36,15 @@ type ConsumeResult = }; const DEFAULT_ONE_TIME_TOKEN_TTL_MINUTES = Duration.minutes(5); +// The desktop-bootstrap grant rides on a trusted IPC channel (fd3 or +// stdin) at backend launch, so it doesn't have to be short-lived the +// way a user-facing pairing link does. Letting it live for the +// lifetime of the backend process (24h is more than long enough for +// practical desktop use, and well under "forever" in case the seed +// gets logged anywhere by accident) means a page reload past the 5-min +// window can still recover by re-bootstrapping rather than locking +// the user out of the backend. +const DESKTOP_BOOTSTRAP_TTL_HOURS = Duration.hours(24); const PAIRING_TOKEN_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; const PAIRING_TOKEN_LENGTH = 12; @@ -90,9 +99,14 @@ export const makeBootstrapCredentialService = Effect.gen(function* () { role: "owner", subject: "desktop-bootstrap", expiresAt: DateTime.add(now, { - milliseconds: Duration.toMillis(DEFAULT_ONE_TIME_TOKEN_TTL_MINUTES), + milliseconds: Duration.toMillis(DESKTOP_BOOTSTRAP_TTL_HOURS), }), - remainingUses: 1, + // Unbounded uses so the renderer can re-exchange the seed for a + // fresh bearer session after a page reload (or after the prior + // bearer expires). The seed itself stays inside the desktop + // process and the rendered page, both of which the user already + // implicitly trusts. + remainingUses: "unbounded", }); } diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 3ae2885f02..ddd1ffe065 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -1577,19 +1577,19 @@ it.layer(NodeServices.layer)("server router seam", (it) => { }).pipe(Effect.provide(NodeHttpServer.layerTest)), ); - it.effect("rejects reusing the same bootstrap credential after it has been exchanged", () => + it.effect("allows reusing the desktop bootstrap credential", () => Effect.gen(function* () { + // The desktop-bootstrap grant is delivered over trusted IPC at + // backend launch and needs to stay claimable after a renderer + // refresh, so it's intentionally reusable (unlike user-facing + // one-time pairing credentials). yield* buildAppUnderTest(); const first = yield* bootstrapBrowserSession(); const second = yield* bootstrapBrowserSession(); assert.equal(first.response.status, 200); - assert.equal(second.response.status, 401); - assert.equal( - (second.body as { readonly error?: string }).error, - "Invalid bootstrap credential.", - ); + assert.equal(second.response.status, 200); }).pipe(Effect.provide(NodeHttpServer.layerTest)), ); diff --git a/bun.lock b/bun.lock index ffc4a5922b..f45134363d 100644 --- a/bun.lock +++ b/bun.lock @@ -17,7 +17,7 @@ }, "apps/desktop": { "name": "@t3tools/desktop", - "version": "0.0.23", + "version": "0.0.24", "dependencies": { "@effect/platform-node": "catalog:", "@t3tools/contracts": "workspace:*", @@ -50,7 +50,7 @@ }, "apps/server": { "name": "t3", - "version": "0.0.23", + "version": "0.0.24", "bin": { "t3": "./dist/bin.mjs", }, @@ -83,7 +83,7 @@ }, "apps/web": { "name": "@t3tools/web", - "version": "0.0.23", + "version": "0.0.24", "dependencies": { "@base-ui/react": "^1.4.1", "@dnd-kit/core": "^6.3.1", @@ -165,7 +165,7 @@ }, "packages/contracts": { "name": "@t3tools/contracts", - "version": "0.0.23", + "version": "0.0.24", "dependencies": { "effect": "catalog:", },