| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | type LoginDriver interface { |
| 11 | Begin(ctx context.Context, providerName, credentialHome string) (LoginChallenge, error) |
| 12 | Poll(ctx context.Context, challengeID string) (LoginResult, error) |
| 13 | Cancel(ctx context.Context, challengeID string) error |
| 14 | } |
| 15 | type LoginChallenge struct { |
| 16 | ID, VerificationURI, UserCode, Mode string |
| 17 | ExpiresAt time.Time |
| 18 | } |
| 19 | type LoginResult struct { |
| 20 | State string |
| 21 | Identity Identity |
| 22 | } |
| 23 | |
| 24 | const ErrLoginRequired Error = "account.sign_in_again" |
| 25 | |
| 26 | const ErrLoginUnavailable Error = "account_setup.unavailable" |
| 27 | |
| 28 | // NewLoginHome allocates an empty private home. Existing accounts are never |
| 29 | // adopted, refreshed or overwritten by the new-account path. |
| 30 | func NewLoginHome(root string) (string, error) { |
| 31 | rules := DefaultHomeRules() |
| 32 | if rules.RefusesPath(root) || refusedMountAlias(root, rules) { |
| 33 | return "", ErrPolicy |
| 34 | } |
| 35 | dir, err := makePrivateRoot(root) |
| 36 | if err != nil { |
| 37 | return "", ErrPolicy |
| 38 | } |
| 39 | defer dir.Close() |
| 40 | if !privateDirectory(dir) { |
| 41 | return "", ErrPolicy |
| 42 | } |
| 43 | return os.MkdirTemp(root, "account-") |
| 44 | } |
| 45 | |
| 46 | // CheckLoginHome checks source ownership and aliases without reading a login. |
| 47 | func CheckLoginHome(home string) error { |
| 48 | rules := DefaultHomeRules() |
| 49 | if rules.RefusesPath(home) || refusedMountAlias(home, rules) { |
| 50 | return ErrPolicy |
| 51 | } |
| 52 | dir, err := pinDirectory(home) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | defer dir.Close() |
| 57 | info, err := dir.Stat() |
| 58 | if err != nil || !privateDirectory(dir) || rules.refusedInode(info) { |
| 59 | return ErrPolicy |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // SignInCommand contains paths only, never credentials or an authorization URL. |
| 65 | func SignInCommand(name, home string, options LoginOptions) string { |
| 66 | quote := func(s string) string { return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" } |
| 67 | prefix := "flock --exclusive --close " + quote(home) + " env HOME=" + quote(home) |
| 68 | if name == "claude" { |
| 69 | 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" |
| 70 | } |
| 71 | return prefix + " CODEX_HOME=" + quote(home) + " " + quote(options.CodexPath) + " login --device-auth" |
| 72 | } |
| 73 | |
| 74 | func OpenAccountWithCLI(ctx context.Context, b Binding, options LoginOptions) (*AccountLease, error) { |
| 75 | return OpenAccountRefreshing(ctx, b, func(ctx context.Context, b Binding) (Identity, error) { return RefreshLogin(ctx, b, options) }) |
| 76 | } |
| 77 | |