| 471 | } |
| 472 | |
| 473 | func (c *AuthConfig) activateUser(hostname, user string) error { |
| 474 | // We first need to idempotently clear out any set tokens for the host |
| 475 | _ = keyring.Delete(keyringServiceName(hostname), "") |
| 476 | _ = c.cfg.Remove([]string{hostsKey, hostname, oauthTokenKey}) |
| 477 | |
| 478 | // Then we'll move the keyring token or insecure token as necessary, only one of the |
| 479 | // following branches should be true. |
| 480 | |
| 481 | // If there is a token in the secure keyring for the user, move it to the active slot |
| 482 | var tokenSwitched bool |
| 483 | if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil { |
| 484 | if err = keyring.Set(keyringServiceName(hostname), "", token); err != nil { |
| 485 | return fmt.Errorf("failed to move active token in keyring: %v", err) |
| 486 | } |
| 487 | tokenSwitched = true |
| 488 | } |
| 489 | |
| 490 | // If there is a token in the insecure config for the user, move it to the active field |
| 491 | if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil { |
| 492 | c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, token) |
| 493 | tokenSwitched = true |
| 494 | } |
| 495 | |
| 496 | if !tokenSwitched { |
| 497 | return fmt.Errorf("no token found for %s", user) |
| 498 | } |
| 499 | |
| 500 | // Then we'll update the active user for the host |
| 501 | c.cfg.Set([]string{hostsKey, hostname, userKey}, user) |
| 502 | |
| 503 | return ghConfig.Write(c.cfg) |
| 504 | } |
| 505 | |
| 506 | func (c *AuthConfig) UsersForHost(hostname string) []string { |
| 507 | users, err := c.cfg.Keys([]string{hostsKey, hostname, usersKey}) |