refactor(auth): delegate credentials to OAuth provider - #185
Merged
Conversation
mattzcarey
force-pushed
the
feat/external-api-token-resolution
branch
from
July 30, 2026 23:37
9fdfeb9 to
6f23438
Compare
mattzcarey
marked this pull request as ready for review
July 31, 2026 08:37
mattzcarey
force-pushed
the
feat/external-api-token-resolution
branch
from
July 31, 2026 13:27
6f23438 to
0507b03
Compare
mattzcarey
force-pushed
the
feat/external-api-token-resolution
branch
from
August 4, 2026 10:47
0507b03 to
4f1f5d1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Move direct Cloudflare bearer credentials behind
workers-oauth-provider'sresolveExternalTokenhook and remove the obsolete refresh-concurrency guard.The provider now owns bearer parsing, internal MCP access-token lookup, request dispatch, no-store error responses, and RFC 6750/9728 challenges. Our resolver runs only when the provider does not find one of its own tokens.
Direct credential behavior
The resolver uses the credential prefix as an ownership hint:
cfat_: query one minimum-size/accounts?per_page=5page and require exactly one accountcfut_: query/userand/accountsin parallel and require a usercfoat_: same user-owned path, including Wrangler OAuth credentialsThe resolver produces a Zod-validated discriminated identity (
accountoruser) before building request props. Expected validation failures become providerExternalTokenErrorresponses with appropriate status, challenge, scope, and retry metadata.The token-hash identity cache remains intentionally:
resolveExternalTokenruns on every direct-credential MCP request and the cache prevents repeated/userand/accountsprobes. Provider-issued MCP tokens resolve internally and never invoke this cache.Refresh concurrency cleanup
Cloudflare's upstream OAuth server now provides an idempotent refresh-token concurrency window. A production probe used an existing local MCP credential and its matching production grant to send 10 simultaneous refreshes with the same upstream refresh token:
/uservalidation/tokenendpointThis makes the old upstream-token guard redundant. Removed its token hashing, failure cache, collision telemetry, and promise-sharing behavior.
The provider still rotates a distinct downstream refresh token per successful concurrent request. A staging control proved this is pre-existing on
origin/main: 20 concurrent refreshes returned 20 distinct tokens, but only one remained usable. This PR therefore adds a best-effort per-grant admission gate using the existingOAUTH_KVbinding:429 temporarily_unavailablewithRetry-After;invalid_grantstill revokes the exact downstream grant.KV is eventually consistent, so this is deliberately documented as race reduction rather than a linearizable mutex. On staging, 100 concurrent refreshes produced exactly one usable
200response and 99 structured429responses; the tombstone rejected an immediate retry and allowed a new refresh after 11 seconds. The shorter window preserves most of Cloudflare OAuth's 90-second upstream retry grace if provider persistence fails after the callback succeeds.Provider
Upgrade
@cloudflare/workers-oauth-providerfrom0.9.0to the latest release,0.9.1.Validation
npm run check: 19 files, 247 testsnpx wrangler deploy --dry-run --env productionnpm audit --omit=dev: one existing moderate Hono advisory; unrelated to this PR