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.
106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
const gwSetupToken = "sk-ant-oat01-gatewaysetuptoken-0123456789abcdefghijklmnop"
|
|
|
|
// A Claude account added by a setup-token (sfss5362 on X) is served by the
|
|
// real Claude Code, given the token in CLAUDE_CODE_OAUTH_TOKEN for runs in
|
|
// that account's own config directory and no other: never on the command
|
|
// line, never to Claude Code's own account or another saved one, and never
|
|
// the token the shell happens to hold.
|
|
func TestClaudeSetupTokenReachesItsOwnRunsOnly(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a shell script stands in for Claude Code")
|
|
}
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(home, ".local", "share"))
|
|
provider.ForgetAccounts()
|
|
t.Cleanup(provider.ForgetAccounts)
|
|
|
|
user, err := provider.AddClaudeSetupToken(gwSetupToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
provider.ForgetAccounts()
|
|
// the directory the gateway is handed for the account, as a turn is
|
|
var dir string
|
|
for _, p := range provider.All() {
|
|
if p.Account != nil && p.Account.Agent == "claude" && p.Account.User == user {
|
|
d, _, err := p.Account.Token(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir = d
|
|
}
|
|
}
|
|
if dir == "" {
|
|
t.Fatalf("no provider serves %q", user)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, ".credentials.json")); !os.IsNotExist(err) {
|
|
t.Fatalf("a sign-in sits in the token account's directory: %v", err)
|
|
}
|
|
|
|
bin := t.TempDir()
|
|
log := filepath.Join(bin, "log")
|
|
text := "Current session: 13% used · resets Oct 1 at 3:30pm (UTC)\n"
|
|
b, _ := json.Marshal(map[string]any{"type": "result", "is_error": false, "num_turns": 0, "result": text})
|
|
os.WriteFile(filepath.Join(bin, "out"), b, 0o644)
|
|
script := "#!/bin/sh\n" +
|
|
"{ printf 'args:'; for a in \"$@\"; do printf '[%s]' \"$a\"; done; echo; echo \"dir:$CLAUDE_CONFIG_DIR\"; echo \"token:$CLAUDE_CODE_OAUTH_TOKEN\"; } >> " + log + "\n" +
|
|
"cat " + filepath.Join(bin, "out") + "\n"
|
|
testenv.Program(t, filepath.Join(bin, "claude"), script)
|
|
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
t.Setenv("CLAUDE_CODE_OAUTH_TOKEN", "leaked")
|
|
|
|
run := func(configDir string) string {
|
|
t.Helper()
|
|
os.Remove(log)
|
|
if _, err := claudeUsage(context.Background(), configDir); err != nil {
|
|
t.Fatalf("%q: %v", configDir, err)
|
|
}
|
|
got, _ := os.ReadFile(log)
|
|
return string(got)
|
|
}
|
|
if got := run(dir); !strings.Contains(got, "token:"+gwSetupToken+"\n") || !strings.Contains(got, "dir:"+dir+"\n") {
|
|
t.Fatalf("the token account's run wasn't given its token:\n%s", got)
|
|
} else if args, _, _ := strings.Cut(got, "\n"); strings.Contains(args, gwSetupToken) {
|
|
t.Fatalf("the token went on the command line: %s", args)
|
|
}
|
|
for _, other := range []string{"", filepath.Join(filepath.Dir(dir), "0123456789abcdef")} {
|
|
if got := run(other); !strings.Contains(got, "token:\n") {
|
|
t.Fatalf("a run in %q was given a token:\n%s", other, got)
|
|
}
|
|
}
|
|
|
|
// the bridge's env is built the same way
|
|
env := inClaudeDir([]string{"CLAUDE_CODE_OAUTH_TOKEN=leaked", "PATH=/bin"}, dir)
|
|
n := 0
|
|
for _, e := range env {
|
|
if strings.HasPrefix(e, "CLAUDE_CODE_OAUTH_TOKEN=") {
|
|
n++
|
|
if e != "CLAUDE_CODE_OAUTH_TOKEN="+gwSetupToken {
|
|
t.Fatalf("bridge env %q", e)
|
|
}
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Fatalf("%d tokens in the bridge's env: %q", n, env)
|
|
}
|
|
}
|