import type { Migration } from "../src/migrate.ts"; /** * RFC-0044 §7 (Phase 2): the remote MCP authorization server's tables. * * 1. Better Auth's `jwt()` and `mcp()` / `@better-auth/oauth-provider` tables (`jwks`, * `oauthClient`, `oauthResource`, `oauthClientResource`, `oauthRefreshToken`, * `oauthAccessToken`, `oauthConsent`, `oauthClientAssertion`) and their indexes. Generated by * better-auth 1.7.18 (`@better-auth/mcp` 1.7.18, `@better-auth/oauth-provider` 1.7.18) * `getMigrations(auth.options).compileMigrations()` on a database at 0030, with the instance * `createInstance` builds when `AuthOptions.mcpOauth` is set; never edit by hand. Nothing in the * existing auth tables changes. * 2. Cloudhal's `agent_grants`: one row per member and OAuth client, written on Studio's consent * screen. It carries what a token does not: the scopes after narrowing by the member's role, * the agent mode, an elevation and the revocation, read on every `/mcp` call (§5). * * 3. Cloudhal's index `oauthRefreshToken_family_idx` (not generated): every refresh finds its * family's first token (`authorizationCodeId`, oldest `createdAt`; rotated rows are kept). * * Plain SQL, one atomic batch; additive, so a downgrade ignores the tables (§7 rollback). */ export const migration0031Oauth: Migration = { name: "0031_oauth", sql: ` create table "jwks" ("id" text not null primary key, "publicKey" text not null, "privateKey" text not null, "createdAt" date not null, "expiresAt" date, "alg" text, "crv" text); create table "oauthClient" ("id" text not null primary key, "clientId" text not null unique, "clientSecret" text, "clientDiscoveryId" text, "disabled" integer, "skipConsent" integer, "enableEndSession" integer, "subjectType" text, "scopes" text, "clientCredentialsScopes" text, "userId" text references "user" ("id") on delete cascade, "createdAt" date, "updatedAt" date, "name" text, "uri" text, "icon" text, "contacts" text, "tos" text, "policy" text, "softwareId" text, "softwareVersion" text, "softwareStatement" text, "redirectUris" text not null, "postLogoutRedirectUris" text, "backchannelLogoutUri" text, "backchannelLogoutSessionRequired" integer, "tokenEndpointAuthMethod" text, "applicationType" text, "jwks" text, "jwksUri" text, "grantTypes" text, "responseTypes" text, "requirePKCE" integer, "dpopBoundAccessTokens" integer, "referenceId" text, "metadata" text); create table "oauthResource" ("id" text not null primary key, "identifier" text not null unique, "name" text not null, "accessTokenTtl" integer, "refreshTokenTtl" integer, "signingAlgorithm" text, "signingKeyId" text, "allowedScopes" text, "customClaims" text, "dpopBoundAccessTokensRequired" integer, "disabled" integer, "createdAt" date, "updatedAt" date, "policyVersion" integer, "metadata" text); create table "oauthClientResource" ("id" text not null primary key, "clientId" text not null references "oauthClient" ("clientId") on delete cascade, "resourceId" text not null references "oauthResource" ("identifier") on delete cascade, "metadata" text, "createdAt" date); create table "oauthRefreshToken" ("id" text not null primary key, "token" text not null unique, "clientId" text not null references "oauthClient" ("clientId") on delete cascade, "sessionId" text references "session" ("id") on delete set null, "userId" text not null references "user" ("id") on delete cascade, "referenceId" text, "authorizationCodeId" text, "resources" text, "requestedUserInfoClaims" text, "expiresAt" date not null, "createdAt" date not null, "revoked" date, "rotatedAt" date, "rotationReplayResponse" text, "rotationReplayExpiresAt" date, "authTime" date, "confirmation" text, "scopes" text not null); create table "oauthAccessToken" ("id" text not null primary key, "token" text not null unique, "clientId" text not null references "oauthClient" ("clientId") on delete cascade, "sessionId" text references "session" ("id") on delete set null, "userId" text references "user" ("id") on delete cascade, "referenceId" text, "authorizationCodeId" text, "resources" text, "requestedUserInfoClaims" text, "refreshId" text references "oauthRefreshToken" ("id") on delete cascade, "expiresAt" date not null, "createdAt" date not null, "revoked" date, "confirmation" text, "scopes" text not null); create table "oauthConsent" ("id" text not null primary key, "clientId" text not null references "oauthClient" ("clientId") on delete cascade, "userId" text references "user" ("id") on delete cascade, "referenceId" text, "resources" text, "requestedUserInfoClaims" text, "scopes" text not null, "createdAt" date not null, "updatedAt" date not null); create table "oauthClientAssertion" ("id" text not null primary key, "expiresAt" date not null); create index "oauthClient_userId_idx" on "oauthClient" ("userId"); create index "oauthClientResource_clientId_idx" on "oauthClientResource" ("clientId"); create index "oauthClientResource_resourceId_idx" on "oauthClientResource" ("resourceId"); create index "oauthRefreshToken_clientId_idx" on "oauthRefreshToken" ("clientId"); create index "oauthRefreshToken_sessionId_idx" on "oauthRefreshToken" ("sessionId"); create index "oauthRefreshToken_userId_idx" on "oauthRefreshToken" ("userId"); create index "oauthRefreshToken_authorizationCodeId_idx" on "oauthRefreshToken" ("authorizationCodeId"); create index "oauthAccessToken_clientId_idx" on "oauthAccessToken" ("clientId"); create index "oauthAccessToken_sessionId_idx" on "oauthAccessToken" ("sessionId"); create index "oauthAccessToken_userId_idx" on "oauthAccessToken" ("userId"); create index "oauthAccessToken_authorizationCodeId_idx" on "oauthAccessToken" ("authorizationCodeId"); create index "oauthAccessToken_refreshId_idx" on "oauthAccessToken" ("refreshId"); create index "oauthConsent_clientId_idx" on "oauthConsent" ("clientId"); create index "oauthConsent_userId_idx" on "oauthConsent" ("userId"); create unique index "oauthClientResource_clientId_resourceId_uidx" on "oauthClientResource" ("clientId", "resourceId"); CREATE INDEX "oauthRefreshToken_family_idx" ON "oauthRefreshToken" ("authorizationCodeId", "createdAt"); CREATE TABLE agent_grants ( id TEXT PRIMARY KEY, user_id TEXT NOT NULL, client_id TEXT NOT NULL, client_name TEXT NOT NULL, scopes TEXT NOT NULL, mode TEXT NOT NULL, elevation TEXT, created_at TEXT NOT NULL, last_used_at TEXT, revoked_at TEXT, UNIQUE (user_id, client_id) ); CREATE INDEX agent_grants_user ON agent_grants (user_id); `, };