| 521 | } |
| 522 | |
| 523 | func (c *AuthConfig) activateUser(hostname, user string) error { |
| 524 | // We first need to idempotently clear out any set tokens for the host |
| 525 | _ = keyring.Delete(keyringServiceName(hostname), "") |
| 526 | _ = c.cfg.Remove([]string{hostsKey, hostname, oauthTokenKey}) |
| 527 | |
| 528 | // Then we'll move the keyring token or insecure token as necessary, only one of the |
| 529 | // following branches should be true. |
| 530 | |
| 531 | // If there is a token in the secure keyring for the user, move it to the active slot |
| 532 | var tokenSwitched bool |
| 533 | if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil { |
| 534 | if err = keyring.Set(keyringServiceName(hostname), "", token); err != nil { |
| 535 | return fmt.Errorf("failed to move active token in keyring: %v", err) |
| 536 | } |
| 537 | tokenSwitched = true |
| 538 | } |
| 539 | |
| 540 | // If there is a token in the insecure config for the user, move it to the active field |
| 541 | if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil { |
| 542 | c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, token) |
| 543 | tokenSwitched = true |
| 544 | } |
| 545 | |
| 546 | if !tokenSwitched { |
| 547 | return fmt.Errorf("no token found for %s", user) |
| 548 | } |
| 549 | |
| 550 | // Then we'll update the active user for the host |
| 551 | c.cfg.Set([]string{hostsKey, hostname, userKey}, user) |
| 552 | |
| 553 | return ghConfig.Write(c.cfg) |
| 554 | } |
| 555 | |
| 556 | func (c *AuthConfig) UsersForHost(hostname string) []string { |
| 557 | users, err := c.cfg.Keys([]string{hostsKey, hostname, usersKey}) |