MCPcopy Create free account
hub / github.com/cli/cli / Logout

Method Logout

internal/config/config.go:601–640  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
601func (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
642func (c *AuthConfig) activateUser(hostname, user string) error {
643 found, err := c.setActiveCredential(hostname, user)

Callers

nothing calls this directly

Calls 9

UsersForHostMethod · 0.95
ActiveUserMethod · 0.95
activateUserMethod · 0.95
DeleteFunction · 0.92
refreshableServiceNameFunction · 0.85
keyringServiceNameFunction · 0.70
RemoveMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected