Files
magpie/internal/provider/signedout_test.go
T
yetone 056cfe3323 provider: Codex's keychain read waits for the user, tells a refusal from no answer, and is kept while the item is unchanged (#2367)
Dony2050 (#2367) runs Codex with cli_auth_credentials_store = "auto" and no
auth.json, so magpie reads "Codex Auth" through /usr/bin/security. Four
faults:
- One 10s timeout covered both of macOS's prompts and the user typing their
  password. A read they approved after 11-16s still failed.
- A timeout and a refusal gave the same error.
- A read was kept for 10s only. With "Allow" (once), background polls
  prompted again about every 30s.
- Only a restart cleared the 10-minute failure cache.

What changed:
- Reading the secret is one ask per item at a time (askCodexKeychain). It
  runs in its own goroutine for up to 2 minutes (keychainAsk), outside
  codexKeychainMu, so no `security` run happens under a lock a reader needs.
  A reader waits at most 5s from when the ask started (keychainHold), then
  gets errKeychainWaiting. A request or a page never waits out the prompt.
  The ask's answer is kept when it lands, unless magpie kept a newer one
  after it started.
- Each failure says what happened:
  - exit 44: no item.
  - exit 51 (errSecAuthFailed) or 128 (errSecUserCanceled): denied or
    cancelled (errKeychainRefused). Checked against SecBase.h. Exit 44 was
    also checked on a throwaway keychain in the scratchpad.
  - the 2 minutes ran out: no answer (errKeychainNoAnswer).
  - anything else: errKeychainUnread.
  None of them counts as signed out.
- A successful read is kept while the item is unchanged. After 10s a reader
  runs Stat (find-generic-password without -w), which shows no prompt and
  returns only the item's mdat. The secret is read again only when the mdat
  changed.
  - Every write of the credential re-derives the cache:
    - codexStore.write keeps the date of what it wrote (keepCodexKeychain).
    - Switch, sign-out (deleteCodexLogin) and refresh forget a read
      (forgetCodexKeychain), so the next read checks the date.
  - forget keeps a refusal for its 10 minutes (#2408).
  - resetCodexKeychain clears everything. Only tests use it.
- The saved accounts' line in the GUI has a "Try again" link beside Remove
  when a read was refused, got no answer or is waiting (Exclusion.Retry).
  It posts /api/provider/keychain-retry, and RetryCodexKeychain reads once
  past the refusal.
  - The reason now says "Always Allow" lets /usr/bin/security read the item,
    so macOS stops asking each time Codex changes it.
  - Two new strings in zh, zh-TW, ja and de.
- docs/subsystems/providers-accounts.md: the "Codex's credential store" row
  describes the above.

Tests:
- TestCodexKeychainAskedSeldom (rewritten) covers:
  - an unchanged item over aged and forgotten rounds asks once;
  - a changed item asks again;
  - a gone item is not found without asking;
  - a refusal is kept through forget.
- TestCodexKeychainPromptWaitedFor: a held prompt gives "waiting" to the
  first and to 8 concurrent readers without blocking. Once answered, the
  read succeeds with one ask.
- TestCodexKeychainRetry: Retry and Why for refused, no answer and no item,
  and RetryCodexKeychain.
- TestSecurityKeychainExits (codex_keychain_ask_unix_test.go): a fake
  `security` built with testenv.Program covers:
  - a 2s answer with keychainWait = 1s succeeds;
  - Stat parses the mdat;
  - exits 44, 51, 128 and 1 map as above;
  - a hang gives no answer.
- signedout_test: Retry is set only for an unread item.
- GUI keychain-retry.test.cjs runs in chromium and webkit, in en and zh:
  - order Try again, Remove;
  - the POST body;
  - the line leaves after a click;
  - no link without retry.

Broken by hand (each restored after):
- Find bounded by keychainWait again: TestSecurityKeychainExits red ("a read
  the user answered late ... no answer in time").
- exit 51 not classed as refused: TestSecurityKeychainExits red ("Find,
  denied").
- Readers wait on the ask (no hold): TestCodexKeychainPromptWaitedFor hangs;
  the package timed out.
- forget clears every read: TestCodexKeychainAskedSeldom red (4 asks).
- mdat match ignored: AskedSeldom red (7 asks).
- Cache never re-checked: AskedSeldom red (the changed item wasn't seen).
- RetryCodexKeychain also deleting the failure cache: still green, because
  a fresh read asks past a refusal. That loop was removed as redundant.

Not tried:
- A real macOS prompt, or an item Codex itself wrote. Every test uses a fake
  `security`; the real keychain and ~/.codex were not touched.
- Put and Remove are still bounded at 10s.

Run at this head, rebased on origin/main 1a16fc0b5:
- gofmt -l: clean.
- go vet -tags nogui ./...: ok for GOOS darwin, linux and windows.
- go test -race -count=2 on the keychain, signed-out and Codex sign-in tests
  in internal/provider: ok.
- GUI (node --test, NODE_PATH playwright 1.62.0): keychain-retry, i18n-keys,
  saved-signedout-remove, gui-de, gui-ja, gui-zh-tw: 24/24 pass.

Then rebased on origin/main 167df7055 (TestSecurityKeychainAnswers' fake is
a testenv.Program; no overlap in hunks): go vet -tags nogui for darwin, linux
and windows on provider and gui, and go test -race ./internal/provider: ok.

Full suite: go test -timeout 45m ./... in a sandbox HOME, set -o pipefail, at
this tree on origin/main 1a16fc0b5: exit 0, 66 packages ok, no FAIL (gateway
1588s, agent 1497s, provider 510s).

Rebased onto origin/main a6f908ba5 (#2440, #2437 Gemini finish_reason; no overlap): go test -tags nogui ./internal/provider ./internal/gui ok.
2026-10-16 03:46:15 +08:00

360 lines
14 KiB
Go

package provider
import (
"encoding/hex"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
// Saved accounts of an agent not signed in where magpie looks (a magpie
// serve under another HOME) are said to be left out, not dropped silently.
func TestSavedButSignedOut(t *testing.T) {
isolate(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("CLAUDE_CONFIG_DIR", "")
if err := writeLogins([]savedLogin{
{Agent: "codex", User: "a@x.com", Auth: []byte(`{}`)},
{Agent: "codex", User: "b@x.com", Auth: []byte(`{}`)},
}); err != nil {
t.Fatal(err)
}
var codex *Exclusion
for _, x := range Excluded() {
if x.Agent == "codex" && x.SignedOut {
codex = &x
}
if x.Agent == "claude" {
t.Errorf("claude has no saved accounts, yet: %+v", x)
}
}
if codex == nil || !strings.Contains(codex.Why, "2 accounts are") || !strings.Contains(codex.Why, filepath.Join(home, ".codex", "auth.json")) {
t.Fatalf("codex: %+v", codex)
}
if strings.Contains(codex.Why, "keychain") {
t.Fatalf("no keyring set, yet: %s", codex.Why)
}
// Codex set to keep its sign-in in the keychain (#2312, #2367). Where
// magpie reads the item (macOS), an empty one is signed out, said of
// the item; a read the keychain refused is not known, and the reason
// says how to let magpie in. Where it doesn't (Linux, Windows, or
// Codex's encrypted auth storage), it is signed in only not where
// magpie looks, and the reason says how to put it there.
os.MkdirAll(filepath.Join(home, ".codex"), 0o700)
excluded := func() *Exclusion {
resetCodexKeychain()
for _, x := range Excluded() {
if x.Agent == "codex" && x.SignedOut {
return &x
}
}
return nil
}
defer func(k keychain, here bool) { codexKeychain, codexKeychainHere = k, here }(codexKeychain, codexKeychainHere)
for _, store := range []string{"keyring", "auto"} {
config := "model = \"gpt-5.6\"\ncli_auth_credentials_store = \"" + store + "\"\n\n[profiles.x]\nmodel = \"o3\"\n"
os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte(config), 0o600)
codexKeychain, codexKeychainHere = &fakeKeychain{}, true
if codex = excluded(); codex == nil || len(codex.Users) != 2 || !strings.Contains(codex.Why, "isn't signed in here") ||
!strings.Contains(codex.Why, `keychain item "Codex Auth"`) {
t.Fatalf("%s, no item: %+v", store, codex)
}
if codex.Retry {
t.Fatalf("%s, no item: Try again offered", store)
}
// an item there, which the keychain won't give
codexKeychain = &fakeKeychain{findErr: errKeychainUnread, items: map[string][]byte{codexKeychainAccount(filepath.Join(home, ".codex")): []byte(`{}`)}}
if codex = excluded(); codex == nil || strings.Contains(codex.Why, "isn't signed in here") || !codex.Retry ||
!strings.Contains(codex.Why, "Allow magpie") || !strings.Contains(codex.Why, `cli_auth_credentials_store = "file"`) {
t.Fatalf("%s, item not read: %+v", store, codex)
}
codexKeychainHere = false
if codex = excluded(); codex == nil || codex.Retry || len(codex.Users) != 2 || strings.Contains(codex.Why, "isn't signed in here") ||
!strings.Contains(codex.Why, `cli_auth_credentials_store = "`+store+`"`) || !strings.Contains(codex.Why, `cli_auth_credentials_store = "file"`) ||
!strings.Contains(codex.Why, "doesn't read") {
t.Fatalf("%s, keychain not read here: %+v", store, codex)
}
codexKeychainHere = true
os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte(config+"\n[features]\nsecret_auth_storage = true\n"), 0o600)
if codex = excluded(); codex == nil || !strings.Contains(codex.Why, "encrypted auth storage") || strings.Contains(codex.Why, "isn't signed in here") {
t.Fatalf("%s, encrypted storage: %+v", store, codex)
}
}
os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte("cli_auth_credentials_store = \"file\"\n"), 0o600)
for _, x := range Excluded() {
if x.Agent == "codex" && x.SignedOut && strings.Contains(x.Why, "keychain") {
t.Fatalf("file store, yet: %s", x.Why)
}
}
// signed in: nothing to say
os.WriteFile(filepath.Join(home, ".codex", "auth.json"), []byte(`{"auth_mode":"chatgpt","tokens":{"access_token":"x","account_id":"acc-1"}}`), 0o600)
for _, x := range Excluded() {
if x.SignedOut {
t.Errorf("signed in, yet: %+v", x)
}
}
}
// Claude accounts saved in magpie are offered while Claude Code is signed
// out — no credentials, or credentials Claude Code says are signed out —
// and are not said to be left out; one whose saved sign-in is gone is
// listed as signed out, not dropped.
func TestSavedButSignedOutClaudeServed(t *testing.T) {
home := claudeHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
if err := writeLogins([]savedLogin{
{Agent: "claude", User: "a@x.com", Auth: []byte(`{}`)},
{Agent: "claude", User: "b@x.com", Auth: []byte(`{"claudeAiOauth":{"accessToken":"tok-b","refreshToken":"r-b"}}`)},
}); err != nil {
t.Fatal(err)
}
check := func(state string) {
t.Helper()
forgetClaudeCredential()
forgetClaudeStatus()
for _, x := range Excluded() {
if x.Agent == "claude" {
t.Fatalf("%s: excluded %+v", state, x)
}
}
if p, ok := claudeAccount(); !ok || p.Account.User != "b@x.com" {
t.Fatalf("%s: account %v %+v", state, ok, p.Account)
}
ls := Logins("claude")
if len(ls) != 2 || ls[0].User != "a@x.com" || ls[0].Lapsed == "" || ls[1].Lapsed != "" || !ls[1].On {
t.Fatalf("%s: logins %+v", state, ls)
}
}
check("no credentials")
shellFakes(t)
claudeSignIn(t, home, time.Now().Add(time.Hour))
exe := filepath.Join(home, "claude")
testenv.Program(t, exe, "#!/bin/sh\necho '{\"loggedIn\": false}'\n")
claudeExecutable = func() string { return exe }
check("auth status signed out")
}
// A saved Claude account whose Claude Code is signed out (a banned account
// logged out, say) is listed, so it can be removed from magpie; removing
// one drops it from magpie's store and leaves Claude Code's own files as
// they are.
func TestSavedButSignedOutCanBeRemoved(t *testing.T) {
home := claudeHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
if err := writeLogins([]savedLogin{
{Agent: "claude", User: "banned@x.com", Auth: []byte(`{}`)},
{Agent: "codex", User: "c@x.com", Auth: []byte(`{}`)},
}); err != nil {
t.Fatal(err)
}
if ls := Logins("claude"); len(ls) != 1 || ls[0].User != "banned@x.com" {
t.Fatalf("claude: %+v", ls)
}
if err := ForgetLogin("claude", "banned@x.com"); err != nil {
t.Fatal(err)
}
if _, ok := claudeAccount(); ok {
t.Error("removed, yet served")
}
if ls := readLogins(); len(ls) != 1 || ls[0].Agent != "codex" {
t.Errorf("logins: %+v", ls)
}
if _, err := os.Stat(filepath.Join(home, ".claude")); !os.IsNotExist(err) {
t.Errorf("Claude Code's folder was touched: %v", err)
}
}
// The keychain item under the account is the sign-in Claude Code goes by,
// emptied or not: it reads no file beside it (yetone/magpie#1742). So an
// emptied item beside a still-parseable file is signed out, and a sign-in
// made in magpie is put in the item, not beside it.
func TestClaudeEmptiedKeychainItemIsTheSignInBesideAFile(t *testing.T) {
shellFakes(t)
home := claudeHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("USER", "tester")
creds := claudeSignIn(t, home, time.Now().Add(time.Hour))
bin := filepath.Join(home, "bin")
os.MkdirAll(bin, 0o755)
added := filepath.Join(home, "added.log")
// what Claude Code left of a refused refresh: the item is there, and it
// reads as JSON, only with no tokens in it
testenv.Program(t, filepath.Join(bin, "security"), `#!/bin/sh
case "$1" in
find-generic-password)
case "$*" in
*"-a tester"*) echo '{"claudeAiOauth":{"accessToken":"","refreshToken":"","expiresAt":0,"refreshTokenExpiresAt":1791392301433}}' ;;
*) exit 1 ;;
esac ;;
add-generic-password)
echo "$*" >> '`+added+`' ;;
*) exit 1 ;;
esac
`)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
claudeKeychain = true
forgetClaudeCredential()
if _, loc, ok := readClaudeCredential(); ok {
t.Fatalf("an emptied keychain item was passed over for the file beside it (%+v)", loc)
} else if !loc.keychain || loc.account != "tester" {
t.Fatalf("the emptied item is not where a sign-in would go: %+v", loc)
}
if err := putClaudeLogin(savedLogin{Agent: "claude", User: "fresh@example.com", Auth: mustJSONRaw(t, map[string]any{
"claudeAiOauth": map[string]any{
"accessToken": "fresh-access", "refreshToken": "fresh-refresh",
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "max",
}})}); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(creds); strings.Contains(string(b), "fresh-access") {
t.Fatal("the fresh sign-in went into the file beside the item Claude Code reads")
}
if b, _ := os.ReadFile(added); !strings.Contains(string(b), "fresh-access") {
t.Fatalf("the fresh sign-in was not put in the keychain item: %s", b)
}
}
// With no item under the account, the file beside it is the sign-in, as
// Claude Code reads it.
func TestClaudeFileSignInWhenNoKeychainItem(t *testing.T) {
shellFakes(t)
home := claudeHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("USER", "tester")
creds := claudeSignIn(t, home, time.Now().Add(time.Hour))
bin := filepath.Join(home, "bin")
os.MkdirAll(bin, 0o755)
testenv.Program(t, filepath.Join(bin, "security"), "#!/bin/sh\nexit 1\n")
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
claudeKeychain = true
forgetClaudeCredential()
c, loc, ok := readClaudeCredential()
if !ok || c.OAuth.AccessToken != "sk-ant-oat01-old" || loc.keychain || loc.path != creds {
t.Fatalf("the file is not the sign-in where no keychain item is: %v %+v %+v", ok, c.OAuth, loc)
}
}
// With CLAUDE_CONFIG_DIR set, Claude Code keeps its sign-in in that
// directory's own item, named for it, not in "Claude Code-credentials":
// that one is read, and a sign-in is put back there.
func TestClaudeConfigDirKeychainItem(t *testing.T) {
shellFakes(t)
home := claudeHome(t)
dir := filepath.Join(home, "elsewhere")
t.Setenv("CLAUDE_CONFIG_DIR", dir)
t.Setenv("USER", "tester")
bin := filepath.Join(home, "bin")
os.MkdirAll(bin, 0o755)
added := filepath.Join(home, "added.log")
testenv.Program(t, filepath.Join(bin, "security"), `#!/bin/sh
case "$1" in
find-generic-password)
case "$*" in
*"-s `+claudeDirService(dir)+` "*"-a tester"*) echo '{"claudeAiOauth":{"accessToken":"dir-item"}}' ;;
*"-s Claude Code-credentials "*) echo '{"claudeAiOauth":{"accessToken":"home-item"}}' ;;
*) exit 1 ;;
esac ;;
add-generic-password)
echo "$*" >> '`+added+`' ;;
*) exit 1 ;;
esac
`)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
claudeKeychain = true
forgetClaudeCredential()
c, loc, ok := readClaudeCredential()
if !ok || c.OAuth.AccessToken != "dir-item" || !loc.keychain {
t.Fatalf("the config directory's item is not the sign-in: %v %+v %+v", ok, c.OAuth, loc)
}
if err := putClaudeLogin(savedLogin{Agent: "claude", User: "fresh@example.com", Auth: mustJSONRaw(t, map[string]any{
"claudeAiOauth": map[string]any{"accessToken": "fresh-access", "refreshToken": "fresh-refresh"},
})}); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(added); !strings.Contains(string(b), "-s "+claudeDirService(dir)+" -a tester") || !strings.Contains(string(b), "fresh-access") {
t.Fatalf("the sign-in was not put in the config directory's item: %s", b)
}
}
// An item magpie wrote over lines comes back from `security -w` as hex,
// which Claude Code reads as no item at all (#70): beside a file that holds
// a sign-in, the file is the one it goes by, and the item is left alone.
// With no file, the item is the sign-in, written again on one line.
func TestClaudeHexKeychainItemIsNoneBesideAFile(t *testing.T) {
shellFakes(t)
home := claudeHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("USER", "tester")
creds := claudeSignIn(t, home, time.Now().Add(time.Hour))
bin := filepath.Join(home, "bin")
os.MkdirAll(bin, 0o755)
added := filepath.Join(home, "added.log")
item := hex.EncodeToString([]byte("{\n \"claudeAiOauth\": {\"accessToken\": \"hexed\", \"refreshToken\": \"r\"}\n}"))
testenv.Program(t, filepath.Join(bin, "security"), `#!/bin/sh
case "$1" in
find-generic-password)
case "$*" in
*"-a tester"*) echo '`+item+`' ;;
*) exit 1 ;;
esac ;;
add-generic-password)
echo "$*" >> '`+added+`' ;;
*) exit 1 ;;
esac
`)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
claudeKeychain = true
forgetClaudeCredential()
c, loc, ok := readClaudeCredential()
if !ok || c.OAuth.AccessToken != "sk-ant-oat01-old" || loc.keychain || loc.path != creds {
t.Fatalf("a hex item Claude Code can't read was taken over the file beside it: %v %+v %+v", ok, c.OAuth, loc)
}
if b, _ := os.ReadFile(added); len(b) > 0 {
t.Fatalf("the hex item was written while the file is the sign-in: %s", b)
}
os.Remove(creds)
forgetClaudeCredential()
c, loc, ok = readClaudeCredential()
if !ok || c.OAuth.AccessToken != "hexed" || !loc.keychain || loc.account != "tester" {
t.Fatalf("with no file the hex item is not the sign-in: %v %+v %+v", ok, c.OAuth, loc)
}
if b, _ := os.ReadFile(added); !strings.Contains(string(b), `{"claudeAiOauth":{"accessToken":"hexed"`) {
t.Fatalf("the hex item was not written again on one line: %s", b)
}
}
// 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) {
shellFakes(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
testenv.Program(t, filepath.Join(bin, "security"), `#!/bin/sh
case "$*" in
*"-a tester"*) echo '{"claudeAiOauth":{"accessToken":"live","subscriptionType":"max"}}' ;;
*) echo '{}' ;;
esac
`)
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)
}
}