Skip to content

fix: handle account token auth when /user probe fails - #49

Merged
mattzcarey merged 4 commits into
cloudflare:mainfrom
dmmulroy:fix/account-token-auth-fallback
Mar 4, 2026
Merged

fix: handle account token auth when /user probe fails#49
mattzcarey merged 4 commits into
cloudflare:mainfrom
dmmulroy:fix/account-token-auth-fallback

Conversation

@dmmulroy

@dmmulroy dmmulroy commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Support account-scoped tokens where /user returns 403 but /accounts succeeds — return { user: null, accounts } so api-token-mode.ts can use account-only auth
  • Only parse /user and /accounts JSON bodies when the response is ok, preventing crashes on non-JSON error bodies (e.g. "Forbidden")
  • Use safeParse instead of parse so malformed payloads log errors and degrade gracefully instead of throwing
  • When both probes fail, map combined status codes to the appropriate OAuthError via throwCombinedCloudflareApiError (prioritizes 5xx > 429 > 401 > 403)
  • Extract fetchCloudflareProbes helper to encapsulate the parallel fetch + network error handling
  • Use schema-derived types (UserSchema, AccountSchema) from types.ts instead of duplicate local type aliases

Test plan

  • npm run check — format, lint, typecheck, and all 83 tests pass
  • New oauth-handler.test.ts covers: account-scoped token fallback, user-only token, dual failures (401/403/429/500/mixed), invalid JSON fallback, invalid payload shape, empty accounts, fetch rejection

dmmulroy added 2 commits March 3, 2026 18:20
Guard JSON parsing behind successful Cloudflare API responses so account-scoped tokens can authenticate when /accounts succeeds and /user returns a non-JSON 403 body. Document request_ip caveat for hosted MCP egress and cover mixed-success cases with tests.
@dmmulroy

dmmulroy commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

/bigbonk review this

Handle mixed-status /user and /accounts failures using combined status classification, normalize fetch failures to OAuthError, and safely parse payload schemas without throwing non-OAuth errors. Expand oauth-handler tests to cover status mappings, malformed/invalid JSON payloads, account fallback semantics, and global fetch stub cleanup.
@mattzcarey

Copy link
Copy Markdown
Contributor

/bonk review this

- Extract fetchCloudflareProbes helper to eliminate awkward let + semicolon-prefix pattern
- Inline single-use parse helpers (parseCloudflareResponse, parseUserFromResponse, parseAccountsFromResponse)
- Replace duplicate CloudflareUser/CloudflareAccount type aliases with schema-derived types from types.ts
- Fix expectOAuthError test helper to catch once instead of awaiting rejected promise twice
- Add clarifying comment for why OAuth callback rejects null user
@mattzcarey
mattzcarey merged commit bd78af8 into cloudflare:main Mar 4, 2026
1 check passed
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.

2 participants