package provider import ( "bytes" "crypto/sha256" "encoding/json" "os" "strings" "golang.org/x/sys/unix" ) // A token connection is a Claude home whose credential is a pasted // `claude setup-token` OAuth token (Connections spec ยง2.4, Paul // 2026-11-09). The token rests only in TokenFile inside the dedicated // home, which every policy denies; a runtime copy never carries it. The // unmodified CLI receives it as OAuthTokenEnv in its environment, never // in argv, never in a policy, manifest or record. const ( // TokenFile is the token's final name inside the home (0600). TokenFile = ".oauth-token" // TokenStaging is where a delivered token waits until it is committed. TokenStaging = ".secret.tmp" // OAuthTokenEnv is the variable the CLI reads the token from. OAuthTokenEnv = "CLAUDE_CODE_OAUTH_TOKEN" // ScrubEnv asks the CLI to keep provider credentials out of the // environment of the subprocesses it starts (its Bash tool, hooks). ScrubEnv = "CLAUDE_CODE_SUBPROCESS_ENV_SCRUB" // MaxTokenBytes bounds a token (the sealed box is capped at 4 KB). MaxTokenBytes = 4096 ) // ValidToken reports whether raw can be a token: one line of printable // ASCII without spaces, as `claude setup-token` prints it. A trailing // newline is not part of it. func ValidToken(raw []byte) bool { if len(raw) == 0 || len(raw) > MaxTokenBytes { return false } for _, c := range raw { if c <= ' ' || c > '~' { return false } } return true } // MaxCodeBytes bounds a sign-in code (wire.PromptAuthorize). const MaxCodeBytes = 1024 // ValidCode reports whether raw can be the one-time code Anthropic's // code page shows: one line of printable ASCII without spaces, as // ValidToken, and never a token (the CLI echoes what is typed, and a // token in its output is taken as the one it made). func ValidCode(raw []byte) bool { return len(raw) >= 8 && len(raw) <= MaxCodeBytes && ValidToken(raw) && !bytes.Contains(raw, []byte("sk-ant")) } // ReadToken reads a token home's credential without following links: // a regular 0600 file of the runner's, one link, a valid token. func ReadToken(home string) ([]byte, error) { dir, err := pinDirectory(home) if err != nil { return nil, err } defer dir.Close() raw, err := readPrivate(dir, TokenFile) if err != nil { return nil, err } if !ValidToken(raw) { clear(raw) return nil, ErrPolicy } return raw, nil } // tokenEnvironment is the login environment of a token home: the CLI // reads the token from its environment. func tokenEnvironment(b Binding) ([]string, error) { env := LoginEnvironment(b.Provider, b.Home) if !b.Token { return env, nil } raw, err := ReadToken(b.Home) if err != nil { return nil, err } defer clear(raw) return append(env, OAuthTokenEnv+"="+string(raw)), nil } // LaunchSecretEnv is the environment a launch on a token connection adds // inside its namespace, outside the policy: the token from the lease // that staged the launch's runtime copy. Nil for every other launch. func LaunchSecretEnv(l Launch) (map[string]string, error) { if l.Policy.IsPi() || l.Policy.IsOpenCode() { return piSecretEnv(l) } if !l.Binding.Token { return nil, nil } if l.Binding.Provider != "claude" { return nil, ErrPolicy } lease, err := l.Binding.AccountLease() if err != nil { return nil, err } lease.mu.Lock() token := string(lease.token) lease.mu.Unlock() if token == "" { return nil, accountContainment("token_missing") } return map[string]string{OAuthTokenEnv: token, ScrubEnv: "1"}, nil } // ClaudeTokenIdentity reads `claude auth status --json` for a token home. // The CLI says whether the token signs it in; the email is the account's // when the CLI reports one. Without one the connection is named for its // home (local), which is never shown as a label. func ClaudeTokenIdentity(raw []byte, local string) (Identity, string, error) { raw, err := canonical(raw) var status struct { LoggedIn bool Email string } if err != nil || json.Unmarshal(raw, &status) != nil { return Identity{}, "", ErrIdentity } if !status.LoggedIn { return Identity{}, "", ErrLoginRequired } if strings.ContainsAny(status.Email, "\r\n\x1b") { return Identity{}, "", ErrIdentity } if status.Email != "" { return Identity{AccountID: status.Email, ProviderVersion: LoginVersion("claude")}, status.Email, nil } return Identity{AccountID: TokenAccountID(local), ProviderVersion: LoginVersion("claude")}, "", nil } // TokenAccountID names a token connection whose CLI reports no email. func TokenAccountID(local string) string { return "token:" + local } // tokenRuntimeMarker stands for the absent credential of a token copy. var tokenRuntimeMarker = []byte("equipo-token-runtime") // stageToken stages a token home's copy: the identity and the account // metadata only. The token stays in the lease's memory for the launch // environment and is never written to the copy, which the model's // session runs in. func (l *AccountLease) stageToken(dir *os.File, raw []byte) error { if err := tokenRuntimeClean(dir); err != nil { return err } metadata, _ := json.Marshal(l.source.Expected) if err := atomicPrivate(dir, IdentityFile, metadata); err != nil { return err } native, err := claudeAccountMetadata(l.dir) if err != nil { return err } if err = atomicPrivate(dir, ".claude.json", native); err != nil { return err } l.claudeIdentity = bytes.Clone(native) clear(l.token) l.token = bytes.Clone(raw) l.runtimeGeneration = sha256.Sum256(tokenRuntimeMarker) return nil } // tokenRuntimeClean checks a token copy holds no credential file. func tokenRuntimeClean(dir *os.File) error { for _, name := range []string{TokenFile, TokenStaging, credentialName("claude")} { var st unix.Stat_t if unix.Fstatat(int(dir.Fd()), name, &st, unix.AT_SYMLINK_NOFOLLOW) != unix.ENOENT { return accountContainment("runtime_token_credential") } } return nil }