Skip to content

feat(oauth): identify CodeWork with a Client ID Metadata Document and unify loopback callback - #189

Merged
sanchitrk merged 1 commit into
canaryfrom
feat/oauth-cimd
Dec 1, 2026
Merged

sanchitrk merged 1 commit into
canaryfrom
feat/oauth-cimd

Conversation

@sanchitrk

@sanchitrk sanchitrk commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Identifies CodeWork to external OAuth authorization servers (MCP servers and Connectors) using a Client ID Metadata Document (CIMD) hosted on https://codework.sh/oauth/, with per-server documents when iss is unsupported and Dynamic Client Registration (DCR) as the fallback, and unifies all CodeWork OAuth loopback callbacks on /oauth/callback.

This allows signing in to OAuth servers that disable DCR (such as Clerk) without configuring a manual clientId, isolates callbacks per resource when RFC 9207 iss is absent (RFC 9700 §4.4.2.2), and preserves 127.0.0.1 -> localhost redirect rewrites at token exchange.

Hosted Metadata Documents (sites/www)

  • Added a Cloudflare Worker (sites/www/src/worker.ts) attached to the existing StaticSite in sites/www/alchemy.run.ts via assets.runWorkerFirst: ["/oauth/*"].
  • Serves GET https://codework.sh/oauth/client.json (shared) and GET https://codework.sh/oauth/<id>/client.json (per-server, validated against /^[A-Za-z0-9_-]{12}$/, returning 404 otherwise) with application/json, Cache-Control, and public GET CORS, listing both http://127.0.0.1/oauth/callback[/:id] and http://localhost/oauth/callback[/:id].

Shared Identity Policy & Harness MCP OAuth (packages/harness)

  • Added packages/harness/src/mcp/cimd.ts (exported as OAuthCimd from @codeworksh/harness/effect) with the deterministic 12-character resource fingerprint (callbackId), CIMD selection rule (configured -> shared CIMD if iss -> per-server CIMD if !iss -> DCR), and callback redirect matching (allowing 127.0.0.1 -> localhost on the bound port and exact path).
  • Moved MCP sign-in from /callback to /oauth/callback (existing mcpauth.json DCR grants still refresh without disruption; new browser logins register or use CIMD on /oauth/callback).
  • Discovers server metadata up front in McpOAuth.signIn, keeps CIMD client IDs out of persisted DCR clientInformation (storing tokenClientId for refreshes so future sign-ins re-decide from discovery), arms the loopback waiter before opening the browser, and sends the exact observed redirect_uri on exchange.

Connectors OAuth, CLI & Desktop (packages/codework, apps/server, apps/web, packages/aikit)

  • Extended connector OAuth discovery in packages/codework/src/connector/oauth.ts to read issuer, client_id_metadata_document_supported, token_endpoint_auth_methods_supported, and authorization_response_iss_parameter_supported, selecting CIMD without requiring registration_endpoint.
  • Kept the two-RPC remote flow (connections.oauth.start / connections.oauth.finish) so loopback listeners stay on the browser's machine across CLI, --server, LAN, Tailscale, and Relay; clients arm the host-chosen callback path before opening the browser and forward the full observed callback URL so the host validates state, path, iss, and localhost rewrites before consuming pending state.
  • Refined the OAuth callback completion page in apps/server/src/oauth-loopback.ts and packages/aikit/src/oauth/interactive.ts to a clean, card-free full-bleed layout supporting both light and dark modes, with escaped error HTML.

Docs & Plugin Comments (packages/plugin, docs/internals)

  • Documented CIMD selection, /oauth/callback, and custom override behavior in packages/plugin/src/plugin/mcp.ts, docs/internals/08_plugins.md, and docs/internals/09_server.md.

Testing

  • vp run verify (vp check, builds, package typechecks, knip files/deps/exports): passed (0 issues).
  • App checks & typechecks (apps/server, apps/web, sites/www): passed.
  • E2E suites & artifacts:
    • packages/harness/test/mcp.oauth.test.ts (2/2 passed, artifact packages/harness/test/__artifacts__/mcp.oauth.cimd.json): covers shared CIMD + iss, per-server CIMD without iss, DCR fallback, clientId/clientName overrides, 127.0.0.1 -> localhost rewrite, wrong-path rejection, mismatched iss rejection, and refreshing legacy /callback DCR grants.
    • packages/codework/test/connector.test.ts (5/5 passed, artifact packages/codework/test/__artifacts__/connector.cimd.json): covers shared and per-server CIMD without DCR, bare-code / bad-iss / wrong-path rejection, localhost rewrite, and CIMD refresh.
    • packages/codework/test/cli.mcp.oauth.test.ts (9/9 passed).
    • apps/server/test/oauth-loopback.test.ts (1/1 passed, artifact apps/server/test/__artifacts__/oauth-loopback.json): covers per-server path acceptance, wrong-path rejection with valid state, localhost rewrite preservation, and HTML escaping.
  • Live verification after deploying sites/www:
    • Confirmed 200 on https://codework.sh/oauth/client.json and https://codework.sh/oauth/-zkQPH0u2sKR/client.json, 404 on https://codework.sh/oauth/test/client.json, and intact static site + 404 page.
    • Confirmed live Clerk dev instance (cimd: true, iss: true, no DCR) accepts https://codework.sh/oauth/client.json (302 to sign-in).
    • Completed a live interactive Linear MCP OAuth login in the Electron desktop app, verifying clientId: "https://codework.sh/oauth/client.json" persisted in connections.db and the new callback page rendered cleanly.

Built with Gemini (Antigravity).


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@sanchitrk
sanchitrk merged commit 71f1942 into canary Dec 1, 2026
3 checks passed
@sanchitrk
sanchitrk deleted the feat/oauth-cimd branch December 1, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant