secrets: an OAuth token request and each refresh name the resource
Jonas Templestein, 2026-11-26 16:35 UTC · 2d73eebad2a6b91224ecd57f592e92ccbb005b5f · parent 4933dde
6 files changed
- M core/auth-worker/src/oauth.test.ts
- M core/lib/src/api.ts
- A core/secrets-worker/src/secret-oauth-resource.test.ts
- M core/secrets-worker/src/secret-oauth.ts
- M core/secrets-worker/src/secret/contract.ts
- M core/secrets-worker/src/secrets.ts
M core/auth-worker/src/oauth.test.ts
@@ -159,6 +159,23 @@ expect(await whoIs(stack, token, HOST)).toBeNull(); }); +test("a code for a mind hostname is taken only when the token request names the resource too, as RFC 8707 says", async () => { + const stack = stackOf({ zones: ["example.com"] }); + // the vault's client before the fix: the authorization request named the resource, the token + // request did not, so the stack's default server took the code and did not know it + const withoutResource = async (request: Request) => { + const form = new URLSearchParams(await request.text()); + form.delete("resource"); + return tokenEndpointResponse(stack, tokenRequest(Object.fromEntries(form)), ctx); + }; + const refused = await exchanged(stack, HOST, owner, "app", withoutResource); + expect(refused.answer).toMatchObject({ status: 400 }); + const taken = await exchanged(stack, HOST, owner); + expect(taken.answer).toMatchObject({ status: 200 }); + const { access_token: token } = TokenAnswer.parse(await taken.answer.json()); + expect(await whoIs(stack, token, HOST)).toMatchObject({ member: true }); +}); + test("a path on a mind hostname names the host, as an MCP client names its server; nothing else changes", () => { const known = (resource: string) => knownResourceOf(env, resource); expect(known("https://guestbook.example.com/mcp")).toBe("https://guestbook.example.com");
M core/lib/src/api.ts
@@ -480,6 +480,10 @@ * the provider: a refresh whose answer names a scope outside it, or names none, is refused, * and the new tokens are not kept. */ grantedWithin?: string[]; + /** RFC 8707: the resource the grant is for (an MCP server's URL), as the authorization + * request named it in `extra.resource`. Each refresh sends it again: an issuer that serves + * more than one resource picks its authorization server by it. */ + resource?: string; /** `cloud`: the client is that of the iterate Cloud at `origin`, and `tokenEndpoint` is that * Cloud's broker, which adds the client secret. The refresh token in the material is sealed * for this deployment and means nothing anywhere else. `stack`: the stack's former client
A core/secrets-worker/src/secret-oauth-resource.test.ts
@@ -0,0 +1,86 @@ +import { expect, test } from "vitest"; +import { beginSecretOAuth, completeSecretOAuth, normalizeSecretOAuth } from "./secret-oauth.ts"; +import { refreshSecretMaterial } from "./secrets.ts"; + +// RFC 8707 and the MCP authorization spec: a client names the resource in the authorization +// request AND in the token requests. The mind's issuer picks its authorization server by the +// token request's `resource`: with none, the stack's default one refuses a pet shop's code (400). +test("the resource that the authorization request named goes in the token request and in each refresh, and none goes when none was named", async () => { + for (const resource of ["https://petshop.iterate.com/mcp", undefined]) { + const options = normalizeSecretOAuth({ + authorizationEndpoint: "https://iterate.iterate.com/oauth/authorize", + tokenEndpoint: "https://iterate.iterate.com/oauth/token", + clientId: "client", + urls: ["https://petshop.iterate.com", "https://iterate.iterate.com"], + extra: resource ? { resource } : {}, + }); + const { pending, authorizationUrl } = await beginSecretOAuth(options, { + redirectUri: "https://iterate.iterate.com/secrets/callback", + state: "state", + nonce: "nonce", + }); + expect(new URL(authorizationUrl).searchParams.get("resource")).toBe(resource || null); + + const forms: URLSearchParams[] = []; + const tokenEndpoint = async (request: Request) => { + forms.push(new URLSearchParams(await request.text())); + return Response.json({ access_token: "a", refresh_token: "r", expires_in: 3600 }); + }; + const plain = async (secret: string) => secret; + const record = await completeSecretOAuth(pending, "code", tokenEndpoint, plain); + expect(forms[0]?.get("grant_type")).toBe("authorization_code"); + expect(forms[0]?.get("resource")).toBe(resource || null); + + // the record keeps it, and the refresh sends it again + const refresh = record.refresh; + if (refresh?.kind !== "oauth-refresh-token") throw new Error("no oauth refresh"); + expect(refresh).toMatchObject({ resource }); + await refreshSecretMaterial(refresh, record.material, tokenEndpoint, plain); + expect(forms[1]?.get("grant_type")).toBe("refresh_token"); + expect(forms[1]?.get("resource")).toBe(resource || null); + } +}); + +test("the resource is the one the authorization URL carried: the endpoint's own when extra names none, and none when extra's is empty", async () => { + const cases = [ + { + endpoint: + "https://iterate.iterate.com/oauth/authorize?resource=https%3A%2F%2Fpetshop.iterate.com%2Fmcp", + extra: {}, + sent: "https://petshop.iterate.com/mcp", + }, + { + endpoint: "https://iterate.iterate.com/oauth/authorize", + extra: { resource: "" }, + sent: null, + }, + ]; + for (const { endpoint, extra, sent } of cases) { + const options = normalizeSecretOAuth({ + authorizationEndpoint: endpoint, + tokenEndpoint: "https://iterate.iterate.com/oauth/token", + clientId: "client", + urls: ["https://petshop.iterate.com", "https://iterate.iterate.com"], + extra, + }); + const { pending, authorizationUrl } = await beginSecretOAuth(options, { + redirectUri: "https://iterate.iterate.com/secrets/callback", + state: "state", + nonce: "nonce", + }); + expect(new URL(authorizationUrl).searchParams.get("resource")).toBe(sent); + const forms: URLSearchParams[] = []; + const record = await completeSecretOAuth( + pending, + "code", + async (request) => { + forms.push(new URLSearchParams(await request.text())); + return Response.json({ access_token: "a", refresh_token: "r" }); + }, + async (secret) => secret, + ); + expect(forms[0]?.get("resource")).toBe(sent); + // the record holds a resource the stored schema takes, or none + expect(record.refresh).toMatchObject({ resource: sent || undefined }); + } +});
M core/secrets-worker/src/secret-oauth.ts
@@ -500,6 +500,15 @@ ): Promise<SecretRecord> { const { options } = pending; const { cloud } = options; + // RFC 8707 and the MCP authorization spec: the resource goes in the token request too. An issuer + // that serves more than one resource (a mind's own, for each of its hostnames) picks its + // authorization server by it, and refuses a code that the default one did not grant. + // It is the one the authorization URL carried: `extra`'s, or else the endpoint's own query's. + // An empty one is none, as it was in the authorization URL. + const resource = + options.extra.resource || + new URL(options.authorizationEndpoint).searchParams.get("resource") || + undefined; const response = await fetchFn( oauthTokenRequest({ tokenEndpoint: options.tokenEndpoint, @@ -513,6 +522,7 @@ redirect_uri: pending.redirectUri, code_verifier: pending.codeVerifier, }), + resource, ...(cloud && { cloud_proof: cloudProof }), }, }), @@ -524,6 +534,7 @@ clientAuth: options.clientAuth, // the ceiling holds at each refresh too grantedWithin: options.grantedWithin, + resource, // the Cloud that the attempt named is the secret's Cloud from here on ...(cloud && { client: { cloud: cloud.name, origin: cloud.origin } }), };
M core/secrets-worker/src/secret/contract.ts
@@ -34,6 +34,8 @@ clientAuth: z.enum(["client_secret_basic", "client_secret_post", "none"]), /** The grant's scope ceiling, checked again at each refresh (../secret-oauth.ts). */ grantedWithin: z.array(z.string().min(1)).optional(), + /** RFC 8707: the resource the grant is for, sent again at each refresh (../secret-oauth.ts). */ + resource: z.string().min(1).optional(), /** The iterate Cloud's client that brokers the refresh, and that Cloud's origin, when the * grant was connected through one (../cloud-client.ts). Or the stack's own client at * Cloudflare, which the stack does not make: such a grant is read, and does not refresh. */
M core/secrets-worker/src/secrets.ts
@@ -654,16 +654,17 @@ return isRecord(body) ? body : {}; } -// A client with no secret is sent as `none`, whatever it declared. +// A client with no secret is sent as `none`, whatever it declared. A param with no value is not sent. export function oauthTokenRequest(input: { tokenEndpoint: string; clientId: string; clientSecret: string; clientAuth: ClientAuth; - params: Record<string, string>; + params: Record<string, string | undefined>; }): Request { const method = input.clientSecret ? input.clientAuth : "none"; - const body = new URLSearchParams(input.params); + const body = new URLSearchParams(); + for (const [key, value] of Object.entries(input.params)) if (value) body.set(key, value); if (method !== "client_secret_basic") body.set("client_id", input.clientId); if (method === "client_secret_post") body.set("client_secret", input.clientSecret); return new Request(input.tokenEndpoint, { @@ -751,7 +752,12 @@ clientId, clientSecret: await clientSecretOf(clientSecret, refresh.tokenEndpoint), clientAuth: refresh.clientAuth || "client_secret_basic", - params: { grant_type: "refresh_token", refresh_token: refreshToken }, + params: { + grant_type: "refresh_token", + refresh_token: refreshToken, + // RFC 8707: the resource that the authorization request named (./secret-oauth.ts) + resource: refresh.resource, + }, }); const response = await fetchFn(request).catch((error: unknown) => { throw new RefreshUnanswered(