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.
172 lines
6.0 KiB
Go
172 lines
6.0 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const testSetupToken = "sk-ant-oat01-setuptoken-abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
// sfss5362 on X: "支持 CLAUDE_CODE_OAUTH_TOKEN 吗". A token `claude
|
|
// setup-token` printed is added as an account of magpie's own: kept in
|
|
// logins.json, named without an email, given to Claude Code for its own
|
|
// config directory only, and Claude Code's own sign-in left as it was.
|
|
func TestClaudeSetupTokenAccount(t *testing.T) {
|
|
home := claudeHome(t)
|
|
cred := claudeSignIn(t, home, time.Now().Add(time.Hour))
|
|
writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": map[string]any{"emailAddress": "pro@x.com",
|
|
"organizationUuid": "org-pro", "organizationName": "pro@x.com's Organization"}})
|
|
forgetAccountCaches()
|
|
rememberLogins(true)
|
|
before, err := os.ReadFile(cred)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
user, err := AddClaudeSetupToken(" CLAUDE_CODE_OAUTH_TOKEN=" + testSetupToken + "\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if user == "" || strings.Contains(user, "sk-ant") || strings.Contains(user, testSetupToken[len(testSetupToken)-6:]) {
|
|
t.Fatalf("named %q: no name, or a name showing the token", user)
|
|
}
|
|
if after, _ := os.ReadFile(cred); string(after) != string(before) {
|
|
t.Fatal("adding a setup-token wrote Claude Code's own sign-in")
|
|
}
|
|
// kept as a credential is, in logins.json
|
|
if b, _ := os.ReadFile(loginsPath()); !strings.Contains(string(b), testSetupToken) {
|
|
t.Fatal("the token isn't kept in logins.json")
|
|
}
|
|
var got *Login
|
|
for _, l := range Logins("claude") {
|
|
if l.User == user {
|
|
got = &l
|
|
}
|
|
}
|
|
if got == nil {
|
|
t.Fatalf("%q isn't listed: %+v", user, Logins("claude"))
|
|
}
|
|
if got.Active || !got.On || !got.Token || got.Lapsed != "" {
|
|
t.Fatalf("setup-token account listed as %+v, want on beside Claude Code's own, not active", *got)
|
|
}
|
|
// the same token again is the same account
|
|
if again, err := AddClaudeSetupToken(testSetupToken); err != nil || again != user {
|
|
t.Fatalf("the same token again: %q, %v", again, err)
|
|
}
|
|
if n := len(Logins("claude")); n != 2 {
|
|
t.Fatalf("%d Claude accounts, want 2", n)
|
|
}
|
|
|
|
// its directory holds no sign-in, and its token is given there alone
|
|
dir, err := claudeSavedDir(user)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if dir != claudeAccountDir(user) {
|
|
t.Fatalf("dir %q, want %q", dir, claudeAccountDir(user))
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, ".credentials.json")); !os.IsNotExist(err) {
|
|
t.Fatalf("a sign-in was put in the setup-token account's directory: %v", err)
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(dir, ".claude.json")); !strings.Contains(string(b), "hasCompletedOnboarding") {
|
|
t.Fatalf("no onboarding in its .claude.json: %s", b)
|
|
}
|
|
if tok := ClaudeDirToken(dir); tok != testSetupToken {
|
|
t.Fatalf("ClaudeDirToken(own dir) = %q", tok)
|
|
}
|
|
if tok := ClaudeDirToken(claudeAccountDir("pro@x.com · pro@x.com's Organization")); tok != "" {
|
|
t.Fatal("another account's directory was given the token")
|
|
}
|
|
if tok := ClaudeDirToken(""); tok != "" {
|
|
t.Fatal("Claude Code's own run was given the token")
|
|
}
|
|
|
|
// Claude Code can't be signed in to it
|
|
if err := SwitchLogin("claude", user); err == nil || !strings.Contains(err.Error(), "setup-token") {
|
|
t.Fatalf("switch onto a setup-token account: %v", err)
|
|
}
|
|
if after, _ := os.ReadFile(cred); string(after) != string(before) {
|
|
t.Fatal("a switch wrote the token into Claude Code's own sign-in")
|
|
}
|
|
// its version is the token's hash, never the token
|
|
if v := ClaudeLoginVersion(user); v == "" || strings.Contains(v, testSetupToken) {
|
|
t.Fatalf("version %q", v)
|
|
}
|
|
}
|
|
|
|
// With Claude Code signed out, the first account added is signed in to in
|
|
// Claude Code; a setup-token never is.
|
|
func TestClaudeSetupTokenSignsClaudeCodeInToNothing(t *testing.T) {
|
|
home := claudeHome(t)
|
|
user, err := AddClaudeSetupToken(testSetupToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, ".claude", ".credentials.json")); !os.IsNotExist(err) {
|
|
t.Fatalf("Claude Code was signed in to a setup-token: %v", err)
|
|
}
|
|
ls := Logins("claude")
|
|
if len(ls) != 1 || ls[0].User != user || ls[0].Active || !ls[0].On || ls[0].Lapsed != "" {
|
|
t.Fatalf("listed as %+v", ls)
|
|
}
|
|
}
|
|
|
|
// A setup-token has the inference scope only: Claude Code's /usage shows
|
|
// the API cost view for it. Its allowance is unknown, never none, and
|
|
// /usage isn't run for it.
|
|
func TestClaudeSetupTokenAllowanceUnknown(t *testing.T) {
|
|
claudeHome(t)
|
|
var text atomic.Value
|
|
text.Store("Total cost: $0.0000\nTotal duration (API): 0s")
|
|
runs := fakeClaudeUsage(t, &text, nil)
|
|
user, err := AddClaudeSetupToken(testSetupToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
ws, err := readSavedClaudeUsage(ctx, user)
|
|
if !errors.Is(err, errClaudeSetupToken) || len(ws) != 0 {
|
|
t.Fatalf("read %+v, %v; want unknown", ws, err)
|
|
}
|
|
if runs.Load() != 0 {
|
|
t.Fatal("/usage was run for a setup-token")
|
|
}
|
|
if claudeUsageDenied.MatchString(err.Error()) {
|
|
t.Fatalf("%q reads as a refusal", err)
|
|
}
|
|
if !passing.MatchString(err.Error()) {
|
|
t.Fatalf("%q isn't kept as saying nothing about the account", err)
|
|
}
|
|
RefreshUsage(ctx, "claude", user)
|
|
q := LoginUsage(ctx, "claude")[user]
|
|
if q.Error != errClaudeSetupToken.Error() || len(q.Windows) != 0 || runs.Load() != 0 {
|
|
t.Fatalf("card %+v after %d runs; want unknown", q, runs.Load())
|
|
}
|
|
}
|
|
|
|
func TestCheckClaudeSetupToken(t *testing.T) {
|
|
for in, ok := range map[string]bool{
|
|
testSetupToken: true,
|
|
"'" + testSetupToken + "'": true,
|
|
"": false,
|
|
"sk-ant-api03-abc": false,
|
|
"sk-ant-ort01-refresh": false,
|
|
"sk-ant-oat01-a b": false,
|
|
`{"claudeAiOauth":{"accessToken":1}}`: false,
|
|
} {
|
|
tok, err := checkClaudeSetupToken(in)
|
|
if (err == nil) != ok {
|
|
t.Errorf("%q: %q %v", in, tok, err)
|
|
}
|
|
if err != nil && strings.Contains(err.Error(), in) && in != "" {
|
|
t.Errorf("%q: the error repeats the token: %v", in, err)
|
|
}
|
|
}
|
|
}
|