Skip to content

Commit e497d01

Browse files
committed
fix(auth): run imported Claude CLI logins as verified native passthrough
1 parent 1227cf2 commit e497d01

6 files changed

Lines changed: 237 additions & 17 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Docs: https://docs.openclaw.ai
8585
- **Control UI cloud session thinking:** expose reasoning level in the New Session model picker and persist the selected level before cloud dispatch.
8686
- **iOS fresh-install setup:** atomically redact spent setup credentials before Keychain cleanup so a deferred item deletion no longer disconnects a successfully paired device. Fixes #107591. Thanks @dagmarjeeves-lab.
8787
- **Tlon SSE connect cleanup:** disarm opening deadlines after failed HTTP responses and rejected stream opens so reconnect attempts cannot leave stale timers behind. (#104585) Thanks @hugenshen.
88+
- **Claude CLI login reuse:** run imported `claude` logins as identity-verified native passthrough instead of forwarding token snapshots, so Claude refreshes its own credentials during runs again; managed Anthropic OAuth profiles refresh and persist before spawn, and selected profiles that cannot materialize, mismatch the host login, or expire fail closed with the exact re-auth command instead of silent 401 runs. Fixes #115137. Thanks @bergenreese for the report.
8889
- **LINE reply-token media kinds:** honor video and audio metadata on inbound replies, share the canonical media builder with proactive sends, and fail visibly instead of recording empty media-only deliveries. (#106515) Thanks @edenfunf.
8990
- **Mattermost websocket connection deadlines:** bound opening handshakes so stalled TCP peers cannot hang channel startup indefinitely and reconnect control resumes after timeout. (#105553) Thanks @hugenshen.
9091
- **Queued TTS retries:** copy local outbound media into queue-owned storage before enqueueing so voice replies survive producer temp cleanup and restart recovery, retain referenced artifacts through retry backoff, and prune unreferenced spool files after one day. Fixes #108501. (#108502) Thanks @masatohoshino.

‎docs/gateway/authentication.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ openclaw models auth login --provider anthropic --method cli --set-default
5858

5959
This is two steps: log Claude Code into Anthropic on the host, then tell OpenClaw to route Anthropic model selection through the local `claude-cli` backend and store the matching OpenClaw auth profile.
6060

61+
At run time OpenClaw treats a reused Claude CLI login as Claude's own credential: it verifies the host's current `claude` login matches the selected profile's account and then lets the `claude` subprocess authenticate natively, so Claude keeps refreshing its own login during runs. OpenClaw never forwards a copied token for this path. If the host login is missing or belongs to a different account, the run fails before spawn with the exact re-authentication commands. OpenClaw-managed credentials (Anthropic OAuth login profiles, setup tokens, API keys) are still delivered to the subprocess directly and refreshed by OpenClaw where applicable.
62+
6163
The gateway service must resolve `claude` on `PATH`. If a deployment needs a
6264
nonstandard executable path, register a wrapper through a
6365
[CLI backend plugin](/plugins/cli-backend-plugins).

‎extensions/anthropic/cli-backend.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ function createClaudeCliAuthInput(params: {
7373
function resolveClaudeCliAuthInput(
7474
credential: ClaudeCliAuthCredential | undefined,
7575
): ClaudeCliPreparedExecution | undefined {
76+
// Forwarded OAuth here is OpenClaw-managed material (its refresh path is
77+
// OpenClaw-owned). Imported native `claude` logins are never forwarded —
78+
// core runs those as identity-verified passthrough — so an expired token
79+
// reaching this point is a real fault worth failing loudly, not refreshable
80+
// state this plugin could repair.
7681
if (credential?.type === "oauth" && "access" in credential) {
7782
const expires = "expires" in credential ? credential.expires : undefined;
7883
if (typeof expires !== "number" || !Number.isFinite(expires) || expires <= Date.now()) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ type ExternalCliSyncProvider = {
5454
};
5555

5656
// Keep this gate aligned with the canonical identity-copy rule in oauth.ts.
57+
// Also the passthrough gate in cli-runner/prepare.ts: a live CLI login that
58+
// this sync would refuse to import must not authenticate a run either.
5759
/** Return true when imported CLI credentials match an existing profile identity. */
58-
function isSafeToUseExternalCliCredential(
60+
export function isSafeToUseExternalCliCredential(
5961
existing: OAuthCredential | undefined,
6062
imported: OAuthCredential,
6163
): boolean {

‎src/agents/cli-runner/prepare.test.ts‎

Lines changed: 155 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
createTestRegistry,
2626
} from "../../test-utils/channel-plugins.js";
2727
import { captureEnv, setTestEnvValue } from "../../test-utils/env.js";
28+
import { readExternalCliBootstrapCredential as readExternalCliBootstrapCredentialImpl } from "../auth-profiles/external-cli-sync.js";
2829
import { resolveApiKeyForProfile as resolveApiKeyForProfileImpl } from "../auth-profiles/oauth.js";
2930
import {
3031
loadAuthProfileStoreWithoutExternalProfiles,
@@ -339,6 +340,7 @@ describe("prepareCliRunContext", () => {
339340
cleanup: vi.fn(async () => undefined),
340341
})),
341342
getClaudeLiveSessionGenerationForOwner: vi.fn(() => undefined),
343+
readExternalCliBootstrapCredential: readExternalCliBootstrapCredentialImpl,
342344
resolveApiKeyForProfile: resolveApiKeyForProfileImpl,
343345
});
344346
mockGetGlobalHookRunner.mockReturnValue(null);
@@ -785,14 +787,14 @@ describe("prepareCliRunContext", () => {
785787
expect(prepareExecution.mock.calls[0]?.[0]).not.toHaveProperty("authCredential");
786788
});
787789

788-
it("persists and forwards a refreshed selected Claude CLI OAuth profile", async () => {
790+
it("persists and forwards a refreshed managed Anthropic OAuth profile", async () => {
789791
const { dir } = fixture.session;
790792
const agentDir = path.join(dir, "agents", "main", "agent");
791-
const authProfileId = "anthropic:claude-cli";
793+
const authProfileId = "anthropic:openclaw-managed";
792794
const prepareExecution = vi.fn(async () => undefined);
793795
const refreshedCredential = {
794796
type: "oauth" as const,
795-
provider: "claude-cli",
797+
provider: "anthropic",
796798
access: "refreshed-access-token",
797799
refresh: "refreshed-refresh-token",
798800
expires: Date.now() + 60 * 60_000,
@@ -804,7 +806,7 @@ describe("prepareCliRunContext", () => {
804806
profiles: {
805807
[authProfileId]: {
806808
type: "oauth",
807-
provider: "claude-cli",
809+
provider: "anthropic",
808810
access: "expired-access-token",
809811
refresh: "stored-refresh-token",
810812
expires: Date.now() - 60_000,
@@ -851,7 +853,7 @@ describe("prepareCliRunContext", () => {
851853
authProfileId,
852854
authCredential: expect.objectContaining({
853855
type: "oauth",
854-
provider: "claude-cli",
856+
provider: "anthropic",
855857
access: refreshedCredential.access,
856858
refresh: refreshedCredential.refresh,
857859
}),
@@ -869,7 +871,61 @@ describe("prepareCliRunContext", () => {
869871
);
870872
});
871873

872-
it("does not revive a selected Claude CLI credential when auth resolution returns null", async () => {
874+
it("runs an imported Claude CLI login natively without forwarding a credential", async () => {
875+
const { dir } = fixture.session;
876+
const agentDir = path.join(dir, "agents", "main", "agent");
877+
const authProfileId = "anthropic:claude-cli";
878+
const prepareExecution = vi.fn(async () => undefined);
879+
fs.mkdirSync(agentDir, { recursive: true });
880+
saveAuthProfileStore(
881+
{
882+
version: 1,
883+
profiles: {
884+
[authProfileId]: {
885+
type: "oauth",
886+
provider: "claude-cli",
887+
access: "expired-imported-access",
888+
refresh: "imported-refresh",
889+
expires: Date.now() - 60_000,
890+
891+
},
892+
},
893+
},
894+
agentDir,
895+
);
896+
setCliBackendForPrepareTest({ prepareExecution, authEpochMode: "profile-only" });
897+
const resolveApiKeyForProfile = vi.fn<typeof resolveApiKeyForProfileImpl>(async () => null);
898+
setCliRunnerPrepareTestDeps({
899+
resolveApiKeyForProfile,
900+
// Live login is expired too: expiry is Claude's to repair via its own
901+
// refresh token, so passthrough must not gate on it.
902+
readExternalCliBootstrapCredential: vi.fn(() => ({
903+
type: "oauth" as const,
904+
provider: "claude-cli",
905+
access: "live-native-access",
906+
refresh: "live-native-refresh",
907+
expires: Date.now() - 30_000,
908+
909+
})),
910+
});
911+
912+
await fixture.prepare({
913+
sessionKey: "agent:main:main",
914+
agentDir,
915+
provider: "claude-cli",
916+
model: "sonnet",
917+
authProfileId,
918+
config: {},
919+
});
920+
921+
expect(prepareExecution).toHaveBeenCalledTimes(1);
922+
expect(prepareExecution).toHaveBeenCalledWith(
923+
expect.objectContaining({ authProfileId, authCredential: undefined }),
924+
);
925+
expect(resolveApiKeyForProfile).not.toHaveBeenCalled();
926+
});
927+
928+
it("fails closed when the imported Claude CLI login is gone from the host", async () => {
873929
const { dir } = fixture.session;
874930
const agentDir = path.join(dir, "agents", "main", "agent");
875931
const authProfileId = "anthropic:claude-cli";
@@ -882,6 +938,95 @@ describe("prepareCliRunContext", () => {
882938
[authProfileId]: {
883939
type: "oauth",
884940
provider: "claude-cli",
941+
access: "expired-imported-access",
942+
refresh: "imported-refresh",
943+
expires: Date.now() - 60_000,
944+
},
945+
},
946+
},
947+
agentDir,
948+
);
949+
setCliBackendForPrepareTest({ prepareExecution, authEpochMode: "profile-only" });
950+
setCliRunnerPrepareTestDeps({
951+
readExternalCliBootstrapCredential: vi.fn(() => null),
952+
});
953+
954+
const preparation = fixture.prepare({
955+
sessionKey: "agent:main:main",
956+
agentDir,
957+
provider: "claude-cli",
958+
model: "sonnet",
959+
authProfileId,
960+
config: {},
961+
});
962+
await expect(preparation).rejects.toThrow("no reusable Claude CLI login is available");
963+
await expect(preparation).rejects.toThrow("claude auth login");
964+
expect(prepareExecution).not.toHaveBeenCalled();
965+
});
966+
967+
it("fails closed when the host Claude CLI login is a different account", async () => {
968+
const { dir } = fixture.session;
969+
const agentDir = path.join(dir, "agents", "main", "agent");
970+
const authProfileId = "anthropic:claude-cli";
971+
const prepareExecution = vi.fn(async () => undefined);
972+
fs.mkdirSync(agentDir, { recursive: true });
973+
saveAuthProfileStore(
974+
{
975+
version: 1,
976+
profiles: {
977+
[authProfileId]: {
978+
type: "oauth",
979+
provider: "claude-cli",
980+
access: "imported-access",
981+
refresh: "imported-refresh",
982+
expires: Date.now() + 60 * 60_000,
983+
accountId: "acct-selected",
984+
985+
},
986+
},
987+
},
988+
agentDir,
989+
);
990+
setCliBackendForPrepareTest({ prepareExecution, authEpochMode: "profile-only" });
991+
setCliRunnerPrepareTestDeps({
992+
readExternalCliBootstrapCredential: vi.fn(() => ({
993+
type: "oauth" as const,
994+
provider: "claude-cli",
995+
access: "other-account-access",
996+
refresh: "other-account-refresh",
997+
expires: Date.now() + 60 * 60_000,
998+
accountId: "acct-other",
999+
1000+
})),
1001+
});
1002+
1003+
const preparation = fixture.prepare({
1004+
sessionKey: "agent:main:main",
1005+
agentDir,
1006+
provider: "claude-cli",
1007+
model: "sonnet",
1008+
authProfileId,
1009+
config: {},
1010+
});
1011+
await expect(preparation).rejects.toThrow(
1012+
"current Claude CLI login belongs to a different account",
1013+
);
1014+
expect(prepareExecution).not.toHaveBeenCalled();
1015+
});
1016+
1017+
it("does not revive a selected managed credential when auth resolution returns null", async () => {
1018+
const { dir } = fixture.session;
1019+
const agentDir = path.join(dir, "agents", "main", "agent");
1020+
const authProfileId = "anthropic:openclaw-managed";
1021+
const prepareExecution = vi.fn(async () => undefined);
1022+
fs.mkdirSync(agentDir, { recursive: true });
1023+
saveAuthProfileStore(
1024+
{
1025+
version: 1,
1026+
profiles: {
1027+
[authProfileId]: {
1028+
type: "oauth",
1029+
provider: "anthropic",
8851030
access: "expired-access-token",
8861031
refresh: "expired-refresh-token",
8871032
expires: Date.now() - 60_000,
@@ -906,7 +1051,7 @@ describe("prepareCliRunContext", () => {
9061051
await expect(preparation).rejects.toThrow(
9071052
`could not materialize selected auth profile "${authProfileId}"`,
9081053
);
909-
await expect(preparation).rejects.toThrow("claude auth login");
1054+
await expect(preparation).rejects.toThrow("openclaw models auth login --provider anthropic");
9101055
expect(prepareExecution).not.toHaveBeenCalled();
9111056
});
9121057

@@ -969,10 +1114,10 @@ describe("prepareCliRunContext", () => {
9691114
expect(prepareExecution).not.toHaveBeenCalled();
9701115
});
9711116

972-
it("surfaces selected Claude CLI refresh failures before backend preparation", async () => {
1117+
it("surfaces managed profile refresh failures before backend preparation", async () => {
9731118
const { dir } = fixture.session;
9741119
const agentDir = path.join(dir, "agents", "main", "agent");
975-
const authProfileId = "anthropic:claude-cli";
1120+
const authProfileId = "anthropic:openclaw-managed";
9761121
const prepareExecution = vi.fn(async () => undefined);
9771122
fs.mkdirSync(agentDir, { recursive: true });
9781123
saveAuthProfileStore(
@@ -981,7 +1126,7 @@ describe("prepareCliRunContext", () => {
9811126
profiles: {
9821127
[authProfileId]: {
9831128
type: "oauth",
984-
provider: "claude-cli",
1129+
provider: "anthropic",
9851130
access: "expired-access-token",
9861131
refresh: "expired-refresh-token",
9871132
expires: Date.now() - 60_000,

0 commit comments

Comments
 (0)