Conversation
williammartin
left a comment
There was a problem hiding this comment.
prefer_expiring_credentials seems like it should be expiring_credentials: true/false to account for if we ever make expiring credentials the default. Otherwise there's no way to config out of that.
| SkipSSHKeyPrompt: opts.SkipSSHKeyPrompt, | ||
| CopyToClipboard: copyToClipboard, | ||
| Expiring: opts.Expiring, | ||
| Expiring: shared.ShouldPreferExpiring(cfg, opts.Expiring, hostname), |
There was a problem hiding this comment.
Food For Thought
What this means is that with:
if opts.Web || opts.Expiring { // <- now also true when only the config is enabled
authMode = 0 // the paste-token option is never offered
} else if opts.Interactive {We will never offer the paste a token option again. I suspect this fine in nearly all cases, but there might be cases where people want to do oauth expiring tokens by default but also from time to time paste a PAT or something.
| // --expiring downgrades it to a non-expiring token. | ||
| wasRefreshable := authCfg.ActiveToken(hostname).IsRefreshable() | ||
| if wasRefreshable && !opts.Expiring { | ||
| if wasRefreshable && !expiring { |
There was a problem hiding this comment.
Nit
It seems like if the user explicitly requested --expiring=false that this shouldn't be shown. Pretty minor though.
aee1d49 to
569a56e
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Authentication behavior depends on the underlying unmerged token stack, and integration validation remains unverified.
No open issues 🎉
1 file not reviewed
internal/gh/mock/config.go: Generated file
What changed in this PR
Adds a configurable preference for expiring credentials to the refreshable-token stack while preserving the disabled default.
Changes:
- Registers
prefer_expiring_credentialswith host-aware lookup. - Makes
--expiringtri-state so explicit values override configuration. - Updates help, mocks, and tests for preference resolution and command wiring.
| File | Description |
|---|---|
| pkg/cmd/config/list/list_test.go | Checks the new default listing. |
| pkg/cmd/auth/shared/login_flow.go | Resolves preference and exposes the authorization test seam. |
| pkg/cmd/auth/shared/login_flow_test.go | Tests configuration and flag precedence. |
| pkg/cmd/auth/refresh/refresh.go | Applies the preference during refresh. |
| pkg/cmd/auth/refresh/refresh_test.go | Tests refresh flags and configuration overrides. |
| pkg/cmd/auth/login/login.go | Applies the preference during login. |
| pkg/cmd/auth/login/login_test.go | Tests login flags and authorization requests. |
| internal/gh/mock/config.go | Adds the configuration accessor mock. |
| internal/gh/gh.go | Extends the configuration interface. |
| internal/config/test.go | Wires the mock accessor. |
| internal/config/config.go | Registers the setting, default, and allowed values. |
Review effort: Balanced
66243bc to
783a601
Compare
Change the --expiring flag on auth login and auth refresh from a bool to a *bool via cmdutil.NilBoolFlag, so an unset flag (nil) is distinguishable from an explicit --expiring=false. An unset flag behaves exactly as before (no expiring token requested), so this is a pure refactor that lays the groundwork for a config-driven default, and it also lets users pass --expiring=false explicitly. Export the shared login flow's authFlow seam as AuthFlow and surface it on the login command options so command-level tests can stub the interactive authorization and assert whether a refresh token was requested. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Add a prefer_expiring_credentials config option (enabled/disabled, default disabled) that sets the default for the --expiring flag on auth login and auth refresh. When --expiring is unset, the new shared.ShouldPreferExpiring consults this config for the host; an explicit --expiring or --expiring=false always wins, so a user can request a non-expiring token even when the config is enabled. This lets users prefer automatically refreshed expiring credentials without passing --expiring on every login and refresh. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
783a601 to
6897f68
Compare
Description
This PR sits on top of #14665 in the refreshable-token stack. It lets a user make expiring credentials the default without passing
--expiringon everygh auth loginandgh auth refresh, by adding aprefer_expiring_credentialsconfiguration option.Expiring credentials are OAuth tokens the server issues with a short expiry that gh refreshes automatically on the user's behalf. Until now the only way to prefer them was the
--expiringflag, which has to be repeated on each login and refresh. A user who always wants expiring credentials had no way to say so once.The work lands in two commits:
refactor(auth): make --expiring a tri-state *bool flagchanges--expiringonloginandrefreshfrom a plainboolto a*boolviacmdutil.NilBoolFlag. This distinguishes three states that a plain bool collapses into two: flag set to true, flag set to false, and flag unset. An unset flag still behaves exactly as before (no expiring token requested), so this commit carries no behavior change on its own. It also makes--expiring=falsean explicit, meaningful value, and exports the shared login flow'sauthFlowtest seam asAuthFlowso command-level tests can stub the interactive authorization and assert whether a refresh token was requested.feat(auth): add prefer_expiring_credentials config optionadds the config option itself (enabled/disabled, defaultdisabled). When--expiringis unset, the newshared.ShouldPreferExpiringresolves the preference from this config for the host; an explicit--expiringor--expiring=falsealways wins. So a user can set the config once to prefer expiring credentials, and still request a non-expiring token on a one-off basis with--expiring=false.How did you test this change?
I built gh from this branch and ran it directly as
./bin/gh.I ran
gh config listandgh config get prefer_expiring_credentialsand saw the new option registered and resolvable for the host.I ran
gh auth login --helpandgh auth refresh --helpand saw the new help text, which explains that enablingprefer_expiring_credentialsprefers expiring credentials without passing--expiring, and that--expiring=falserequests a non-expiring token even when the config is enabled.I did not manually drive the full interactive login and refresh OAuth flow for each flag and config combination in this session. The preference resolution is a small, deterministic function (
shared.ShouldPreferExpiring), and the flag plumbing through login and refresh is exercised by the stack's end-to-end scripts underscript/.Key points
The flag is intentionally split into its own refactor commit so the
*boolchange (which enables distinguishing "unset" from "false") can be reviewed separately from the config option that consumes it.Precedence is explicit and flag-wins: the flag overrides the config in both directions, so
--expiring=falsedisables expiring credentials even when the config enables them. The default isdisabled, so existing behavior is unchanged for anyone who does not set the config.Notes for reviewers
Start at
pkg/cmd/auth/shared/login_flow.goforShouldPreferExpiring, which is the single resolution point, then see howlogin.goandrefresh.gocall it when the flag is unset. The config option registration lives ininternal/config/config.goandinternal/gh/gh.go(with the regenerated mock).TestShouldPreferExpiringinlogin_flow_test.gocovers all six flag and config combinations.This PR targets
babakks/refresh-token-rename-flag-to-expiring(the tip of the stack, #14665), not trunk. Review it after the underlying stack.Authorship and follow-up
Who wrote this:
Who answers review comments: