A Codex account can now be imported, or ticked later, as "Access token only". magpie uses the access token it was given and never refreshes it. The refresh token is never spent or rotated, so another computer signed in to the same ChatGPT account stays signed in. - Every renewal of a saved account goes through renewSaved. For an access-only account it calls accessOnlyToken, which hands out the token until it runs out and never calls codexRefresh. That covers requests, usage reads, the idle read, models, resets, warm-up, the 401 recovery and the keep-alive, which also skips such accounts. - Once the token runs out, the account is marked Lapsed with refreshRefused. The provider editor shows "Access expired" and "Import again", which opens the import with the tick set. A new import or sign-in replaces the entry and clears both. - Such an account is never switched into Codex, since Codex would renew it. That applies to switchable, switchSavedLogin, NextLogin, keep-as and signInToFirst. The live or held account can't be made access-only (errAccessOnlyHeld). Its row says "magpie only". - Import: ImportCodexAccessOnly accepts files that have only an access token, refuses an expired token or a missing id_token, and makes no refresh call. CLI: `magpie accounts import codex --access-only`, `magpie accounts access-only codex <email> [on|off]`. GUI: the import box tick, the row tick and POST /api/login/access-only|renewed. - The core scripts stay within budget: app.js is smaller than main's. This change's own comments and nine sign-in/import comment blocks were cut, with no raise. Run (on mote): - gofmt -l clean; go vet for darwin, linux (nogui) and windows ok. - go test ./internal/provider ./internal/cli ./internal/gui ok. - GUI files in chromium and webkit, en and zh: codex-access-only (new), login-import, account-return, i18n-keys, account-row-line, agent-switch-accounts and signin-paste-import: 64/64 at the final tree. In an earlier grouped run, signin-paste-import failed once on its Import button's wait. It then passed 6/6 alone and in the rerun. Broken by hand: - With the renewSaved branch and the keep-alive skip taken out, TestCodexAccessOnlyNeverRefreshed went red: the keep-alive renewed the account. TestCodexAccessOnlyRunOutLapses went red too: a run-out token was handed out. - With the branch and the import path taken out, a forced renewal went through and an expired-token import was "added". Both tests went red. Not tried: a real ChatGPT account, so I did not check that another computer stays signed in. No Windows run, since nothing here differs by OS.
130 lines
5.3 KiB
Go
130 lines
5.3 KiB
Go
package provider
|
|
|
|
// A ChatGPT account kept "access token only" (EastonXvii on X): magpie
|
|
// sends its requests with the access token it was given and never
|
|
// refreshes the sign-in. A refresh rotates the refresh token, and the
|
|
// sign-in it came from (Codex on another computer, the tool the file was
|
|
// exported from) is then signed out. Kept so, that one stays signed in,
|
|
// and magpie uses the account until its access token runs out (about ten
|
|
// days for a Codex sign-in): from then on it is lapsed, passed over by the
|
|
// gateway, and its row says to import it again or sign in to it again.
|
|
//
|
|
// Every path that renews a saved ChatGPT sign-in goes through renewSaved
|
|
// (requests, usage reads and their idle look, the warm-up, a 401's
|
|
// recovery, a revoked sign-in's check), which never refreshes such an
|
|
// account; the keep-alive passes it over. Codex itself is never signed in
|
|
// to it (switchSavedLogin), as Codex would refresh it as it runs.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// errCodexAccessOnlySwitch: Codex is never signed in to an account kept
|
|
// access token only, which it would refresh.
|
|
const errCodexAccessOnlySwitch = "%s is kept access token only: magpie's requests use it, but Codex itself isn't signed in to it, as Codex would renew its sign-in"
|
|
|
|
// accessOnlyLapse is what an account kept access token only says once
|
|
// its access token has run out, or ChatGPT turned it away.
|
|
func accessOnlyLapse(user string) string {
|
|
return user + "'s access token has expired, and it is kept access token only, so magpie doesn't renew it — import it again, or sign in to it again"
|
|
}
|
|
|
|
// accessOnlyExpired says the access token of the Codex sign-in auth has
|
|
// run out by now. One whose expiry can't be read hasn't.
|
|
func accessOnlyExpired(auth json.RawMessage, now time.Time) bool {
|
|
var a codexAuth
|
|
return json.Unmarshal(auth, &a) == nil && accessTokenRunOut(a.Tokens.AccessToken, now)
|
|
}
|
|
|
|
// accessTokenRunOut says the JWT access token tok has run out by now.
|
|
func accessTokenRunOut(tok string, now time.Time) bool {
|
|
exp, _ := jwtClaims(tok)["exp"].(float64)
|
|
return exp != 0 && !now.Before(time.Unix(int64(exp), 0))
|
|
}
|
|
|
|
// accessOnlyToken is renewSaved for a saved ChatGPT account kept access
|
|
// token only: its access token while it has time left by its own exp
|
|
// (not refreshed five minutes early: it can't be), and never a refresh.
|
|
// One run out, or turned away by the backend (stale), lapses the account,
|
|
// and one lapsed so stays lapsed until it is imported again.
|
|
// A refresh forced with the token still good (a revoked sign-in's check)
|
|
// is an error that lapses nothing.
|
|
func accessOnlyToken(user, lapsed string, a codexAuth, accountID string, force bool, stale string) (string, string, error) {
|
|
tok := a.Tokens.AccessToken
|
|
if stale != "" && tok != stale {
|
|
return tok, accountID, nil // imported again since
|
|
}
|
|
if lapsed == accessOnlyLapse(user) {
|
|
// turned away already: imported again, it is a new entry
|
|
return "", "", refreshRefused(lapsed)
|
|
}
|
|
if stale == "" && !accessTokenRunOut(tok, time.Now()) {
|
|
if force {
|
|
return "", "", fmt.Errorf("%s is kept access token only: magpie doesn't renew its sign-in", user)
|
|
}
|
|
return tok, accountID, nil
|
|
}
|
|
msg := accessOnlyLapse(user)
|
|
_ = editSideLogin("codex", user, func(ls []savedLogin, i int) ([]savedLogin, error) {
|
|
ls[i].Lapsed = msg
|
|
return ls, nil
|
|
})
|
|
return "", "", refreshRefused(msg)
|
|
}
|
|
|
|
// SetLoginAccessOnly keeps a saved ChatGPT account access token only, or
|
|
// lets magpie renew its sign-in again. The account Codex is signed in to,
|
|
// here or in another of its folders, can't be kept so: Codex renews it
|
|
// itself as it runs.
|
|
func SetLoginAccessOnly(agent, user string, on bool) error {
|
|
if discoveryOff() {
|
|
return errAccountDiscoveryOff
|
|
}
|
|
if agent != "codex" {
|
|
return fmt.Errorf("only ChatGPT accounts can be kept access token only")
|
|
}
|
|
// not while the account is being refreshed: that refresh would finish
|
|
// after the account was said to be left alone
|
|
savedTokenMu.Lock()
|
|
defer savedTokenMu.Unlock()
|
|
loginsMu.Lock()
|
|
defer loginsMu.Unlock()
|
|
ls := readLogins()
|
|
i := pickSaved(ls, func(l savedLogin) bool { return l.Agent == agent && strings.EqualFold(l.User, user) })
|
|
if i < 0 {
|
|
return fmt.Errorf("no saved %s account %q", agent, user)
|
|
}
|
|
l := ls[i]
|
|
if on {
|
|
if live, ok := liveLogin(agent); ok && (sameLogin(live, l) || sameRefresh(live.Auth, l.Auth)) {
|
|
return fmt.Errorf("Codex is signed in to %s, and renews its sign-in itself as it runs: switch Codex to another account first", l.User)
|
|
}
|
|
if home := codexHeldIn(l); home != "" {
|
|
return fmt.Errorf("%s is signed in to Codex in %s, which renews its sign-in itself as it runs", l.User, home)
|
|
}
|
|
}
|
|
if l.AccessOnly == on {
|
|
return nil
|
|
}
|
|
ls[i].AccessOnly = on
|
|
if !on && ls[i].Lapsed == accessOnlyLapse(l.User) {
|
|
// renewed again from now on: the next request refreshes it, or
|
|
// says the refresh was refused
|
|
ls[i].Lapsed = ""
|
|
}
|
|
if err := writeLogins(ls); err != nil {
|
|
return err
|
|
}
|
|
loginsSeenAt.Store(0)
|
|
forgetAccountCaches()
|
|
return nil
|
|
}
|
|
|
|
// errAccessOnlyHeld: an account Codex is signed in to on this computer
|
|
// isn't imported access token only; Codex renews it.
|
|
var errAccessOnlyHeld = errors.New("Codex on this computer is signed in to this account and renews its sign-in itself; import it without \"access token only\", or sign Codex out of it first")
|