Skip to content

Commit 5a202f6

Browse files
Gforce10-designsudolsteipete
authored
fix(auth): bootstrap codex cli credential without clobbering local (#71310)
* fix(auth): bootstrap codex cli credential without clobbering local readCodexCliCredentialsCached was imported but never registered in EXTERNAL_CLI_SYNC_PROVIDERS, so overlayExternalAuthProfiles could not seed openai-codex:default on fresh agents and runtime surfaced "No API key found for provider openai-codex" even after a successful codex login. Register the provider with a new bootstrapOnly flag. Providers flagged bootstrapOnly are adopted only to fill an empty slot: the overlay skips them when a local OAuth credential already exists for the profile, and readExternalCliBootstrapCredential returns null so the refresh path never replaces the locally stored canonical refresh token with stale CLI state. Minimax keeps its existing replace-on-expiry behavior. * test(auth): cover codex cli bootstrap --------- Co-authored-by: sudol <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 6d49681 commit 5a202f6

4 files changed

Lines changed: 98 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Docs: https://docs.openclaw.ai
106106

107107
### Fixes
108108

109+
- Auth/Codex: bootstrap `openai-codex:default` from Codex CLI credentials on fresh installs without replacing a locally refreshed OpenClaw OAuth token later. Fixes #71305. Thanks @Gforce10-design.
109110
- Plugin SDK/tool-result transforms: bound middleware `details`, validate in-place result mutations, and mark fail-closed middleware fallbacks with canonical `error` status. Thanks @vincentkoc.
110111
- Discord/gateway: prevent startup from getting stuck at `awaiting gateway readiness` when Carbon gateway registration races with a lifecycle reconnect. Fixes #52372. (#68159) Thanks @IVY-AI-gif.
111112
- Discord/gateway: supervise Carbon's async gateway registration promise so fatal Discord metadata failures surface through startup instead of process-level unhandled rejections. (#62451) Thanks @safzanpirani.

‎docs/concepts/oauth.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ To reduce that, OpenClaw treats `auth-profiles.json` as a **token sink**:
4444

4545
- the runtime reads credentials from **one place**
4646
- we can keep multiple profiles and route them deterministically
47-
- when credentials are reused from an external CLI like Codex CLI, OpenClaw
48-
mirrors them with provenance and re-reads that external source instead of
49-
rotating the refresh token itself
47+
- external CLI reuse is provider-specific: Codex CLI can bootstrap an empty
48+
`openai-codex:default` profile, but once OpenClaw has a local OAuth profile,
49+
the local refresh token is canonical; other integrations can remain
50+
externally managed and re-read their CLI auth store
5051

5152
## Storage (where tokens live)
5253

@@ -128,8 +129,11 @@ At runtime:
128129

129130
- if `expires` is in the future → use the stored access token
130131
- if expired → refresh (under a file lock) and overwrite the stored credentials
131-
- exception: reused external CLI credentials stay externally managed; OpenClaw
132-
re-reads the CLI auth store and never spends the copied refresh token itself
132+
- exception: some external CLI credentials stay externally managed; OpenClaw
133+
re-reads those CLI auth stores instead of spending copied refresh tokens.
134+
Codex CLI bootstrap is intentionally narrower: it seeds an empty
135+
`openai-codex:default` profile, then OpenClaw-owned refreshes keep the local
136+
profile canonical.
133137

134138
The refresh flow is automatic; you generally don't need to manage tokens manually.
135139

‎src/agents/auth-profiles.external-cli-sync.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,59 @@ describe("external cli oauth resolution", () => {
236236
expect(credential).toBeNull();
237237
});
238238

239+
it("bootstraps the default codex profile from Codex CLI credentials when missing locally", () => {
240+
mocks.readCodexCliCredentialsCached.mockReturnValue(
241+
makeOAuthCredential({
242+
provider: "openai-codex",
243+
access: "codex-cli-access",
244+
refresh: "codex-cli-refresh",
245+
expires: Date.now() + 5 * 24 * 60 * 60_000,
246+
accountId: "acct-codex",
247+
}),
248+
);
249+
250+
const profiles = resolveExternalCliAuthProfiles(makeStore());
251+
252+
expect(profiles).toEqual([
253+
{
254+
profileId: OPENAI_CODEX_DEFAULT_PROFILE_ID,
255+
credential: expect.objectContaining({
256+
provider: "openai-codex",
257+
access: "codex-cli-access",
258+
refresh: "codex-cli-refresh",
259+
accountId: "acct-codex",
260+
}),
261+
},
262+
]);
263+
});
264+
265+
it("keeps any existing default codex oauth over Codex CLI bootstrap credentials", () => {
266+
mocks.readCodexCliCredentialsCached.mockReturnValue(
267+
makeOAuthCredential({
268+
provider: "openai-codex",
269+
access: "codex-cli-fresh-access",
270+
refresh: "codex-cli-fresh-refresh",
271+
expires: Date.now() + 5 * 24 * 60 * 60_000,
272+
accountId: "acct-codex",
273+
}),
274+
);
275+
276+
const profiles = resolveExternalCliAuthProfiles(
277+
makeStore(
278+
OPENAI_CODEX_DEFAULT_PROFILE_ID,
279+
makeOAuthCredential({
280+
provider: "openai-codex",
281+
access: "local-expired-access",
282+
refresh: "local-canonical-refresh",
283+
expires: Date.now() - 5_000,
284+
accountId: "acct-codex",
285+
}),
286+
),
287+
);
288+
289+
expect(profiles).toEqual([]);
290+
});
291+
239292
it("returns null when the profile id/provider do not map to the same external source", () => {
240293
mocks.readCodexCliCredentialsCached.mockReturnValue(
241294
makeOAuthCredential({ provider: "openai-codex" }),

‎src/agents/auth-profiles/external-cli-sync.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { readMiniMaxCliCredentialsCached } from "../cli-credentials.js";
2-
import { EXTERNAL_CLI_SYNC_TTL_MS, MINIMAX_CLI_PROFILE_ID } from "./constants.js";
1+
import {
2+
readCodexCliCredentialsCached,
3+
readMiniMaxCliCredentialsCached,
4+
} from "../cli-credentials.js";
5+
import {
6+
EXTERNAL_CLI_SYNC_TTL_MS,
7+
MINIMAX_CLI_PROFILE_ID,
8+
OPENAI_CODEX_DEFAULT_PROFILE_ID,
9+
} from "./constants.js";
310
import { log } from "./constants.js";
411
import {
512
areOAuthCredentialsEquivalent,
@@ -29,6 +36,12 @@ type ExternalCliSyncProvider = {
2936
profileId: string;
3037
provider: string;
3138
readCredentials: () => OAuthCredential | null;
39+
// bootstrapOnly providers adopt the external CLI credential only to
40+
// seed an empty slot; once a local OAuth credential exists for the
41+
// profile, the local refresh token is treated as canonical and the
42+
// CLI state must not replace or shadow it. Codex requires this to
43+
// avoid clobbering a locally refreshed token with stale CLI state.
44+
bootstrapOnly?: boolean;
3245
};
3346

3447
function normalizeAuthIdentityToken(value: string | undefined): string | undefined {
@@ -72,6 +85,12 @@ export function isSafeToUseExternalCliCredential(
7285
}
7386

7487
const EXTERNAL_CLI_SYNC_PROVIDERS: ExternalCliSyncProvider[] = [
88+
{
89+
profileId: OPENAI_CODEX_DEFAULT_PROFILE_ID,
90+
provider: "openai-codex",
91+
readCredentials: () => readCodexCliCredentialsCached({ ttlMs: EXTERNAL_CLI_SYNC_TTL_MS }),
92+
bootstrapOnly: true,
93+
},
7594
{
7695
profileId: MINIMAX_CLI_PROFILE_ID,
7796
provider: "minimax-portal",
@@ -103,6 +122,13 @@ export function readExternalCliBootstrapCredential(params: {
103122
if (!provider) {
104123
return null;
105124
}
125+
// bootstrapOnly providers must not replace an existing local credential
126+
// during runtime refresh. The oauth-manager only calls this hook when a
127+
// local credential is already present, so returning null here keeps the
128+
// locally stored refresh token canonical.
129+
if (provider.bootstrapOnly) {
130+
return null;
131+
}
106132
return provider.readCredentials();
107133
}
108134

@@ -132,6 +158,13 @@ export function resolveExternalCliAuthProfiles(
132158
});
133159
continue;
134160
}
161+
if (providerConfig.bootstrapOnly && existingOAuth) {
162+
log.debug("kept local oauth over external cli bootstrap-only provider", {
163+
profileId: providerConfig.profileId,
164+
provider: providerConfig.provider,
165+
});
166+
continue;
167+
}
135168
if (existingOAuth && !isSafeToUseExternalCliCredential(existingOAuth, creds)) {
136169
log.warn("refused external cli oauth bootstrap: identity mismatch", {
137170
profileId: providerConfig.profileId,

0 commit comments

Comments
 (0)