Repository navigation
feat: sign in with a ChatGPT account - #38
Merged
Merged
Conversation
A ChatGPT subscription becomes an upstream like any other: sign in once in
the browser, and the credentials land in config.yaml next to every other
provider.
Login (control plane)
- POST /chatgpt/login starts an OAuth authorization-code + PKCE flow and
waits for the browser on 127.0.0.1:1455 (or 1457), the redirect URIs
registered for the Codex client. GET/DELETE /chatgpt/login/{id} report or
cancel it; LoginFinished is emitted when it settles.
- The callback exchanges the code through the chosen proxy and writes the
provider: protocol chatgpt, refresh + access token + expiry, the account
ID header, subscription billing. Signing in again replaces only the
credentials. The page can hand back to the app through an app-scheme
return_to; web URLs are refused so it cannot be an open redirect.
- A callback with the wrong state is ignored, duplicates exchange the code
once, and the callback port is released as soon as the login settles.
- Deleting a signed-in provider revokes its refresh token, unless another
provider still uses the same one.
Usage and rate-limit reset credits
- GET /providers/{name}/chatgpt/usage returns plan, windows and the number
of reset credits, without the email or user ID in the same response.
- GET/POST /providers/{name}/chatgpt/resets lists and consumes reset
credits. Consuming requires an idempotency key and only happens on an
explicit request; the gateway never spends one on its own.
Gateway
- Requests identify as ThinkWatch (originator, user agent, session ID);
identity headers are not configurable and the client's are not forwarded.
- The backend only streams and rejects max_output_tokens: requests are
shaped accordingly, and a client that asked for a whole response gets one
collected from the stream. The stream has no Content-Type, so success
responses are marked as SSE. This also fixes converted streams being sent
as application/json to non-streaming clients.
- A 401 from an OAuth upstream gets one fresh token and one retry, unless
the token was obtained within the last minute. Refreshes are single-flight
per provider, since rotated refresh tokens are single-use.
- Access tokens and their expiry are persisted to config.yaml on every
renewal, so a restart reuses them instead of rotating the refresh token.
- CredentialExpired and QuotaExhausted are emitted once per transition.
Codex quota windows are named from window-minutes; zero-length windows
are skipped. Models come from /models?client_version.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Thanks for the PR. One thing before review: this targets
No need to close and reopen: click Edit next to the PR title and change the base branch to See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract. |
Merged
fylorn
added a commit
that referenced
this pull request
Sep 17, 2026
Sign in with a ChatGPT account (#38) is the release's headline: the subscription becomes an upstream like any other. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
3 tasks done
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
A ChatGPT subscription becomes an upstream like any other. The user signs in once in the browser, and the credentials go into
config.yamlalongside every other provider.This is phase 3 of the ChatGPT account work. Phase 1 (header credentials) shipped in v0.3.0 and phase 2 (any-to-any conversion) in v0.4.0. The desktop UI comes in phase 4.
Login (control plane)
POST /chatgpt/loginGET /chatgpt/login/{id}pending/done/failed/expired/cancelledDELETE /chatgpt/login/{id}Callback
127.0.0.1:1455, falling back to:1457. These are the redirect URIs registered for the Codex OAuth client. OpenAI does not offer third-party client registration.protocol: chatgptChatGPT-Account-Idheaderbilling: subscriptionRe-login and hand-back
return_tolets the callback page hand back to the app (thinkwatch://…). Web URLs are refused, so this cannot become an open redirect.Robustness
stateis ignored, and the login keeps waiting.LoginFinishedis emitted, so the UI does not need to poll.Revoke on delete
Usage and rate-limit reset credits
GET /providers/{name}/chatgpt/usagereturns the plan, quota windows and the number of available reset credits. The same backend response also carries the email and user ID; those are not passed on.GET /providers/{name}/chatgpt/resetslists the reset credits.POST /providers/{name}/chatgpt/resetsconsumes one.Gateway
Identity
originator, user agent, session ID.Request and response shape
max_output_tokens, so requests are shaped to match.Content-Type, so successful responses are marked as SSE.application/json.OAuth
config.yamlon every renewal. A restart reuses them instead of rotating the refresh token.Events and quota
CredentialExpiredandQuotaExhaustedare emitted once per transition. They are the inputs for desktop notifications.window-minutes, and zero-length windows are skipped./models?client_version=….Test plan
cargo fmt --all -- --checkcargo clippy --workspace --all-targets -- -D warningscargo test --workspace --no-fail-fasttw-gateway/tests/chatgpt.rs(8): identity, whole-response collection, 401 retry, no refresh storm, single-flight, revoked login, quota exhaustion, modelstw-control/tests/chatgpt.rs(14):./scripts/smoke.sh: 48 passed, 0 failed. Memory is 34 MB against a 30 MB target, which the script reports as over target but not a regression.🤖 Generated with Claude Code