From 819473c7b28af17d7a124a29e6b7332a3e7a99c5 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 24 Oct 2026 00:37:01 +0100 Subject: [PATCH] fix(chat): Claude chats sign in with the operator's login file when the service's default login is not signed in --- internal/claude/auth_env.go | 77 ++++++++++++++++++++++++++++++++ internal/claude/auth_env_test.go | 47 +++++++++++++++++++ internal/claude/claude.go | 2 + internal/claude/process.go | 2 +- 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 internal/claude/auth_env.go create mode 100644 internal/claude/auth_env_test.go diff --git a/internal/claude/auth_env.go b/internal/claude/auth_env.go new file mode 100644 index 00000000..2714dc1d --- /dev/null +++ b/internal/claude/auth_env.go @@ -0,0 +1,77 @@ +package claude + +import ( + "context" + "encoding/json" + "os/exec" + "strings" + "sync" + "time" +) + +// secureStorageEnvKey points Claude Code's credential lookup at a config +// directory. With it set to the ordinary ~/.claude, Claude Code reads the +// login file a terminal sign-in keeps there instead of a keychain entry. +const secureStorageEnvKey = "CLAUDE_SECURESTORAGE_CONFIG_DIR" + +// authStatus runs `claude auth status` with [env] and reports loggedIn. +type authStatus func(ctx context.Context, binary string, env []string) bool + +func cliLoggedIn(ctx context.Context, binary string, env []string) bool { + ctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, binary, "auth", "status") + cmd.Env = env + out, err := cmd.Output() + if err != nil { + return false + } + var status struct { + LoggedIn bool `json:"loggedIn"` + } + return json.Unmarshal(out, &status) == nil && status.LoggedIn +} + +// authEnv chooses how Claude Code processes started by the server sign in. +// A server running as a background service can see a stale keychain entry +// where the operator's terminal sees a valid login file; when the default is +// not signed in but the login file is, the server uses the login file. +type authEnv struct { + once sync.Once + extra []string + status authStatus +} + +func (a *authEnv) resolve(ctx context.Context, binary string, base []string) []string { + a.once.Do(func() { + status := a.status + if status == nil { + status = cliLoggedIn + } + if hasEnv(base, secureStorageEnvKey) || hasEnv(base, "CLAUDE_CONFIG_DIR") || + hasEnv(base, "CLAUDE_CODE_OAUTH_TOKEN") || hasEnv(base, "ANTHROPIC_API_KEY") { + return + } + if status(ctx, binary, base) { + return + } + dir := ConfigDir() + if dir == "" { + return + } + candidate := append(append([]string{}, base...), secureStorageEnvKey+"="+dir) + if status(ctx, binary, candidate) { + a.extra = []string{secureStorageEnvKey + "=" + dir} + } + }) + return append(append([]string{}, base...), a.extra...) +} + +func hasEnv(env []string, key string) bool { + for _, item := range env { + if strings.HasPrefix(item, key+"=") { + return true + } + } + return false +} diff --git a/internal/claude/auth_env_test.go b/internal/claude/auth_env_test.go new file mode 100644 index 00000000..320f7cfe --- /dev/null +++ b/internal/claude/auth_env_test.go @@ -0,0 +1,47 @@ +package claude + +import ( + "context" + "slices" + "testing" +) + +func TestAuthEnvKeepsAWorkingDefault(t *testing.T) { + calls := 0 + a := authEnv{status: func(_ context.Context, _ string, env []string) bool { + calls++ + return !hasEnv(env, secureStorageEnvKey) + }} + got := a.resolve(context.Background(), "claude", []string{"HOME=/h"}) + if hasEnv(got, secureStorageEnvKey) || calls != 1 { + t.Fatalf("env = %v after %d checks", got, calls) + } +} + +func TestAuthEnvFallsBackToTheLoginFile(t *testing.T) { + t.Setenv("CLAUDE_CONFIG_DIR", "") + a := authEnv{status: func(_ context.Context, _ string, env []string) bool { + return hasEnv(env, secureStorageEnvKey) + }} + got := a.resolve(context.Background(), "claude", []string{"HOME=/h"}) + if !slices.Contains(got, secureStorageEnvKey+"="+ConfigDir()) { + t.Fatalf("env = %v", got) + } + // Decided once; later processes reuse the choice. + again := a.resolve(context.Background(), "claude", []string{"HOME=/h"}) + if !hasEnv(again, secureStorageEnvKey) { + t.Fatalf("second env = %v", again) + } +} + +func TestAuthEnvLeavesExplicitCredentialsAlone(t *testing.T) { + checked := false + a := authEnv{status: func(context.Context, string, []string) bool { + checked = true + return false + }} + base := []string{"CLAUDE_CODE_OAUTH_TOKEN=x"} + if got := a.resolve(context.Background(), "claude", base); !slices.Equal(got, base) || checked { + t.Fatalf("env = %v checked=%v", got, checked) + } +} diff --git a/internal/claude/claude.go b/internal/claude/claude.go index e76422e1..4227b7c8 100644 --- a/internal/claude/claude.go +++ b/internal/claude/claude.go @@ -91,6 +91,8 @@ type Chat struct { ctx context.Context cancel context.CancelFunc binary string + // auth picks how started CLI processes sign in (see authEnv). + auth authEnv mu sync.Mutex sessions map[string]*session diff --git a/internal/claude/process.go b/internal/claude/process.go index fecc85c3..cbd1994b 100644 --- a/internal/claude/process.go +++ b/internal/claude/process.go @@ -82,7 +82,7 @@ func (s *session) spawn(ctx context.Context) (*process, error) { } cmd := exec.CommandContext(s.chat.ctx, s.chat.binary, args...) cmd.Dir = s.directory - cmd.Env = withServiceToken(os.Environ()) + cmd.Env = s.chat.auth.resolve(s.chat.ctx, s.chat.binary, withServiceToken(os.Environ())) cmd.WaitDelay = 5 * time.Second stdin, err := cmd.StdinPipe() if err != nil {