accounts: a setup-token pasted into the Claude import is added as one, not taken for a refresh token
liukaybee on X asked whether the token `claude setup-token` prints can be pasted into Providers > Claude > Import accounts. It couldn't: the paste box read every bare line as a refresh token, and a Claude one with no email failed with "the file doesn't say which Claude account this is". parseLoginImport now reads each bare line (and a JSON string) through bareImport. A line that is a setup-token (sk-ant-oat..., bare, as CLAUDE_CODE_OAUTH_TOKEN=... or export CLAUDE_CODE_OAUTH_TOKEN="...") is checked by checkClaudeSetupToken and added by AddClaudeSetupToken, the same path as "Add an account by setup-token...": kept in logins.json, named by its hash, nothing asked of Anthropic. One magpie already has is "exists", twice in one paste is "in the files twice", an sk-ant-api key pasted there is refused as an API key. In the ChatGPT import a setup-token is refused as Claude's and never sent to ChatGPT's token endpoint. Refresh-token lines and JSON files in the same import work as before. No result row carries the token; nothing here logs. Run (mica, mica-run): - TestImportClaudeSetupToken red on origin/main's login_import.go (#2..#5 failed, setup-token lines "doesn't say which Claude account"), green with the fix. - go test ./internal/provider -run 'Import|SetupToken|Login': ok - go vet ./internal/provider: ok - go test -timeout 45m ./internal/provider ./internal/cli: ok, ok - gofmt -l internal/provider: clean Not tried: a real setup-token pasted in the real app (the add path it calls is the existing one, itself not tried with a real token).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -15,7 +15,9 @@ package provider
|
||||
// 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
|
||||
// a line is taken too, and for Claude a setup-token a line (what `claude
|
||||
// setup-token` prints, sk-ant-oat…): that one is added as the setup-token
|
||||
// path adds it (AddClaudeSetupToken), never taken for a refresh token. 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
|
||||
@@ -55,6 +57,28 @@ type loginImport struct {
|
||||
// milliseconds, 0 when it doesn't say: a refresh gives a later one,
|
||||
// so it tells which of two sign-ins of one account is the newer
|
||||
expiresAt int64
|
||||
// setupToken: a Claude setup-token pasted on a line of its own
|
||||
// (claude_setup_token.go), added as one, never as a sign-in
|
||||
setupToken string
|
||||
}
|
||||
|
||||
// bareImport is one token a line (or a JSON string) holds: a refresh
|
||||
// token, or for Claude a setup-token. A setup-token is told by its own
|
||||
// prefix, so it is never sent anywhere as a refresh token, nor an API key.
|
||||
func bareImport(agent, line string) loginImport {
|
||||
tok := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "export "))
|
||||
tok = strings.Trim(strings.TrimPrefix(tok, "CLAUDE_CODE_OAUTH_TOKEN="), `"'`)
|
||||
switch {
|
||||
case strings.HasPrefix(tok, "sk-ant-oat") && agent != "claude":
|
||||
return loginImport{err: "a Claude setup-token, not a ChatGPT sign-in"}
|
||||
case strings.HasPrefix(tok, "sk-ant-oat"), strings.HasPrefix(tok, "sk-ant-api") && agent == "claude":
|
||||
t, err := checkClaudeSetupToken(tok)
|
||||
if err != nil {
|
||||
return loginImport{err: err.Error()}
|
||||
}
|
||||
return loginImport{setupToken: t}
|
||||
}
|
||||
return loginImport{refreshToken: strings.TrimSpace(line)}
|
||||
}
|
||||
|
||||
// jsonMillis is the number under key, 0 when there is none.
|
||||
@@ -123,7 +147,7 @@ func parseLoginImport(agent, data string) ([]loginImport, error) {
|
||||
}
|
||||
out = append(out, loginEntry(agent, x))
|
||||
case string:
|
||||
out = append(out, loginImport{refreshToken: strings.TrimSpace(x)})
|
||||
out = append(out, bareImport(agent, x))
|
||||
default:
|
||||
out = append(out, loginImport{err: "not an account"})
|
||||
}
|
||||
@@ -135,6 +159,10 @@ func parseLoginImport(agent, data string) ([]loginImport, error) {
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
if e := bareImport(agent, line); e.refreshToken == "" {
|
||||
out = append(out, e) // a setup-token, or why it isn't one
|
||||
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) {
|
||||
@@ -408,6 +436,9 @@ func importLogins(ctx context.Context, agent string, files []string, accessOnly
|
||||
if t := loginAccessToken(l); t != "" {
|
||||
haveAccess[t] = l.User
|
||||
}
|
||||
if t := claudeSetupToken(l.Auth); t != "" && agent == "claude" {
|
||||
have["setup:"+t] = l.User
|
||||
}
|
||||
}
|
||||
loginsMu.Unlock()
|
||||
|
||||
@@ -438,6 +469,15 @@ func importLogins(ctx context.Context, agent string, files []string, accessOnly
|
||||
out[i] = ImportedAccount{User: name, Status: "failed", Error: "in the files twice"}
|
||||
continue
|
||||
case accessOnly:
|
||||
case e.setupToken != "" && have["setup:"+e.setupToken] != "":
|
||||
out[i] = ImportedAccount{User: have["setup:"+e.setupToken], Status: "exists"}
|
||||
continue
|
||||
case e.setupToken != "":
|
||||
key = "setup:" + e.setupToken
|
||||
if seen[key] {
|
||||
out[i] = ImportedAccount{User: claudeTokenName(e.setupToken), Status: "failed", Error: "in the files twice"}
|
||||
continue
|
||||
}
|
||||
case e.refreshToken == "":
|
||||
out[i] = ImportedAccount{User: name, Status: "failed", Error: "no refresh_token in it"}
|
||||
continue
|
||||
@@ -460,6 +500,10 @@ func importLogins(ctx context.Context, agent string, files []string, accessOnly
|
||||
out[i] = importAccessOnly(e, name, kept)
|
||||
return
|
||||
}
|
||||
if e.setupToken != "" {
|
||||
out[i] = importSetupToken(e.setupToken)
|
||||
return
|
||||
}
|
||||
out[i] = importLogin(c, agent, e, name, kept)
|
||||
}()
|
||||
}
|
||||
@@ -559,6 +603,18 @@ func importAccessOnly(e loginImport, name string, kept []savedLogin) ImportedAcc
|
||||
return ImportedAccount{User: l.User, Status: status, Plan: l.Plan}
|
||||
}
|
||||
|
||||
// importSetupToken adds a pasted Claude setup-token as the setup-token
|
||||
// path does: kept for Claude Code's CLAUDE_CODE_OAUTH_TOKEN, nothing asked
|
||||
// of Anthropic. Its name is the token's short hash; the token is never
|
||||
// said back.
|
||||
func importSetupToken(token string) ImportedAccount {
|
||||
user, err := AddClaudeSetupToken(token)
|
||||
if err != nil {
|
||||
return ImportedAccount{User: claudeTokenName(token), Status: "failed", Error: err.Error()}
|
||||
}
|
||||
return ImportedAccount{User: user, Status: "added"}
|
||||
}
|
||||
|
||||
// claudeImportOlder is why a Claude sign-in in a file wasn't taken.
|
||||
const claudeImportOlder = "magpie has a newer sign-in for this account (Claude Code's own, when it is signed in to it): kept, not replaced by this file's"
|
||||
|
||||
|
||||
@@ -227,3 +227,57 @@ func TestImportCockpitExports(t *testing.T) {
|
||||
t.Errorf("team seats kept: %v (%d logins)", ws, len(ls))
|
||||
}
|
||||
}
|
||||
|
||||
// liukaybee on X: the token `claude setup-token` prints, pasted into the
|
||||
// Claude import, is added as a setup-token account (AddClaudeSetupToken),
|
||||
// never taken for a refresh token; beside it in one paste a refresh token
|
||||
// a line is read as before. Nothing is asked of Anthropic, the token is
|
||||
// never said back, and a ChatGPT import says it is Claude's without
|
||||
// sending it to ChatGPT.
|
||||
func TestImportClaudeSetupToken(t *testing.T) {
|
||||
claudeHome(t)
|
||||
asked := 0
|
||||
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
asked++
|
||||
w.WriteHeader(500)
|
||||
}))
|
||||
defer fake.Close()
|
||||
oldBase, oldToken := claudeBase, codexTokenURL
|
||||
t.Cleanup(func() { claudeBase, codexTokenURL = oldBase, oldToken })
|
||||
claudeBase, codexTokenURL = fake.URL, fake.URL
|
||||
|
||||
paste := "# from claude setup-token\nexport CLAUDE_CODE_OAUTH_TOKEN=\"" + testSetupToken + "\"\nsk-ant-ort01-bare\n" + testSetupToken + "\nsk-ant-api03-key\n"
|
||||
cpa := `{"type":"claude","access_token":"sk-ant-oat01-old","refresh_token":"sk-ant-ort01-cpa","email":"max@example.com"}`
|
||||
rs, err := ImportLogins(context.Background(), "claude", []string{cpa, paste})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := claudeTokenName(testSetupToken)
|
||||
if got := importStatuses(rs); got != "max@example.com:added,"+name+":added,#3:failed,"+name+":failed,#5:failed" || asked != 0 {
|
||||
t.Fatalf("got %s %+v, asked %d", got, rs, asked)
|
||||
}
|
||||
if !strings.Contains(rs[4].Error, "API key") {
|
||||
t.Fatalf("an API key pasted: %q", rs[4].Error)
|
||||
}
|
||||
for _, r := range rs {
|
||||
if strings.Contains(r.User+r.Error, "sk-ant-oat01-setup") {
|
||||
t.Fatalf("the token said back: %+v", r)
|
||||
}
|
||||
}
|
||||
if tok := ClaudeDirToken(claudeAccountDir(name)); tok != testSetupToken {
|
||||
t.Fatalf("the setup-token account's token %q", tok)
|
||||
}
|
||||
if !claudeTokenAccount(name) {
|
||||
t.Fatal("not kept as a setup-token account")
|
||||
}
|
||||
// the same token again is the account magpie has
|
||||
rs, err = ImportLogins(context.Background(), "claude", []string{testSetupToken})
|
||||
if err != nil || importStatuses(rs) != name+":exists" {
|
||||
t.Fatalf("again: %+v %v", rs, err)
|
||||
}
|
||||
|
||||
rs, err = ImportLogins(context.Background(), "codex", []string{testSetupToken})
|
||||
if err != nil || len(rs) != 1 || rs[0].Status != "failed" || !strings.Contains(rs[0].Error, "Claude setup-token") || asked != 0 {
|
||||
t.Fatalf("a setup-token in the ChatGPT import: %+v %v, asked %d", rs, err, asked)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user