package provider import ( "context" "os" "strings" "time" ) type LoginDriver interface { Begin(ctx context.Context, providerName, credentialHome string) (LoginChallenge, error) Poll(ctx context.Context, challengeID string) (LoginResult, error) Cancel(ctx context.Context, challengeID string) error } type LoginChallenge struct { ID, VerificationURI, UserCode, Mode string ExpiresAt time.Time } type LoginResult struct { State string Identity Identity } const ErrLoginRequired Error = "account.sign_in_again" const ErrLoginUnavailable Error = "account_setup.unavailable" // NewLoginHome allocates an empty private home. Existing accounts are never // adopted, refreshed or overwritten by the new-account path. func NewLoginHome(root string) (string, error) { rules := DefaultHomeRules() if rules.RefusesPath(root) || refusedMountAlias(root, rules) { return "", ErrPolicy } dir, err := makePrivateRoot(root) if err != nil { return "", ErrPolicy } defer dir.Close() if !privateDirectory(dir) { return "", ErrPolicy } return os.MkdirTemp(root, "account-") } // CheckLoginHome checks source ownership and aliases without reading a login. func CheckLoginHome(home string) error { rules := DefaultHomeRules() if rules.RefusesPath(home) || refusedMountAlias(home, rules) { return ErrPolicy } dir, err := pinDirectory(home) if err != nil { return err } defer dir.Close() info, err := dir.Stat() if err != nil || !privateDirectory(dir) || rules.refusedInode(info) { return ErrPolicy } return nil } // SignInCommand contains paths only, never credentials or an authorization URL. func SignInCommand(name, home string, options LoginOptions) string { quote := func(s string) string { return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" } prefix := "flock --exclusive --close " + quote(home) + " env HOME=" + quote(home) if name == "claude" { return prefix + " DISABLE_AUTOUPDATER=1 CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 CLAUDE_CONFIG_DIR=" + quote(home) + " CLAUDE_SECURESTORAGE_CONFIG_DIR=" + quote(home) + " " + quote(options.ClaudePath) + " auth login" } return prefix + " CODEX_HOME=" + quote(home) + " " + quote(options.CodexPath) + " login --device-auth" } func OpenAccountWithCLI(ctx context.Context, b Binding, options LoginOptions) (*AccountLease, error) { return OpenAccountRefreshing(ctx, b, func(ctx context.Context, b Binding) (Identity, error) { return RefreshLogin(ctx, b, options) }) }