2 Commits
Author SHA1 Message Date
mysqtoandClaude Opus 5 550aef90f0 Address the credential store Claude Code actually uses
Claude Code's macOS credential store is a composite named
keychain-with-plaintext-fallback: it reads the Keychain first and falls
back to <config dir>/.credentials.json, and a failed Keychain write
migrates the credential into that file. ccswitch looked only at the
Keychain, under a hardcoded service name, and passed the token on argv.
Three consequences, all fixed here.

Headless and SSH machines. Over SSH the login Keychain cannot be
unlocked without a GUI prompt (security exits 36,
errSecInteractionNotAllowed), so Claude Code lives entirely in the
plaintext file. read() saw nothing, so save reported "no active
credential" on a signed-in machine and sync_current silently dropped
rotated tokens; write() could not land the incoming credential, leaving
~/.claude.json naming the new account while the live token still
belonged to the old one, which Claude Code reports as not logged in.
KeychainStore now mirrors the same arbitration and keeps exactly one
authoritative copy: a successful Keychain write deletes the plaintext
file, a failed one deletes the Keychain item, so a stale copy cannot
shadow the switch from the other kind of session. $CCSWITCH_CREDENTIALS
=file forces the plaintext path outright.

$CLAUDE_CONFIG_DIR. It moves .claude.json and the credential fallback,
and it renames the Keychain item: Claude Code namespaces the service
with the first eight hex characters of sha256($CLAUDE_CONFIG_DIR). With
the variable set, ccswitch was reading and writing a different account's
credential. This affected ordinary use, because ccswitch isolate
launches claude with CLAUDE_CONFIG_DIR set to the isolate directory.
claude_env.rs owns the derivation, including the related
$CLAUDE_SECURESTORAGE_CONFIG_DIR and $CLAUDE_CODE_CUSTOM_OAUTH_URL.

Token exposure. add-generic-password -w put the OAuth token in argv,
which ps shows to every user on the machine. It is now hex-encoded and
piped to security -i, with the same argv fallback Claude Code uses past
the stdin limit. The Keychain port collapses to one run(args, stdin) so
that choice is a tested decision in creds.rs rather than shim detail.

Also fixed: account_attr parsed stderr, but find-generic-password -g
prints attributes on stdout and the password on stderr, so it always
returned None and every profile stored an empty keychain_account. Writes
fell through to $USER, which is what Claude Code looks items up by, so
this was invisible. Account attributes are now sanitized the same way
Claude Code sanitizes them.

sha256.rs is hand-rolled against the FIPS vectors rather than pulling
sha2 and six transitive crates for one eight-character string, per the
dependency policy in AGENTS.md. Claude Code NFC-normalizes the directory
before hashing and we hash the bytes as given; that agrees for every
ASCII path, and the limitation is documented with its workaround.

Behavior change: profile and isolate storage now default to accounts/
and profiles/ inside the resolved config directory rather than always
~/.claude. Identical with $CLAUDE_CONFIG_DIR unset; with it set, each
config directory gets its own profiles. Set $CCSWITCH_HOME to a fixed
path to share one set across all of them.

legacy-fish is synced with the same three fixes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 22:45:40 +08:00
mysqtoandClaude Opus 4.8 d01079e91d Rewrite ccswitch as a cross-shell Rust CLI
Replaces the fish-only plugin with one portable binary — completions for
bash/zsh/fish/PowerShell via `ccswitch completions <shell>`. Same UX
(save/add/use/list/current/rm/isolate/seed/search + bare-name switch-and-launch).

- Fixes the auth-loss bug: the OAuth token is per-account (org lives only in
  ~/.claude.json), so switching between same-login/different-org profiles left
  the sibling holding a rotated, dead refresh token. A switch now propagates the
  refreshed credential to every profile sharing the account.
- Adds `save --force` / `add --force` to overwrite a profile without rm first.
- macOS Keychain via `security` (shim); file-based creds on Linux/Windows.
- Ports-and-adapters; 100% line / 98.86% region coverage; clippy + fmt clean.
- Homebrew formula (auto-installs completions) + cask + release workflow.
- Old fish plugin preserved under legacy-fish/ (also at lei-wego/ccswitch-fish).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 17:17:01 +08:00