The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { AuthStandardClientScopes } from "@t3tools/contracts"; |
| 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { expect, it } from "@effect/vitest"; |
| 5 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import * as Queue from "effect/Queue"; |
| 11 | import * as TestClock from "effect/testing/TestClock"; |
| 12 | import { |
| 13 | DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS, |
| 14 | currentDesktopBootstrapToken, |
| 15 | } from "@t3tools/shared/desktopBootstrapToken"; |
| 16 | |
| 17 | import * as ServerConfig from "../config.ts"; |
| 18 | import { MCP_APPROVAL_SUBJECT_PREFIX } from "../persistence/AuthPairingLinks.ts"; |
| 19 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 20 | import * as PairingGrantStore from "./PairingGrantStore.ts"; |
| 21 | |
| 22 | const layerServerConfig = ( |
| 23 | overrides?: Partial< |
| 24 | Pick<ServerConfig.ServerConfig["Service"], "desktopBootstrapToken" | "desktopBootstrapSecret"> |
| 25 | >, |
| 26 | ) => |
| 27 | Layer.effect( |
| 28 | ServerConfig.ServerConfig, |
| 29 | Effect.gen(function* () { |
| 30 | const config = yield* ServerConfig.ServerConfig; |
| 31 | return { |
| 32 | ...config, |
| 33 | ...overrides, |
| 34 | } satisfies ServerConfig.ServerConfig["Service"]; |
| 35 | }), |
| 36 | ).pipe( |
| 37 | Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-auth-bootstrap-test-" })), |
| 38 | ); |
| 39 | |
| 40 | const layerPairingGrantStore = ( |
| 41 | overrides?: Partial< |
| 42 | Pick<ServerConfig.ServerConfig["Service"], "desktopBootstrapToken" | "desktopBootstrapSecret"> |
| 43 | >, |
| 44 | ) => |
| 45 | PairingGrantStore.layer.pipe( |
| 46 | Layer.provide(SqlitePersistence.layerMemory), |
| 47 | Layer.provide(layerServerConfig(overrides)), |
| 48 | ); |
| 49 | |
| 50 | it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PairingGrantStore.layer", (it) => { |
| 51 | it.effect("accepts lowercase one-time tokens exactly once", () => |
| 52 | Effect.gen(function* () { |
| 53 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 54 | const issued = yield* bootstrapCredentials.issueOneTimeToken({ label: "Julius iPhone" }); |
| 55 | const first = yield* bootstrapCredentials.consume(issued.credential.toLowerCase()); |
| 56 | const second = yield* Effect.flip(bootstrapCredentials.consume(issued.credential)); |
| 57 | |
| 58 | expect(first.method).toBe("one-time-token"); |
| 59 | expect(first.scopes).toEqual(AuthStandardClientScopes); |
| 60 | expect(first.subject).toBe("one-time-token"); |
| 61 | expect(first.label).toBe("Julius iPhone"); |
| 62 | expect(issued.label).toBe("Julius iPhone"); |
| 63 | expect(second._tag).toBe("UnknownBootstrapCredentialError"); |
| 64 | expect(second.message).toContain("Unknown bootstrap credential"); |
| 65 | }).pipe( |
| 66 | Effect.provide(layerPairingGrantStore()), |
| 67 | Effect.provideService( |
| 68 | SecureRandom.SecureRandom, |
| 69 | SecureRandom.make((size) => new Uint8Array(size).fill(8)), |
| 70 | ), |
| 71 | ), |
| 72 | ); |
| 73 | |
| 74 | it.effect("accepts a one-time token typed in hyphen- or space-separated groups", () => |
| 75 | Effect.gen(function* () { |
| 76 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 77 | const issued = yield* bootstrapCredentials.issueOneTimeToken(); |
| 78 | const formatted = issued.credential.match(/.{1,4}/g)!.join("-"); |
| 79 | const consumed = yield* bootstrapCredentials.consume(formatted.toLowerCase()); |
| 80 | const second = yield* bootstrapCredentials.issueOneTimeToken(); |
| 81 | const spaced = yield* bootstrapCredentials.consume( |
| 82 | second.credential.match(/.{1,4}/g)!.join(" "), |
| 83 | ); |
| 84 | |
| 85 | expect(consumed.method).toBe("one-time-token"); |
| 86 | expect(spaced.method).toBe("one-time-token"); |
| 87 | }).pipe(Effect.provide(layerPairingGrantStore())), |
| 88 | ); |
| 89 | |
| 90 | it.effect("atomically consumes a one-time token when multiple requests race", () => |
| 91 | Effect.gen(function* () { |
| 92 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 93 | const token = yield* bootstrapCredentials.issueOneTimeToken(); |
| 94 | const results = yield* Effect.all( |
| 95 | Array.from({ length: 8 }, () => |
| 96 | Effect.result( |
| 97 | bootstrapCredentials.consume(token.credential, { |
| 98 | requestedScopes: ["orchestration:read"], |
| 99 | }), |
| 100 | ), |
| 101 | ), |
| 102 | { |
| 103 | concurrency: "unbounded", |
| 104 | }, |
| 105 | ); |
| 106 | |
| 107 | const successes = results.filter((result) => result._tag === "Success"); |
| 108 | const failures = results.filter((result) => result._tag === "Failure"); |
| 109 | |
| 110 | expect(successes).toHaveLength(1); |
| 111 | expect(failures).toHaveLength(7); |
| 112 | for (const failure of failures) { |
| 113 | expect(failure.failure._tag).toBe("UnknownBootstrapCredentialError"); |
| 114 | expect(failure.failure.message).toContain("Unknown bootstrap credential"); |
| 115 | } |
| 116 | }).pipe(Effect.provide(layerPairingGrantStore())), |
| 117 | ); |
| 118 | |
| 119 | it.effect("requires the bound proof key thumbprint when present", () => |
| 120 | Effect.gen(function* () { |
| 121 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 122 | const token = yield* bootstrapCredentials.issueOneTimeToken({ |
| 123 | proofKeyThumbprint: "client-proof-key-thumbprint", |
| 124 | scopes: ["orchestration:read"], |
| 125 | }); |
| 126 | |
| 127 | const missing = yield* Effect.flip(bootstrapCredentials.consume(token.credential)); |
| 128 | const wrong = yield* Effect.flip( |
| 129 | bootstrapCredentials.consume(token.credential, { |
| 130 | proofKeyThumbprint: "other-proof-key-thumbprint", |
| 131 | requestedScopes: ["access:write"], |
| 132 | }), |
| 133 | ); |
| 134 | const forbiddenScope = yield* Effect.flip( |
| 135 | bootstrapCredentials.consume(token.credential, { |
| 136 | proofKeyThumbprint: "client-proof-key-thumbprint", |
| 137 | requestedScopes: ["access:write"], |
| 138 | }), |
| 139 | ); |
| 140 | const consumed = yield* bootstrapCredentials.consume(token.credential, { |
| 141 | proofKeyThumbprint: "client-proof-key-thumbprint", |
| 142 | requestedScopes: ["orchestration:read"], |
| 143 | }); |
| 144 | |
| 145 | expect(missing.message).toContain("proof key mismatch"); |
| 146 | expect(wrong.message).toContain("proof key mismatch"); |
| 147 | expect(forbiddenScope._tag).toBe("BootstrapCredentialScopeNotGrantedError"); |
| 148 | expect(consumed.proofKeyThumbprint).toBe("client-proof-key-thumbprint"); |
| 149 | }).pipe(Effect.provide(layerPairingGrantStore())), |
| 150 | ); |
| 151 | |
| 152 | it.effect("matches a seeded desktop bootstrap credential exactly", () => |
| 153 | Effect.gen(function* () { |
| 154 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 155 | const exact = yield* bootstrapCredentials.consume("abcdefghjkmn"); |
| 156 | const otherCase = yield* Effect.flip(bootstrapCredentials.consume("ABCDEFGHJKMN")); |
| 157 | |
| 158 | expect(exact.method).toBe("desktop-bootstrap"); |
| 159 | expect(otherCase._tag).toBe("UnknownBootstrapCredentialError"); |
| 160 | }).pipe( |
| 161 | Effect.provide( |
| 162 | layerPairingGrantStore({ |
| 163 | desktopBootstrapToken: "abcdefghjkmn", |
| 164 | }), |
| 165 | ), |
| 166 | ), |
| 167 | ); |
| 168 | |
| 169 | it.effect("reports seeded desktop bootstrap credentials as expired after their ttl", () => |
| 170 | Effect.gen(function* () { |
| 171 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 172 | |
| 173 | yield* TestClock.adjust(Duration.hours(12)); |
| 174 | const stillValid = yield* bootstrapCredentials.consume("desktop-bootstrap-token"); |
| 175 | expect(stillValid.method).toBe("desktop-bootstrap"); |
| 176 | |
| 177 | yield* TestClock.adjust(Duration.hours(13)); |
| 178 | const expired = yield* Effect.flip(bootstrapCredentials.consume("desktop-bootstrap-token")); |
| 179 | |
| 180 | expect(expired._tag).toBe("ExpiredBootstrapCredentialError"); |
| 181 | expect(expired.message).toContain("Bootstrap credential expired"); |
| 182 | }).pipe( |
| 183 | Effect.provide( |
| 184 | Layer.merge( |
| 185 | layerPairingGrantStore({ |
| 186 | desktopBootstrapToken: "desktop-bootstrap-token", |
| 187 | }), |
| 188 | TestClock.layer(), |
| 189 | ), |
| 190 | ), |
| 191 | ), |
| 192 | ); |
| 193 | |
| 194 | it.effect("accepts rotating desktop bootstrap tokens derived from the desktop secret", () => |
| 195 | Effect.gen(function* () { |
| 196 | const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; |
| 197 | const window = DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS; |
| 198 | |
| 199 | yield* TestClock.adjust(Duration.days(5)); |
| 200 | const now = 5 * 24 * 60 * 60 * 1000; |
| 201 | const current = yield* bootstrapCredentials.consume( |
| 202 | yield* currentDesktopBootstrapToken("desktop-secret", now).pipe( |
| 203 | Effect.provide(NodeCrypto.layerMac), |
| 204 | ), |
| 205 | ); |
| 206 | expect(current.method).toBe("desktop-bootstrap"); |
| 207 | |
| 208 | const staleToken = yield* currentDesktopBootstrapToken( |
| 209 | "desktop-secret", |
| 210 | now - 2 * window, |
| 211 | ).pipe(Effect.provide(NodeCrypto.layerMac)); |
| 212 | const stale = yield* Effect.flip(bootstrapCredentials.consume(staleToken)); |
| 213 | expect(stale._tag).toBe("UnknownBootstrapCredentialError"); |
| 214 | |
| 215 | const launch = yield* Effect.flip(bootstrapCredentials.consume("desktop-bootstrap-token")); |
| 216 | expect(launch._tag).toBe("UnknownBootstrapCredentialError"); |
| 217 | }).pipe( |
| 218 | Effect.provide( |
| 219 | Layer.merge( |
| 220 | layerPairingGrantStore({ |
| 221 | desktopBootstrapToken: "desktop-bootstrap-token", |
| 222 | desktopBootstrapSecret: "desktop-secret", |
| 223 | }), |
| 224 | TestClock.layer(), |
| 225 | ), |
| 226 | ), |
| 227 | ), |
| 228 | ); |
| 229 | |
| 230 | it.effect("keeps credentials out of pairing lists and change events", () => |
| 231 | Effect.gen(function* () { |
| 232 | const grants = yield* PairingGrantStore.PairingGrantStore; |
| 233 | const changes = yield* Queue.unbounded<PairingGrantStore.BootstrapCredentialChange>(); |
| 234 | yield* grants.streamChanges.pipe( |
| 235 | Stream.runForEach((change) => Queue.offer(changes, change)), |
| 236 | Effect.forkScoped({ startImmediately: true }), |
| 237 | ); |
| 238 | // An app approval code sends no update, issued or spent; the next belongs to a plain code. |
| 239 | const approval = yield* grants.issueOneTimeToken({ mcpApproval: "auto" }); |
| 240 | yield* grants.consume(approval.credential, { |
| 241 | allowedSubject: `${MCP_APPROVAL_SUBJECT_PREFIX}auto`, |
| 242 | }); |
| 243 | for (const input of [{}, { label: "Synthetic phone" }]) { |
| 244 | const issued = yield* grants.issueOneTimeToken(input); |
| 245 | const change = yield* Queue.take(changes); |
| 246 | expect(change?.type).toBe("pairingLinkUpserted"); |
| 247 | if (change?.type !== "pairingLinkUpserted") |
| 248 | throw new Error("Expected a pairing link update"); |
| 249 | expect(change.pairingLink.id).toBe(issued.id); |
| 250 | expect(change.pairingLink).not.toHaveProperty("credential"); |
| 251 | const listed = (yield* grants.listActive()).find((link) => link.id === issued.id); |
| 252 | expect(listed).toEqual(change.pairingLink); |
| 253 | const consumed = yield* grants.consume(issued.credential); |
| 254 | expect(consumed.scopes).toEqual(change.pairingLink.scopes); |
| 255 | expect(yield* Queue.take(changes)).toEqual({ type: "pairingLinkRemoved", id: issued.id }); |
| 256 | } |
| 257 | }).pipe(Effect.scoped, Effect.provide(layerPairingGrantStore())), |
| 258 | ); |
| 259 | }); |