| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // LoginOptions is supplied by the terminal command, never by the runner service. |
| 14 | // Paths are explicit so a test or an installation cannot fall back to another CLI. |
| 15 | type LoginOptions struct { |
| 16 | ClaudePath, CodexPath string |
| 17 | Input io.Reader |
| 18 | Output, ErrorOutput io.Writer |
| 19 | } |
| 20 | |
| 21 | type terminalLoginDriver struct { |
| 22 | options LoginOptions |
| 23 | cmd *exec.Cmd |
| 24 | done chan error |
| 25 | cancel context.CancelFunc |
| 26 | challenge LoginChallenge |
| 27 | name, home string |
| 28 | result *LoginResult |
| 29 | } |
| 30 | |
| 31 | func NewLoginDriver(options ...LoginOptions) LoginDriver { |
| 32 | d := &terminalLoginDriver{} |
| 33 | if len(options) == 1 { |
| 34 | d.options = options[0] |
| 35 | } |
| 36 | return d |
| 37 | } |
| 38 | |
| 39 | func LoginVersion(name string) string { |
| 40 | return map[string]string{"claude": ClaudeVersion, "codex": CodexVersion}[name] |
| 41 | } |
| 42 | func (d *terminalLoginDriver) binary(name string) string { |
| 43 | if name == "claude" { |
| 44 | return d.options.ClaudePath |
| 45 | } |
| 46 | if name == "codex" { |
| 47 | return d.options.CodexPath |
| 48 | } |
| 49 | return "" |
| 50 | } |
| 51 | |
| 52 | // LoginEnvironment matches the dedicated-home variables in policy.go. |
| 53 | // No inherited API keys, provider homes or user config. |
| 54 | func LoginEnvironment(name, home string) []string { |
| 55 | env := []string{"HOME=" + home, "PATH=" + os.Getenv("PATH"), "LANG=C.UTF-8", "TERM=" + os.Getenv("TERM")} |
| 56 | if tmp := os.Getenv("TMPDIR"); tmp != "" { |
| 57 | env = append(env, "TMPDIR="+tmp) |
| 58 | } |
| 59 | if name == "codex" { |
| 60 | return append(env, "CODEX_HOME="+home) |
| 61 | } |
| 62 | return append(env, "CLAUDE_CONFIG_DIR="+home, "CLAUDE_SECURESTORAGE_CONFIG_DIR="+home, "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1", "DISABLE_AUTOUPDATER=1") |
| 63 | } |
| 64 | func loginArgs(name string) []string { |
| 65 | // Claude signs in with "auth login". Codex device auth is |
| 66 | // mandated by the Task 17 fix-round controller ruling. |
| 67 | if name == "claude" { |
| 68 | return []string{"auth", "login"} |
| 69 | } |
| 70 | return []string{"login", "--device-auth"} |
| 71 | } |
| 72 | func (d *terminalLoginDriver) Supported(name string) bool { |
| 73 | if d.binary(name) == "" || LoginVersion(name) == "" { |
| 74 | return false |
| 75 | } |
| 76 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 77 | defer cancel() |
| 78 | // Probe in disposable empty config, never the person's interactive home. |
| 79 | home, err := os.MkdirTemp(os.Getenv("TMPDIR"), "equipo-login-probe-") |
| 80 | if err != nil { |
| 81 | return false |
| 82 | } |
| 83 | defer os.RemoveAll(home) |
| 84 | cmd := exec.CommandContext(ctx, d.binary(name), "--version") |
| 85 | cmd.Env = LoginEnvironment(name, home) |
| 86 | cmd.Dir = home |
| 87 | out := &limitedLoginOutput{} |
| 88 | cmd.Stdout = out |
| 89 | if cmd.Run() != nil { |
| 90 | return false |
| 91 | } |
| 92 | version := strings.TrimSpace(out.String()) |
| 93 | if name == "claude" && version != LoginVersion(name)+" (Claude Code)" { |
| 94 | return false |
| 95 | } |
| 96 | if name == "codex" && version != "codex-cli "+LoginVersion(name) && version != LoginVersion(name) { |
| 97 | return false |
| 98 | } |
| 99 | args := append(loginArgs(name), "--help") |
| 100 | cmd = exec.CommandContext(ctx, d.binary(name), args...) |
| 101 | cmd.Env = LoginEnvironment(name, home) |
| 102 | cmd.Dir = home |
| 103 | return cmd.Run() == nil |
| 104 | } |
| 105 | func (d *terminalLoginDriver) Begin(ctx context.Context, name, home string) (LoginChallenge, error) { |
| 106 | if d.cmd != nil { |
| 107 | return LoginChallenge{}, ErrAccountBusy |
| 108 | } |
| 109 | if !d.Supported(name) || d.options.Input == nil || d.options.Output == nil || d.options.ErrorOutput == nil { |
| 110 | return LoginChallenge{}, ErrLoginUnavailable |
| 111 | } |
| 112 | if DefaultHomeRules().RefusesPath(home) { |
| 113 | return LoginChallenge{}, ErrPolicy |
| 114 | } |
| 115 | dir, err := pinDirectory(home) |
| 116 | if err != nil { |
| 117 | return LoginChallenge{}, err |
| 118 | } |
| 119 | if !privateDirectory(dir) { |
| 120 | dir.Close() |
| 121 | return LoginChallenge{}, ErrPolicy |
| 122 | } |
| 123 | dir.Close() |
| 124 | child, cancel := context.WithCancel(ctx) |
| 125 | cmd := exec.CommandContext(child, d.binary(name), loginArgs(name)...) |
| 126 | cmd.Env = LoginEnvironment(name, home) |
| 127 | cmd.Dir = home |
| 128 | cmd.Stdin, cmd.Stdout, cmd.Stderr = d.options.Input, d.options.Output, d.options.ErrorOutput |
| 129 | // Forward interrupt and allow the official CLI to close its browser listener. |
| 130 | cmd.Cancel = func() error { return cmd.Process.Signal(os.Interrupt) } |
| 131 | cmd.WaitDelay = 2 * time.Second |
| 132 | if err := cmd.Start(); err != nil { |
| 133 | cancel() |
| 134 | return LoginChallenge{}, ErrLoginUnavailable |
| 135 | } |
| 136 | d.cmd, d.cancel, d.name, d.home = cmd, cancel, name, home |
| 137 | d.challenge = LoginChallenge{ID: rand.Text(), Mode: map[string]string{"claude": "browser", "codex": "device"}[name], ExpiresAt: time.Now().Add(10 * time.Minute)} |
| 138 | d.done = make(chan error, 1) |
| 139 | go func() { d.done <- cmd.Wait(); close(d.done) }() |
| 140 | return d.challenge, nil |
| 141 | } |
| 142 | func (d *terminalLoginDriver) Poll(ctx context.Context, id string) (LoginResult, error) { |
| 143 | if d.cmd == nil || id != d.challenge.ID { |
| 144 | return LoginResult{}, ErrIdentity |
| 145 | } |
| 146 | if d.result != nil { |
| 147 | return *d.result, nil |
| 148 | } |
| 149 | select { |
| 150 | case err := <-d.done: |
| 151 | d.cancel() |
| 152 | r := LoginResult{State: "denied"} |
| 153 | if err == nil { |
| 154 | identity, e := d.Probe(ctx, Binding{Provider: d.name, Home: d.home}) |
| 155 | if e == nil { |
| 156 | identity.HomeID = rand.Text() |
| 157 | r = LoginResult{State: "connected", Identity: identity} |
| 158 | } else { |
| 159 | r.State = "unavailable" |
| 160 | d.result = &r |
| 161 | return r, ErrIdentity |
| 162 | } |
| 163 | } |
| 164 | d.result = &r |
| 165 | return r, nil |
| 166 | default: |
| 167 | return LoginResult{State: "pending"}, nil |
| 168 | } |
| 169 | } |
| 170 | func (d *terminalLoginDriver) Cancel(_ context.Context, id string) error { |
| 171 | if d.cmd == nil || id != d.challenge.ID { |
| 172 | return ErrIdentity |
| 173 | } |
| 174 | d.cancel() |
| 175 | <-d.done |
| 176 | if d.result == nil { |
| 177 | d.result = &LoginResult{State: "cancelled"} |
| 178 | } |
| 179 | return nil |
| 180 | } |
| 181 | |