| 539 | } |
| 540 | |
| 541 | func (c *AuthConfig) login(hostname, username, token, gitProtocol string, secureStorage bool) (bool, error) { |
| 542 | // In this section we set up the users config |
| 543 | var setErr error |
| 544 | if secureStorage { |
| 545 | // Try to set the token for this user in the encrypted storage for later switching |
| 546 | setErr = keyring.Set(keyringServiceName(hostname), username, token) |
| 547 | if setErr == nil { |
| 548 | // Clean up the previous oauth_token from the config file, if there were one |
| 549 | _ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username, oauthTokenKey}) |
| 550 | } |
| 551 | } |
| 552 | insecureStorageUsed := false |
| 553 | if !secureStorage || setErr != nil { |
| 554 | // And set the oauth token under the user for later switching |
| 555 | c.cfg.Set([]string{hostsKey, hostname, usersKey, username, oauthTokenKey}, token) |
| 556 | insecureStorageUsed = true |
| 557 | } |
| 558 | |
| 559 | // Remove any refreshable credential for this user so the non-refreshable and refreshable forms never coexist. This |
| 560 | // mirrors loginRefreshable, which removes the non-expiring credential when storing a refreshable one. Without it, |
| 561 | // setActiveCredential keeps resolving the stale refreshable record as active and never copies the new |
| 562 | // non-refreshable token into the host-level slot, which also leaves go-gh extensions unable to read a token. |
| 563 | if err := c.purgeRefreshableForLogin(hostname, username); err != nil { |
| 564 | return insecureStorageUsed, err |
| 565 | } |
| 566 | |
| 567 | return insecureStorageUsed, c.completeLogin(hostname, username, gitProtocol) |
| 568 | } |
| 569 | |
| 570 | // completeLogin sets the host git protocol, ensures the user entry is persisted, and activates the user. It is shared |
| 571 | // by Login and LoginRefreshable once the credential has been stored. |