|
| 1 | +import { TokenStore } from "@t3tools/client-runtime/authorization"; |
| 2 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import { describe, expect, it } from "@effect/vitest"; |
| 4 | +import * as Deferred from "effect/Deferred"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as Encoding from "effect/Encoding"; |
| 7 | +import * as Fiber from "effect/Fiber"; |
| 8 | +import * as Option from "effect/Option"; |
| 9 | +import * as Schema from "effect/Schema"; |
| 10 | +import { vi } from "vite-plus/test"; |
| 11 | + |
| 12 | +vi.mock("react-native", () => ({ |
| 13 | + Platform: { OS: "ios" }, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("expo-secure-store", () => ({ |
| 17 | + deleteItemAsync: vi.fn(), |
| 18 | + getItemAsync: vi.fn(), |
| 19 | + setItemAsync: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +import * as CatalogStore from "./catalog-store"; |
| 23 | +import { REMOTE_DPOP_TOKEN_KEY_PREFIX, make } from "./token-store"; |
| 24 | +import { MobileSecureStorage } from "../persistence/mobile-secure-storage"; |
| 25 | + |
| 26 | +const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); |
| 27 | + |
| 28 | +const TOKEN = new TokenStore.RemoteDpopAccessToken({ |
| 29 | + environmentId: ENVIRONMENT_ID, |
| 30 | + label: "Remote", |
| 31 | + endpoint: { |
| 32 | + httpBaseUrl: "https://remote.example.test", |
| 33 | + wsBaseUrl: "wss://remote.example.test", |
| 34 | + providerKind: "cloudflare_tunnel", |
| 35 | + }, |
| 36 | + accessToken: "dpop-token", |
| 37 | + expiresAtEpochMs: 1_000_000, |
| 38 | + dpopThumbprint: "thumbprint", |
| 39 | +}); |
| 40 | + |
| 41 | +const TOKEN_KEY = `${REMOTE_DPOP_TOKEN_KEY_PREFIX}${Encoding.encodeBase64Url(ENVIRONMENT_ID)}`; |
| 42 | + |
| 43 | +const encodeToken = Schema.encodeSync(TokenStore.RemoteDpopAccessToken); |
| 44 | + |
| 45 | +function makeStorage(initial: Readonly<Record<string, string>> = {}) { |
| 46 | + const values = new Map(Object.entries(initial)); |
| 47 | + const writes: Array<string> = []; |
| 48 | + const storage = MobileSecureStorage.of({ |
| 49 | + getItem: (key) => Effect.sync(() => values.get(key) ?? null), |
| 50 | + setItem: (key, value) => |
| 51 | + Effect.sync(() => { |
| 52 | + writes.push(key); |
| 53 | + values.set(key, value); |
| 54 | + }), |
| 55 | + removeItem: (key) => |
| 56 | + Effect.sync(() => { |
| 57 | + values.delete(key); |
| 58 | + }), |
| 59 | + }); |
| 60 | + return { storage, values, writes }; |
| 61 | +} |
| 62 | + |
| 63 | +const makeStores = Effect.fn("makeStores")(function* (storage: MobileSecureStorage["Service"]) { |
| 64 | + const catalog = yield* CatalogStore.make().pipe( |
| 65 | + Effect.provideService(MobileSecureStorage, storage), |
| 66 | + ); |
| 67 | + const tokens = yield* make(catalog).pipe(Effect.provideService(MobileSecureStorage, storage)); |
| 68 | + return { catalog, tokens }; |
| 69 | +}); |
| 70 | + |
| 71 | +describe("mobile remote DPoP token store", () => { |
| 72 | + it.effect("stores each token under its own key without touching the catalog", () => |
| 73 | + Effect.gen(function* () { |
| 74 | + const memory = makeStorage(); |
| 75 | + const { catalog, tokens } = yield* makeStores(memory.storage); |
| 76 | + |
| 77 | + yield* tokens.put(TOKEN); |
| 78 | + expect(memory.values.has(TOKEN_KEY)).toBe(true); |
| 79 | + expect((yield* catalog.read).remoteDpopTokens).toEqual([]); |
| 80 | + |
| 81 | + const loaded = yield* tokens.get(ENVIRONMENT_ID); |
| 82 | + expect(Option.getOrThrow(loaded)).toMatchObject({ accessToken: "dpop-token" }); |
| 83 | + |
| 84 | + yield* tokens.remove(ENVIRONMENT_ID); |
| 85 | + expect(memory.values.has(TOKEN_KEY)).toBe(false); |
| 86 | + expect(Option.isNone(yield* tokens.get(ENVIRONMENT_ID))).toBe(true); |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + it.effect("serves repeat reads from memory without re-reading the keychain", () => |
| 91 | + Effect.gen(function* () { |
| 92 | + const memory = makeStorage(); |
| 93 | + const { tokens } = yield* makeStores(memory.storage); |
| 94 | + |
| 95 | + yield* tokens.put(TOKEN); |
| 96 | + // Corrupt the persisted copy: a cached read must not notice. |
| 97 | + memory.values.set(TOKEN_KEY, "{not-json"); |
| 98 | + const loaded = yield* tokens.get(ENVIRONMENT_ID); |
| 99 | + expect(Option.getOrThrow(loaded).accessToken).toBe("dpop-token"); |
| 100 | + }), |
| 101 | + ); |
| 102 | + |
| 103 | + it.effect("migrates a token persisted inside the catalog document to its own key", () => |
| 104 | + Effect.gen(function* () { |
| 105 | + const memory = makeStorage(); |
| 106 | + const { catalog, tokens } = yield* makeStores(memory.storage); |
| 107 | + yield* catalog.update((document) => ({ |
| 108 | + ...document, |
| 109 | + remoteDpopTokens: [TOKEN], |
| 110 | + })); |
| 111 | + |
| 112 | + const loaded = yield* tokens.get(ENVIRONMENT_ID); |
| 113 | + expect(Option.getOrThrow(loaded)).toMatchObject({ accessToken: "dpop-token" }); |
| 114 | + expect(memory.values.has(TOKEN_KEY)).toBe(true); |
| 115 | + expect((yield* catalog.read).remoteDpopTokens).toEqual([]); |
| 116 | + }), |
| 117 | + ); |
| 118 | + |
| 119 | + it.effect("does not resurrect a token removed while a cold read is in flight", () => |
| 120 | + Effect.gen(function* () { |
| 121 | + const readGate = yield* Deferred.make<void>(); |
| 122 | + const readStarted = yield* Deferred.make<void>(); |
| 123 | + const memory = makeStorage(); |
| 124 | + // getItem captures the stored value immediately (like a real Keychain |
| 125 | + // read that already completed) but only delivers it after the gate, so |
| 126 | + // the remove can run while the read is in flight. |
| 127 | + const gatedStorage = MobileSecureStorage.of({ |
| 128 | + getItem: (key) => |
| 129 | + key === TOKEN_KEY |
| 130 | + ? Effect.sync(() => memory.values.get(key) ?? null).pipe( |
| 131 | + Effect.tap(() => Deferred.succeed(readStarted, undefined)), |
| 132 | + Effect.tap(() => Deferred.await(readGate)), |
| 133 | + ) |
| 134 | + : Effect.sync(() => memory.values.get(key) ?? null), |
| 135 | + setItem: (key, value) => |
| 136 | + Effect.sync(() => { |
| 137 | + memory.values.set(key, value); |
| 138 | + }), |
| 139 | + removeItem: (key) => |
| 140 | + Effect.sync(() => { |
| 141 | + memory.values.delete(key); |
| 142 | + }), |
| 143 | + }); |
| 144 | + const { tokens } = yield* makeStores(gatedStorage); |
| 145 | + yield* Effect.sync(() => memory.values.set(TOKEN_KEY, JSON.stringify(encodeToken(TOKEN)))); |
| 146 | + |
| 147 | + const pendingGet = yield* tokens.get(ENVIRONMENT_ID).pipe(Effect.forkScoped); |
| 148 | + yield* Deferred.await(readStarted); |
| 149 | + const pendingRemove = yield* tokens.remove(ENVIRONMENT_ID).pipe(Effect.forkScoped); |
| 150 | + // Give remove every chance to run while the read is parked on the gate. |
| 151 | + // With per-environment serialization it stays queued behind the get; a |
| 152 | + // regression lets it delete the keychain entry here, and the resumed |
| 153 | + // get would then resurrect the removed token into the cache. |
| 154 | + for (let i = 0; i < 20 && memory.values.has(TOKEN_KEY); i += 1) { |
| 155 | + yield* Effect.yieldNow; |
| 156 | + } |
| 157 | + yield* Deferred.succeed(readGate, undefined); |
| 158 | + yield* Fiber.join(pendingGet); |
| 159 | + yield* Fiber.join(pendingRemove); |
| 160 | + |
| 161 | + // remove ran after get completed (same-env operations serialize), so |
| 162 | + // the token is gone from both the keychain and the in-memory cache. |
| 163 | + expect(memory.values.has(TOKEN_KEY)).toBe(false); |
| 164 | + expect(Option.isNone(yield* tokens.get(ENVIRONMENT_ID))).toBe(true); |
| 165 | + }), |
| 166 | + ); |
| 167 | + |
| 168 | + it.effect("discards a corrupt persisted token instead of failing the connection", () => |
| 169 | + Effect.gen(function* () { |
| 170 | + const memory = makeStorage({ [TOKEN_KEY]: "{not-json" }); |
| 171 | + const { tokens } = yield* makeStores(memory.storage); |
| 172 | + |
| 173 | + expect(Option.isNone(yield* tokens.get(ENVIRONMENT_ID))).toBe(true); |
| 174 | + expect(memory.values.has(TOKEN_KEY)).toBe(false); |
| 175 | + }), |
| 176 | + ); |
| 177 | +}); |
0 commit comments