| 78 | } |
| 79 | |
| 80 | func Login(opts *LoginOptions) error { |
| 81 | cfg := opts.Config |
| 82 | hostname := opts.Hostname |
| 83 | httpClient := opts.HTTPClient |
| 84 | cs := opts.IO.ColorScheme() |
| 85 | |
| 86 | // Choose how to authenticate before anything that depends on expiring intent. A pasted token can never be |
| 87 | // expiring, so the effective intent is only known once the method is chosen. |
| 88 | var authMode int |
| 89 | if opts.Web || opts.ExpiringExplicitlyRequested { |
| 90 | // When the user explicitly passes --expiring, force the web flow so the paste option cannot silently ignore |
| 91 | // that request. A config-derived preference does not suppress paste: choosing it yields a non-expiring token. |
| 92 | authMode = 0 |
| 93 | } else if opts.Interactive { |
| 94 | options := []string{"Login with a web browser", "Paste an authentication token"} |
| 95 | var err error |
| 96 | authMode, err = opts.Prompter.Select( |
| 97 | "How would you like to authenticate GitHub CLI?", |
| 98 | options[0], |
| 99 | options) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | } |
| 104 | expiring := opts.Expiring && authMode == 0 |
| 105 | |
| 106 | // Begin the auth-state telemetry event. It is nil for disabled telemetry or enterprise hosts, and all event methods |
| 107 | // are nil-tolerant, so the calls below stay unconditional. |
| 108 | authEvent := BeginAuthTelemetry(opts.Telemetry, "login", hostname, expiring) |
| 109 | |
| 110 | gitProtocol := strings.ToLower(opts.GitProtocol) |
| 111 | if opts.Interactive && gitProtocol == "" { |
| 112 | options := []string{ |
| 113 | "HTTPS", |
| 114 | "SSH", |
| 115 | } |
| 116 | result, err := opts.Prompter.Select( |
| 117 | "What is your preferred protocol for Git operations on this host?", |
| 118 | options[0], |
| 119 | options) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | proto := options[result] |
| 124 | gitProtocol = strings.ToLower(proto) |
| 125 | } |
| 126 | |
| 127 | var additionalScopes []string |
| 128 | |
| 129 | // Record the selected git operations protocol for every sign-in, before any prompt. |
| 130 | authEvent.RecordGitProtocol(gitProtocol) |
| 131 | |
| 132 | // Record the configured credential helper up front, before any prompt, so this telemetry is captured for every |
| 133 | // sign-in, including non-interactive and SSH flows that never reach the prompt. |
| 134 | authEvent.RecordHelperBefore(opts.CredentialFlow.DetectConfiguredHelper(hostname)) |
| 135 | |
| 136 | if opts.Interactive && gitProtocol == "https" { |
| 137 | err := opts.CredentialFlow.Prompt(hostname, expiring) |