Skip to content

feat(connect): reuse a sign-in another CLI already completed - #427

Merged
mohitranka merged 2 commits into
mainfrom
feat/reuse-provider-logins
Aug 23, 2026
Merged

mohitranka merged 2 commits into
mainfrom
feat/reuse-provider-logins

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

Forge keeps its own credential store and reads nothing else, so someone already signed in to codex, opencode or grok on the same machine still got sent through a second device-code grant in a browser. Nothing about that second grant is more secure — same account, same OAuth client, same machine. It's friction, and during the four-way benchmark it was the one thing that stopped Forge from starting at all.

What it does

forge-connect gains an import module that finds existing sessions:

File Source Satisfies
~/.codex/auth.json Codex openai_codex, openai
~/.local/share/opencode/auth.json OpenCode openai_codex, xai, opencode_go, anthropic
~/.grok/auth.json Grok xai

A route with a reusable session shows a reuse Codex tag in the provider picker's existing right-aligned tag column:

▶ ▾ OpenAI
      API
      ChatGPT                                   reuse Codex

Pressing Enter on that row is the consent — it imports instead of opening a browser. Discovery itself is read-only and never adopts anything on its own, and a failed import falls through to the ordinary flow rather than stranding you.

Why the tokens actually work

Client ids line up, so imported refresh tokens stay refreshable. Forge's openai-codex profile already uses the Codex CLI's own public client (oauth_openai_codex.rs:8), and the Grok store is literally keyed by the same client id oauth_xai.rs:21 defaults to.

Two format traps

  • OpenCode stores expiries in milliseconds; Forge's parser reads bare digits as epoch seconds. Passing them through unchanged would have dated every token ~50,000 years out, so it would never have been refreshed. Converted, with a test.
  • Codex records last_refresh, not an expiry, so that import leaves expires_at unset — "use until the API rejects it" is the honest reading. Inventing a lifetime would either refresh needlessly or claim validity we can't know.

Handling secrets

DiscoveredLogin and DiscoveredSecret have hand-written Debug impls that redact, with a test asserting no token material survives a {:?}. Discovery takes its paths as arguments rather than reading XDG_DATA_HOME internally, so it's a pure function of them — the first version read the env var inside, which meant that with the var set it ignored its own home argument and read the real user's file, and the tests passed only by luck.

Also: the other half of #426

ConversationRenderKey carried no field for connection state or model, but the home splash bakes both into its card. The card was built once and frozen, so a freshly connected session still read provider ● not connected while the footer said otherwise. Invalidating the credential cache in #426 was necessary but not sufficient — the render cache had to learn about it too. Caught by running this feature in a PTY, not by reading code.

Verification

cargo fmt --all -- --check clean · cargo clippy --all-targets clean · cargo test --workspace 45/45 binaries, 0 failures · 9 discovery tests + 5 wiring tests.

End to end, from a Forge home containing nothing but a copied .codex/auth.json:

model      openai-codex/gpt-5.6-sol
provider   OpenAI  ● connected
...
Reply with exactly: PONG
PONG
✓  Answered in 1.8s

No browser, and on the profile's declared default model.

https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs

Forge keeps its own credential store and reads nothing else, so a user
already signed in to codex, opencode or grok on the same machine was still
sent through a second device-code grant in a browser. Nothing about that
second grant is more secure — same account, same OAuth client, same machine
— it is just friction, and it was the one thing that blocked Forge from
starting at all during the four-way benchmark.

forge-connect gains an `import` module that finds those sessions:

  ~/.codex/auth.json                      Codex     -> openai_codex, openai
  ~/.local/share/opencode/auth.json       OpenCode  -> openai_codex, xai,
                                                       opencode_go, anthropic
  ~/.grok/auth.json                       Grok      -> xai

Discovery is read-only and never adopts anything on its own. A route that
has a reusable session shows a `reuse Codex` tag in the existing
right-aligned tag column of the provider picker, and pressing Enter on that
row — which is the consent — imports instead of opening a browser. A failed
import falls through to the ordinary flow rather than stranding the user.

Client ids line up, so imported refresh tokens stay refreshable: Forge's
openai-codex profile already uses the Codex CLI's own public client, and the
Grok store is literally keyed by the same client id `oauth_xai` defaults to.

Two format details that would have caused silent misbehaviour. OpenCode
stores expiries in milliseconds while Forge's parser reads bare digits as
epoch seconds, so passing them through unchanged would have dated every
token ~50,000 years out and it would never have been refreshed. Codex
records `last_refresh` rather than an expiry, so that import leaves
`expires_at` unset — "use until the API rejects it" is the honest reading.

Secrets never reach a log line: `DiscoveredLogin` and `DiscoveredSecret`
have hand-written Debug impls that redact, with a test that asserts it.
Discovery takes its paths as arguments rather than reading XDG_DATA_HOME
internally, so it is a pure function of them and the tests are hermetic
rather than passing by luck on a machine where the var happens to be unset.

Also fixes the other half of the connect contradiction #426 addressed.
ConversationRenderKey carried no field for connection state or model, but
the home splash bakes both into its card — so the card was built once and
frozen, and a freshly connected session still read "not connected" while
the footer said otherwise. Invalidating the credential cache (#426) was
necessary but not sufficient; the render cache had to learn about it too.

Verified end to end: a Forge home containing nothing but a copied
.codex/auth.json reaches `provider OpenAI ● connected` on
openai-codex/gpt-5.6-sol with no browser, and answers a real turn.

Claude-Session: https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs
It passed locally and failed on CI, for a reason worth recording.

The fixture sets `provider: "mock"`, and `is_mock_provider` short-circuits
`provider_connected_cached` to true — so on CI the card read "connected"
before the test had connected anything, and the first assertion failed. It
passed on this machine only because `TuiApp::new` restored a real persisted
selection (`native` / `xai/grok-3`) over the fixture's mock, which happens
to make the connection logic run for real.

So the test was asserting against whichever provider the host happened to
leave behind. Pin both fields explicitly after construction; neither the
fixture nor the host can reach them now.

Claude-Session: https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs
@mohitranka
mohitranka merged commit acffa04 into main Aug 23, 2026
7 checks passed
@mohitranka
mohitranka deleted the feat/reuse-provider-logins branch August 23, 2026 00:40
mohitranka added a commit that referenced this pull request Sep 11, 2026
* feat(connect): reuse a sign-in another CLI already completed

Forge keeps its own credential store and reads nothing else, so a user
already signed in to codex, opencode or grok on the same machine was still
sent through a second device-code grant in a browser. Nothing about that
second grant is more secure — same account, same OAuth client, same machine
— it is just friction, and it was the one thing that blocked Forge from
starting at all during the four-way benchmark.

forge-connect gains an `import` module that finds those sessions:

  ~/.codex/auth.json                      Codex     -> openai_codex, openai
  ~/.local/share/opencode/auth.json       OpenCode  -> openai_codex, xai,
                                                       opencode_go, anthropic
  ~/.grok/auth.json                       Grok      -> xai

Discovery is read-only and never adopts anything on its own. A route that
has a reusable session shows a `reuse Codex` tag in the existing
right-aligned tag column of the provider picker, and pressing Enter on that
row — which is the consent — imports instead of opening a browser. A failed
import falls through to the ordinary flow rather than stranding the user.

Client ids line up, so imported refresh tokens stay refreshable: Forge's
openai-codex profile already uses the Codex CLI's own public client, and the
Grok store is literally keyed by the same client id `oauth_xai` defaults to.

Two format details that would have caused silent misbehaviour. OpenCode
stores expiries in milliseconds while Forge's parser reads bare digits as
epoch seconds, so passing them through unchanged would have dated every
token ~50,000 years out and it would never have been refreshed. Codex
records `last_refresh` rather than an expiry, so that import leaves
`expires_at` unset — "use until the API rejects it" is the honest reading.

Secrets never reach a log line: `DiscoveredLogin` and `DiscoveredSecret`
have hand-written Debug impls that redact, with a test that asserts it.
Discovery takes its paths as arguments rather than reading XDG_DATA_HOME
internally, so it is a pure function of them and the tests are hermetic
rather than passing by luck on a machine where the var happens to be unset.

Also fixes the other half of the connect contradiction #426 addressed.
ConversationRenderKey carried no field for connection state or model, but
the home splash bakes both into its card — so the card was built once and
frozen, and a freshly connected session still read "not connected" while
the footer said otherwise. Invalidating the credential cache (#426) was
necessary but not sufficient; the render cache had to learn about it too.

Verified end to end: a Forge home containing nothing but a copied
.codex/auth.json reaches `provider OpenAI ● connected` on
openai-codex/gpt-5.6-sol with no browser, and answers a real turn.

Claude-Session: https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs

* test(tui): pin the route in the home-card repaint test

It passed locally and failed on CI, for a reason worth recording.

The fixture sets `provider: "mock"`, and `is_mock_provider` short-circuits
`provider_connected_cached` to true — so on CI the card read "connected"
before the test had connected anything, and the first assertion failed. It
passed on this machine only because `TuiApp::new` restored a real persisted
selection (`native` / `xai/grok-3`) over the fixture's mock, which happens
to make the connection logic run for real.

So the test was asserting against whichever provider the host happened to
leave behind. Pin both fields explicitly after construction; neither the
fixture nor the host can reach them now.

Claude-Session: https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs
mohitranka added a commit that referenced this pull request Sep 11, 2026
* feat(connect): reuse a sign-in another CLI already completed

Forge keeps its own credential store and reads nothing else, so a user
already signed in to codex, opencode or grok on the same machine was still
sent through a second device-code grant in a browser. Nothing about that
second grant is more secure — same account, same OAuth client, same machine
— it is just friction, and it was the one thing that blocked Forge from
starting at all during the four-way benchmark.

forge-connect gains an `import` module that finds those sessions:

  ~/.codex/auth.json                      Codex     -> openai_codex, openai
  ~/.local/share/opencode/auth.json       OpenCode  -> openai_codex, xai,
                                                       opencode_go, anthropic
  ~/.grok/auth.json                       Grok      -> xai

Discovery is read-only and never adopts anything on its own. A route that
has a reusable session shows a `reuse Codex` tag in the existing
right-aligned tag column of the provider picker, and pressing Enter on that
row — which is the consent — imports instead of opening a browser. A failed
import falls through to the ordinary flow rather than stranding the user.

Client ids line up, so imported refresh tokens stay refreshable: Forge's
openai-codex profile already uses the Codex CLI's own public client, and the
Grok store is literally keyed by the same client id `oauth_xai` defaults to.

Two format details that would have caused silent misbehaviour. OpenCode
stores expiries in milliseconds while Forge's parser reads bare digits as
epoch seconds, so passing them through unchanged would have dated every
token ~50,000 years out and it would never have been refreshed. Codex
records `last_refresh` rather than an expiry, so that import leaves
`expires_at` unset — "use until the API rejects it" is the honest reading.

Secrets never reach a log line: `DiscoveredLogin` and `DiscoveredSecret`
have hand-written Debug impls that redact, with a test that asserts it.
Discovery takes its paths as arguments rather than reading XDG_DATA_HOME
internally, so it is a pure function of them and the tests are hermetic
rather than passing by luck on a machine where the var happens to be unset.

Also fixes the other half of the connect contradiction #426 addressed.
ConversationRenderKey carried no field for connection state or model, but
the home splash bakes both into its card — so the card was built once and
frozen, and a freshly connected session still read "not connected" while
the footer said otherwise. Invalidating the credential cache (#426) was
necessary but not sufficient; the render cache had to learn about it too.

Verified end to end: a Forge home containing nothing but a copied
.codex/auth.json reaches `provider OpenAI ● connected` on
openai-codex/gpt-5.6-sol with no browser, and answers a real turn.

Claude-Session: https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs

* test(tui): pin the route in the home-card repaint test

It passed locally and failed on CI, for a reason worth recording.

The fixture sets `provider: "mock"`, and `is_mock_provider` short-circuits
`provider_connected_cached` to true — so on CI the card read "connected"
before the test had connected anything, and the first assertion failed. It
passed on this machine only because `TuiApp::new` restored a real persisted
selection (`native` / `xai/grok-3`) over the fixture's mock, which happens
to make the connection logic run for real.

So the test was asserting against whichever provider the host happened to
leave behind. Pin both fields explicitly after construction; neither the
fixture nor the host can reach them now.

Claude-Session: https://claude.ai/code/session_018ihDDc3ahfrxu5AYL9moXs
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