| 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 |
| 462 | _ = keyring.Delete(keyringServiceName(hostname), "") |
| 463 | _ = c.cfg.Remove([]string{hostsKey, hostname, oauthTokenKey}) |
| 464 | |
| 465 | // Then we'll move the keyring token or insecure token as necessary, only one of the |
| 466 | // following branches should be true. |
| 467 | |
| 468 | // If there is a token in the secure keyring for the user, move it to the active slot |
| 469 | var tokenSwitched bool |
| 470 | if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil { |
| 471 | if err = keyring.Set(keyringServiceName(hostname), "", token); err != nil { |
| 472 | return fmt.Errorf("failed to move active token in keyring: %v", err) |
| 473 | } |
| 474 | tokenSwitched = true |
| 475 | } |
| 476 | |
| 477 | // If there is a token in the insecure config for the user, move it to the active field |
| 478 | if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil { |
| 479 | c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, token) |
| 480 | tokenSwitched = true |
| 481 | } |
| 482 | |
| 483 | if !tokenSwitched { |
| 484 | return fmt.Errorf("no token found for %s", user) |
| 485 | } |
| 486 | |
| 487 | // Then we'll update the active user for the host |
| 488 | c.cfg.Set([]string{hostsKey, hostname, userKey}, user) |
| 489 | |
| 490 | return ghConfig.Write(c.cfg) |
| 491 | } |
| 492 | |
| 493 | func (c *AuthConfig) UsersForHost(hostname string) []string { |
| 494 | users, err := c.cfg.Keys([]string{hostsKey, hostname, usersKey}) |