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