oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package provider
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/json"
7 "os"
8 "strings"
9
10 "golang.org/x/sys/unix"
11)
12
13// A token connection is a Claude home whose credential is a pasted
14// `claude setup-token` OAuth token (Connections spec §2.4, Paul
15// 2026-11-09). The token rests only in TokenFile inside the dedicated
16// home, which every policy denies; a runtime copy never carries it. The
17// unmodified CLI receives it as OAuthTokenEnv in its environment, never
18// in argv, never in a policy, manifest or record.
19const (
20 // TokenFile is the token's final name inside the home (0600).
21 TokenFile = ".oauth-token"
22 // TokenStaging is where a delivered token waits until it is committed.
23 TokenStaging = ".secret.tmp"
24 // OAuthTokenEnv is the variable the CLI reads the token from.
25 OAuthTokenEnv = "CLAUDE_CODE_OAUTH_TOKEN"
26 // ScrubEnv asks the CLI to keep provider credentials out of the
27 // environment of the subprocesses it starts (its Bash tool, hooks).
28 ScrubEnv = "CLAUDE_CODE_SUBPROCESS_ENV_SCRUB"
29 // MaxTokenBytes bounds a token (the sealed box is capped at 4 KB).
30 MaxTokenBytes = 4096
31)
32
33// ValidToken reports whether raw can be a token: one line of printable
34// ASCII without spaces, as `claude setup-token` prints it. A trailing
35// newline is not part of it.
36func ValidToken(raw []byte) bool {
37 if len(raw) == 0 || len(raw) > MaxTokenBytes {
38 return false
39 }
40 for _, c := range raw {
41 if c <= ' ' || c > '~' {
42 return false
43 }
44 }
45 return true
46}
47
48// MaxCodeBytes bounds a sign-in code (wire.PromptAuthorize).
49const MaxCodeBytes = 1024
50
51// ValidCode reports whether raw can be the one-time code Anthropic's
52// code page shows: one line of printable ASCII without spaces, as
53// ValidToken, and never a token (the CLI echoes what is typed, and a
54// token in its output is taken as the one it made).
55func ValidCode(raw []byte) bool {
56 return len(raw) >= 8 && len(raw) <= MaxCodeBytes && ValidToken(raw) && !bytes.Contains(raw, []byte("sk-ant"))
57}
58
59// ReadToken reads a token home's credential without following links:
60// a regular 0600 file of the runner's, one link, a valid token.
61func ReadToken(home string) ([]byte, error) {
62 dir, err := pinDirectory(home)
63 if err != nil {
64 return nil, err
65 }
66 defer dir.Close()
67 raw, err := readPrivate(dir, TokenFile)
68 if err != nil {
69 return nil, err
70 }
71 if !ValidToken(raw) {
72 clear(raw)
73 return nil, ErrPolicy
74 }
75 return raw, nil
76}
77
78// tokenEnvironment is the login environment of a token home: the CLI
79// reads the token from its environment.
80func tokenEnvironment(b Binding) ([]string, error) {
81 env := LoginEnvironment(b.Provider, b.Home)
82 if !b.Token {
83 return env, nil
84 }
85 raw, err := ReadToken(b.Home)
86 if err != nil {
87 return nil, err
88 }
89 defer clear(raw)
90 return append(env, OAuthTokenEnv+"="+string(raw)), nil
91}
92
93// LaunchSecretEnv is the environment a launch on a token connection adds
94// inside its namespace, outside the policy: the token from the lease
95// that staged the launch's runtime copy. Nil for every other launch.
96func LaunchSecretEnv(l Launch) (map[string]string, error) {
97 if l.Policy.IsPi() || l.Policy.IsOpenCode() {
98 return piSecretEnv(l)
99 }
100 if !l.Binding.Token {
101 return nil, nil
102 }
103 if l.Binding.Provider != "claude" {
104 return nil, ErrPolicy
105 }
106 lease, err := l.Binding.AccountLease()
107 if err != nil {
108 return nil, err
109 }
110 lease.mu.Lock()
111 token := string(lease.token)
112 lease.mu.Unlock()
113 if token == "" {
114 return nil, accountContainment("token_missing")
115 }
116 return map[string]string{OAuthTokenEnv: token, ScrubEnv: "1"}, nil
117}
118
119// ClaudeTokenIdentity reads `claude auth status --json` for a token home.
120// The CLI says whether the token signs it in; the email is the account's
121// when the CLI reports one. Without one the connection is named for its
122// home (local), which is never shown as a label.
123func ClaudeTokenIdentity(raw []byte, local string) (Identity, string, error) {
124 raw, err := canonical(raw)
125 var status struct {
126 LoggedIn bool
127 Email string
128 }
129 if err != nil || json.Unmarshal(raw, &status) != nil {
130 return Identity{}, "", ErrIdentity
131 }
132 if !status.LoggedIn {
133 return Identity{}, "", ErrLoginRequired
134 }
135 if strings.ContainsAny(status.Email, "\r\n\x1b") {
136 return Identity{}, "", ErrIdentity
137 }
138 if status.Email != "" {
139 return Identity{AccountID: status.Email, ProviderVersion: LoginVersion("claude")}, status.Email, nil
140 }
141 return Identity{AccountID: TokenAccountID(local), ProviderVersion: LoginVersion("claude")}, "", nil
142}
143
144// TokenAccountID names a token connection whose CLI reports no email.
145func TokenAccountID(local string) string { return "token:" + local }
146
147// tokenRuntimeMarker stands for the absent credential of a token copy.
148var tokenRuntimeMarker = []byte("equipo-token-runtime")
149
150// stageToken stages a token home's copy: the identity and the account
151// metadata only. The token stays in the lease's memory for the launch
152// environment and is never written to the copy, which the model's
153// session runs in.
154func (l *AccountLease) stageToken(dir *os.File, raw []byte) error {
155 if err := tokenRuntimeClean(dir); err != nil {
156 return err
157 }
158 metadata, _ := json.Marshal(l.source.Expected)
159 if err := atomicPrivate(dir, IdentityFile, metadata); err != nil {
160 return err
161 }
162 native, err := claudeAccountMetadata(l.dir)
163 if err != nil {
164 return err
165 }
166 if err = atomicPrivate(dir, ".claude.json", native); err != nil {
167 return err
168 }
169 l.claudeIdentity = bytes.Clone(native)
170 clear(l.token)
171 l.token = bytes.Clone(raw)
172 l.runtimeGeneration = sha256.Sum256(tokenRuntimeMarker)
173 return nil
174}
175
176// tokenRuntimeClean checks a token copy holds no credential file.
177func tokenRuntimeClean(dir *os.File) error {
178 for _, name := range []string{TokenFile, TokenStaging, credentialName("claude")} {
179 var st unix.Stat_t
180 if unix.Fstatat(int(dir.Fd()), name, &st, unix.AT_SYMLINK_NOFOLLOW) != unix.ENOENT {
181 return accountContainment("runtime_token_credential")
182 }
183 }
184 return nil
185}
186