claude: its sign-in is read from the keychain under its account, as Claude Code reads it (@taresky: claude auth status loggedIn, the keychain item there, magpie still said not signed in) — magpie asked for "Claude Code-credentials" by service alone, and with more than one item for it (one an earlier sign-in left, say) the keychain may hand back one that isn't the sign-in in use; now it asks under $USER first, as Claude Code does, and by service alone only when that finds none that reads

This commit is contained in:
yetone
2026-09-29 13:08:36 +08:00
parent 89d8a59f15
commit 58b676663b
2 changed files with 45 additions and 6 deletions
+20 -6
View File
@@ -290,14 +290,28 @@ func readClaudeCredential() (claudeCredentials, claudeCredentialLocation, bool)
if !claudeKeychain {
return claudeCredentials{}, claudeCredentialLocation{}, false
}
out, err := proc.Command("security", "find-generic-password", "-s", "Claude Code-credentials", "-w").Output()
if err != nil {
// Claude Code reads the item under its account ($USER); by service
// alone the keychain may hand back another one — left from an earlier
// sign-in — which isn't the sign-in in use
account := claudeKeychainAccount()
var c claudeCredentials
var ok, wasHex bool
for _, args := range [][]string{{"-a", account}, nil} {
out, err := proc.Command("security", append([]string{"find-generic-password", "-s", "Claude Code-credentials", "-w"}, args...)...).Output()
if err != nil {
continue
}
var b []byte
b, wasHex = keychainText(bytes.TrimSpace(out))
if c, ok = parseClaudeCredentials(b); ok {
break
}
}
if !ok {
return claudeCredentials{}, claudeCredentialLocation{}, false
}
b, wasHex := keychainText(bytes.TrimSpace(out))
c, ok := parseClaudeCredentials(b)
loc := claudeCredentialLocation{keychain: true, account: claudeKeychainAccount()}
if ok && wasHex {
loc := claudeCredentialLocation{keychain: true, account: account}
if wasHex {
// written by magpie before it wrote them on one line: Claude Code
// reads that hex as no sign-in, so it is written again as it
// writes it
+25
View File
@@ -73,3 +73,28 @@ func TestSavedButSignedOutClaudeSaysWhy(t *testing.T) {
t.Fatalf("signed out: %q", w)
}
}
// Claude Code's sign-in is read from the keychain as Claude Code reads it,
// under its account: another item for the same service (one an earlier
// sign-in left) that comes first by service alone isn't taken for it.
func TestClaudeKeychainReadsItsAccount(t *testing.T) {
home := claudeHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("USER", "tester")
bin := filepath.Join(home, "bin")
os.MkdirAll(bin, 0o755)
// the leftover comes first unless the account is asked for
os.WriteFile(filepath.Join(bin, "security"), []byte(`#!/bin/sh
case "$*" in
*"-a tester"*) echo '{"claudeAiOauth":{"accessToken":"live","subscriptionType":"max"}}' ;;
*) echo '{}' ;;
esac
`), 0o755)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
claudeKeychain = true
forgetClaudeCredential()
c, loc, ok := claudeCredential()
if !ok || c.OAuth.AccessToken != "live" || !loc.keychain || loc.account != "tester" {
t.Fatalf("read %v %+v %+v", ok, c.OAuth, loc)
}
}