-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
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.
Lines 448 to 458 in 140edf7
| 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) | |
| } |
Line 203 in 140edf7
| 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.