fix(chat): Claude chats sign in with the operator's login file when the service's default login is not signed in
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user