|
| 1 | +# Unified provider authentication POC |
| 2 | + |
| 3 | +Status: accepted for implementation |
| 4 | +Date: 2026-08-09 |
| 5 | + |
| 6 | +## Goal |
| 7 | + |
| 8 | +Prove that JCode can bind a Provider to a managed account instead of copying a |
| 9 | +short-lived access token into `config.json`. The POC covers the three managed |
| 10 | +login methods already implemented by cc-switch: |
| 11 | + |
| 12 | +| JCode login | Device authorization | Runtime API | |
| 13 | +| --- | --- | --- | |
| 14 | +| ChatGPT / Codex | `auth.openai.com/api/accounts/deviceauth/*`, then OAuth code exchange | OpenAI Responses at `chatgpt.com/backend-api/codex/responses` | |
| 15 | +| Grok / xAI | OIDC discovery plus OAuth 2.0 Device Authorization Grant | OpenAI Responses at `api.x.ai/v1/responses` | |
| 16 | +| GitHub Copilot | GitHub device flow, then GitHub-to-Copilot token exchange | Account catalog selects `/v1/responses` for OpenAI models and `/chat/completions` for other vendors | |
| 17 | + |
| 18 | +API-key authentication remains supported and is the default for existing |
| 19 | +configuration. Managed login is opt-in and backward compatible. |
| 20 | + |
| 21 | +## Evidence copied from cc-switch |
| 22 | + |
| 23 | +The implementation contract is derived from the local checkout at |
| 24 | +`/Users/jack/workpath/opensource/cc-switch`, not from a generic OAuth |
| 25 | +assumption: |
| 26 | + |
| 27 | +- Provider configuration stores only an account binding; token lookup happens |
| 28 | + immediately before each upstream request. |
| 29 | +- Access tokens are memory-only. Refresh tokens (or the long-lived GitHub |
| 30 | + token for Copilot) are durable, owner-only secrets. |
| 31 | +- Account lists and the default account are shared across Providers. A Provider |
| 32 | + may bind a specific account or follow the current default. |
| 33 | +- Refresh is serialized per account and checked again after taking the lock. |
| 34 | +- An invalid refresh marks the account as requiring reauthentication. It never |
| 35 | + falls back to an API key or another Provider silently. |
| 36 | +- Managed authentication pins the upstream origin, wire protocol and protected |
| 37 | + headers. User-supplied headers cannot replace them. |
| 38 | + |
| 39 | +## POC boundaries |
| 40 | + |
| 41 | +The automated POC is executable through unit and HTTP-handler tests without a |
| 42 | +developer account and never sends a live billable model request. |
| 43 | + |
| 44 | +The test transport substitutes local servers for each fixed remote endpoint |
| 45 | +and proves: |
| 46 | + |
| 47 | +1. start returns a public verification URL, user code, bounded expiry and a |
| 48 | + random opaque flow ID; the upstream device token is never returned; |
| 49 | +2. poll maps pending, slow-down, denied, expired and success responses into one |
| 50 | + public state machine; |
| 51 | +3. successful login persists the durable credential before publishing an |
| 52 | + in-memory access token; |
| 53 | +4. concurrent inference requests perform at most one refresh per account; |
| 54 | +5. refresh-token rotation uses compare-and-swap semantics and invalid refresh |
| 55 | + persists `requires_reauth`; |
| 56 | +6. a Provider binding contains only `method` and optional `account_id`; |
| 57 | +7. model requests resolve a fresh credential and inject protected headers at |
| 58 | + dispatch time; |
| 59 | +8. ChatGPT/xAI chat requests select Responses. Copilot resolves the live model |
| 60 | + vendor per account, using Responses for OpenAI models and Chat Completions |
| 61 | + for other advertised vendors; |
| 62 | +9. public flow responses, Provider bindings, durable-state fixtures and runtime |
| 63 | + credential serialization expose no access token, refresh token, GitHub |
| 64 | + token, authorization code or device token; |
| 65 | +10. the durable store is `0700`/`0600`, written through fsync plus atomic |
| 66 | + rename, and mutations are guarded across processes. |
| 67 | +11. redirects cannot forward a managed model body, bearer token, or protected |
| 68 | + header to a second origin; |
| 69 | +12. cancel and logout are linearized with authorization commit, including |
| 70 | + flows owned by a second manager process, and flow capacity is reserved |
| 71 | + before any authorization-server request; |
| 72 | +13. Copilot user, tool-continuation, and subagent requests receive the correct |
| 73 | + initiator/interaction headers while one session keeps a stable opaque |
| 74 | + interaction ID. |
| 75 | +14. managed model catalogs are fetched with the selected account credential, |
| 76 | + bounded to 1 MiB, filtered by provider visibility, and never expose an |
| 77 | + upstream response body on error. Explicitly enabled live chat models retain |
| 78 | + their wire protocol across restart; |
| 79 | +15. xAI image and video entries are separated from chat. The official managed |
| 80 | + xAI image profile pins `api.x.ai/v1/images/generations`, resolves its token |
| 81 | + only at dispatch, and reuses the billable-operation approval, journal, |
| 82 | + quota, Artifact, and safe-download pipeline. Its adapter emits native |
| 83 | + `aspect_ratio` and `resolution` fields and never mixes them with OpenAI |
| 84 | + `size` controls; |
| 85 | +16. the Web/Desktop Provider catalog uses a token-free, account-aware local |
| 86 | + stale-while-revalidate cache. It renders the last successful list before |
| 87 | + the live request completes, retains it on transient failure, and rejects |
| 88 | + stale responses from an earlier account or refresh generation. |
| 89 | + |
| 90 | +## Accepted result |
| 91 | + |
| 92 | +The POC is accepted when the focused auth, catalog, model transport and provider |
| 93 | +API tests pass without network access. A manual account-scoped smoke test also |
| 94 | +confirmed that xAI and GitHub Copilot return their live model catalogs, and that |
| 95 | +the connected xAI account can read the official image-generation catalog. A |
| 96 | +later, explicitly user-authorized one-shot smoke test also confirmed that the |
| 97 | +managed xAI OAuth credential can generate one image without a separate API key; |
| 98 | +the generated asset and credentials were not committed. That billable smoke |
| 99 | +used `grok-imagine-image`; the quality variant is covered by the same local wire |
| 100 | +contract but was not invoked live to avoid a second provider charge. |
0 commit comments