provider: a saved ChatGPT account Codex is signed in to is refreshed in Codex's auth.json, not in its copy
A ChatGPT account switched in to Codex is one sign-in in two places:
logins.json's copy and Codex's auth.json hold one refresh token.
renewSaved refreshed the copy whenever it was asked to: a provider made
for the account while another was Codex's, held over a switch
(SwitchWhenSpent, keep-first), a usage or models read, keep-alive or a
revoked-token check decided "not the live one" just before a switch.
OpenAI rotates the refresh token as it answers, so auth.json was left
with a spent one. The Codex app of an edu or business workspace fetches
its workspace config bundle (/backend-api/wham/config/bundle) at start
with it; the refresh is refused (refresh_token_reused), app-server
answers configRequirements/read with -32603 "failed to read
configuration layers: Your access token could not be refreshed ...",
and the app stays paused on "无法加载组织设置". Reinstalling the app or
changing the preferred account doesn't help: auth.json stays.
renewSaved now asks, under savedTokenMu (which a switch holds too),
whether the saved account is the one Codex is signed in to (sameLogin,
or the same refresh token) and renews it through codexTokenRenewing on
Codex's own store instead.
Reported by 万贤凯 on Discord (two edu_plus accounts, "Codex 始终登录首选
账号" on, both used up).
Run:
- TestSavedRefreshOfCodexOwnRenewsCodexOwn: red on the old code (auth.json
kept refresh token "r" after it was spent), green with the fix.
- Codex app-server 0.163.0-alpha.10.2 in a sandbox CODEX_HOME, edu_plus
auth.json, fake bundle (401) and token endpoint (refresh_token_reused):
configRequirements/read gives the -32603 above, the reply the desktop
shows as the org-settings dialog. magpie's openai_base_url and
[model_providers.magpie] don't move the bundle request (it goes to
chatgpt_base_url, which magpie never writes).
- gofmt -l; go vet darwin, linux -tags nogui, windows.
- Full suite (go test -tags nogui -timeout 45m ./..., through suite-lock,
at dd1e8822d plus this change): every package ok except internal/gui's
TestRestartHandsTheGatewayOver (one request during the handover got
EOF, 12 of 13). It passes 5 of 5 alone and this change doesn't reach
it; filed as #3451.
Not tried: the reporter's own auth.json and logins.json, so which caller
spent their token isn't known; a real edu account; the GUI (unchanged).
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A saved ChatGPT account switched in to Codex is one sign-in in two
|
||||
// places: logins.json's copy and Codex's auth.json hold one refresh token.
|
||||
// A refresh asked for through the copy (an account in use beside Codex's
|
||||
// own when the switch came, the backend calling its token expired) spent
|
||||
// that token under Codex, and auth.json was left with a dead one. The
|
||||
// Codex app of an edu workspace reads its workspace's settings with it at
|
||||
// start, and stayed paused on "无法加载组织设置" (万贤凯 on Discord).
|
||||
func TestSavedRefreshOfCodexOwnRenewsCodexOwn(t *testing.T) {
|
||||
home, refreshes, _ := codexExpiredFakes(t, staleExpired, false)
|
||||
if err := SwitchLogin("codex", "me@example.com"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var live codexAuth
|
||||
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
|
||||
if live.Tokens.RefreshToken != "r" {
|
||||
t.Fatalf("switched in: %+v", live.Tokens)
|
||||
}
|
||||
|
||||
// as the provider made for me@ while work@ was Codex's asks, held
|
||||
// over the switch
|
||||
tok, accountID, err := renewSavedLoginFrom(context.Background(), "codex", "me@example.com", live.Tokens.AccessToken)
|
||||
if err != nil || tok != "fresh-r" || accountID != "acct-1" {
|
||||
t.Fatalf("renewed: %q %q %v", tok, accountID, err)
|
||||
}
|
||||
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
|
||||
if live.Tokens.AccessToken != "fresh-r" || live.Tokens.RefreshToken != "r-2" {
|
||||
t.Fatalf("Codex's auth.json kept the refresh token spent: %+v", live.Tokens)
|
||||
}
|
||||
if got := refreshes(); len(got) != 1 || got[0] != "r" {
|
||||
t.Fatalf("refreshed with %q, want r once", got)
|
||||
}
|
||||
|
||||
// and by keep-alive's force: Codex's own is fresh, so nothing is sent
|
||||
// that would spend it
|
||||
if _, _, err := renewSavedLogin(context.Background(), "codex", "me@example.com", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := refreshes(); len(got) != 1 {
|
||||
t.Fatalf("refreshed again with %q", got)
|
||||
}
|
||||
|
||||
// work@, the one switched away from, still renews its own copy, and
|
||||
// only that
|
||||
for _, l := range readLogins() {
|
||||
if l.User == "work@example.com" && !strings.Contains(string(l.Auth), `"r-work"`) {
|
||||
t.Fatalf("work@'s copy: %s", l.Auth)
|
||||
}
|
||||
}
|
||||
if tok, _, err := renewSavedLogin(context.Background(), "codex", "work@example.com", true); err != nil || tok != "fresh-r-work" {
|
||||
t.Fatalf("work@: %q %v", tok, err)
|
||||
}
|
||||
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
|
||||
if live.Tokens.RefreshToken != "r-2" {
|
||||
t.Fatalf("work@'s refresh reached auth.json: %+v", live.Tokens)
|
||||
}
|
||||
}
|
||||
@@ -233,6 +233,18 @@ func renewSaved(ctx context.Context, agent, user string, force bool, stale strin
|
||||
if l == nil {
|
||||
return "", "", fmt.Errorf("no saved %s account %q", agent, user)
|
||||
}
|
||||
if agent == "codex" && heldByCodex(*l) {
|
||||
// the account Codex is signed in to now (switched to since this
|
||||
// was asked for, or the copy kept of it as it was): one sign-in in
|
||||
// two places, whose refresh token a refresh here would spend under
|
||||
// Codex. Codex's own auth.json then holds a dead one, and the app
|
||||
// of an edu or business workspace, which reads its workspace's
|
||||
// settings with it at start, stays paused on "无法加载组织设置"
|
||||
// (万贤凯 on Discord). So it is renewed where Codex keeps it, as
|
||||
// Codex's own (codexTokenRenewing), and the copy is left as it is:
|
||||
// a switch away saves it afresh from Codex's.
|
||||
return codexTokenRenewing(ctx, codexHome(), stale)
|
||||
}
|
||||
var auth []byte
|
||||
switch agent {
|
||||
case "codex":
|
||||
@@ -274,6 +286,23 @@ func renewSaved(ctx context.Context, agent, user string, force bool, stale strin
|
||||
return tok, accountID, writeLogins(ls)
|
||||
}
|
||||
|
||||
// heldByCodex says the saved ChatGPT account l is the one Codex is signed
|
||||
// in to: the same login, or the same refresh token. Asked under
|
||||
// savedTokenMu, which a switch holds too, so it can't change before the
|
||||
// refresh it decides is done.
|
||||
func heldByCodex(l savedLogin) bool {
|
||||
live, ok := liveLogin("codex")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if sameLogin(live, l) {
|
||||
return true
|
||||
}
|
||||
var a, b codexAuth
|
||||
return json.Unmarshal(live.Auth, &a) == nil && json.Unmarshal(l.Auth, &b) == nil &&
|
||||
a.Tokens.RefreshToken != "" && a.Tokens.RefreshToken == b.Tokens.RefreshToken
|
||||
}
|
||||
|
||||
// savedRefreshFailed words a saved account's failed refresh. One the vendor
|
||||
// refused is marked lapsed on the account: that sign-in is gone and has to be
|
||||
// made again, in magpie; the agent's own login command would sign the agent
|
||||
|
||||
Reference in New Issue
Block a user