What changed: Settings → Privacy → Subscription accounts (On/Off), `magpie accounts discovery [on|off]` and MAGPIE_NO_ACCOUNTS=1 turn off account discovery. It stays on by default. While it is off, magpie reads none of the agents' sign-ins: ~/.claude/.credentials.json, .claude.json's account, ~/.codex/auth.json, Copilot's, Cursor's, Devin's, Kiro's, Gemini's, ZCode's, Command Code's and OpenCode's files. It runs no `security` / `secret-tool` for the Keychain or Secret Service, and asks no agent CLI who is signed in. It serves no subscription. API-key providers, middleware plugins and Pi extensions work as before. Why: the reporter runs magpie on machines where reading other tools' credentials isn't wanted. The owner delegated the call. How: settings.NoAccountDiscovery (kept per computer by KeepOwn) and settings.AccountDiscoveryOff(). The variable only ever turns discovery off. The gates sit at every reader (readAccountFile, discoveryOff) and at every route that reads or writes a sign-in, so a route that skips Accounts still reads nothing: - Accounts, All, Logins, usage, renewal, warm-up, session identities, check-ins; - switching, forgetting, SetLoginOn, StartSignIn, the imports; - moves onto plugins. The ones that write refuse with an error that names how to turn it back on. On the agent side, Agent.Source and OpenCode's auth.json are gated, and Codex wiring goes by magpie's stash instead of auth.json, so a toggle never rewires Codex. logins.json and plugin-auth.json are left untouched and serve again when discovery is turned back on. A change applies at once (ForgetAccounts). Docs: providers-accounts.md, README (en, zh-CN), and the site's CLI table (en, zh, ja). Run: - TestAccountDiscoveryOffReadsNoSignIn (provider) and TestAccountDiscoveryOffAgentsReadNoSignIn (agent). Every credential path is a FIFO that records each open, and security, secret-tool, claude, codex, cursor-agent, devin, kiro-cli, copilot and grok are fakes on PATH. Both fail with the gate removed: they show the opens and `security find-generic-password -s Claude Code-credentials`, among others. Both pass with the gate in place. - TestAccountDiscoverySetting and TestAccountsDiscoveryCmd. - account-discovery.test.cjs: chromium and webkit, 900 and 360 wide, en/zh/zh-TW/ja/de, 32/32. The neighbouring privacy, redact, settings-sections and i18n-keys GUI tests: 49/49. - The full `go test -tags nogui ./...`, gofmt -l, and go vet for darwin, linux and windows on the touched packages. Not tried: a real machine under fs_usage, and Linux and Windows at runtime. The FIFO tests are !windows.
398 lines
13 KiB
Go
398 lines
13 KiB
Go
package provider
|
|
|
|
// ChatGPT (Codex) and Claude accounts brought in from another tool's files
|
|
// rather than signed in again: CLIProxyAPI keeps each as
|
|
// {type: "codex", id_token, access_token, refresh_token, account_id, email, …}
|
|
// or {type: "claude", access_token, refresh_token, email, …}, Codex CLI as
|
|
// auth.json ({tokens: {…}}), Claude Code as .credentials.json
|
|
// ({claudeAiOauth: {…}}), and Sub2API as {accounts: [{platform, type:
|
|
// "oauth", credentials: {…}}]}. Cockpit Tools (jlcodes99/cockpit-tools)
|
|
// exports its Codex accounts in these shapes: its own (CLIProxyAPI's),
|
|
// Codex's auth.json, CPA and Sub2API. codexbar (lizhelang/codexbar), a
|
|
// menu-bar switcher, keeps its ChatGPT accounts in ~/.codexbar/config.json
|
|
// (see codexbarEntries); its own export is Sub2API's. A bare refresh token
|
|
// a line is taken too. They all sign in with the agents' own OAuth clients, the ones
|
|
// magpie uses.
|
|
//
|
|
// A ChatGPT one is checked the way a sign-in is finished: its refresh token
|
|
// traded for new tokens and the account asked for. That rotates the token,
|
|
// so the file it came from stops working: the account is magpie's from then
|
|
// on, as a sign-in in magpie would make it. A Claude one is kept as it
|
|
// came, magpie asking Anthropic nothing; Claude Code refreshes it the first
|
|
// time it runs on it. Tokens are never logged or sent back.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// loginImport is one account read from a file.
|
|
type loginImport struct {
|
|
email, idToken, accessToken, refreshToken, accountID string
|
|
scopes []string
|
|
err string
|
|
file int // a file that held no account (ImportedAccount.File)
|
|
}
|
|
|
|
// claudeImportScopes are what Claude Code's sign-in asks for, taken when a
|
|
// file doesn't say.
|
|
var claudeImportScopes = []string{"user:inference", "user:profile", "user:sessions:claude_code", "user:mcp_servers"}
|
|
|
|
// parseLoginImport reads the accounts in a file for agent (codex, claude).
|
|
func parseLoginImport(agent, data string) ([]loginImport, error) {
|
|
s := strings.TrimSpace(strings.TrimPrefix(data, "\xef\xbb\xbf"))
|
|
if s == "" {
|
|
return nil, errors.New("nothing to import")
|
|
}
|
|
var out []loginImport
|
|
if s[0] == '[' || s[0] == '{' {
|
|
var v any
|
|
if err := json.Unmarshal([]byte(s), &v); err != nil {
|
|
return nil, fmt.Errorf("not valid JSON: %v", err)
|
|
}
|
|
var walk func(v any)
|
|
walk = func(v any) {
|
|
switch x := v.(type) {
|
|
case []any:
|
|
for _, it := range x {
|
|
walk(it)
|
|
}
|
|
case map[string]any:
|
|
// a Sub2API account: its vendor in platform, its sign-in
|
|
// in credentials
|
|
if c, ok := x["credentials"].(map[string]any); ok && jsonStr(x, "platform") != "" {
|
|
out = append(out, sub2apiEntry(agent, x, c))
|
|
return
|
|
}
|
|
if es, ok := codexbarEntries(agent, x); ok {
|
|
out = append(out, es...)
|
|
return
|
|
}
|
|
if as, ok := x["accounts"].([]any); ok && jsonStr(x, "refresh_token", "refreshToken") == "" {
|
|
walk(as)
|
|
return
|
|
}
|
|
out = append(out, loginEntry(agent, x))
|
|
case string:
|
|
out = append(out, loginImport{refreshToken: strings.TrimSpace(x)})
|
|
default:
|
|
out = append(out, loginImport{err: "not an account"})
|
|
}
|
|
}
|
|
walk(v)
|
|
} else {
|
|
for _, line := range strings.Split(s, "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
if strings.ContainsAny(line, " \t") {
|
|
out = append(out, loginImport{err: "not a refresh token"})
|
|
continue
|
|
}
|
|
out = append(out, loginImport{refreshToken: line})
|
|
}
|
|
}
|
|
if len(out) == 0 {
|
|
return nil, errors.New("no accounts in it")
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// loginEntry reads one account object.
|
|
func loginEntry(agent string, x map[string]any) loginImport {
|
|
name := map[string]string{"codex": "Codex", "claude": "Claude"}[agent]
|
|
e := loginImport{email: jsonStr(x, "email")}
|
|
// Codex CLI's auth.json keeps the sign-in under tokens, Claude Code's
|
|
// credentials under claudeAiOauth
|
|
src := x
|
|
if t, ok := x["tokens"].(map[string]any); ok && agent == "codex" {
|
|
src = t
|
|
}
|
|
if t, ok := x["claudeAiOauth"].(map[string]any); ok && agent == "claude" {
|
|
src = t
|
|
}
|
|
e.idToken = jsonStr(src, "id_token", "idToken")
|
|
e.accessToken = jsonStr(src, "access_token", "accessToken")
|
|
e.refreshToken = jsonStr(src, "refresh_token", "refreshToken")
|
|
e.accountID = jsonStr(src, "account_id", "accountId")
|
|
switch sc := src["scopes"].(type) {
|
|
case []any:
|
|
for _, s := range sc {
|
|
if s, ok := s.(string); ok && s != "" {
|
|
e.scopes = append(e.scopes, s)
|
|
}
|
|
}
|
|
case string:
|
|
e.scopes = strings.Fields(sc)
|
|
}
|
|
typ := strings.ToLower(jsonStr(x, "type"))
|
|
switch {
|
|
case typ != "" && typ != agent:
|
|
e.err = fmt.Sprintf("a %s sign-in, not %s's", jsonStr(x, "type"), name)
|
|
case agent == "codex" && x["claudeAiOauth"] != nil, agent == "claude" && x["tokens"] != nil:
|
|
e.err = "not a " + name + " sign-in"
|
|
case e.refreshToken == "" && jsonStr(x, "OPENAI_API_KEY", "api_key", "apiKey") != "":
|
|
e.err = "an API key, not a sign-in; add it as a key instead"
|
|
case e.refreshToken == "" && (e.accessToken != "" || jsonStr(x, "personal_access_token") != ""):
|
|
e.err = accessOnly
|
|
}
|
|
return e
|
|
}
|
|
|
|
// codexbarEntries reads codexbar's config.json, as its CodexBarConfigStore
|
|
// writes it: {version, active, openAI: {remoteConnectionAccounts: […]},
|
|
// providers: [{id, kind, accounts: […]}]}. The ChatGPT sign-ins are the
|
|
// accounts of the provider of kind "openai_oauth" and the remote
|
|
// connection's, each {id, kind: "oauth_tokens" or "api_key", email,
|
|
// openAIAccountId, accessToken, refreshToken, idToken, …}: the account's
|
|
// workspace is openAIAccountId, its id is codexbar's own. The other
|
|
// providers are other vendors' API endpoints, not accounts, and are left
|
|
// alone. A sign-in in both places is read once.
|
|
func codexbarEntries(agent string, x map[string]any) ([]loginImport, bool) {
|
|
ps, ok := x["providers"].([]any)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
var accts []any
|
|
for _, p := range ps {
|
|
if p, ok := p.(map[string]any); ok && jsonStr(p, "kind") == "openai_oauth" {
|
|
as, _ := p["accounts"].([]any)
|
|
accts = append(accts, as...)
|
|
}
|
|
}
|
|
if o, ok := x["openAI"].(map[string]any); ok {
|
|
as, _ := o["remoteConnectionAccounts"].([]any)
|
|
accts = append(accts, as...)
|
|
}
|
|
var out []loginImport
|
|
seen := map[string]bool{}
|
|
for _, a := range accts {
|
|
a, ok := a.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
e := loginImport{email: jsonStr(a, "email"), idToken: jsonStr(a, "idToken"), accessToken: jsonStr(a, "accessToken"),
|
|
refreshToken: jsonStr(a, "refreshToken"), accountID: jsonStr(a, "openAIAccountId")}
|
|
if e.refreshToken != "" {
|
|
if seen[e.refreshToken] {
|
|
continue
|
|
}
|
|
seen[e.refreshToken] = true
|
|
}
|
|
switch {
|
|
case agent != "codex":
|
|
e.err = "a ChatGPT account, not Claude's"
|
|
case jsonStr(a, "kind") == "api_key":
|
|
e.err = "an API key, not a sign-in; add it as a key instead"
|
|
case e.refreshToken == "" && e.accessToken != "":
|
|
e.err = accessOnly
|
|
}
|
|
out = append(out, e)
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// accessOnly: an account with an access token and no refresh token (a
|
|
// ChatGPT web session, an access-token export) works for days at most and
|
|
// can't be renewed, so it isn't kept as an account that would just stop.
|
|
const accessOnly = "only an access token, no refresh_token: it would stop working within days and can't be renewed; sign in to this account instead"
|
|
|
|
// sub2apiEntry reads one account of a Sub2API export.
|
|
func sub2apiEntry(agent string, x, c map[string]any) loginImport {
|
|
name := map[string]string{"codex": "Codex", "claude": "Claude"}[agent]
|
|
platform := jsonStr(x, "platform")
|
|
ours := map[string]string{"openai": "codex", "anthropic": "claude", "claude": "claude"}[strings.ToLower(platform)] == agent
|
|
e := loginImport{email: jsonStr(c, "email"), idToken: jsonStr(c, "id_token"), accessToken: jsonStr(c, "access_token"),
|
|
refreshToken: jsonStr(c, "refresh_token"), accountID: jsonStr(c, "chatgpt_account_id", "account_id")}
|
|
if n := jsonStr(x, "name"); e.email == "" && strings.Contains(n, "@") {
|
|
e.email = n // Cockpit Tools names each account by its email
|
|
}
|
|
switch typ := strings.ToLower(jsonStr(x, "type")); {
|
|
case !ours:
|
|
e.err = fmt.Sprintf("a %s account, not %s's", platform, name)
|
|
case typ == "apikey":
|
|
e.err = "an API key, not a sign-in; add it as a key instead"
|
|
case typ != "oauth":
|
|
e.err = "not a " + name + " sign-in"
|
|
case e.refreshToken == "" && e.accessToken != "":
|
|
e.err = accessOnly
|
|
}
|
|
return e
|
|
}
|
|
|
|
// ImportLogins brings in ChatGPT (agent "codex") or Claude ("claude")
|
|
// accounts from other tools' files (see above), each file's text one
|
|
// element of files, and says what became of each.
|
|
func ImportLogins(ctx context.Context, agent string, files []string) ([]ImportedAccount, error) {
|
|
if settings.AccountDiscoveryOff() {
|
|
return nil, errAccountDiscoveryOff
|
|
}
|
|
if agent != "codex" && agent != "claude" {
|
|
return nil, fmt.Errorf("accounts can't be imported for %s", agent)
|
|
}
|
|
var all []loginImport
|
|
for i, f := range files {
|
|
es, err := parseLoginImport(agent, f)
|
|
if err != nil {
|
|
if len(files) == 1 {
|
|
return nil, err
|
|
}
|
|
// one file of several that holds none: said, not passed over
|
|
all = append(all, loginImport{file: i + 1, err: err.Error()})
|
|
continue
|
|
}
|
|
all = append(all, es...)
|
|
}
|
|
if len(all) == 0 {
|
|
return nil, errors.New("no accounts in these files")
|
|
}
|
|
if len(all) > maxGoogleImport {
|
|
return nil, fmt.Errorf("%d accounts; at most %d at a time", len(all), maxGoogleImport)
|
|
}
|
|
// the refresh tokens magpie holds already, the agent's own included
|
|
have := map[string]string{}
|
|
loginsMu.Lock()
|
|
ls := readLogins()
|
|
if l, ok := liveLogin(agent); ok {
|
|
ls = append(ls, l)
|
|
}
|
|
var kept []savedLogin
|
|
for _, l := range ls {
|
|
if l.Agent != agent {
|
|
continue
|
|
}
|
|
kept = append(kept, l)
|
|
if t := loginRefreshToken(l); t != "" {
|
|
have[t] = l.User
|
|
}
|
|
}
|
|
loginsMu.Unlock()
|
|
|
|
out := make([]ImportedAccount, len(all))
|
|
seen := map[string]bool{}
|
|
var wg sync.WaitGroup
|
|
sem := make(chan struct{}, 4)
|
|
for i, e := range all {
|
|
name := e.email
|
|
if name == "" && e.file == 0 {
|
|
name = fmt.Sprintf("#%d", i+1)
|
|
}
|
|
switch {
|
|
case e.err != "":
|
|
out[i] = ImportedAccount{User: name, File: e.file, Status: "failed", Error: e.err}
|
|
continue
|
|
case e.refreshToken == "":
|
|
out[i] = ImportedAccount{User: name, Status: "failed", Error: "no refresh_token in it"}
|
|
continue
|
|
case have[e.refreshToken] != "":
|
|
out[i] = ImportedAccount{User: have[e.refreshToken], Status: "exists"}
|
|
continue
|
|
case seen[e.refreshToken]:
|
|
out[i] = ImportedAccount{User: name, Status: "failed", Error: "in the files twice"}
|
|
continue
|
|
}
|
|
seen[e.refreshToken] = true
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
sem <- struct{}{}
|
|
defer func() { <-sem }()
|
|
c, cancel := context.WithTimeout(ctx, 45*time.Second)
|
|
defer cancel()
|
|
out[i] = importLogin(c, agent, e, name, kept)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
for _, r := range out {
|
|
if r.Status == "added" || r.Status == "updated" {
|
|
_ = ShowAccount(agent)
|
|
break
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// loginRefreshToken is the refresh token a saved sign-in holds.
|
|
func loginRefreshToken(l savedLogin) string {
|
|
switch l.Agent {
|
|
case "codex":
|
|
var a codexAuth
|
|
if json.Unmarshal(l.Auth, &a) == nil {
|
|
return a.Tokens.RefreshToken
|
|
}
|
|
case "claude":
|
|
if c, ok := parseClaudeCredentials(l.Auth); ok {
|
|
return c.OAuth.RefreshToken
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// importLogin checks one account as a sign-in is finished and keeps it.
|
|
func importLogin(ctx context.Context, agent string, e loginImport, name string, kept []savedLogin) ImportedAccount {
|
|
fail := func(msg string) ImportedAccount {
|
|
return ImportedAccount{User: name, Status: "failed", Error: msg}
|
|
}
|
|
refused := func(vendor string, err error) ImportedAccount {
|
|
var r refreshRefused
|
|
if errors.As(err, &r) {
|
|
return fail(vendor + " didn't take the refresh token — it has been used since (by the tool it came from, which refreshes it), expired or signed out")
|
|
}
|
|
return fail(err.Error())
|
|
}
|
|
var l savedLogin
|
|
var err error
|
|
switch agent {
|
|
case "codex":
|
|
raw := map[string]any{"tokens": map[string]any{"refresh_token": e.refreshToken}}
|
|
if _, err := codexRefresh(ctx, raw); err != nil {
|
|
return refused("ChatGPT", err)
|
|
}
|
|
t := raw["tokens"].(map[string]any)
|
|
str := func(k string) string { s, _ := t[k].(string); return s }
|
|
id := str("id_token")
|
|
if id == "" {
|
|
id = e.idToken
|
|
}
|
|
l, err = codexLogin(id, str("access_token"), str("refresh_token"), e.accountID)
|
|
case "claude":
|
|
scopes := e.scopes
|
|
if len(scopes) == 0 {
|
|
scopes = claudeImportScopes
|
|
}
|
|
// kept as it came, not tried: trying it would ask Anthropic, which
|
|
// magpie never does with a Claude sign-in. Claude Code refreshes it
|
|
// the first time it runs on the account, its access token taken
|
|
// for spent.
|
|
c := claudeCredentials{raw: map[string]any{}, OAuth: claudeAuth{AccessToken: e.accessToken, RefreshToken: e.refreshToken,
|
|
ExpiresAt: 1, Scopes: scopes}}
|
|
if e.email == "" {
|
|
return fail("the file doesn't say which Claude account this is; sign in to it from magpie instead")
|
|
}
|
|
acct := map[string]any{"emailAddress": e.email}
|
|
l, err = claudeLogin(c, acct)
|
|
}
|
|
if err != nil {
|
|
return fail(err.Error())
|
|
}
|
|
status := "added"
|
|
for _, k := range kept {
|
|
if sameLogin(k, l) {
|
|
status = "updated"
|
|
break
|
|
}
|
|
}
|
|
if _, err := addLogin(l); err != nil {
|
|
return fail(err.Error())
|
|
}
|
|
return ImportedAccount{User: l.User, Status: status, Plan: l.Plan}
|
|
}
|