(hostname, user string)
| 403 | } |
| 404 | |
| 405 | func (c *AuthConfig) SwitchUser(hostname, user string) error { |
| 406 | previouslyActiveUser, err := c.ActiveUser(hostname) |
| 407 | if err != nil { |
| 408 | return fmt.Errorf("failed to get active user: %s", err) |
| 409 | } |
| 410 | |
| 411 | previouslyActiveToken, previousSource := c.ActiveToken(hostname) |
| 412 | if previousSource != "keyring" && previousSource != "oauth_token" { |
| 413 | return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource) |
| 414 | } |
| 415 | |
| 416 | err = c.activateUser(hostname, user) |
| 417 | if err != nil { |
| 418 | // Given that activateUser can only fail before the config is written, or when writing the config |
| 419 | // we know for sure that the config has not been written. However, we still should restore it back |
| 420 | // to its previous clean state just in case something else tries to make use of the config, or tries |
| 421 | // to write it again. |
| 422 | if previousSource == "keyring" { |
| 423 | if setErr := keyring.Set(keyringServiceName(hostname), "", previouslyActiveToken); setErr != nil { |
| 424 | err = errors.Join(err, setErr) |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if previousSource == "oauth_token" { |
| 429 | c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, previouslyActiveToken) |
| 430 | } |
| 431 | c.cfg.Set([]string{hostsKey, hostname, userKey}, previouslyActiveUser) |
| 432 | |
| 433 | return err |
| 434 | } |
| 435 | |
| 436 | return nil |
| 437 | } |
| 438 | |
| 439 | // Logout will remove user, git protocol, and auth token for the given hostname. |
| 440 | // It will remove the auth token from the encrypted storage if it exists there. |
nothing calls this directly
no test coverage detected