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)
| 426 | // Logout will remove user, git protocol, and auth token for the given hostname. |
| 427 | // It will remove the auth token from the encrypted storage if it exists there. |
| 428 | func (c *AuthConfig) Logout(hostname, username string) error { |
| 429 | users := c.UsersForHost(hostname) |
| 430 | |
| 431 | // If there is only one (or zero) users, then we remove the host |
| 432 | // and unset the keyring tokens. |
| 433 | if len(users) < 2 { |
| 434 | _ = c.cfg.Remove([]string{hostsKey, hostname}) |
| 435 | _ = keyring.Delete(keyringServiceName(hostname), "") |
| 436 | _ = keyring.Delete(keyringServiceName(hostname), username) |
| 437 | return ghConfig.Write(c.cfg) |
| 438 | } |
| 439 | |
| 440 | // Otherwise, we remove the user from this host |
| 441 | _ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username}) |
| 442 | |
| 443 | // This error is ignorable because we already know there is an active user for the host |
| 444 | activeUser, _ := c.ActiveUser(hostname) |
| 445 | |
| 446 | // If the user we're removing isn't active, then we just write the config |
| 447 | if activeUser != username { |
| 448 | return ghConfig.Write(c.cfg) |
| 449 | } |
| 450 | |
| 451 | // Otherwise we get the first user in the slice that isn't the user we're removing |
| 452 | switchUserIdx := slices.IndexFunc(users, func(n string) bool { |
| 453 | return n != username |
| 454 | }) |
| 455 | |
| 456 | // And activate them |
| 457 | return c.activateUser(hostname, users[switchUserIdx]) |
| 458 | } |
| 459 | |
| 460 | func (c *AuthConfig) activateUser(hostname, user string) error { |
| 461 | // We first need to idempotently clear out any set tokens for the host |
nothing calls this directly
no test coverage detected