cc-provider-switcher 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -1
- package/dist/cli.js +224 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -235,7 +235,28 @@ ccs account list # list all saved accounts
|
|
|
235
235
|
ccs account delete personal # delete "personal"
|
|
236
236
|
```
|
|
237
237
|
|
|
238
|
-
|
|
238
|
+
ccs reads and writes the login where Claude Code keeps it:
|
|
239
|
+
|
|
240
|
+
- macOS: the `Claude Code-credentials` item in the login Keychain
|
|
241
|
+
- Linux and Windows: `~/.claude/.credentials.json`
|
|
242
|
+
|
|
243
|
+
A profile also saves the `oauthAccount` block from `~/.claude.json` (the account email and organization), and `switch` writes it back.
|
|
244
|
+
|
|
245
|
+
ccs keeps profiles in `~/.ccs/accounts/` with `0600` permissions.
|
|
246
|
+
|
|
247
|
+
Quit every Claude Code session before you run `ccs account switch`, then start Claude Code again. A session left open can write its old login back when it refreshes its token.
|
|
248
|
+
|
|
249
|
+
Claude Code refreshes the login after you save it, so `switch` first copies the current login into the profile with the same account and organization. If no profile matches, `switch` stops and changes nothing. Run `ccs account save <name>` first.
|
|
250
|
+
|
|
251
|
+
### When a provider is active
|
|
252
|
+
|
|
253
|
+
While a provider is active, Claude Code does not use your `/login` account. `ccs <provider>` writes `ANTHROPIC_AUTH_TOKEN`, and Claude Code ranks that token above the login. `ccs account switch` still swaps the saved login, and Claude Code uses it again once you remove the provider with `ccs reset`.
|
|
254
|
+
|
|
255
|
+
`ccs status` shows the account you are logged in to and the credential Claude Code will use. When that credential is not the login, `ccs status` and `ccs account switch` say where it comes from and how to remove it. `ccs account list` puts a `*` next to the profile you are logged in with.
|
|
256
|
+
|
|
257
|
+
With a provider active and a login saved, Claude Code shows "Both ANTHROPIC_AUTH_TOKEN and /login managed key set · auth may not work as expected". This is expected: requests go to the provider with its token. Run `ccs reset` to go back to the login.
|
|
258
|
+
|
|
259
|
+
ccs does not yet handle `CLAUDE_CONFIG_DIR` or Claude Code's opt-in Windows Credential Manager storage.
|
|
239
260
|
|
|
240
261
|
---
|
|
241
262
|
|
package/dist/cli.js
CHANGED
|
@@ -216,9 +216,13 @@ var CCS_ACCOUNTS_DIR = path.join(CCS_DIR, "accounts");
|
|
|
216
216
|
var CLAUDE_DIR = path.join(os.homedir(), ".claude");
|
|
217
217
|
var CLAUDE_SETTINGS_FILE = path.join(CLAUDE_DIR, "settings.json");
|
|
218
218
|
var CLAUDE_CREDENTIALS_FILE = path.join(CLAUDE_DIR, ".credentials.json");
|
|
219
|
+
var CLAUDE_GLOBAL_CONFIG_FILE = path.join(os.homedir(), ".claude.json");
|
|
219
220
|
function getProjectSettingsPath() {
|
|
220
221
|
return path.join(process.cwd(), ".claude", "settings.local.json");
|
|
221
222
|
}
|
|
223
|
+
function getSharedProjectSettingsPath() {
|
|
224
|
+
return path.join(process.cwd(), ".claude", "settings.json");
|
|
225
|
+
}
|
|
222
226
|
|
|
223
227
|
// src/utils/fs.ts
|
|
224
228
|
import fs from "fs/promises";
|
|
@@ -366,6 +370,9 @@ function success(msg) {
|
|
|
366
370
|
function error(msg) {
|
|
367
371
|
console.error(chalk.red("\u2717 " + msg));
|
|
368
372
|
}
|
|
373
|
+
function warn(msg) {
|
|
374
|
+
console.warn(chalk.yellow("\u26A0 " + msg));
|
|
375
|
+
}
|
|
369
376
|
function info(msg) {
|
|
370
377
|
console.log(chalk.cyan("\u2139 " + msg));
|
|
371
378
|
}
|
|
@@ -433,6 +440,77 @@ function registerSwitchCommand(program2) {
|
|
|
433
440
|
});
|
|
434
441
|
}
|
|
435
442
|
|
|
443
|
+
// src/config/auth-source.ts
|
|
444
|
+
import path4 from "path";
|
|
445
|
+
var CLOUD_FLAGS = ["CLAUDE_CODE_USE_BEDROCK", "CLAUDE_CODE_USE_VERTEX", "CLAUDE_CODE_USE_FOUNDRY"];
|
|
446
|
+
var PRECEDENCE = [
|
|
447
|
+
...CLOUD_FLAGS,
|
|
448
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
449
|
+
"ANTHROPIC_API_KEY",
|
|
450
|
+
"apiKeyHelper",
|
|
451
|
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
452
|
+
"ANTHROPIC_PROFILE",
|
|
453
|
+
// Federation counts only together with ANTHROPIC_ORGANIZATION_ID.
|
|
454
|
+
"ANTHROPIC_FEDERATION_RULE_ID"
|
|
455
|
+
];
|
|
456
|
+
var USER_SCOPE_ONLY = /* @__PURE__ */ new Set(["CLAUDE_CODE_OAUTH_TOKEN"]);
|
|
457
|
+
var ORIGIN_LABEL = {
|
|
458
|
+
shell: "your shell",
|
|
459
|
+
user: "~/.claude/settings.json",
|
|
460
|
+
shared: ".claude/settings.json",
|
|
461
|
+
local: ".claude/settings.local.json"
|
|
462
|
+
};
|
|
463
|
+
function resolveAuthSource(layers) {
|
|
464
|
+
const highestFirst = [...layers].reverse();
|
|
465
|
+
const lookup = (name) => {
|
|
466
|
+
for (const layer of highestFirst) {
|
|
467
|
+
if (USER_SCOPE_ONLY.has(name) && (layer.from === "shared" || layer.from === "local")) continue;
|
|
468
|
+
const value = name === "apiKeyHelper" ? layer.apiKeyHelper : layer.env?.[name];
|
|
469
|
+
if (value === void 0) continue;
|
|
470
|
+
const on = CLOUD_FLAGS.includes(name) ? typeof value === "string" && /^(1|true|yes|on)$/i.test(value) : typeof value === "string" && value !== "";
|
|
471
|
+
return on ? { name, from: layer.from } : void 0;
|
|
472
|
+
}
|
|
473
|
+
return void 0;
|
|
474
|
+
};
|
|
475
|
+
for (const name of PRECEDENCE) {
|
|
476
|
+
const source = lookup(name);
|
|
477
|
+
if (!source) continue;
|
|
478
|
+
if (name === "ANTHROPIC_FEDERATION_RULE_ID" && !lookup("ANTHROPIC_ORGANIZATION_ID")) continue;
|
|
479
|
+
return source;
|
|
480
|
+
}
|
|
481
|
+
return void 0;
|
|
482
|
+
}
|
|
483
|
+
async function readAuthState(claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
484
|
+
const sharedPath = getSharedProjectSettingsPath();
|
|
485
|
+
const sharedIsUser = path4.resolve(sharedPath) === path4.resolve(CLAUDE_SETTINGS_FILE);
|
|
486
|
+
const [user, shared, local, config] = await Promise.all([
|
|
487
|
+
readUserSettings(),
|
|
488
|
+
sharedIsUser ? {} : readProjectSettings(sharedPath),
|
|
489
|
+
readProjectSettings(getProjectSettingsPath()),
|
|
490
|
+
readJsonFile(claudeConfigPath)
|
|
491
|
+
]);
|
|
492
|
+
const source = resolveAuthSource([
|
|
493
|
+
{ from: "shell", env: process.env },
|
|
494
|
+
{ ...user, from: "user" },
|
|
495
|
+
{ ...shared, from: "shared" },
|
|
496
|
+
{ ...local, from: "local" }
|
|
497
|
+
]);
|
|
498
|
+
const account = config?.oauthAccount;
|
|
499
|
+
return { account: account && typeof account === "object" ? account : void 0, source };
|
|
500
|
+
}
|
|
501
|
+
function describeAuthSource(source) {
|
|
502
|
+
const text = `${source.name} from ${ORIGIN_LABEL[source.from]}`;
|
|
503
|
+
return source.name === "ANTHROPIC_API_KEY" ? `${text} (once approved in Claude Code)` : text;
|
|
504
|
+
}
|
|
505
|
+
function loginOverrideWarning(source) {
|
|
506
|
+
let fix;
|
|
507
|
+
if (source.from === "shell") fix = `unset ${source.name} in your shell`;
|
|
508
|
+
else if (source.name === "ANTHROPIC_AUTH_TOKEN" && source.from === "user") fix = 'run "ccs reset"';
|
|
509
|
+
else if (source.name === "ANTHROPIC_AUTH_TOKEN" && source.from === "local") fix = 'run "ccs reset --project"';
|
|
510
|
+
else fix = `remove ${source.name} from ${ORIGIN_LABEL[source.from]}`;
|
|
511
|
+
return `Claude Code uses ${describeAuthSource(source)}, not your /login account. To use the login, ${fix}.`;
|
|
512
|
+
}
|
|
513
|
+
|
|
436
514
|
// src/commands/status.ts
|
|
437
515
|
function registerStatusCommand(program2) {
|
|
438
516
|
program2.command("status").description("Show current provider, model, and key status").action(async () => {
|
|
@@ -470,6 +548,15 @@ ${scope} settings:`);
|
|
|
470
548
|
if (!hasAny) {
|
|
471
549
|
info('No provider configured. Run "ccs <provider>" to switch.');
|
|
472
550
|
}
|
|
551
|
+
const { account, source } = await readAuthState();
|
|
552
|
+
const email = typeof account?.emailAddress === "string" ? account.emailAddress : void 0;
|
|
553
|
+
const org = typeof account?.organizationName === "string" ? account.organizationName : void 0;
|
|
554
|
+
console.log("\nClaude account:");
|
|
555
|
+
table([
|
|
556
|
+
["Login", email ? org ? `${email} (${org})` : email : "not logged in"],
|
|
557
|
+
["Credential", source ? describeAuthSource(source) : email ? "/login" : "none"]
|
|
558
|
+
]);
|
|
559
|
+
if (source && email) warn(loginOverrideWarning(source));
|
|
473
560
|
});
|
|
474
561
|
}
|
|
475
562
|
|
|
@@ -608,35 +695,152 @@ function registerDoctorCommand(program2) {
|
|
|
608
695
|
import readline3 from "readline";
|
|
609
696
|
|
|
610
697
|
// src/accounts/account-manager.ts
|
|
698
|
+
import fs5 from "fs/promises";
|
|
699
|
+
import path5 from "path";
|
|
700
|
+
|
|
701
|
+
// src/accounts/credential-store.ts
|
|
611
702
|
import fs4 from "fs/promises";
|
|
612
|
-
import
|
|
613
|
-
|
|
703
|
+
import os2 from "os";
|
|
704
|
+
import { spawnSync } from "child_process";
|
|
705
|
+
function fileStore(filePath) {
|
|
706
|
+
return {
|
|
707
|
+
where: filePath,
|
|
708
|
+
async read() {
|
|
709
|
+
try {
|
|
710
|
+
return await fs4.readFile(filePath, "utf-8");
|
|
711
|
+
} catch {
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
},
|
|
715
|
+
async write(data) {
|
|
716
|
+
await writeFileAtomically(filePath, data, 384);
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
var KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
721
|
+
var KEYCHAIN_NOT_FOUND = 44;
|
|
722
|
+
var SECURITY_LINE_LIMIT = 4032;
|
|
723
|
+
function keychainAccount() {
|
|
724
|
+
let name;
|
|
725
|
+
try {
|
|
726
|
+
name = process.env.USER || os2.userInfo().username;
|
|
727
|
+
} catch {
|
|
728
|
+
return "claude-code-user";
|
|
729
|
+
}
|
|
730
|
+
return /^[a-zA-Z0-9._-]+$/.test(name) ? name : "claude-code-user";
|
|
731
|
+
}
|
|
732
|
+
function keychainStore(service = KEYCHAIN_SERVICE, account = keychainAccount()) {
|
|
733
|
+
return {
|
|
734
|
+
where: `macOS Keychain item "${service}"`,
|
|
735
|
+
async read() {
|
|
736
|
+
const res = spawnSync("security", ["find-generic-password", "-a", account, "-s", service, "-w"], {
|
|
737
|
+
encoding: "utf-8"
|
|
738
|
+
});
|
|
739
|
+
if (res.status === KEYCHAIN_NOT_FOUND) return null;
|
|
740
|
+
if (res.status !== 0) {
|
|
741
|
+
throw new Error(`Keychain read failed (security exit ${res.status}): ${res.stderr.trim()}`);
|
|
742
|
+
}
|
|
743
|
+
return res.stdout.trim();
|
|
744
|
+
},
|
|
745
|
+
async write(data) {
|
|
746
|
+
const hex = Buffer.from(data, "utf-8").toString("hex");
|
|
747
|
+
const line = `add-generic-password -U -a "${account}" -s "${service}" -X "${hex}"
|
|
748
|
+
`;
|
|
749
|
+
const res = line.length <= SECURITY_LINE_LIMIT ? spawnSync("security", ["-i"], { input: line, encoding: "utf-8" }) : spawnSync("security", ["add-generic-password", "-U", "-a", account, "-s", service, "-X", hex], {
|
|
750
|
+
encoding: "utf-8"
|
|
751
|
+
});
|
|
752
|
+
if (res.status !== 0) {
|
|
753
|
+
throw new Error(`Keychain write failed (security exit ${res.status}): ${res.stderr.trim()}`);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
function defaultCredentialStore(platform = process.platform) {
|
|
759
|
+
const file = fileStore(CLAUDE_CREDENTIALS_FILE);
|
|
760
|
+
if (platform !== "darwin") return file;
|
|
761
|
+
const keychain = keychainStore();
|
|
762
|
+
return {
|
|
763
|
+
where: `${keychain.where} or ${file.where}`,
|
|
764
|
+
async read() {
|
|
765
|
+
return await keychain.read() ?? await file.read();
|
|
766
|
+
},
|
|
767
|
+
write: keychain.write
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// src/accounts/account-manager.ts
|
|
772
|
+
async function saveAccount(name, store = defaultCredentialStore(), accountsDir = CCS_ACCOUNTS_DIR, claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
773
|
+
const raw = await store.read();
|
|
614
774
|
let credentials;
|
|
615
775
|
try {
|
|
616
|
-
|
|
617
|
-
credentials = JSON.parse(raw);
|
|
776
|
+
credentials = JSON.parse(raw ?? "");
|
|
618
777
|
} catch {
|
|
619
|
-
throw new Error(`Cannot read credentials
|
|
778
|
+
throw new Error(`Cannot read credentials from ${store.where}. Are you logged in to Claude?`);
|
|
620
779
|
}
|
|
780
|
+
const claudeConfig = await readJsonFile(claudeConfigPath);
|
|
621
781
|
await ensureDir(accountsDir);
|
|
622
|
-
const snapshot = {
|
|
623
|
-
|
|
782
|
+
const snapshot = {
|
|
783
|
+
credentials,
|
|
784
|
+
oauthAccount: claudeConfig?.oauthAccount,
|
|
785
|
+
savedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
786
|
+
};
|
|
787
|
+
await writeJsonFile(path5.join(accountsDir, `${name}.json`), snapshot, 384);
|
|
624
788
|
}
|
|
625
|
-
async function switchAccount(name,
|
|
626
|
-
const accountPath =
|
|
789
|
+
async function switchAccount(name, store = defaultCredentialStore(), accountsDir = CCS_ACCOUNTS_DIR, claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
790
|
+
const accountPath = path5.join(accountsDir, `${name}.json`);
|
|
627
791
|
const snapshot = await readJsonFile(accountPath);
|
|
628
792
|
if (!snapshot) throw new Error(`Account "${name}" not found.`);
|
|
629
|
-
await
|
|
630
|
-
await
|
|
793
|
+
const claudeConfig = await readJsonFile(claudeConfigPath);
|
|
794
|
+
await saveBackCurrent(store, accountsDir, claudeConfig?.oauthAccount);
|
|
795
|
+
await store.write(JSON.stringify(snapshot.credentials));
|
|
796
|
+
if (snapshot.oauthAccount === void 0 || !claudeConfig) return;
|
|
797
|
+
claudeConfig.oauthAccount = snapshot.oauthAccount;
|
|
798
|
+
await writeJsonFile(claudeConfigPath, claudeConfig, 384);
|
|
799
|
+
}
|
|
800
|
+
async function saveBackCurrent(store, accountsDir, currentAccount) {
|
|
801
|
+
let credentials;
|
|
802
|
+
try {
|
|
803
|
+
credentials = JSON.parse(await store.read() ?? "");
|
|
804
|
+
} catch {
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
const id = accountId(currentAccount);
|
|
808
|
+
if (!id) return;
|
|
809
|
+
for (const { name } of await listAccounts(accountsDir)) {
|
|
810
|
+
const file = path5.join(accountsDir, `${name}.json`);
|
|
811
|
+
const saved = await readJsonFile(file);
|
|
812
|
+
if (saved && accountId(saved.oauthAccount) === id) {
|
|
813
|
+
const snapshot = {
|
|
814
|
+
credentials,
|
|
815
|
+
oauthAccount: currentAccount,
|
|
816
|
+
savedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
817
|
+
};
|
|
818
|
+
await writeJsonFile(file, snapshot, 384);
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
const email = currentAccount.emailAddress ?? id;
|
|
823
|
+
throw new Error(
|
|
824
|
+
`The current login (${email}) is not saved in any profile and would be lost. Run "ccs account save <name>" first.`
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
function accountId(oauthAccount) {
|
|
828
|
+
const a = oauthAccount;
|
|
829
|
+
return typeof a?.accountUuid === "string" ? `${a.accountUuid}/${String(a.organizationUuid)}` : void 0;
|
|
631
830
|
}
|
|
632
|
-
async function listAccounts(accountsDir = CCS_ACCOUNTS_DIR) {
|
|
831
|
+
async function listAccounts(accountsDir = CCS_ACCOUNTS_DIR, claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
832
|
+
const current = accountId((await readJsonFile(claudeConfigPath))?.oauthAccount);
|
|
633
833
|
try {
|
|
634
|
-
const files = await
|
|
834
|
+
const files = await fs5.readdir(accountsDir);
|
|
635
835
|
const accounts = [];
|
|
636
836
|
for (const file of files) {
|
|
637
837
|
if (!file.endsWith(".json")) continue;
|
|
638
|
-
const raw = await readJsonFile(
|
|
639
|
-
accounts.push({
|
|
838
|
+
const raw = await readJsonFile(path5.join(accountsDir, file));
|
|
839
|
+
accounts.push({
|
|
840
|
+
name: file.replace(/\.json$/, ""),
|
|
841
|
+
savedAt: raw?.savedAt ?? "",
|
|
842
|
+
active: current !== void 0 && accountId(raw?.oauthAccount) === current
|
|
843
|
+
});
|
|
640
844
|
}
|
|
641
845
|
return accounts.sort((a, b) => a.name.localeCompare(b.name));
|
|
642
846
|
} catch {
|
|
@@ -644,9 +848,9 @@ async function listAccounts(accountsDir = CCS_ACCOUNTS_DIR) {
|
|
|
644
848
|
}
|
|
645
849
|
}
|
|
646
850
|
async function deleteAccount(name, accountsDir = CCS_ACCOUNTS_DIR) {
|
|
647
|
-
const accountPath =
|
|
851
|
+
const accountPath = path5.join(accountsDir, `${name}.json`);
|
|
648
852
|
try {
|
|
649
|
-
await
|
|
853
|
+
await fs5.unlink(accountPath);
|
|
650
854
|
} catch {
|
|
651
855
|
throw new Error(`Account "${name}" not found.`);
|
|
652
856
|
}
|
|
@@ -668,6 +872,8 @@ function registerAccountCommand(program2) {
|
|
|
668
872
|
try {
|
|
669
873
|
await switchAccount(name);
|
|
670
874
|
success(`Switched to account "${name}".`);
|
|
875
|
+
const { source } = await readAuthState();
|
|
876
|
+
if (source) warn(loginOverrideWarning(source));
|
|
671
877
|
} catch (err) {
|
|
672
878
|
error(String(err));
|
|
673
879
|
process.exit(1);
|
|
@@ -679,7 +885,7 @@ function registerAccountCommand(program2) {
|
|
|
679
885
|
info('No saved accounts. Run "ccs account save <name>" to save one.');
|
|
680
886
|
return;
|
|
681
887
|
}
|
|
682
|
-
table(accounts.map((a) => [a.name, a.savedAt]));
|
|
888
|
+
table(accounts.map((a) => [(a.active ? "* " : " ") + a.name, a.savedAt]));
|
|
683
889
|
});
|
|
684
890
|
account.command("delete <name>").description("Delete a saved account profile").action(async (name) => {
|
|
685
891
|
const confirmed = await confirm2(`Delete account "${name}"?`);
|