kernel / core / secrets-worker / src / secret-oauth-resource.test.tsmain at 949894f
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 });
}
});