oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "crypto/rand"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "strings"
14
15 "amadan.net/oficina/equipo/internal/runner"
16 "amadan.net/oficina/equipo/internal/runner/provider"
17 runnersetup "amadan.net/oficina/equipo/internal/runner/setup"
18 "amadan.net/oficina/equipo/internal/runnerproto"
19 "golang.org/x/sys/unix"
20)
21
22// DefaultModels are the coordinator models recorded for a new login.
23var DefaultModels = map[string]string{"claude": "claude-opus-5-5", "codex": "gpt-6-astra"}
24
25// LoginOptions configure `equipo-runner login <provider>`.
26type LoginOptions struct {
27 // Root holds dedicated homes: <Root>/<provider>-<n>.
28 Root string
29 Input io.Reader
30 Output io.Writer
31 // Command runs the provider's own interactive sign-in. Tests replace it.
32 Command func(ctx context.Context, binary string, args []string, dir string, env []string) error
33}
34
35// DefaultAccountsRoot is ~/.local/share/equipo/accounts.
36func DefaultAccountsRoot() string {
37 home, _ := os.UserHomeDir()
38 return filepath.Join(home, ".local", "share", "equipo", "accounts")
39}
40
41// Login runs the official login into a new dedicated home, records the
42// email the CLI observed and adds the account to the bindings file for the
43// runner's own person. Credentials stay in the home; nothing is printed
44// but the provider's own sign-in prompts and the observed email.
45func Login(ctx context.Context, c runner.Config, name string, o LoginOptions) error {
46 if name != "claude" && name != "codex" {
47 return provider.ErrProvider
48 }
49 creds, err := runner.ReadCredentials(filepath.Join(c.StateDir, "credentials.json"))
50 if err != nil {
51 return err
52 }
53 person := creds.Bearer.Person
54 if person == "" {
55 return provider.ErrIdentity
56 }
57 if o.Root == "" {
58 o.Root = DefaultAccountsRoot()
59 }
60 if o.Command == nil {
61 o.Command = func(ctx context.Context, binary string, args []string, dir string, env []string) error {
62 cmd := exec.CommandContext(ctx, binary, args...)
63 cmd.Dir, cmd.Env = dir, env
64 cmd.Stdin, cmd.Stdout, cmd.Stderr = o.Input, o.Output, o.Output
65 return cmd.Run()
66 }
67 }
68 if err = os.MkdirAll(o.Root, 0700); err != nil {
69 return err
70 }
71 id, home, err := nextHome(o.Root, name)
72 if err != nil {
73 return err
74 }
75 if err = provider.CheckLoginHome(home); err != nil {
76 os.Remove(home)
77 return err
78 }
79 lock, err := os.Open(home)
80 if err != nil {
81 return err
82 }
83 defer lock.Close()
84 if err = unix.Flock(int(lock.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
85 return provider.ErrAccountBusy
86 }
87 binary, args := c.ClaudePath, []string{"auth", "login"}
88 if name == "codex" {
89 binary, args = c.CodexPath, []string{"login", "--device-auth"}
90 }
91 env := provider.LoginEnvironment(name, home)
92 for _, key := range []string{"BROWSER", "DISPLAY", "WAYLAND_DISPLAY", "XDG_RUNTIME_DIR", "DBUS_SESSION_BUS_ADDRESS"} {
93 if v := os.Getenv(key); v != "" {
94 env = append(env, key+"="+v)
95 }
96 }
97 if err = o.Command(ctx, binary, args, home, env); err != nil {
98 return fmt.Errorf("%w: sign-in did not finish", provider.ErrLoginRequired)
99 }
100 // The probe runs the CLI in the home under its lock.
101 observed, err := finishLogin(ctx, c, person, name, id, home)
102 if err != nil {
103 return err
104 }
105 if err = unix.Flock(int(lock.Fd()), unix.LOCK_UN); err != nil {
106 return err
107 }
108 _, err = fmt.Fprintf(o.Output, "Signed in as %s. Saved as %s.\n", observed.AccountID, id) // COPY ACCEPTED FOR NOW (Paul, 2026-11-04; iterate in context)
109 return err
110}
111
112// finishLogin records a home the provider's own CLI just signed in: the
113// identity the CLI reports (never its login output), the identity file,
114// and the binding for the person. Credentials stay in the home.
115func finishLogin(ctx context.Context, c runner.Config, person, name, id, home string) (provider.Identity, error) {
116 options := provider.LoginOptions{ClaudePath: c.ClaudePath, CodexPath: c.CodexPath}
117 probe, ok := provider.NewLoginDriver(options).(interface {
118 Probe(context.Context, provider.Binding) (provider.Identity, error)
119 })
120 if !ok {
121 return provider.Identity{}, provider.ErrLoginUnavailable
122 }
123 observed, err := probe.Probe(ctx, provider.Binding{ID: id, Home: home, Provider: name})
124 if err != nil {
125 return observed, err
126 }
127 return observed, saveLogin(ctx, c, person, name, id, home, observed)
128}
129
130// saveLogin records the identity the CLI reported for a home: the
131// identity file and the binding for the person.
132func saveLogin(ctx context.Context, c runner.Config, person, name, id, home string, observed provider.Identity) error {
133 if observed.AccountID == "" || strings.ContainsAny(observed.AccountID, "\r\n\x1b") {
134 return provider.ErrIdentity
135 }
136 observed.HomeID = rand.Text()
137 raw, _ := json.Marshal(observed)
138 if err := os.WriteFile(filepath.Join(home, provider.IdentityFile), raw, 0600); err != nil {
139 return err
140 }
141 b := provider.Binding{ID: id, Home: home, Provider: name, AccountID: observed.AccountID, Expected: observed}
142 return runnersetup.SaveAccountBinding(ctx, c.AccountBindingsPath, person, DefaultModels[name], runnerproto.AccountSetup{ID: id, Provider: name, Label: id}, b)
143}
144
145// nextHome creates the first free <provider>-<n> directory.
146// uniqueHome makes a home whose name is never reused: name and a random
147// suffix.
148func uniqueHome(root, name string) (string, string, error) {
149 for n := 0; n < 10; n++ {
150 id := name + "-" + strings.ToLower(rand.Text()[:12])
151 home := filepath.Join(root, id)
152 err := os.Mkdir(home, 0700)
153 if err == nil {
154 return id, home, nil
155 }
156 if !errors.Is(err, os.ErrExist) {
157 return "", "", err
158 }
159 }
160 return "", "", provider.ErrAccountMaintenance
161}
162
163func nextHome(root, name string) (string, string, error) {
164 for n := 1; n < 1000; n++ {
165 id := fmt.Sprintf("%s-%d", name, n)
166 home := filepath.Join(root, id)
167 err := os.Mkdir(home, 0700)
168 if err == nil {
169 return id, home, nil
170 }
171 if !errors.Is(err, os.ErrExist) {
172 return "", "", err
173 }
174 }
175 return "", "", provider.ErrAccountMaintenance
176}
177