Two new test files exercising the per-user secret shadowing path: - multi-scope-bearer.test.ts — static bearer token resolved from the innermost scope first, then falls back outward. - multi-scope-oauth.test.ts — OAuth2 flow writes tokens at the innermost scope so different users in the same org can hold independent credentials.
Unified · Split
| @@ -0,0 +1,272 @@ | ||
| 1 | +// --------------------------------------------------------------------------- | |
| 2 | +// End-to-end shape test for multi-user bearer-token auth on the OpenAPI | |
| 3 | +// plugin. Models the Vercel-style scenario: | |
| 4 | +// | |
| 5 | +// - An org admin uploads the Vercel OpenAPI spec once. The stored source | |
| 6 | +// carries an `Authorization: Bearer <vercel_api_token>` header | |
| 7 | +// reference, but NOT the token value itself. | |
| 8 | +// - Each user (alice, bob) writes their own personal access token at | |
| 9 | +// their own user scope under the same secret id (`vercel_api_token`). | |
| 10 | +// - Invoking a Vercel tool through alice injects alice's token; | |
| 11 | +// through bob injects bob's. The org scope never stores a value — | |
| 12 | +// per-user scopes are the only source of truth for the bearer. | |
| 13 | +// | |
| 14 | +// This is the tier-1 win: the scope-partitioning `SecretProvider` lets | |
| 15 | +// the same secret id carry a distinct value in each user's scope, so a | |
| 16 | +// single stored source description serves every user without duplicating | |
| 17 | +// source rows per tenant. | |
| 18 | +// --------------------------------------------------------------------------- | |
| 19 | + | |
| 20 | +import { expect, layer } from "@effect/vitest"; | |
| 21 | +import { Effect, Layer, Schema } from "effect"; | |
| 22 | +import { | |
| 23 | + HttpApi, | |
| 24 | + HttpApiBuilder, | |
| 25 | + HttpApiEndpoint, | |
| 26 | + HttpApiGroup, | |
| 27 | + HttpClient, | |
| 28 | + HttpServerRequest, | |
| 29 | + OpenApi, | |
| 30 | +} from "@effect/platform"; | |
| 31 | +import { NodeHttpServer } from "@effect/platform-node"; | |
| 32 | + | |
| 33 | +import { | |
| 34 | + collectSchemas, | |
| 35 | + createExecutor, | |
| 36 | + definePlugin, | |
| 37 | + makeInMemoryBlobStore, | |
| 38 | + Scope, | |
| 39 | + ScopeId, | |
| 40 | + SecretId, | |
| 41 | + SetSecretInput, | |
| 42 | + type InvokeOptions, | |
| 43 | + type SecretProvider, | |
| 44 | +} from "@executor/sdk"; | |
| 45 | +import { makeMemoryAdapter } from "@executor/storage-core/testing/memory"; | |
| 46 | + | |
| 47 | +import { openApiPlugin } from "./plugin"; | |
| 48 | + | |
| 49 | +const autoApprove: InvokeOptions = { onElicitation: "accept-all" }; | |
| 50 | + | |
| 51 | +// --------------------------------------------------------------------------- | |
| 52 | +// Test API — a single endpoint that echoes the Authorization header so the | |
| 53 | +// test can assert which user's token got injected. | |
| 54 | +// --------------------------------------------------------------------------- | |
| 55 | + | |
| 56 | +class EchoHeaders extends Schema.Class<EchoHeaders>("EchoHeaders")({ | |
| 57 | + authorization: Schema.optional(Schema.String), | |
| 58 | +}) {} | |
| 59 | + | |
| 60 | +const ProjectsGroup = HttpApiGroup.make("projects").add( | |
| 61 | + HttpApiEndpoint.get("list", "/v9/projects").addSuccess(EchoHeaders), | |
| 62 | +); | |
| 63 | + | |
| 64 | +const VercelApi = HttpApi.make("vercelApi").add(ProjectsGroup); | |
| 65 | +const specJson = JSON.stringify(OpenApi.fromApi(VercelApi)); | |
| 66 | + | |
| 67 | +const ProjectsGroupLive = HttpApiBuilder.group(VercelApi, "projects", (handlers) => | |
| 68 | + handlers.handle("list", () => | |
| 69 | + Effect.gen(function* () { | |
| 70 | + const req = yield* HttpServerRequest.HttpServerRequest; | |
| 71 | + return new EchoHeaders({ | |
| 72 | + authorization: req.headers["authorization"], | |
| 73 | + }); | |
| 74 | + }), | |
| 75 | + ), | |
| 76 | +); | |
| 77 | + | |
| 78 | +const ApiLive = HttpApiBuilder.api(VercelApi).pipe(Layer.provide(ProjectsGroupLive)); | |
| 79 | + | |
| 80 | +const TestLayer = HttpApiBuilder.serve().pipe( | |
| 81 | + Layer.provide(ApiLive), | |
| 82 | + Layer.provideMerge(NodeHttpServer.layerTest), | |
| 83 | +); | |
| 84 | + | |
| 85 | +// --------------------------------------------------------------------------- | |
| 86 | +// Tests | |
| 87 | +// --------------------------------------------------------------------------- | |
| 88 | + | |
| 89 | +layer(TestLayer)("OpenAPI multi-scope bearer (Vercel-style)", (it) => { | |
| 90 | + it.effect( | |
| 91 | + "admin-added source; each user's per-scope token wins on invocation", | |
| 92 | + () => | |
| 93 | + Effect.gen(function* () { | |
| 94 | + // Scope-partitioning in-memory provider. The composite key is | |
| 95 | + // what makes the tier-1 fix observable: same secret id, different | |
| 96 | + // value per scope. A flat `Map<id, value>` provider would lose | |
| 97 | + // one of the users' tokens on the second write. | |
| 98 | + const secretStore = new Map<string, string>(); | |
| 99 | + const key = (scope: string, id: string) => `${scope}\u0000${id}`; | |
| 100 | + const memoryProvider: SecretProvider = { | |
| 101 | + key: "memory", | |
| 102 | + writable: true, | |
| 103 | + get: (id, scope) => | |
| 104 | + Effect.sync(() => secretStore.get(key(scope, id)) ?? null), | |
| 105 | + set: (id, value, scope) => | |
| 106 | + Effect.sync(() => { | |
| 107 | + secretStore.set(key(scope, id), value); | |
| 108 | + }), | |
| 109 | + delete: (id, scope) => | |
| 110 | + Effect.sync(() => secretStore.delete(key(scope, id))), | |
| 111 | + }; | |
| 112 | + const memorySecretsPlugin = definePlugin(() => ({ | |
| 113 | + id: "memory-secrets" as const, | |
| 114 | + storage: () => ({}), | |
| 115 | + secretProviders: [memoryProvider], | |
| 116 | + })); | |
| 117 | + | |
| 118 | + const httpClient = yield* HttpClient.HttpClient; | |
| 119 | + const clientLayer = Layer.succeed(HttpClient.HttpClient, httpClient); | |
| 120 | + const plugins = [ | |
| 121 | + openApiPlugin({ httpClientLayer: clientLayer }), | |
| 122 | + memorySecretsPlugin(), | |
| 123 | + ] as const; | |
| 124 | + | |
| 125 | + // One adapter + blob store backing all three executors: mirrors a | |
| 126 | + // multi-tenant deployment where admin + users share infra but | |
| 127 | + // each sits at a different scope stack. | |
| 128 | + const schema = collectSchemas(plugins); | |
| 129 | + const adapter = makeMemoryAdapter({ schema }); | |
| 130 | + const blobs = makeInMemoryBlobStore(); | |
| 131 | + | |
| 132 | + const now = new Date(); | |
| 133 | + const orgScope = new Scope({ | |
| 134 | + id: ScopeId.make("org"), | |
| 135 | + name: "acme-org", | |
| 136 | + createdAt: now, | |
| 137 | + }); | |
| 138 | + const aliceScope = new Scope({ | |
| 139 | + id: ScopeId.make("user-alice"), | |
| 140 | + name: "alice", | |
| 141 | + createdAt: now, | |
| 142 | + }); | |
| 143 | + const bobScope = new Scope({ | |
| 144 | + id: ScopeId.make("user-bob"), | |
| 145 | + name: "bob", | |
| 146 | + createdAt: now, | |
| 147 | + }); | |
| 148 | + | |
| 149 | + const adminExec = yield* createExecutor({ | |
| 150 | + scopes: [orgScope], | |
| 151 | + adapter, | |
| 152 | + blobs, | |
| 153 | + plugins, | |
| 154 | + }); | |
| 155 | + const aliceExec = yield* createExecutor({ | |
| 156 | + scopes: [aliceScope, orgScope], | |
| 157 | + adapter, | |
| 158 | + blobs, | |
| 159 | + plugins, | |
| 160 | + }); | |
| 161 | + const bobExec = yield* createExecutor({ | |
| 162 | + scopes: [bobScope, orgScope], | |
| 163 | + adapter, | |
| 164 | + blobs, | |
| 165 | + plugins, | |
| 166 | + }); | |
| 167 | + | |
| 168 | + // ------------------------------------------------------------- | |
| 169 | + // 1. Admin adds the Vercel OpenAPI source at org scope. The | |
| 170 | + // stored source carries a HeaderValue *reference* to the | |
| 171 | + // secret id — the value itself is deliberately NOT written | |
| 172 | + // at the org scope. Each user will supply their own. | |
| 173 | + // ------------------------------------------------------------- | |
| 174 | + yield* adminExec.openapi.addSpec({ | |
| 175 | + spec: specJson, | |
| 176 | + scope: orgScope.id as string, | |
| 177 | + namespace: "vercel", | |
| 178 | + baseUrl: "", | |
| 179 | + headers: { | |
| 180 | + Authorization: { | |
| 181 | + secretId: "vercel_api_token", | |
| 182 | + prefix: "Bearer ", | |
| 183 | + }, | |
| 184 | + }, | |
| 185 | + }); | |
| 186 | + | |
| 187 | + // ------------------------------------------------------------- | |
| 188 | + // 2. Each user writes their personal access token under the | |
| 189 | + // same secret id, but at their own scope. Tier-1 scope | |
| 190 | + // routing means these coexist in the provider — alice's | |
| 191 | + // write does not overwrite bob's. | |
| 192 | + // ------------------------------------------------------------- | |
| 193 | + yield* aliceExec.secrets.set( | |
| 194 | + new SetSecretInput({ | |
| 195 | + id: SecretId.make("vercel_api_token"), | |
| 196 | + scope: aliceScope.id, | |
| 197 | + name: "Vercel API Token (alice)", | |
| 198 | + value: "alice-vercel-token", | |
| 199 | + }), | |
| 200 | + ); | |
| 201 | + yield* bobExec.secrets.set( | |
| 202 | + new SetSecretInput({ | |
| 203 | + id: SecretId.make("vercel_api_token"), | |
| 204 | + scope: bobScope.id, | |
| 205 | + name: "Vercel API Token (bob)", | |
| 206 | + value: "bob-vercel-token", | |
| 207 | + }), | |
| 208 | + ); | |
| 209 | + | |
| 210 | + // ------------------------------------------------------------- | |
| 211 | + // 3. Invoking the shared tool through each user's executor | |
| 212 | + // resolves `vercel_api_token` via scope fall-through | |
| 213 | + // (innermost first). Alice's scope yields her token; bob's | |
| 214 | + // scope yields his. Same source, same tool, different | |
| 215 | + // injected bearer. | |
| 216 | + // ------------------------------------------------------------- | |
| 217 | + const aliceResult = (yield* aliceExec.tools.invoke( | |
| 218 | + "vercel.projects.list", | |
| 219 | + {}, | |
| 220 | + autoApprove, | |
| 221 | + )) as { data: { authorization?: string } | null; error: unknown }; | |
| 222 | + expect(aliceResult.error).toBeNull(); | |
| 223 | + expect(aliceResult.data?.authorization).toBe("Bearer alice-vercel-token"); | |
| 224 | + | |
| 225 | + const bobResult = (yield* bobExec.tools.invoke( | |
| 226 | + "vercel.projects.list", | |
| 227 | + {}, | |
| 228 | + autoApprove, | |
| 229 | + )) as { data: { authorization?: string } | null; error: unknown }; | |
| 230 | + expect(bobResult.error).toBeNull(); | |
| 231 | + expect(bobResult.data?.authorization).toBe("Bearer bob-vercel-token"); | |
| 232 | + | |
| 233 | + // ------------------------------------------------------------- | |
| 234 | + // 4. Scope attribution: each user's token is pinned to their | |
| 235 | + // own scope, never smuggled into the org fallback. | |
| 236 | + // ------------------------------------------------------------- | |
| 237 | + const aliceRows = yield* aliceExec.secrets.list(); | |
| 238 | + const aliceToken = aliceRows.find( | |
| 239 | + (r) => (r.id as unknown as string) === "vercel_api_token", | |
| 240 | + ); | |
| 241 | + expect(aliceToken?.scopeId as unknown as string).toBe("user-alice"); | |
| 242 | + | |
| 243 | + const bobRows = yield* bobExec.secrets.list(); | |
| 244 | + const bobToken = bobRows.find( | |
| 245 | + (r) => (r.id as unknown as string) === "vercel_api_token", | |
| 246 | + ); | |
| 247 | + expect(bobToken?.scopeId as unknown as string).toBe("user-bob"); | |
| 248 | + | |
| 249 | + // Admin's scope never received a token — `get` at the org | |
| 250 | + // scope yields null and the source is effectively unusable | |
| 251 | + // for the admin role, exactly as designed. | |
| 252 | + const adminToken = yield* adminExec.secrets.get("vercel_api_token"); | |
| 253 | + expect(adminToken).toBeNull(); | |
| 254 | + | |
| 255 | + // ------------------------------------------------------------- | |
| 256 | + // 5. Cross-user isolation on enumeration: alice does not see | |
| 257 | + // bob's token row, and vice versa. | |
| 258 | + // ------------------------------------------------------------- | |
| 259 | + const aliceIds = new Set( | |
| 260 | + aliceRows.map((r) => `${r.scopeId as unknown as string}:${r.id as unknown as string}`), | |
| 261 | + ); | |
| 262 | + expect(aliceIds).toContain("user-alice:vercel_api_token"); | |
| 263 | + expect(aliceIds).not.toContain("user-bob:vercel_api_token"); | |
| 264 | + | |
| 265 | + const bobIds = new Set( | |
| 266 | + bobRows.map((r) => `${r.scopeId as unknown as string}:${r.id as unknown as string}`), | |
| 267 | + ); | |
| 268 | + expect(bobIds).toContain("user-bob:vercel_api_token"); | |
| 269 | + expect(bobIds).not.toContain("user-alice:vercel_api_token"); | |
| 270 | + }), | |
| 271 | + ); | |
| 272 | +}); | |
| @@ -0,0 +1,393 @@ | ||
| 1 | +// --------------------------------------------------------------------------- | |
| 2 | +// End-to-end shape test for multi-scope OAuth on the OpenAPI plugin. | |
| 3 | +// | |
| 4 | +// Models the production scenario: an org-level admin uploads the shared | |
| 5 | +// client credentials, each member of the org runs their own OAuth flow, | |
| 6 | +// and each member's access token is stamped at the per-user scope so the | |
| 7 | +// org fallback provides client id/secret and the user scope provides | |
| 8 | +// the access token the invoker actually injects. | |
| 9 | +// | |
| 10 | +// Three executors share a single backing adapter + blob store + secret | |
| 11 | +// provider: | |
| 12 | +// - admin: scopes = [org] | |
| 13 | +// - alice: scopes = [user-a, org] | |
| 14 | +// - bob: scopes = [user-b, org] | |
| 15 | +// | |
| 16 | +// Expectations: | |
| 17 | +// 1. Admin writes `petstore_client_id`/`petstore_client_secret` at org. | |
| 18 | +// 2. Both users addSpec at their own scope with OAuth2Auth referencing | |
| 19 | +// the shared org client secrets + user-unique access/refresh token ids. | |
| 20 | +// 3. Each user runs startOAuth / completeOAuth; tokens land at their | |
| 21 | +// own scope via the session's `tokenScope`. | |
| 22 | +// 4. Invoking `petstore.items.echoHeaders` through alice returns | |
| 23 | +// alice's token; through bob returns bob's. | |
| 24 | +// 5. `secrets.list()` for alice surfaces the org client creds + her | |
| 25 | +// own tokens but NOT bob's. | |
| 26 | +// --------------------------------------------------------------------------- | |
| 27 | + | |
| 28 | +import { afterEach } from "vitest"; | |
| 29 | +import { expect, layer } from "@effect/vitest"; | |
| 30 | +import { Effect, Layer, Schema } from "effect"; | |
| 31 | +import { | |
| 32 | + HttpApi, | |
| 33 | + HttpApiBuilder, | |
| 34 | + HttpApiEndpoint, | |
| 35 | + HttpApiGroup, | |
| 36 | + HttpClient, | |
| 37 | + HttpServerRequest, | |
| 38 | + OpenApi, | |
| 39 | +} from "@effect/platform"; | |
| 40 | +import { NodeHttpServer } from "@effect/platform-node"; | |
| 41 | + | |
| 42 | +import { | |
| 43 | + collectSchemas, | |
| 44 | + createExecutor, | |
| 45 | + definePlugin, | |
| 46 | + makeInMemoryBlobStore, | |
| 47 | + Scope, | |
| 48 | + ScopeId, | |
| 49 | + SecretId, | |
| 50 | + SetSecretInput, | |
| 51 | + type InvokeOptions, | |
| 52 | + type SecretProvider, | |
| 53 | +} from "@executor/sdk"; | |
| 54 | +import { makeMemoryAdapter } from "@executor/storage-core/testing/memory"; | |
| 55 | + | |
| 56 | +import { OAuth2Auth } from "./types"; | |
| 57 | +import { openApiPlugin } from "./plugin"; | |
| 58 | + | |
| 59 | +const autoApprove: InvokeOptions = { onElicitation: "accept-all" }; | |
| 60 | + | |
| 61 | +// --------------------------------------------------------------------------- | |
| 62 | +// Test API — a single endpoint that echoes the Authorization header so the | |
| 63 | +// test can assert which user's token got injected. | |
| 64 | +// --------------------------------------------------------------------------- | |
| 65 | + | |
| 66 | +class EchoHeaders extends Schema.Class<EchoHeaders>("EchoHeaders")({ | |
| 67 | + authorization: Schema.optional(Schema.String), | |
| 68 | +}) {} | |
| 69 | + | |
| 70 | +const ItemsGroup = HttpApiGroup.make("items").add( | |
| 71 | + HttpApiEndpoint.get("echoHeaders", "/echo-headers").addSuccess(EchoHeaders), | |
| 72 | +); | |
| 73 | + | |
| 74 | +const TestApi = HttpApi.make("testApi").add(ItemsGroup); | |
| 75 | +const specJson = JSON.stringify(OpenApi.fromApi(TestApi)); | |
| 76 | + | |
| 77 | +const ItemsGroupLive = HttpApiBuilder.group(TestApi, "items", (handlers) => | |
| 78 | + handlers.handle("echoHeaders", () => | |
| 79 | + Effect.gen(function* () { | |
| 80 | + const req = yield* HttpServerRequest.HttpServerRequest; | |
| 81 | + return new EchoHeaders({ | |
| 82 | + authorization: req.headers["authorization"], | |
| 83 | + }); | |
| 84 | + }), | |
| 85 | + ), | |
| 86 | +); | |
| 87 | + | |
| 88 | +const ApiLive = HttpApiBuilder.api(TestApi).pipe(Layer.provide(ItemsGroupLive)); | |
| 89 | + | |
| 90 | +const TestLayer = HttpApiBuilder.serve().pipe( | |
| 91 | + Layer.provide(ApiLive), | |
| 92 | + Layer.provideMerge(NodeHttpServer.layerTest), | |
| 93 | +); | |
| 94 | + | |
| 95 | +// --------------------------------------------------------------------------- | |
| 96 | +// Fetch override for the token endpoint. Each user's OAuth callback code | |
| 97 | +// deterministically maps to a different access_token in the mock | |
| 98 | +// response so we can assert per-user isolation at invocation time. | |
| 99 | +// --------------------------------------------------------------------------- | |
| 100 | + | |
| 101 | +const originalFetch = globalThis.fetch; | |
| 102 | + | |
| 103 | +const mockTokenFetch = (tokenByCode: Record<string, string>) => { | |
| 104 | + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { | |
| 105 | + const bodyText = | |
| 106 | + init?.body instanceof URLSearchParams | |
| 107 | + ? init.body.toString() | |
| 108 | + : typeof init?.body === "string" | |
| 109 | + ? init.body | |
| 110 | + : ""; | |
| 111 | + const params = new URLSearchParams(bodyText); | |
| 112 | + const code = params.get("code") ?? ""; | |
| 113 | + const token = tokenByCode[code]; | |
| 114 | + if (!token) { | |
| 115 | + return new Response( | |
| 116 | + JSON.stringify({ error: "invalid_grant", code }), | |
| 117 | + { status: 400, headers: { "content-type": "application/json" } }, | |
| 118 | + ); | |
| 119 | + } | |
| 120 | + // Omit `expires_in` so the resulting OAuth2Auth has expiresAt=null. | |
| 121 | + // That sidesteps the refresh path during invocation — the invoker | |
| 122 | + // returns the stored access token directly. | |
| 123 | + return new Response( | |
| 124 | + JSON.stringify({ | |
| 125 | + access_token: token, | |
| 126 | + token_type: "Bearer", | |
| 127 | + refresh_token: `${token}-refresh`, | |
| 128 | + }), | |
| 129 | + { status: 200, headers: { "content-type": "application/json" } }, | |
| 130 | + ); | |
| 131 | + }) as unknown as typeof fetch; | |
| 132 | +}; | |
| 133 | + | |
| 134 | +afterEach(() => { | |
| 135 | + globalThis.fetch = originalFetch; | |
| 136 | +}); | |
| 137 | + | |
| 138 | +// --------------------------------------------------------------------------- | |
| 139 | +// Tests | |
| 140 | +// --------------------------------------------------------------------------- | |
| 141 | + | |
| 142 | +layer(TestLayer)("OpenAPI multi-scope OAuth", (it) => { | |
| 143 | + it.effect( | |
| 144 | + "per-user access tokens coexist with a shared org-level client credential", | |
| 145 | + () => | |
| 146 | + Effect.gen(function* () { | |
| 147 | + // Shared secret provider. No `list()` implementation — we don't | |
| 148 | + // want the scope-agnostic provider-enumeration fallback to leak | |
| 149 | + // user-scoped ids across executors during this test. In | |
| 150 | + // production, providers that enumerate (1password, workos-vault) | |
| 151 | + // partition by their own tenancy; the in-memory provider here | |
| 152 | + // is flat, so removing `list()` keeps the core-table walk the | |
| 153 | + // only resolver. | |
| 154 | + const secretStore = new Map<string, string>(); | |
| 155 | + const key = (scope: string, id: string) => `${scope}\u0000${id}`; | |
| 156 | + const memoryProvider: SecretProvider = { | |
| 157 | + key: "memory", | |
| 158 | + writable: true, | |
| 159 | + get: (id, scope) => | |
| 160 | + Effect.sync(() => secretStore.get(key(scope, id)) ?? null), | |
| 161 | + set: (id, value, scope) => | |
| 162 | + Effect.sync(() => { | |
| 163 | + secretStore.set(key(scope, id), value); | |
| 164 | + }), | |
| 165 | + delete: (id, scope) => | |
| 166 | + Effect.sync(() => secretStore.delete(key(scope, id))), | |
| 167 | + }; | |
| 168 | + const memorySecretsPlugin = definePlugin(() => ({ | |
| 169 | + id: "memory-secrets" as const, | |
| 170 | + storage: () => ({}), | |
| 171 | + secretProviders: [memoryProvider], | |
| 172 | + })); | |
| 173 | + | |
| 174 | + // Route OpenAPI tool invocations through the Effect test server. | |
| 175 | + const httpClient = yield* HttpClient.HttpClient; | |
| 176 | + const clientLayer = Layer.succeed(HttpClient.HttpClient, httpClient); | |
| 177 | + const plugins = [ | |
| 178 | + openApiPlugin({ httpClientLayer: clientLayer }), | |
| 179 | + memorySecretsPlugin(), | |
| 180 | + ] as const; | |
| 181 | + | |
| 182 | + // One adapter + blob store behind all three executors — exactly | |
| 183 | + // what a multi-tenant deployment looks like in production. | |
| 184 | + const schema = collectSchemas(plugins); | |
| 185 | + const adapter = makeMemoryAdapter({ schema }); | |
| 186 | + const blobs = makeInMemoryBlobStore(); | |
| 187 | + | |
| 188 | + const now = new Date(); | |
| 189 | + const orgScope = new Scope({ | |
| 190 | + id: ScopeId.make("org"), | |
| 191 | + name: "acme-org", | |
| 192 | + createdAt: now, | |
| 193 | + }); | |
| 194 | + const aliceScope = new Scope({ | |
| 195 | + id: ScopeId.make("user-alice"), | |
| 196 | + name: "alice", | |
| 197 | + createdAt: now, | |
| 198 | + }); | |
| 199 | + const bobScope = new Scope({ | |
| 200 | + id: ScopeId.make("user-bob"), | |
| 201 | + name: "bob", | |
| 202 | + createdAt: now, | |
| 203 | + }); | |
| 204 | + | |
| 205 | + const adminExec = yield* createExecutor({ | |
| 206 | + scopes: [orgScope], | |
| 207 | + adapter, | |
| 208 | + blobs, | |
| 209 | + plugins, | |
| 210 | + }); | |
| 211 | + const aliceExec = yield* createExecutor({ | |
| 212 | + scopes: [aliceScope, orgScope], | |
| 213 | + adapter, | |
| 214 | + blobs, | |
| 215 | + plugins, | |
| 216 | + }); | |
| 217 | + const bobExec = yield* createExecutor({ | |
| 218 | + scopes: [bobScope, orgScope], | |
| 219 | + adapter, | |
| 220 | + blobs, | |
| 221 | + plugins, | |
| 222 | + }); | |
| 223 | + | |
| 224 | + // ------------------------------------------------------------- | |
| 225 | + // 1. Admin seeds the org-level client credentials. | |
| 226 | + // ------------------------------------------------------------- | |
| 227 | + yield* adminExec.secrets.set( | |
| 228 | + new SetSecretInput({ | |
| 229 | + id: SecretId.make("petstore_client_id"), | |
| 230 | + scope: orgScope.id, | |
| 231 | + name: "Petstore Client ID", | |
| 232 | + value: "client-abc", | |
| 233 | + }), | |
| 234 | + ); | |
| 235 | + yield* adminExec.secrets.set( | |
| 236 | + new SetSecretInput({ | |
| 237 | + id: SecretId.make("petstore_client_secret"), | |
| 238 | + scope: orgScope.id, | |
| 239 | + name: "Petstore Client Secret", | |
| 240 | + value: "secret-xyz", | |
| 241 | + }), | |
| 242 | + ); | |
| 243 | + | |
| 244 | + // ------------------------------------------------------------- | |
| 245 | + // 2. Each user adds the petstore source at their own scope, | |
| 246 | + // pre-populating OAuth2Auth that points at: | |
| 247 | + // - shared org client creds (read via scope fallthrough) | |
| 248 | + // - user-unique access/refresh token ids (read directly | |
| 249 | + // from the user scope). | |
| 250 | + // `expiresAt: null` keeps the invoker on the no-refresh | |
| 251 | + // path — it returns the stored access token verbatim. | |
| 252 | + // ------------------------------------------------------------- | |
| 253 | + const makeAuth = (user: "alice" | "bob"): OAuth2Auth => | |
| 254 | + new OAuth2Auth({ | |
| 255 | + kind: "oauth2", | |
| 256 | + securitySchemeName: "oauth2", | |
| 257 | + flow: "authorizationCode", | |
| 258 | + tokenUrl: "https://token.example.com/token", | |
| 259 | + clientIdSecretId: "petstore_client_id", | |
| 260 | + clientSecretSecretId: "petstore_client_secret", | |
| 261 | + accessTokenSecretId: `petstore_access_token_${user}`, | |
| 262 | + refreshTokenSecretId: `petstore_refresh_token_${user}`, | |
| 263 | + tokenType: "Bearer", | |
| 264 | + expiresAt: null, | |
| 265 | + scope: null, | |
| 266 | + scopes: ["read"], | |
| 267 | + }); | |
| 268 | + | |
| 269 | + yield* aliceExec.openapi.addSpec({ | |
| 270 | + spec: specJson, | |
| 271 | + scope: aliceScope.id as string, | |
| 272 | + namespace: "petstore", | |
| 273 | + baseUrl: "", | |
| 274 | + oauth2: makeAuth("alice"), | |
| 275 | + }); | |
| 276 | + yield* bobExec.openapi.addSpec({ | |
| 277 | + spec: specJson, | |
| 278 | + scope: bobScope.id as string, | |
| 279 | + namespace: "petstore", | |
| 280 | + baseUrl: "", | |
| 281 | + oauth2: makeAuth("bob"), | |
| 282 | + }); | |
| 283 | + | |
| 284 | + // ------------------------------------------------------------- | |
| 285 | + // 3. Each user runs the OAuth flow. Mock the token endpoint to | |
| 286 | + // return a distinct access_token per `code` so we can tell | |
| 287 | + // them apart downstream. | |
| 288 | + // ------------------------------------------------------------- | |
| 289 | + mockTokenFetch({ | |
| 290 | + "code-alice": "alice-token", | |
| 291 | + "code-bob": "bob-token", | |
| 292 | + }); | |
| 293 | + | |
| 294 | + const startInputFor = (user: "alice" | "bob", scope: ScopeId) => ({ | |
| 295 | + displayName: `Petstore (${user})`, | |
| 296 | + securitySchemeName: "oauth2", | |
| 297 | + flow: "authorizationCode" as const, | |
| 298 | + authorizationUrl: "https://auth.example.com/authorize", | |
| 299 | + tokenUrl: "https://token.example.com/token", | |
| 300 | + redirectUrl: "https://app.example.com/oauth/callback", | |
| 301 | + clientIdSecretId: "petstore_client_id", | |
| 302 | + clientSecretSecretId: "petstore_client_secret", | |
| 303 | + scopes: ["read"], | |
| 304 | + tokenScope: scope as unknown as string, | |
| 305 | + accessTokenSecretId: `petstore_access_token_${user}`, | |
| 306 | + refreshTokenSecretId: `petstore_refresh_token_${user}`, | |
| 307 | + }); | |
| 308 | + | |
| 309 | + const aliceStart = yield* aliceExec.openapi.startOAuth( | |
| 310 | + startInputFor("alice", aliceScope.id), | |
| 311 | + ); | |
| 312 | + const bobStart = yield* bobExec.openapi.startOAuth( | |
| 313 | + startInputFor("bob", bobScope.id), | |
| 314 | + ); | |
| 315 | + | |
| 316 | + yield* aliceExec.openapi.completeOAuth({ | |
| 317 | + state: aliceStart.sessionId, | |
| 318 | + code: "code-alice", | |
| 319 | + }); | |
| 320 | + yield* bobExec.openapi.completeOAuth({ | |
| 321 | + state: bobStart.sessionId, | |
| 322 | + code: "code-bob", | |
| 323 | + }); | |
| 324 | + | |
| 325 | + // ------------------------------------------------------------- | |
| 326 | + // 4. Invoke the tool through each executor. The injected | |
| 327 | + // Authorization header must carry THAT user's token. | |
| 328 | + // ------------------------------------------------------------- | |
| 329 | + const aliceResult = (yield* aliceExec.tools.invoke( | |
| 330 | + "petstore.items.echoHeaders", | |
| 331 | + {}, | |
| 332 | + autoApprove, | |
| 333 | + )) as { data: { authorization?: string } | null; error: unknown }; | |
| 334 | + expect(aliceResult.error).toBeNull(); | |
| 335 | + expect(aliceResult.data?.authorization).toBe("Bearer alice-token"); | |
| 336 | + | |
| 337 | + const bobResult = (yield* bobExec.tools.invoke( | |
| 338 | + "petstore.items.echoHeaders", | |
| 339 | + {}, | |
| 340 | + autoApprove, | |
| 341 | + )) as { data: { authorization?: string } | null; error: unknown }; | |
| 342 | + expect(bobResult.error).toBeNull(); | |
| 343 | + expect(bobResult.data?.authorization).toBe("Bearer bob-token"); | |
| 344 | + | |
| 345 | + // ------------------------------------------------------------- | |
| 346 | + // 5. Scope isolation on `secrets.list()`: | |
| 347 | + // - alice sees org client creds + her own tokens. | |
| 348 | + // - alice does NOT see bob's tokens. | |
| 349 | + // - bob symmetrically sees only the org creds + his tokens. | |
| 350 | + // - admin sees only the org creds. | |
| 351 | + // ------------------------------------------------------------- | |
| 352 | + const aliceIds = new Set( | |
| 353 | + (yield* aliceExec.secrets.list()).map((s) => s.id as unknown as string), | |
| 354 | + ); | |
| 355 | + expect(aliceIds).toContain("petstore_client_id"); | |
| 356 | + expect(aliceIds).toContain("petstore_client_secret"); | |
| 357 | + expect(aliceIds).toContain("petstore_access_token_alice"); | |
| 358 | + expect(aliceIds).toContain("petstore_refresh_token_alice"); | |
| 359 | + expect(aliceIds).not.toContain("petstore_access_token_bob"); | |
| 360 | + expect(aliceIds).not.toContain("petstore_refresh_token_bob"); | |
| 361 | + | |
| 362 | + const bobIds = new Set( | |
| 363 | + (yield* bobExec.secrets.list()).map((s) => s.id as unknown as string), | |
| 364 | + ); | |
| 365 | + expect(bobIds).toContain("petstore_client_id"); | |
| 366 | + expect(bobIds).toContain("petstore_access_token_bob"); | |
| 367 | + expect(bobIds).toContain("petstore_refresh_token_bob"); | |
| 368 | + expect(bobIds).not.toContain("petstore_access_token_alice"); | |
| 369 | + | |
| 370 | + const adminIds = new Set( | |
| 371 | + (yield* adminExec.secrets.list()).map((s) => s.id as unknown as string), | |
| 372 | + ); | |
| 373 | + expect(adminIds).toContain("petstore_client_id"); | |
| 374 | + expect(adminIds).toContain("petstore_client_secret"); | |
| 375 | + expect(adminIds).not.toContain("petstore_access_token_alice"); | |
| 376 | + expect(adminIds).not.toContain("petstore_access_token_bob"); | |
| 377 | + | |
| 378 | + // ------------------------------------------------------------- | |
| 379 | + // 6. Secret-row scope attribution: alice's tokens are pinned to | |
| 380 | + // her scope, not smuggled in under the org fallback. | |
| 381 | + // ------------------------------------------------------------- | |
| 382 | + const aliceRows = yield* aliceExec.secrets.list(); | |
| 383 | + const aliceAccess = aliceRows.find( | |
| 384 | + (r) => (r.id as unknown as string) === "petstore_access_token_alice", | |
| 385 | + ); | |
| 386 | + expect(aliceAccess?.scopeId as unknown as string).toBe("user-alice"); | |
| 387 | + const aliceClient = aliceRows.find( | |
| 388 | + (r) => (r.id as unknown as string) === "petstore_client_id", | |
| 389 | + ); | |
| 390 | + expect(aliceClient?.scopeId as unknown as string).toBe("org"); | |
| 391 | + }), | |
| 392 | + ); | |
| 393 | +}); | |