Skip to content

Account incorrectly reported active in gh auth status #10136

@med8bra

Description

@med8bra

Describe the bug

Running gh auth status reports account as active, but API calls use another account.

> gh --version
gh version 2.63.0 (1980-01-01)
https://github.com/cli/cli/releases/tag/v2.63.0

Steps to reproduce the behavior

  1. You have two users on same hostname github.com, last active account is user1.
  2. Switch to another folder with custom GH_CONFIG_DIR (via direnv), with a custom config which has one user user2
  3. Run gh auth status
> gh auth status
github.com
  ✓ Logged in to github.com account user2 (keyring)
  - Active account: true
  - Git operations protocol: ssh
  - Token: gho_************************************
  - Token scopes: 'gist', 'read:org', 'repo', 'workflow'
  1. Run gh api /user | jq .name => prints user1, contradicting auth status output

The expected behavior is to use the token for the given host+user of current repository without needing to gh auth switch, and report active account correctly.

Context

Active token is fetched from keyring using only hostname

// ActiveToken will retrieve the active auth token for the given hostname,
// searching environment variables, plain text config, and
// lastly encrypted storage.
func (c *AuthConfig) ActiveToken(hostname string) (string, string) {
if c.tokenOverride != nil {
return c.tokenOverride(hostname)
}
token, source := ghauth.TokenFromEnvOrConfig(hostname)
if token == "" {
var err error
token, err = c.TokenFromKeyring(hostname)
if err == nil {
source = "keyring"
}
}
return token, source
}

Related to #9111 (comment))

Metadata

Metadata

Labels

bugSomething isn't workinghelp wantedContributions welcomeneeds-designAn engineering task needs design to proceedpriority-3Affects a small number of users or is largely cosmetic

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions