Skip to content

Allow gh auth git-credential get to get a token for non-active user #9111

@emiksk

Description

@emiksk

Describe the feature or problem you’d like to solve

Currently gh auth git-credential get can get a token for only active account in specified host.

e.g.

$ gh auth status
github.com
  ✓ Logged in to github.com account emiksk (keyring)
  - Active account: true
  - Git operations protocol: https
  - Token: gho_************************************
  - Token scopes: 'gist', 'read:org', 'repo', 'workflow'

  ✓ Logged in to github.com account non-emiksk (keyring)
  - Active account: false
  - Git operations protocol: https
  - Token: gho_************************************
  - Token scopes: 'gist', 'read:org', 'repo', 'workflow'

$ gh auth git-credential get
protocol=https
host=github.com

protocol=https
host=github.com
username=emiksk
password=gho_************************************

$ gh auth git-credential get
protocol=https
host=github.com
username=non-emiksk


$

I think that gh auth git-credential get should be able to get a token regardless of whether user is active or not if the specific username is given.

Proposed solution

This enhancement will allow developers to control credentials by remote url (such as https://emiksk@github.com/org/repo and https://non-emiksk@github.com/org/repo) instead of using gh auth switch.

Additional context

This behavior is caused by using ActiveToken() method in helper.go even if a username is given.
It would be better to use the TokenForUser() method when username is given, as in token.go.

However, TokenForUser() method ignores environment variables such as GITHUB_TOKEN unlike ActiveToken() method.

func (c *AuthConfig) TokenForUser(hostname, user string) (string, string, error) {
if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil {
return token, "keyring", nil
}
if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil {
return token, "oauth_token", nil
}
return "", "default", fmt.Errorf("no token found for '%s'", user)
}

token, source := ghAuth.TokenFromEnvOrConfig(hostname)

Therefore, if the logic of token.go and helper.go are simply made common, a behavior is changed when an environment variable exists and username is explicitly given.
Since this behavior also exists in the test case, it needs to consider whether the behavior should be changed or whether different logic should be implemented instead of sharing the same logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreThis issue is not accepting PRs from outside contributorsenhancementa request to improve CLIgh-authrelating to the gh auth command

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions