From 5d7babcdddcf4d44edbae3a176e8876ad7ae3863 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 2 Oct 2026 14:10:27 +0800 Subject: [PATCH] =?UTF-8?q?claude:=20a=20Claude=20Code=20/logout=20takes?= =?UTF-8?q?=20only=20the=20account=20it=20was=20signed=20in=20to,=20and=20?= =?UTF-8?q?that=20one=20is=20never=20served=20in=20its=20place=20(StringKe?= =?UTF-8?q?=20on=20Discord:=20Claude-Code=20->=20Magpie=20->=20Claude=20Su?= =?UTF-8?q?bscription=20=E7=9A=84=E8=BF=99=E4=B8=AA=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E9=A1=BA=E7=95=85=E5=90=97=EF=BC=8C=E6=98=A8?= =?UTF-8?q?=E5=A4=A9=20/logout=20=E4=B9=8B=E5=90=8E=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=85=AD=E4=B8=AA=E8=B4=A6=E5=8F=B7=E5=85=A8=E9=83=A8=E9=83=BD?= =?UTF-8?q?=E8=A6=81=E9=87=8D=E6=96=B0=E7=99=BB=E5=BD=95)=20=E2=80=94=20be?= =?UTF-8?q?fore=2041d50e15=20(v0.1.588)=20a=20logout=20left=20magpie=20no?= =?UTF-8?q?=20Claude=20account=20at=20all=20and=20told=20the=20user=20to?= =?UTF-8?q?=20sign=20in=20again;=20since=20then=20the=20account=20seen=20l?= =?UTF-8?q?ast=20stands=20in.=20That=20one=20is=20the=20account=20Claude?= =?UTF-8?q?=20Code=20was=20signed=20in=20to,=20and=20Claude=20Code's=20/lo?= =?UTF-8?q?gout=20revokes=20the=20refresh=20token=20it=20holds=20(2.1.287'?= =?UTF-8?q?s=20performLogout:=20POST=20/revoke=20with=20claud?= =?UTF-8?q?eAiOauth.refreshToken,=20best=20effort,=20before=20it=20deletes?= =?UTF-8?q?=20its=20keychain=20item):=20magpie's=20copy=20of=20it=20in=20l?= =?UTF-8?q?ogins.json,=20bookmarked=20every=2030s,=20is=20that=20same=20si?= =?UTF-8?q?gn-in,=20so=20the=20stand-in=20served=20first=20was=20a=20revok?= =?UTF-8?q?ed=20one=20and=20every=20request=20paid=20a=20failed=20Claude?= =?UTF-8?q?=20Code=20run=20before=20moving=20on.=20The=20saved=20accounts?= =?UTF-8?q?=20in=20config=20directories=20of=20their=20own=20are=20sign-in?= =?UTF-8?q?s=20of=20their=20own=20and=20outlive=20the=20logout.=20remember?= =?UTF-8?q?Logins=20now=20marks=20the=20account=20Claude=20Code=20holds=20?= =?UTF-8?q?(savedLogin.Held,=20cleared=20on=20the=20others);=20when=20Clau?= =?UTF-8?q?de=20Code=20is=20next=20seen=20signed=20out,=20that=20one=20is?= =?UTF-8?q?=20marked=20lapsed=20("Claude=20Code's=20/logout=20signed=20it?= =?UTF-8?q?=20out=20(it=20revokes=20the=20sign-in=20it=20holds);=20sign=20?= =?UTF-8?q?in=20again")=20and=20the=20stand-in=20is=20picked=20among=20the?= =?UTF-8?q?=20others=20=E2=80=94=20never=20the=20held=20or=20logged-out=20?= =?UTF-8?q?one,=20one=20with=20a=20saved=20sign-in,=20on=20before=20off,?= =?UTF-8?q?=20then=20the=20one=20seen=20last.=20A=20switch=20in=20magpie?= =?UTF-8?q?=20isn't=20taken=20for=20a=20logout=20(the=20account=20switched?= =?UTF-8?q?=20off=20is=20saved=20as=20it=20was),=20and=20a=20/login=20in?= =?UTF-8?q?=20Claude=20Code=20to=20the=20same=20account=20clears=20the=20l?= =?UTF-8?q?apse.=20TestClaudeAccountsOutliveLogout=20(now:=20the=20revoked?= =?UTF-8?q?=20account=20lapsed=20and=20never=20put=20in=20a=20config=20dir?= =?UTF-8?q?ectory,=20the=20one=20on=20standing=20in,=20another=20on=20behi?= =?UTF-8?q?nd=20it)=20and=20TestClaudeSignedOut=20fail=20without=20the=20c?= =?UTF-8?q?hange;=20TestClaudeSwitchIsNoLogout=20guards=20the=20switch.=20?= =?UTF-8?q?No=20sign-in=20was=20made=20and=20nothing=20was=20asked=20of=20?= =?UTF-8?q?Anthropic.=20go=20vet=20./internal/...,=20go=20build=20-tags=20?= =?UTF-8?q?nogui=20(darwin,=20GOOS=3Dlinux),=20GOOS=3Dwindows=20go=20build?= =?UTF-8?q?,=20go=20test=20-tags=20nogui=20-count=3D1=20./internal/...=20(?= =?UTF-8?q?under=20a=20temp=20HOME)=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/provider/claude_dirs.go | 56 ++++++++++++++----- internal/provider/claude_logout_test.go | 71 +++++++++++++++++++++---- internal/provider/logins.go | 13 +++++ 3 files changed, 118 insertions(+), 22 deletions(-) diff --git a/internal/provider/claude_dirs.go b/internal/provider/claude_dirs.go index 895c478f5..b316c72d5 100644 --- a/internal/provider/claude_dirs.go +++ b/internal/provider/claude_dirs.go @@ -150,28 +150,60 @@ func takeClaudeDir(l *savedLogin, c claudeCredentials) (bool, error) { // claudeStandIn is the saved account served in the place of Claude Code's // own while Claude Code is signed out — the user logged out of it, as it -// tells them to when magpie's token is set beside a claude.ai sign-in. It -// is the account seen last, which is the one Claude Code was signed in to -// until then (rememberLogins sees that one every 30s), passing over one -// whose saved sign-in is gone while another has one; "" when none is -// saved. magpie keeps every account's sign-in itself, so a logout signs -// none of them out of magpie: the stand-in runs Claude Code in a config -// directory of its own, as the others on do. +// tells them to when magpie's token is set beside a claude.ai sign-in. +// magpie keeps the other accounts' sign-ins itself, each its own, so a +// logout signs none of them out of magpie: the stand-in runs Claude Code +// in a config directory of its own, as the others on do. Never the one +// Claude Code was signed in to, whose sign-in its /logout revoked +// (claudeLoggedOut, or still Held before rememberLogins next looks: it is +// asked only while Claude Code is signed out); of the others, one with a +// saved sign-in, on before off, then the one seen last; one whose saved +// sign-in is gone only when no other has one; "" when there is none. func claudeStandIn(ls []savedLogin) string { - user, signedIn := "", false + user, best := "", -1 var seen time.Time for _, l := range ls { - if l.Agent != "claude" { + if l.Agent != "claude" || l.Held || l.Lapsed == claudeLogoutLapse { continue } - _, ok := parseClaudeCredentials(l.Auth) - if user == "" || ok && !signedIn || ok == signedIn && l.Seen.After(seen) { - user, seen, signedIn = l.User, l.Seen, ok + rank := 0 + if _, ok := parseClaudeCredentials(l.Auth); ok { + rank = 2 + if l.On { + rank++ + } + } + if rank > best || rank == best && l.Seen.After(seen) { + user, seen, best = l.User, l.Seen, rank } } return user } +// claudeLogoutLapse is why the account Claude Code was signed in to can't +// be used once Claude Code logged out. +const claudeLogoutLapse = "Claude Code's /logout signed it out (it revokes the sign-in it holds); sign in again" + +// claudeLoggedOut marks the account Claude Code held, now that it is +// signed out, as lapsed: /logout revokes the refresh token Claude Code +// holds (POST /revoke, Claude Code 2.1.x's performLogout), and +// magpie's copy of that account is the same sign-in, so it is gone too. +// The accounts magpie keeps in config directories of their own are +// sign-ins of their own and stay. It says whether ls changed. +func claudeLoggedOut(ls []savedLogin) bool { + changed := false + for i := range ls { + if ls[i].Agent != "claude" || !ls[i].Held { + continue + } + ls[i].Held, changed = false, true + if _, ok := parseClaudeCredentials(ls[i].Auth); ok && ls[i].Lapsed == "" { + ls[i].Lapsed = claudeLogoutLapse + } + } + return changed +} + // claudeSignedOut is why a saved Claude account can't be used: its saved // sign-in is gone, and it has to be signed in again; "" when it has one. func claudeSignedOut(l savedLogin) string { diff --git a/internal/provider/claude_logout_test.go b/internal/provider/claude_logout_test.go index ca0c7f5b3..a038369aa 100644 --- a/internal/provider/claude_logout_test.go +++ b/internal/provider/claude_logout_test.go @@ -27,6 +27,7 @@ func TestClaudeAccountsOutliveLogout(t *testing.T) { writeFile(t, loginsPath(), []map[string]any{ {"agent": "claude", "user": "on@example.com", "plan": "max", "on": true, "seen": seen, "auth": oauth("tok-on")}, {"agent": "claude", "user": "off@example.com", "plan": "pro", "seen": seen, "auth": oauth("tok-off")}, + {"agent": "claude", "user": "two@example.com", "plan": "max", "on": true, "seen": seen.Add(-time.Hour), "auth": oauth("tok-two")}, }) loginsMu.Lock() loginsSeenAt = time.Time{} @@ -43,34 +44,43 @@ func TestClaudeAccountsOutliveLogout(t *testing.T) { loginsSeenAt = time.Time{} loginsMu.Unlock() + // the one Claude Code held went with its /logout, which revokes the + // refresh token it holds — magpie's copy of it too (StringKe on + // Discord: all six had to be signed in again). The others are sign-ins + // of their own: one on stands in, never the revoked one. p, ok := find(All(), "claude") - if !ok || p.Account == nil || p.Account.User != "own@example.com" { + if !ok || p.Account == nil || p.Account.User != "on@example.com" { t.Fatalf("logged out: %v %+v", ok, p.Account) } dir, own, err := p.Account.Token(context.Background()) - if err != nil || !own || dir != claudeAccountDir("own@example.com") { + if err != nil || !own || dir != claudeAccountDir("on@example.com") { t.Fatalf("dir %q %v %v", dir, own, err) } - if c, ok := readClaudeDir(dir); !ok || c.OAuth.RefreshToken != "sk-ant-ort01-old" { + if c, ok := readClaudeDir(dir); !ok || c.OAuth.RefreshToken != "r-tok-on" { t.Fatalf("dir credentials %+v", c.OAuth) } + if _, err := os.Stat(claudeAccountDir("own@example.com")); !os.IsNotExist(err) { + t.Fatalf("the revoked sign-in was put to use: %v", err) + } var also []string for _, q := range p.AlsoOn() { also = append(also, q.Account.User) } - if len(also) != 1 || also[0] != "on@example.com" { + if len(also) != 1 || also[0] != "two@example.com" { t.Fatalf("also on: %v", also) } ls := Logins("claude") - if len(ls) != 3 { + if len(ls) != 4 { t.Fatalf("logins: %+v", ls) } for _, l := range ls { - if l.Active || l.On != (l.User != "off@example.com") { + revoked := l.User == "own@example.com" + if l.Active || l.On != (l.User == "on@example.com" || l.User == "two@example.com") || + (l.Lapsed == claudeLogoutLapse) != revoked { t.Fatalf("login %+v", l) } } - if u := InUseLogin("claude"); u != "own@example.com" { + if u := InUseLogin("claude"); u != "on@example.com" { t.Fatalf("in use %q", u) } for _, x := range savedButSignedOut() { @@ -79,16 +89,57 @@ func TestClaudeAccountsOutliveLogout(t *testing.T) { } } - // it can be paused behind the one on, as when Claude Code was signed in to it - if err := SetLoginOn("claude", "own@example.com", false); err != nil { + // the stand-in can be paused behind another on, as Claude Code's own could + if err := SetLoginOn("claude", "on@example.com", false); err != nil { t.Fatal(err) } if p, _ = find(All(), "claude"); !p.OwnPaused() { t.Fatal("not paused") } - if u := InUseLogin("claude"); u != "on@example.com" { + if u := InUseLogin("claude"); u != "two@example.com" { t.Fatalf("in use %q", u) } + + // /login again in Claude Code, to the same account: a sign-in anew, + // no longer lapsed + writeFile(t, cred, oauth("tok-again")) + writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": map[string]any{"emailAddress": "own@example.com"}}) + ForgetAccounts() + for _, l := range Logins("claude") { + if l.User == "own@example.com" && (!l.Active || l.Lapsed != "") { + t.Fatalf("signed in again: %+v", l) + } + } +} + +// A switch in magpie takes Claude Code off one account and puts it on +// another, saving the first's sign-in as it was: nothing revoked it, and it +// isn't taken for logged out. +func TestClaudeSwitchIsNoLogout(t *testing.T) { + home := claudeHome(t) + t.Setenv("CLAUDE_CONFIG_DIR", "") + noAnthropic(t) + claudeSignIn(t, home, time.Now().Add(time.Hour)) + writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": map[string]any{"emailAddress": "own@example.com"}}) + far := time.Now().Add(24 * time.Hour).UnixMilli() + writeFile(t, loginsPath(), []map[string]any{ + {"agent": "claude", "user": "other@example.com", "plan": "max", "seen": time.Now().Add(-time.Hour).UTC(), + "profile": map[string]any{"emailAddress": "other@example.com"}, + "auth": map[string]any{"claudeAiOauth": map[string]any{"accessToken": "tok-other", "refreshToken": "r-other", "expiresAt": far}}}, + }) + ForgetAccounts() + if u := InUseLogin("claude"); u != "own@example.com" { + t.Fatalf("in use %q", u) + } + if err := SwitchLogin("claude", "other@example.com"); err != nil { + t.Fatal(err) + } + ForgetAccounts() + for _, l := range Logins("claude") { + if l.Lapsed != "" || l.Active != (l.User == "other@example.com") { + t.Fatalf("login %+v", l) + } + } } // Logged out of Claude Code with accounts in magpie, an account signed in diff --git a/internal/provider/logins.go b/internal/provider/logins.go index c087bb440..a64933bde 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -90,6 +90,10 @@ type savedLogin struct { // paused it in magpie (#263): the gateway passes over it while another // of the agent's accounts is on, the agent staying signed in to it. Paused bool `json:"paused,omitempty"` + // Held is the Claude account Claude Code itself was signed in to when + // magpie last looked: its saved copy is that very sign-in, which + // Claude Code's /logout revokes (claudeLoggedOut). + Held bool `json:"held,omitempty"` } var ( @@ -470,9 +474,18 @@ func rememberLogins(force bool) { } l, ok := liveLogin(agent) if !ok { + if agent == "claude" && claudeLoggedOut(ls) { + changed = true + } continue } l.Seen = time.Now().UTC().Truncate(time.Second) + if agent == "claude" { + for i := range ls { + ls[i].Held = false + } + l.Held = true + } ls = upsertLogin(ls, l) changed = true }