Setup configures git to use the newly obtained credential for the given hostname. If no credential helper is configured for the host, gh configures itself as the helper. Otherwise a non-gh helper is already configured and the behavior depends on how the login was started: - For an expiring login t
(hostname, username, authToken string, refreshable bool)
| 194 | // during the auth flow, so the --expiring preference alone cannot determine it beforehand. Setup is only reached |
| 195 | // when the user already opted into git setup during the interactive prompt, so prompting again here is safe. |
| 196 | func (flow *GitCredentialFlow) Setup(hostname, username, authToken string, refreshable bool) (string, error) { |
| 197 | // If there is no credential helper configured then we will set ourselves up as |
| 198 | // the credential helper for this host. |
| 199 | if !flow.helper.IsConfigured() { |
| 200 | flow.configuredOurs = true |
| 201 | return "", flow.HelperConfig.ConfigureOurs(hostname) |
| 202 | } |
| 203 | |
| 204 | // For an expiring login the credential-helper decision was already made up-front in Prompt. Setup only runs when |
| 205 | // the user chose to configure gh, so honor that choice here without re-prompting and regardless of whether the |
| 206 | // server actually issued a refreshable token. |
| 207 | if flow.expiring { |
| 208 | flow.configuredOurs = true |
| 209 | return "", flow.HelperConfig.ConfigureOurs(hostname) |
| 210 | } |
| 211 | |
| 212 | // A non-gh helper is configured. It cannot refresh an expiring token and would keep serving the token after it |
| 213 | // expires, so storing a refreshable credential in it is a trap. Offer to make gh the helper instead, since gh can |
| 214 | // refresh the token transparently. |
| 215 | if refreshable { |
| 216 | setupGh, err := flow.Prompter.Confirm( |
| 217 | fmt.Sprintf("The git credential helper configured for %s cannot refresh expiring tokens. Set up gh as the git credential helper instead?", hostname), |
| 218 | true, |
| 219 | ) |
| 220 | if err != nil { |
| 221 | return "", err |
| 222 | } |
| 223 | if setupGh { |
| 224 | flow.configuredOurs = true |
| 225 | return "", flow.HelperConfig.ConfigureOurs(hostname) |
| 226 | } |
| 227 | |
| 228 | // The user declined, so we clear any existing credential and store nothing, surfacing the problem immediately |
| 229 | // rather than silently after expiry, and return a warning for the caller to show. |
| 230 | if err := flow.Updater.RejectExisting(hostname); err != nil { |
| 231 | return "", err |
| 232 | } |
| 233 | return fmt.Sprintf( |
| 234 | "The configured git credential helper for %[1]s cannot refresh expiring tokens, so the new credential was not stored in it. Git operations may prompt for credentials or fail on the next attempt. Run 'gh auth setup-git --hostname %[1]s' to let gh manage git authentication.", |
| 235 | hostname, |
| 236 | ), nil |
| 237 | } |
| 238 | |
| 239 | // Otherwise, we'll tell git to inform the existing credential helper of the new credentials. |
| 240 | return "", flow.Updater.Update(hostname, username, authToken) |
| 241 | } |