Files
tutti/services/tuttid/service/agent/composer_live_model_auth_scope.go
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

100 lines
3.1 KiB
Go

package agent
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"strings"
)
// claudeAuthScopeEnvKeys are the environment inputs that select which Claude
// account / billing context a session runs under — and therefore which model
// list the SDK advertises. Fingerprinting them into the live-model cache key
// keeps one auth context's model list from being served after the user switches
// to another (e.g. OAuth subscription -> ANTHROPIC_API_KEY billing) when no
// running session is present to correct it via the reuse path.
//
// Keychain secrets are intentionally not read here. Non-secret account identity
// from ~/.claude.json is included below, so OAuth account switches still move
// to a fresh scope without touching credential storage.
var claudeAuthScopeEnvKeys = []string{
"ANTHROPIC_API_KEY",
"ANTHROPIC_AUTH_TOKEN",
"ANTHROPIC_BASE_URL",
"ANTHROPIC_API_BASE_URL",
"CLAUDE_CONFIG_DIR",
}
// liveModelAuthScope returns a stable, non-secret fingerprint of the auth
// context for the provider, or "" when the provider's model list is not
// auth-sensitive (every non-Claude provider today). The fingerprint is a hash,
// so raw credentials never enter the cache key.
func liveModelAuthScope(provider string) string {
if !isClaudeSDKLiveModelProvider(provider) {
return ""
}
settingsEnv := claudeSettingsEnvBlock()
var b strings.Builder
for _, key := range claudeAuthScopeEnvKeys {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
value = strings.TrimSpace(settingsEnv[key])
}
b.WriteString(key)
b.WriteByte('=')
b.WriteString(value)
b.WriteByte('\n')
}
if identity, err := json.Marshal(claudeAuthIdentity()); err == nil {
b.WriteString("CLAUDE_AUTH_IDENTITY=")
b.Write(identity)
b.WriteByte('\n')
}
sum := sha256.Sum256([]byte(b.String()))
return hex.EncodeToString(sum[:8])
}
func claudeAuthIdentity() map[string]any {
home, err := os.UserHomeDir()
if err != nil {
return nil
}
state := readJSONRecord(filepath.Join(home, ".claude.json"))
identity := make(map[string]any, 2)
for _, key := range []string{"oauthAccount", "userID"} {
if value, ok := state[key]; ok {
identity[key] = value
}
}
return identity
}
// claudeSettingsEnvBlock reads the `env` block of ~/.claude/settings.json (or
// $CLAUDE_CONFIG_DIR/settings.json). Claude Code merges these into the process
// environment, so an ANTHROPIC_API_KEY declared there is as authoritative as one
// exported in the shell. Missing file or malformed JSON yields an empty map.
func claudeSettingsEnvBlock() map[string]string {
claudeConfigDir := strings.TrimSpace(os.Getenv("CLAUDE_CONFIG_DIR"))
if claudeConfigDir == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil
}
claudeConfigDir = filepath.Join(home, ".claude")
}
settings := readJSONRecord(filepath.Join(claudeConfigDir, "settings.json"))
envRaw, ok := settings["env"].(map[string]any)
if !ok {
return nil
}
result := make(map[string]string, len(envRaw))
for key, raw := range envRaw {
if value, ok := raw.(string); ok {
result[key] = value
}
}
return result
}