StringKe (#4532) is moving from claude-swap (cswap 0.29.0) to magpie, and `magpie accounts import claude` didn't read cswap's export. It does now: parseLoginImport takes cswap's format-1 envelope ({version: 1, swapVersion, encrypted, accounts: [...]}, read from cswap 0.29.0's transfer.py) and cswapEntry reads each account's credentials.claudeAiOauth and config.oauthAccount: email, account and organization uuid, and the organization's name. The tokens are kept as the file has them; magpie asks Anthropic nothing. Refused, each with what to do: an encrypted export (decrypt with gpg -d first), another format version, a Codex entry, and an API-key entry (added as a key instead). An entry with no refresh token is accessOnly, as for every other file. cswap's alias becomes the account's name in magpie (importNames) when the account was added or updated, has no name yet, and no other account is called that or signed in as it, so the name stays a selector for one account (the previous commit's rule). testdata/cswap_0.29.0_export.json is claude-swap 0.29.0's own `cswap export` output, made from its source in a sandbox HOME in WSL with two accounts and fake tokens (cswap's add step asked Anthropic once with the fake refresh token, which it called dead; no real account was involved). Run (mote unless said): - At this commit's tree (on origin/main47173f593): gofmt -l clean; GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./... ok; go test -timeout 45m ./internal/provider (ok 100.7s), ./internal/cli (ok 31.8s), ./internal/gui: red on 11 real-window AppKit tests killed at their deadline on mote over ssh (#4125, #2645) and TestSettingsSaveKeepsWhatItDoesNotSend's SearchAll, red on main (#4514); none touches accounts. - Rebased on origin/main7aa67e9e5(4 commits, none in accounts), then at the pushed tree: go test ./internal/provider (ok 163.5s), ./internal/cli (ok 55.8s), ./internal/gui -run 'Budget|I18n|Key' (ok), gofmt -l clean. - TestImportCswapExport, TestImportCswapRefusals. Broken by hand, each red: no cswapEntry, no importNames, the taken-name check off (red on "the team seat took pro's name"), the encrypted check off, no organization name. - CLI in a sandbox HOME in WSL: import of the fixture adds max@x.com and team@x.com, a second import says both are in magpie already, the alias "work" is team's name, `accounts forget claude work` picks it; `accounts import --help`, a missing file and an unknown agent each say what's wrong. - On mote over ssh the same import added one account and failed the other on the Keychain ("Unable to obtain authorization", no GUI session there), and the failed one was still saved, so the second import called it "in magpie already". That's addLogin writing logins.json before signing Claude Code in, the same for every import and sign-in, not this change; not changed here. - GUI: login-import in Chromium and WebKit on Linux (WSL), green. Not tried: an export from a real cswap install with real accounts, Claude Code running on an imported account, or cswap's encrypted export decrypted and imported. A cswap account's plan (subscriptionType) isn't carried: magpie reads it on the first usage read, as for CLIProxyAPI.
520 lines
18 KiB
Go
520 lines
18 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. claude-swap (cswap,
|
|
// realiti4/claude-swap) exports its Claude accounts as {version: 1,
|
|
// swapVersion, encrypted, accounts: [{email, uuid, organizationUuid,
|
|
// organizationName, credentials: {claudeAiOauth: {…}}, config:
|
|
// {oauthAccount: {…}}, alias, kind}]} (see cswapEntry). 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 (
|
|
"cmp"
|
|
"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)
|
|
// claudeAccount and claudeOrg: the Claude account's and organization's
|
|
// uuids, Claude Code's oauthAccount accountUuid and organizationUuid,
|
|
// when the file has them
|
|
claudeAccount, claudeOrg string
|
|
// claudeOrgName is the organization's name, as oauthAccount has it;
|
|
// alias the name the tool it came from gave the account (cswap's)
|
|
claudeOrgName, alias string
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
if x, ok := v.(map[string]any); ok && x["swapVersion"] != nil {
|
|
// as cswap's own import refuses them (transfer.py)
|
|
if x["encrypted"] == true {
|
|
return nil, errors.New("an encrypted claude-swap export: decrypt it first (gpg -d), then import what that gives")
|
|
}
|
|
if n, ok := x["version"].(float64); !ok || n != 1 {
|
|
return nil, fmt.Errorf("a claude-swap export of format %v: magpie reads format 1 (claude-swap 0.29)", x["version"])
|
|
}
|
|
}
|
|
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
|
|
}
|
|
// a cswap account: credentials and config, no platform
|
|
if _, ok := x["config"].(map[string]any); ok && x["credentials"] != nil && jsonStr(x, "email") != "" {
|
|
out = append(out, cswapEntry(agent, x))
|
|
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
|
|
}
|
|
// an address (a sign-in's callback among them) is never sent to
|
|
// the vendor as a token
|
|
if strings.ContainsAny(line, " \t") || looksLikeAddress(line) {
|
|
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")
|
|
e.scopes = importScopes(src["scopes"])
|
|
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
|
|
}
|
|
|
|
// cswapEntry reads one account of a claude-swap export (cswap --export,
|
|
// transfer.py): {number, email, uuid, organizationUuid, organizationName,
|
|
// added, credentials, config, alias, kind}. credentials is Claude Code's
|
|
// own ({claudeAiOauth: {…}}), or with --full whatever else it kept beside
|
|
// it; config is {oauthAccount: {…}}, or with --full all of ~/.claude.json.
|
|
// An API-key account's credentials are the sk-ant-… key, its kind
|
|
// "api_key".
|
|
func cswapEntry(agent string, x map[string]any) loginImport {
|
|
cfg, _ := x["config"].(map[string]any)
|
|
acct, _ := cfg["oauthAccount"].(map[string]any)
|
|
e := loginImport{email: cmp.Or(jsonStr(acct, "emailAddress"), jsonStr(x, "email")),
|
|
claudeAccount: cmp.Or(jsonStr(acct, "accountUuid"), jsonStr(x, "uuid")),
|
|
claudeOrg: cmp.Or(jsonStr(acct, "organizationUuid"), jsonStr(x, "organizationUuid")),
|
|
claudeOrgName: cmp.Or(jsonStr(acct, "organizationName"), jsonStr(x, "organizationName")),
|
|
alias: jsonStr(x, "alias")}
|
|
creds, ok := x["credentials"].(map[string]any)
|
|
switch {
|
|
case agent != "claude":
|
|
e.err = "a Claude account (claude-swap's), not Codex's"
|
|
return e
|
|
case jsonStr(x, "kind") == "api_key" || !ok:
|
|
e.err = "an API key, not a sign-in; add it as a key instead"
|
|
return e
|
|
}
|
|
src := creds
|
|
if t, ok := creds["claudeAiOauth"].(map[string]any); ok {
|
|
src = t
|
|
}
|
|
e.accessToken = jsonStr(src, "accessToken", "access_token")
|
|
e.refreshToken = jsonStr(src, "refreshToken", "refresh_token")
|
|
e.scopes = importScopes(src["scopes"])
|
|
if e.refreshToken == "" && e.accessToken != "" {
|
|
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"
|
|
|
|
// importScopes reads a sign-in's scopes, a list or one string of them
|
|
// apart by spaces, as OAuth's scope is.
|
|
func importScopes(v any) []string {
|
|
var out []string
|
|
switch sc := v.(type) {
|
|
case []any:
|
|
for _, s := range sc {
|
|
if s, ok := s.(string); ok && s != "" {
|
|
out = append(out, s)
|
|
}
|
|
}
|
|
case string:
|
|
out = strings.Fields(sc)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// sub2apiEntry reads one account of a Sub2API export. A ChatGPT one keeps
|
|
// email, id_token and chatgpt_account_id in credentials. A Claude one's
|
|
// credentials are the token Sub2API was given, with Anthropic's own names
|
|
// (email_address, scope, account_uuid, org_uuid), and extra holds the
|
|
// account's email_address and uuids again (useAccountOAuth's
|
|
// buildExtraInfo): email_address alone was "the file doesn't say which
|
|
// Claude account this is" (#1975).
|
|
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
|
|
extra, _ := x["extra"].(map[string]any)
|
|
e := loginImport{email: cmp.Or(jsonStr(c, "email", "email_address"), jsonStr(extra, "email_address", "email")),
|
|
idToken: jsonStr(c, "id_token"), accessToken: jsonStr(c, "access_token"), refreshToken: jsonStr(c, "refresh_token"),
|
|
accountID: jsonStr(c, "chatgpt_account_id", "account_id"), scopes: importScopes(c["scope"]),
|
|
claudeAccount: cmp.Or(jsonStr(c, "account_uuid"), jsonStr(extra, "account_uuid")),
|
|
claudeOrg: cmp.Or(jsonStr(c, "org_uuid"), jsonStr(extra, "org_uuid"))}
|
|
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()
|
|
importNames(agent, all, out)
|
|
for _, r := range out {
|
|
if r.Status == "added" || r.Status == "updated" {
|
|
_ = ShowAccount(agent)
|
|
break
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// importNames gives each account brought in the name the tool it came from
|
|
// gave it (cswap's alias), while it has none of the user's own and the
|
|
// name picks no other account: not another's name, email or email part
|
|
// (AccountByName). A name that can't be given is left off, the account
|
|
// still added.
|
|
func importNames(agent string, es []loginImport, out []ImportedAccount) {
|
|
for i, e := range es {
|
|
r := out[i]
|
|
if e.alias == "" || (r.Status != "added" && r.Status != "updated") || AccountNameOf(agent, r.User) != "" {
|
|
continue
|
|
}
|
|
var users []string
|
|
for _, l := range Logins(agent) {
|
|
users = append(users, l.User)
|
|
}
|
|
taken := false
|
|
for _, u := range users {
|
|
email, _, _ := strings.Cut(u, " · ")
|
|
if !strings.EqualFold(u, r.User) && (strings.EqualFold(AccountNameOf(agent, u), e.alias) ||
|
|
strings.EqualFold(u, e.alias) || strings.EqualFold(strings.TrimSpace(email), e.alias)) {
|
|
taken = true
|
|
}
|
|
}
|
|
if !taken {
|
|
_ = SetAccountName(agent, r.User, e.alias)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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, codexRefreshForced); 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}
|
|
if e.claudeAccount != "" {
|
|
acct["accountUuid"] = e.claudeAccount
|
|
}
|
|
if e.claudeOrg != "" {
|
|
acct["organizationUuid"] = e.claudeOrg
|
|
}
|
|
if e.claudeOrgName != "" {
|
|
acct["organizationName"] = e.claudeOrgName
|
|
}
|
|
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}
|
|
}
|