Skip to content

feat(oauth): code-display login mode for hosts with no reachable localhost (carry #6239) - #7170

Merged
lidge-jun merged 6 commits into
devfrom
carry/6239-oauth-code-login
Oct 15, 2026
Merged

lidge-jun merged 6 commits into
devfrom
carry/6239-oauth-code-login

Conversation

@lidge-jun

Copy link
Copy Markdown
Owner

Summary

Carries #6239 by @vadymhimself (source head 0192c2e4b) onto current dev. That branch was 676 commits behind and conflicted in the Anthropic OAuth, login-registry, and CLI files.

Problem. Anthropic login always redirects to http://localhost:54545/callback. When ocx runs somewhere the operator's browser can't reach (a container, a remote server, a hosted gateway), approval lands on a dead localhost page.

Change. Anthropic's code-display redirect (https://platform.claude.com/oauth/code/callback, the same OAuth client Claude Code uses) shows a code#state string that the user pastes back. No callback port is bound and no browser is launched.

  • POST /api/oauth/login accepts an optional mode. Omitting it, or sending "callback", keeps today's flow and response shape. "code" returns {url, mode, flowId, expiresAt}. Providers without a code-display redirect are refused with code_mode_unsupported and never fall back to localhost.
  • POST /api/oauth/login/code with that flowId answers with the outcome of the token exchange: {ok:true, account} or {ok:false, error}, where error is state_mismatch, invalid_or_expired_code, no_pending_login, provider_unreachable or malformed_input. Requests without a flowId keep the existing accept-only contract.
  • In-memory login flow state is keyed by flow id, so several code logins can be in flight. Provider-only callers (GUI, CLI, the Codex auth API) still act on the provider's newest flow. Callback mode still allows one flow per provider because it binds a fixed port.
  • Bounds: a 10-minute paste window, at most 64 rows per provider, and expired flows reaped when the next login starts. A code flow is single-use once a paste reaches the token exchange. A paste with a mismatched state is refused before any token request. The PKCE verifier is per flow, and the same redirect URI is used for authorize and exchange.
  • CLI: ocx login anthropic --headless prints the URL and reads the code back. Codex spellings are refused with a pointer to ocx account login codex --device. Other unsupported providers exit with the list of supported ones.

Differences from the source branch.

  • Code mode is wired through the shared anthropicOAuthDefinition added on dev since the PR was opened, so anthropic and anthropic2 both support it. The anthropic2 local-import refusal and the Claude-import identity binding are unchanged.
  • The CLI flag is --headless instead of --code. On dev, --code <value> carries secret authorization material (ocx account login --code), and the argument redactor scrubs the word after it from stderr. That turned ocx login --code codex into <redacted> has no code-display login.
  • Fixes from an independent security review (three rounds, GPT-6.1-Sol, high reasoning):
    • The paste route reports the account this flow wrote, taken from the store's write receipt (saveCredential now returns the receipt it already produced).
    • A flow cleared during its exchange answers no_pending_login instead of success.
    • A flowId is honoured only for a code flow of the same provider, in the paste route and in status.
    • A paste that races the start is refused instead of using up the flow.
    • A failed --headless login prints only fixed text, never the provider error body.

Docs: structure/providers/oauth-code-display-login.md (new owner doc, linked from gui-and-management-api.md), docs-site CLI reference, the providers guide, and the management API reference.

Co-authored-by: Vadym O. [email protected]

Verification

On head 252db6821, branch is on the current dev tip (0 behind), sandboxed OPENCODEX_HOME/CODEX_HOME:

bun run typecheck                                   # clean
bun test tests/oauth/oauth-code-login.test.ts       # 29 pass / 0 fail
bun test tests/oauth                                # 895 pass / 1 skip / 0 fail
bun test tests/cli/cli-provider.test.ts tests/providers/kiro/kiro-oauth.test.ts  # 118 pass / 0 fail
bun test tests/server/management                    # 565 pass / 0 fail (on 2474604a0)
bun test tests/cli                                  # 6363 pass / 43 skip / 2 fail (on 2474604a0, see below)
bun test tests/test-layout.test.ts tests/test-layout-tooling.test.ts \
  tests/ci-workflows/structure-ssot.test.ts tests/ci-workflows/file-size-ratchet.test.ts \
  tests/ci-workflows/skill-ocx.test.ts              # 102 pass / 0 fail
bun scripts/file-size-ratchet.ts                    # passed
bun run structure:check                             # passed
bun run skill:surface:check                         # passed
bun run privacy:scan                                # passed

bun run test:changed on 2474604a0 ran 1767 files: 38455 pass, 107 skip, 23 fail, 4 errors, all in areas this change does not touch (service install state, live audio, desktop settings, Codex/Grok toggles, headless parity, sibling-home client sync). Run alone, each of those files passes on this branch except tests/cli/sibling-home-client-sync.test.ts ("a secondary ensure parent preserves shared Grok, Codex and Claude agent bytes", 30s timeout). That test fails the same way on a clean checkout of the merge base 7640a942b, where tests/cli/cli-headless-parity.test.ts also fails 2 tests. The two tests/cli failures above are those same two files. The last commit (252db6821, reauth write reporting plus test stub return types) was re-verified with the oauth, CLI provider and Kiro runs listed above.

New tests in tests/oauth/oauth-code-login.test.ts cover each review fix: single-use, own-account reply, concurrent same-account logins, clear during exchange, a paste racing the start, provider and callback flow-id scoping, and no provider body on CLI stderr. Each new case was confirmed to fail or hang with its source fix stashed. The full bun run test was not run locally because of contention across concurrent worktrees; full cross-platform CI was dispatched on the carry branch: https://github.com/lidge-jun/opencodex/actions/runs/38717906093 (queued at the time of writing).

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

GOAT and others added 4 commits October 15, 2026 18:33
…lhost (carry #6239)

Anthropic's code-display redirect lets a login complete from a container or
remote host: the user approves in any browser and pastes the displayed
code#state, and no callback port is bound. POST /api/oauth/login accepts
mode:"code" and returns a flowId and expiresAt; POST /api/oauth/login/code
answers with the token-exchange outcome for a code flow. Login flow state
is keyed by flow id so concurrent code logins stay separate, with a
10-minute paste window and a 64-row per-provider cap. The CLI spelling is
`ocx login <provider> --headless`.

Carried onto current dev from #6239 (head 0192c2e). Changes from the
source: code mode is wired through the shared anthropicOAuthDefinition so
anthropic and anthropic2 both support it; the CLI flag is --headless
because --code carries a secret value elsewhere in the CLI and its operand
is scrubbed from stderr; a code flow is single-use once a paste reaches
the token exchange.

Co-authored-by: Vadym O. <[email protected]>
…eep flow ids scoped

Independent review fixes for the carried code-display login:
- the paste route reports the account this flow persisted, and a flow
  cleared during its exchange answers no_pending_login instead of success
- a flowId is honoured only for a code flow of the same provider, in both
  the paste route and getLoginStatus, so only single-use code flows hold
  a waiting request
- a paste that races the start is refused instead of spending the flow
- ocx login --headless prints only fixed text on failure, never the
  provider error body

Co-authored-by: Vadym O. <[email protected]>
Matching the returned refresh token against the account set missed the row
when a concurrent login for the same account rotated the token before the
earlier flow settled, so that flow answered without its account.
saveCredential now returns the write receipt it already produced, and
runLogin reports the persisted account id to startLoginFlow.

Co-authored-by: Vadym O. <[email protected]>
saveAccountCredential resolves false when the account vanished before the
queued write, and runLogin reports the persisted account only after a real
write. Test stubs of saveCredential now return the write receipt.

Co-authored-by: Vadym O. <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Repository: lidge-jun/opencodex/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 1c318125-ce1b-4b84-8199-57abb7fe4734
📥 Commits

Reviewing files that changed from the base of the PR and between 973204b and 93c1aa3.

📒 Files selected for processing: 23
  • docs-site/src/content/docs/guides/providers.md
  • docs-site/src/content/docs/reference/cli/providers-accounts.md
  • docs-site/src/content/docs/reference/management-api.md
  • scripts/test-layout/layout.json
  • src/cli/dispatch.ts
  • src/cli/registry.ts
  • src/oauth/anthropic-oauth-definitions.ts
  • src/oauth/anthropic.ts
  • src/oauth/callback-server.ts
  • src/oauth/index.ts
  • src/oauth/login-cli.ts
  • src/oauth/login-flow-state.ts
  • src/oauth/store.ts
  • src/server/management/oauth-account-routes.ts
  • structure/INDEX.md
  • structure/gui-and-management-api.md
  • structure/manifest.json
  • structure/providers/oauth-code-display-login.md
  • tests/cli/cli-provider.test.ts
  • tests/fixtures/test-layout-expected.json
  • tests/oauth/oauth-code-login.test.ts
  • tests/oauth/oauth-public-surface.test.ts
  • tests/oauth/oauth-reauth-bind.test.ts
+-----------------------------------------------+
| Crouching Tiger, Hidden NullPointerException. |
+-----------------------------------------------+
  \
   \     /)/)
        ( '')
        /  |
      o(___)
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

GOAT and others added 2 commits October 16, 2026 00:50
# Conflicts:
#	structure/INDEX.md
# Conflicts:
#	structure/INDEX.md
@lidge-jun

Copy link
Copy Markdown
Owner Author

Maintainer integration into dev (MAINTAINERS.md dev maintainer-integration policy; batch integration authorized by @lidge-jun on 2026-10-15).

  • Head: 93c1aa3b45b0968a5bc4587e27902fd56bcb15b9 (tree 49da0650541923e73768d4a9277ddb542f6e668a) = this branch merged with origin/dev 973204b521. Integration note: auto-resolved union conflicts: structure/INDEX.md.
  • Exact-head evidence: GitHub-hosted Actions for this account have not been assigning runners since 2026-10-15 13:30 UTC, so by owner decision the evidence is a full bun run test (Bun 1.4.3 test runner, root and GUI dependencies installed, GUI built) of this exact tree on a maintainer host (macOS arm64): exit 1, 9 min, 4 failing test(s), each also failing in the same host's baseline run of dev 60bf4c7 (environment-only): a secondary ensure parent preserves shared Grok, Codex and Claude agent bytes, connected Desktop uses hub IDs across a cold restart (stored profile=false), live audio readiness and bounded closure > native open and non-readiness events cannot cla, update stops the running proxy before replacing files > npm launcher restarts the stopped .
  • Local gates on the same tree: bun run typecheck, file-size ratchet, test-layout and structure:check.
  • Hosted CI checks for this head may still be queued; they are not claimed as evidence.

@lidge-jun
lidge-jun marked this pull request as ready for review October 15, 2026 19:44
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner October 15, 2026 19:45
@lidge-jun
lidge-jun merged commit 37fcd24 into dev Oct 15, 2026
5 of 6 checks passed
@lidge-jun
lidge-jun deleted the carry/6239-oauth-code-login branch October 15, 2026 19:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T19:47:56.043552Z 93c1aa3 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 93c1aa3b45

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +311 to +314
const { url: authUrl, instructions, deviceCode, expiresAt } = await startLoginFlow(provider, {
forceLogin: body.addAccount === true || reauth,
...(accountId ? { reauthAccountId: accountId } : {}),
...(codeMode ? { codeMode: true } : {}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Suppress browser launch in code mode

When POST /api/oauth/login requests mode: "code" without explicitly setting openBrowser: false, the new branch still falls through to the existing launcher, and shouldOpenBrowserForLogin(undefined, config) defaults to true. This violates the headless mode contract and unexpectedly opens the authorization page on the proxy host; skip the browser-launch block whenever codeMode is true.

Useful? React with 👍 / 👎.

Comment on lines +185 to +188
if (error instanceof AnthropicTokenError) {
return error.httpStatus !== undefined && error.httpStatus >= 500
? "provider_unreachable"
: "invalid_or_expired_code";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Report pre-connection DNS failures as unreachable

When a direct Anthropic token request fails DNS resolution, postJson deliberately wraps it in AnthropicTokenError with requestNotSent === true and no HTTP status. This branch classifies that network failure as invalid_or_expired_code before the generic network checks can run, so headless callers receive incorrect remediation; classify requestNotSent as provider_unreachable.

Useful? React with 👍 / 👎.

Comment on lines +343 to +348
return jsonResponse({
url: authUrl,
instructions,
deviceCode,
browserLaunch,
...(codeMode ? { mode: "code", flowId, expiresAt } : {}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the returned flow ID when reading OAuth status

The code-mode start response now returns a flowId, but GET /api/oauth/status?provider=anthropic&flowId=<id> still calls getLoginStatus without passing that query parameter. With two concurrent code-display logins, status for flow A therefore reports whichever flow is newest (usually B), despite getLoginStatus supporting an explicit ID; pass the validated flowId through for non-Kiro OAuth flows.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant