provider: an account signed in from magpie is listed even when the agent's own sign-in of it can't be read (tinysnake: workbuddy 登录以后,对应的 provider 并未出现) — once WorkBuddy's own account had been seen, logins.json remembered it as WorkBuddy's own (no secrets). With WorkBuddy then signed out, or its tokens kept encrypted ({"$wbEncrypted":…}), that entry is hidden, and signing the same account in from magpie was taken for a duplicate of it and dropped (addSideLogin): the sign-in said done, the toast showed, and no WorkBuddy provider appeared. addSideLogin is now told who the agent is signed in to right now; only when that is the same account is the new sign-in a duplicate, otherwise the remembered entry takes the new sign-in, and sideLogins doesn't add the own account twice once it's back. Copilot, Grok, ZCode and Gemini had the same flaw through the same code and are fixed with it. The window no longer says "added" when a finished sign-in isn't in the Providers list: it says so as an error. Tested with a fake WorkBuddy sign-in server in a sandbox HOME (own account remembered, then signed out or encrypted, then signed in from magpie: listed and signing with the new token; WorkBuddy readable again: still one account); not tested against the real WorkBuddy, and whether the reporter's WorkBuddy was signed out or encrypted isn't known

This commit is contained in:
yetone
2026-09-28 21:38:02 +08:00
parent 46454d9801
commit bb7ce5ec31
9 changed files with 170 additions and 21 deletions
+3 -1
View File
@@ -3592,7 +3592,9 @@ async function followSignIn(id) {
const p = providers.providers.find((x) => x.account?.agent === st.agent);
if (p) { editing = p.id; draft = null; adding = false; presetQuery = ""; }
renderProviders();
status(st.using ? t("Signed in as {user}", { user: st.user }) : t("{user} added — switch to it any time", { user: st.user }), "ok");
// signed in but listed nowhere (#155): say so rather than "added"
if (!p) status(t("{user} signed in, but magpie can't list it — please report this", { user: st.user }), "err");
else status(st.using ? t("Signed in as {user}", { user: st.user }) : t("{user} added — switch to it any time", { user: st.user }), "ok");
state = await api("state");
renderAgents();
setTimeout(() => { justAdded = ""; }, 2000);
+1
View File
@@ -315,6 +315,7 @@ const I18N = {
"Try again": "重试",
"Signed in as {user}": "已登录 {user}",
"{user} added — switch to it any time": "已添加 {user},随时可以切换",
"{user} signed in, but magpie can't list it — please report this": "{user} 已登录,但 magpie 无法列出它,请反馈此问题",
"{user} removed": "已移除 {user}",
"magpie forgets this account's sign-in; the account itself is untouched": "magpie 忘掉这个账号的登录信息;账号本身不受影响",
"Sign {agent} in to this account": "让 {agent} 登录到这个账号",
+12 -9
View File
@@ -45,14 +45,8 @@ type copilotLoginApp struct {
// copilotLogins lists the Copilot accounts, the first in use first, then
// the rest as they were added.
func copilotLogins(cfg string) []copilotLoginApp {
own, hasOwn := copilotLogin(cfg)
ownUser := ""
if hasOwn {
ownUser = own.User
if ownUser == "" {
ownUser = "GitHub"
}
}
own, _ := copilotLogin(cfg)
ownUser := copilotOwnUser(cfg)
var out []copilotLoginApp
for _, l := range sideLogins("copilot", ownUser, func(l savedLogin) bool {
_, ok := copilotSaved(l)
@@ -68,6 +62,15 @@ func copilotLogins(cfg string) []copilotLoginApp {
return out
}
// copilotOwnUser is who the editors' or CLI's Copilot sign-in is, "" for none.
func copilotOwnUser(cfg string) string {
own, ok := copilotLogin(cfg)
if !ok {
return ""
}
return firstNonEmpty(own.User, "GitHub")
}
func copilotSide() []sideLogin {
var out []sideLogin
for _, c := range copilotLogins(copilotConfigDir()) {
@@ -93,7 +96,7 @@ func forgetCopilotLogin(user string) error {
// addCopilotLogin keeps an account magpie just signed in.
func addCopilotLogin(user, plan, token string) error {
auth, _ := json.Marshal(map[string]string{"oauth_token": token})
return addSideLogin(savedLogin{Agent: "copilot", User: user, Plan: plan, Auth: auth}, func(savedLogin) {})
return addSideLogin(savedLogin{Agent: "copilot", User: user, Plan: plan, Auth: auth}, copilotOwnUser(copilotConfigDir()), func(savedLogin) {})
}
// copilotAccount is the Copilot account in use first.
+5 -1
View File
@@ -282,7 +282,11 @@ func forgetGoogleLogin(agent, user string) error {
// addGoogleLogin keeps an account magpie just signed in.
func addGoogleLogin(agent, user, plan string, a googleAuth) error {
auth, _ := json.Marshal(a)
return addSideLogin(savedLogin{Agent: agent, User: user, Plan: plan, Auth: auth, Project: a.Project}, func(savedLogin) {})
ownUser := ""
if own, ok := geminiOwnLogin(); ok && agent == "gemini" {
ownUser = own.user
}
return addSideLogin(savedLogin{Agent: agent, User: user, Plan: plan, Auth: auth, Project: a.Project}, ownUser, func(savedLogin) {})
}
// SetGoogleProject names the Google Cloud project an account's requests
+2 -1
View File
@@ -94,7 +94,8 @@ func addGrokLogin(home string) (string, error) {
removeGrokHome(home)
return "", errors.New("grok login finished without an account")
}
return c.Email, addSideLogin(savedLogin{Agent: "grok", User: c.Email, Home: home},
own, _ := readGrokCredential(GrokHome())
return c.Email, addSideLogin(savedLogin{Agent: "grok", User: c.Email, Home: home}, own.Email,
func(l savedLogin) { removeGrokHome(l.Home) })
}
+14 -5
View File
@@ -9,6 +9,7 @@ package provider
import (
"fmt"
"slices"
"strings"
"time"
)
@@ -43,7 +44,9 @@ func sideLogins(agent, ownUser string, usable func(savedLogin) bool) []sideLogin
}
}
}
if !found {
// one of magpie's that is the same account stands for it: it took
// the agent's own over while that couldn't be read (addSideLogin)
if !found && !slices.ContainsFunc(ls, func(l savedLogin) bool { return l.Agent == agent && strings.EqualFold(l.User, ownUser) }) {
ls = append(ls, savedLogin{Agent: agent, User: ownUser, Seen: time.Now().UTC().Truncate(time.Second)})
_ = writeLogins(ls)
}
@@ -156,21 +159,27 @@ func forgetSideLogin(agent, user, ownHow string, ls []sideLogin, gone func(saved
// addSideLogin keeps an account magpie just signed in, in use beside the
// others. Signed in again, an account keeps the newer sign-in; one that is
// the agent's own already is not kept twice (dup is told of what is let
// go either way).
func addSideLogin(l savedLogin, dup func(savedLogin)) error {
// go either way). ownUser is who the agent is signed in to now, "" for no
// one it can be read as: the agent's own account remembered from before,
// while the agent is signed out or keeps its tokens encrypted, takes the
// new sign-in rather than letting it go — let go, it was listed nowhere
// (#155).
func addSideLogin(l savedLogin, ownUser string, dup func(savedLogin)) error {
loginsMu.Lock()
defer loginsMu.Unlock()
ls := readLogins()
l.Seen = time.Now().UTC().Truncate(time.Second)
for i := range ls {
if ls[i].Agent == l.Agent && strings.EqualFold(ls[i].User, l.User) {
if ls[i].own() {
if ls[i].own() && strings.EqualFold(ownUser, l.User) {
dup(l)
return nil
}
old := ls[i]
ls[i].Auth, ls[i].Home, ls[i].Plan, ls[i].Seen = l.Auth, l.Home, l.Plan, l.Seen
if old.Home != l.Home {
if old.own() {
ls[i].On = true
} else if old.Home != l.Home {
dup(old)
}
return writeLogins(ls)
+5 -2
View File
@@ -654,14 +654,17 @@ func startWorkBuddySignIn(s *signInFlow) error {
c.UID = acc.UID
who := wbWho(acc.Nickname, acc.PhoneNumber, acc.UID)
auth, _ := json.Marshal(c)
if err := addSideLogin(savedLogin{Agent: "workbuddy", User: who, Auth: auth}, func(savedLogin) {}); err != nil {
ownUser, _, hasOwn := wbOwn()
if !hasOwn {
ownUser = ""
}
if err := addSideLogin(savedLogin{Agent: "workbuddy", User: who, Auth: auth}, ownUser, func(savedLogin) {}); err != nil {
fail(err.Error())
return
}
wbTokens.Lock()
wbTokens.m[c.UID] = c
wbTokens.Unlock()
ownUser, _, hasOwn := wbOwn()
s.finish(SignInState{State: "done", User: who, Using: hasOwn && strings.EqualFold(ownUser, who)})
}()
return nil
+123
View File
@@ -0,0 +1,123 @@
package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
// wbFakeSignIn serves WorkBuddy's sign-in for one account, uid u1 "Me".
func wbFakeSignIn(t *testing.T) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ok := func(data any) { json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": data}) }
switch r.URL.Path {
case "/v2/plugin/auth/state":
ok(map[string]any{"state": "s1", "authUrl": "https://login.codebuddy.cn/oauth?client_id=c"})
case "/v2/plugin/auth/token":
ok(map[string]any{"accessToken": "new-access", "refreshToken": "new-refresh",
"expiresIn": 3600, "refreshExpiresIn": 7200, "domain": "www.codebuddy.cn", "tokenType": "Bearer"})
case "/v2/plugin/login/account":
ok(map[string]any{"uid": "u1", "nickname": "Me"})
default:
w.WriteHeader(404)
}
}))
t.Cleanup(srv.Close)
oldEnd, oldInt := wbEndpoint, wbPollInterval
wbEndpoint, wbPollInterval = srv.URL, 5*time.Millisecond
t.Cleanup(func() { wbEndpoint, wbPollInterval = oldEnd, oldInt })
}
func wbSignInNow(t *testing.T) SignInState {
t.Helper()
st, err := StartSignIn("workbuddy")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
st, _ = WaitSignIn(ctx, st.ID)
return st
}
func wbWriteOwn(t *testing.T, home string, access any) {
t.Helper()
writeFile(t, wbAuthFile(home), map[string]any{
"account": map[string]any{"uid": "u1", "nickname": "Me"},
"auth": map[string]any{"accessToken": access, "refreshToken": "r",
"expiresAt": time.Now().Add(24 * time.Hour).UnixMilli(), "domain": "www.codebuddy.cn"},
})
}
// WorkBuddy's own account was seen once (so logins.json remembers it as
// WorkBuddy's own), then WorkBuddy can no longer be read — signed out, or its
// tokens encrypted at rest. Signing that account in from magpie must list
// it: it was taken for WorkBuddy's own and dropped, so the sign-in said done
// and the Providers page showed no WorkBuddy (#155).
func TestWorkBuddySignInOverUnreadableOwn(t *testing.T) {
for _, gone := range []string{"signed out", "encrypted"} {
t.Run(gone, func(t *testing.T) {
home := signIn(t)
wbTokens.Lock()
wbTokens.m = map[string]wbCreds{}
wbTokens.Unlock()
wbFakeSignIn(t)
wbWriteOwn(t, home, "own-access")
if ls := Logins("workbuddy"); len(ls) != 1 || ls[0].User != "Me" {
t.Fatalf("own not remembered: %+v", ls)
}
if gone == "signed out" {
os.Remove(wbAuthFile(home))
} else {
wbWriteOwn(t, home, map[string]any{"$wbEncrypted": 1, "envelope": "x"})
}
if _, ok := find(All(), "workbuddy"); ok {
t.Fatal("listed with no readable account")
}
if st := wbSignInNow(t); st.State != "done" || st.User != "Me" {
t.Fatalf("sign-in: %+v", st)
}
p, ok := find(All(), "workbuddy")
if !ok || p.Account.User != "Me" {
t.Fatalf("signed in, not listed: %v %+v", ok, p.Account)
}
req, _ := http.NewRequest("POST", p.Chat+"/chat/completions", nil)
if err := p.Sign(context.Background(), req, Chat, []byte(`{}`)); err != nil || req.Header.Get("Authorization") != "Bearer new-access" {
t.Fatalf("signs with: %v %q", err, req.Header.Get("Authorization"))
}
// WorkBuddy readable again as that account: still one of it
wbWriteOwn(t, home, "own-access")
if ls := Logins("workbuddy"); len(ls) != 1 || ls[0].User != "Me" || !ls[0].Active {
t.Fatalf("after WorkBuddy is back: %+v", ls)
}
if _, ok := find(All(), "workbuddy"); !ok {
t.Fatal("not listed after WorkBuddy is back")
}
})
}
}
// Signed in as the account WorkBuddy itself is signed in to and can be
// read: one account, in use, as before.
func TestWorkBuddySignInSameAsOwn(t *testing.T) {
home := signIn(t)
wbTokens.Lock()
wbTokens.m = map[string]wbCreds{}
wbTokens.Unlock()
wbFakeSignIn(t)
wbWriteOwn(t, home, "own-access")
Logins("workbuddy")
if st := wbSignInNow(t); st.State != "done" || !st.Using {
t.Fatalf("sign-in: %+v", st)
}
if ls := Logins("workbuddy"); len(ls) != 1 || ls[0].User != "Me" {
t.Fatalf("logins: %+v", ls)
}
}
+5 -2
View File
@@ -589,11 +589,14 @@ func startZCodeSignIn(s *signInFlow) error {
return
}
auth, _ := json.Marshal(k)
if err := addSideLogin(savedLogin{Agent: "zcode", User: who, Plan: plan, Auth: auth}, func(savedLogin) {}); err != nil {
ownUser, _, ok := zcodeOwn()
if !ok {
ownUser = ""
}
if err := addSideLogin(savedLogin{Agent: "zcode", User: who, Plan: plan, Auth: auth}, ownUser, func(savedLogin) {}); err != nil {
fail(err.Error())
return
}
ownUser, _, ok := zcodeOwn()
s.finish(SignInState{State: "done", User: who, Plan: plan, Using: ok && strings.EqualFold(ownUser, who)})
return
}