Files
magpie/internal/provider/claude_setup_token.go
T
yetone 8ebf9cbea9 claude: add an account from a setup-token (CLAUDE_CODE_OAUTH_TOKEN)
sfss5362 on X asked "支持 CLAUDE_CODE_OAUTH_TOKEN 吗". It didn't work
before: cleanClaudeEnv strips the variable everywhere, and the import
flow needs a refresh token and an email.

A Claude account can now be added from the token `claude setup-token`
prints. In the window: the sign-in's "Use a setup-token instead…" and
the accounts list's "Add an account by setup-token…", which post to
POST /api/signin/claude-token.

The token is kept in logins.json as {"setupToken":…}. The real Claude
Code gets it in CLAUDE_CODE_OAUTH_TOKEN, and only for runs in that
account's own config directory (inClaudeDir via
provider.ClaudeDirToken). It never goes on the command line or into a
log. magpie never asks Anthropic anything with it. The bridge, the
sdk-cli entrypoint and the weighting are unchanged.

What such a token can't do: it has the inference scope only.
- Claude Code's `auth status` gives no email or plan. The account is
  named "Claude setup-token <6 hex of its hash>".
- Its /usage prints the API cost view. /usage is never run for it, so
  its allowance is unknown (errClaudeSetupToken, which `passing` keeps
  as saying nothing about the account), not zero. Windows heard on
  real replies still fill it in.
- Its config directory holds no .credentials.json, so Claude Code has
  no sign-in to fall back to if the token is refused.
- Claude Code itself is never signed in to it. addLogin, switchLogin,
  putClaudeLogin, SetKeepLoginAs, NextLogin and signInToFirst all pass
  it over. Its row shows Remove and "magpie only", and the switch menu
  leaves it out.
New strings are in zh, zh-TW, ja and de. Docs: providers-accounts.md
and claude-subscription-bridge.md.

Run:
- mote: gofmt -l (clean); go vet ./internal/...; GOOS=linux go vet
  -tags nogui and GOOS=windows go vet of provider, gateway and gui; go
  build ./...
- mote: go test of provider and gateway, -run on SetupToken, Claude
  usage, warm-up, saved, account, logins, switch and keep-login: ok.
- New tests, with a fake claude binary in a sandbox HOME:
  - TestClaudeSetupTokenAccount, TestClaudeSetupTokenSignsClaudeCodeInToNothing,
    TestClaudeSetupTokenAllowanceUnknown and TestCheckClaudeSetupToken
    in provider;
  - TestClaudeSetupTokenReachesItsOwnRunsOnly in gateway: the token
    reaches only its account's run, not "" or another dir, even with
    the shell's CLAUDE_CODE_OAUTH_TOKEN=leaked, and never in args;
  - claude-setup-token.test.cjs: Chromium on mote and WebKit on
    xiaomi-gpu, each in en, zh, zh-TW, ja and de.
- GUI files the change reaches (signin-link, signin-web-blocked,
  signin-closes-sheet, revoked-signin-account, account-eye,
  account-row-line, i18n-keys and the new one): 73/73 on xiaomi-gpu
  Chromium.
- On mote, 33 account, sign-in and i18n files had 96 reds. mote was
  overloaded by another session's full GUI suite. Six of those files
  fail 58 of 64 the same way at origin/main dafe4af82 there, and pass
  on xiaomi-gpu. WebKit on mote timed out on the untouched
  copilot-ghe.test.cjs too.

Size: app.js was 2,885 bytes over its budget after the rebase. The
new comments were cut, and the budgets were raised to the measured sizes
(app.js 1,317,705, core 1,539,423) with no room added, as
core_budget_test.go says. After the rebase onto origin/main 969868ccd,
these were run on mote at this head: TestCoreScriptBudget and the
SetupToken tests ok, and claude-setup-token plus i18n-keys in Chromium
9/9.

Broken by hand (each went red):
- without the token line in inClaudeDir, the gateway test;
- with addLogin allowed to sign Claude Code in, two provider tests;
- with /usage run for a token account, AllowanceUnknown ("told no
  allowance").
- With switchLogin's guard removed, the tests stay green, because
  putClaudeLogin still refuses.

Not tried: a real setup-token against Anthropic. The full Go and GUI
suites will be run after the push.
2026-10-30 14:50:44 +08:00

174 lines
6.1 KiB
Go

package provider
// A Claude account added by a setup-token: the long-lived token `claude
// setup-token` prints, which Claude Code takes in CLAUDE_CODE_OAUTH_TOKEN.
// magpie keeps it in logins.json as it keeps a sign-in, and hands it to the
// real Claude Code, in that variable, for runs in the account's own config
// directory only (ClaudeDirToken); it never asks Anthropic anything with it.
//
// What such a token can't do: it has the inference scope only, so Claude
// Code tells no email, plan or organization for it (`auth status` says
// "oauth_token" and no more), and its /usage shows the API cost view, not
// the subscription's windows. The account is named by the token's own
// short hash, and its allowance is unknown (errClaudeSetupToken) until a
// reply's rate-limit headers tell it. Claude Code itself can't be signed in
// to it: there is nothing to put in its keychain or .credentials.json.
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"unicode"
"github.com/yetone/magpie/internal/settings"
)
// claudeTokenAuth is a setup-token as savedLogin.Auth keeps it.
type claudeTokenAuth struct {
SetupToken string `json:"setupToken"`
}
// claudeSetupToken is the setup-token a saved Claude login's Auth holds, ""
// for a sign-in.
func claudeSetupToken(auth []byte) string {
if len(auth) == 0 {
return ""
}
var a claudeTokenAuth
if json.Unmarshal(auth, &a) != nil {
return ""
}
return a.SetupToken
}
// claudeTokenVersion is the credential version of a setup-token
// (claudeLoginVersion): its hash, never the token.
func claudeTokenVersion(token string) string {
sum := sha256.Sum256([]byte(token))
return "setup-token:" + hex.EncodeToString(sum[:])[:16]
}
// claudeTokenName is the name a setup-token account gets: Claude tells no
// email for it, so a short hash of the token tells two apart without
// showing any of it.
func claudeTokenName(token string) string {
sum := sha256.Sum256([]byte(token))
return "Claude setup-token " + hex.EncodeToString(sum[:])[:6]
}
// errClaudeSetupToken is a setup-token account's allowance: unknown, never
// zero. Its words must not read as a refusal (claudeUsageDenied).
var errClaudeSetupToken = errors.New("unknown: a setup-token can't read the allowance (Claude Code's /usage needs a full sign-in); it shows once a reply tells it")
// errClaudeTokenSwitch is the answer to signing Claude Code in to a
// setup-token account.
const errClaudeTokenSwitch = "%s was added by a setup-token: magpie's requests use it, but Claude Code itself can't be signed in to it"
// checkClaudeSetupToken is token trimmed, or why it isn't a setup-token.
func checkClaudeSetupToken(token string) (string, error) {
token = strings.TrimSpace(token)
token = strings.TrimPrefix(token, "CLAUDE_CODE_OAUTH_TOKEN=")
token = strings.Trim(token, `"'`)
switch {
case token == "":
return "", errors.New("paste the token `claude setup-token` printed")
case strings.HasPrefix(token, "sk-ant-api"):
return "", errors.New("this is an Anthropic API key, not a setup-token: add it as an API key provider instead")
case !strings.HasPrefix(token, "sk-ant-oat"):
return "", errors.New("this isn't a setup-token: `claude setup-token` prints one that starts with sk-ant-oat")
case len(token) > 512 || strings.IndexFunc(token, func(r rune) bool { return unicode.IsSpace(r) || r > unicode.MaxASCII }) >= 0:
return "", errors.New("this isn't a setup-token: it has spaces or other characters a token doesn't")
}
return token, nil
}
// AddClaudeSetupToken adds a Claude account from a setup-token, served by
// Claude Code with the token in CLAUDE_CODE_OAUTH_TOKEN. Claude Code's own
// sign-in is left as it is. It answers the account's name; a token already
// saved answers the account that has it.
func AddClaudeSetupToken(token string) (string, error) {
if settings.AccountDiscoveryOff() {
return "", errAccountDiscoveryOff
}
token, err := checkClaudeSetupToken(token)
if err != nil {
return "", err
}
loginsMu.Lock()
for _, l := range readLogins() {
if l.Agent == "claude" && claudeSetupToken(l.Auth) == token {
loginsMu.Unlock()
return l.User, nil
}
}
loginsMu.Unlock()
auth, err := json.Marshal(claudeTokenAuth{SetupToken: token})
if err != nil {
return "", err
}
l := savedLogin{Agent: "claude", User: claudeTokenName(token), Auth: auth}
if _, err := addLogin(l); err != nil {
return "", err
}
return l.User, nil
}
// ClaudeDirToken is the setup-token of the saved Claude account whose
// config directory is dir (claudeAccountDir), "" when it has a sign-in of
// its own or is no saved account's. A run there is given it, and no other.
func ClaudeDirToken(dir string) string {
if dir == "" {
return ""
}
dir = filepath.Clean(dir)
loginsMu.Lock()
defer loginsMu.Unlock()
for _, l := range readLogins() {
if l.Agent != "claude" {
continue
}
if tok := claudeSetupToken(l.Auth); tok != "" && filepath.Clean(claudeAccountDir(l.User)) == dir {
return tok
}
}
return ""
}
// claudeTokenAccount says the saved Claude account user was added by a
// setup-token.
func claudeTokenAccount(user string) bool {
loginsMu.Lock()
defer loginsMu.Unlock()
for _, l := range readLogins() {
if l.Agent == "claude" && strings.EqualFold(l.User, user) {
return claudeSetupToken(l.Auth) != ""
}
}
return false
}
// claudeTokenDir makes the config directory a setup-token account runs
// in: no sign-in in it (on a refused token Claude Code would otherwise
// take one it found there; addLogin cleared the directory's keychain item
// with it, forgetClaudeDir), only the onboarding Claude Code asks for
// before its first run.
func claudeTokenDir(user string) (string, error) {
dir := claudeAccountDir(user)
if err := os.MkdirAll(dir, 0o700); err != nil {
return "", err
}
if err := os.Remove(filepath.Join(dir, ".credentials.json")); err != nil && !os.IsNotExist(err) {
return "", fmt.Errorf("a sign-in left in %s: %w", dir, err)
}
profile := filepath.Join(dir, ".claude.json")
if _, err := os.Stat(profile); os.IsNotExist(err) {
_ = writePrivate(profile, []byte("{\n \"hasCompletedOnboarding\": true\n}\n"))
}
return dir, nil
}