Logout will remove user, git protocol, and auth token for the given hostname. It will remove the auth token from the encrypted storage if it exists there.
(hostname, username string)
| 599 | // Logout will remove user, git protocol, and auth token for the given hostname. |
| 600 | // It will remove the auth token from the encrypted storage if it exists there. |
| 601 | func (c *AuthConfig) Logout(hostname, username string) error { |
| 602 | // Run under the shared refresh lock so logout wins any race with an in-flight refresh: a concurrent refresh |
| 603 | // cannot persist a rotated credential after logout has removed the account and reported success. |
| 604 | return c.withFreshConfigForRefresh(func(lockErr error) error { |
| 605 | users := c.UsersForHost(hostname) |
| 606 | |
| 607 | // The user's per-user keyring records are not covered by removing config keys, so delete them explicitly. |
| 608 | // Historically the per-user token was only removed in the single-user branch, leaking it when logging out of a |
| 609 | // multi-user host. |
| 610 | _ = keyring.Delete(refreshableServiceName(hostname), username) |
| 611 | _ = keyring.Delete(keyringServiceName(hostname), username) |
| 612 | |
| 613 | // If there is only one (or zero) users, then we remove the host |
| 614 | // and unset the keyring tokens. |
| 615 | if len(users) < 2 { |
| 616 | _ = c.cfg.Remove([]string{hostsKey, hostname}) |
| 617 | _ = keyring.Delete(keyringServiceName(hostname), "") |
| 618 | return ghConfig.Write(c.cfg) |
| 619 | } |
| 620 | |
| 621 | // Otherwise, we remove the user from this host |
| 622 | _ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username}) |
| 623 | |
| 624 | // This error is ignorable because we already know there is an active user for the host |
| 625 | activeUser, _ := c.ActiveUser(hostname) |
| 626 | |
| 627 | // If the user we're removing isn't active, then we just write the config |
| 628 | if activeUser != username { |
| 629 | return ghConfig.Write(c.cfg) |
| 630 | } |
| 631 | |
| 632 | // Otherwise we get the first user in the slice that isn't the user we're removing |
| 633 | switchUserIdx := slices.IndexFunc(users, func(n string) bool { |
| 634 | return n != username |
| 635 | }) |
| 636 | |
| 637 | // And activate them |
| 638 | return c.activateUser(hostname, users[switchUserIdx]) |
| 639 | }) |
| 640 | } |
| 641 | |
| 642 | func (c *AuthConfig) activateUser(hostname, user string) error { |
| 643 | found, err := c.setActiveCredential(hostname, user) |
nothing calls this directly
no test coverage detected