Registry Mirror
Skip to content

Conversation

@babakks

@babakks babakks commented Oct 13, 2026

Copy link
Copy Markdown
Member

Description

This PR sits on top of #14665 in the refreshable-token stack. It lets a user make expiring credentials the default without passing --expiring on every gh auth login and gh auth refresh, by adding a prefer_expiring_credentials configuration 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 --expiring flag, 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 flag changes --expiring on login and refresh from a plain bool to a *bool via cmdutil.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=false an explicit, meaningful value, and exports the shared login flow's authFlow test seam as AuthFlow so command-level tests can stub the interactive authorization and assert whether a refresh token was requested.

  • feat(auth): add prefer_expiring_credentials config option adds the config option itself (enabled/disabled, default disabled). When --expiring is unset, the new shared.ShouldPreferExpiring resolves the preference from this config for the host; an explicit --expiring or --expiring=false always 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 list and gh config get prefer_expiring_credentials and saw the new option registered and resolvable for the host.

I ran gh auth login --help and gh auth refresh --help and saw the new help text, which explains that enabling prefer_expiring_credentials prefers expiring credentials without passing --expiring, and that --expiring=false requests 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 under script/.

Key points

The flag is intentionally split into its own refactor commit so the *bool change (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=false disables expiring credentials even when the config enables them. The default is disabled, so existing behavior is unchanged for anyone who does not set the config.

Notes for reviewers

Start at pkg/cmd/auth/shared/login_flow.go for ShouldPreferExpiring, which is the single resolution point, then see how login.go and refresh.go call it when the flag is unset. The config option registration lives in internal/config/config.go and internal/gh/gh.go (with the regenerated mock). TestShouldPreferExpiring in login_flow_test.go covers 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:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @babakks will read and reply directly.
  • An agent will draft replies and @babakks will read them before they are posted.
  • Nobody has explicitly committed to replying.

@babakks
babakks requested a review from a team as a code owner October 13, 2026 21:35
@babakks
babakks requested review from sergiou87 and removed request for a team October 13, 2026 21:35
@babakks
babakks added this pull request to stack #14457 October 13, 2026 21:36
@babakks
babakks requested review from williammartin and removed request for sergiou87 October 13, 2026 22:04

@williammartin williammartin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

It seems like if the user explicitly requested --expiring=false that this shouldn't be shown. Pretty minor though.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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_credentials with host-aware lookup.
  • Makes --expiring tri-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

@babakks
babakks force-pushed the babakks/refresh-token-prefer-expiring-config branch 7 times, most recently from 66243bc to 783a601 Compare October 21, 2026 10:56
Base automatically changed from babakks/refresh-token-rename-flag-to-expiring to trunk October 21, 2026 11:08
babakks and others added 2 commits October 21, 2026 12:08
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
@babakks
babakks force-pushed the babakks/refresh-token-prefer-expiring-config branch from 783a601 to 6897f68 Compare October 21, 2026 11:08
@babakks
babakks merged commit 56682b6 into trunk Oct 21, 2026
11 checks passed
@babakks
babakks deleted the babakks/refresh-token-prefer-expiring-config branch October 21, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants