GitHub CLI v2.103.0:新增可选的过期自动刷新 OAuth Token
GitHub CLI: Opt in to expiring, auto-refreshing OAuth tokens
核心开发工具的安全机制变更,影响所有依赖 GitHub CLI 进行身份验证的工作流。建议开发者尽快评估自动化脚本中是否存在硬编码或缓存 token 的行为,并在生效前完成迁移。
You can now use expiring, automatically refreshing OAuth credentials in GitHub CLI as part of a public preview. When you opt in, gh stores an expiring access token alongside a rotating refresh token and automatically refreshes the token whenever it is needed. You stay authenticated without reauthenticating, and a leaked access token becomes unusable within hours instead of lasting indefinitely.
你现在可以在 GitHub CLI 中使用具有过期时间且自动刷新的 OAuth 凭据,该功能目前处于公开预览阶段。当你选择加入后,gh 会存储一个具有过期时间的访问令牌以及一个轮换的刷新令牌,并在需要时自动刷新令牌。你无需重新认证即可保持登录状态,而泄露的访问令牌将在数小时内失效,而非无限期有效。
Opting in at login
在登录时选择加入
Starting with v2.103.0, pass the new --expiring flag when you log in with gh auth login --expiring. To opt in for every sign-in without passing the flag each time, run gh config set expiring_credentials enabled. From then on, there is nothing new to run day to day: gh refreshes the credential for you in the background. To check the state of an expiring credential, including when the current access token expires, run gh auth status.
从 v2.103.0 版本开始,在使用 gh auth login --expiring 登录时传递新的 --expiring 标志。若要每次登录都默认选择加入而无需每次都传递该标志,请运行 gh config set expiring_credentials enabled。此后,你无需在日常操作中执行任何新命令:gh 会在后台为你刷新凭据。要检查过期凭据的状态(包括当前访问令牌的过期时间),请运行 gh auth status。
Using expiring tokens with git
在 git 中使用过期令牌
If you authenticate to git over HTTPS and want to use expiring tokens, we strongly recommend configuring gh as your git credential helper by running gh auth setup-git. This lets gh serve and refresh the credential for git, so your expiring token is correctly handled.
如果你通过 HTTPS 对 git 进行身份验证并希望使用过期令牌,我们强烈建议通过运行 gh auth setup-git 将 gh 配置为你的 git 凭据助手。这允许 gh 为 git 提供并刷新凭据,从而正确处理你的过期令牌。
We also strongly recommend upgrading to git 2.46 or newer, which lets gh mark expiring credentials so they are not cached past their lifetime. On older git versions, gh still works but prints a one-time recommendation to upgrade.
我们还强烈建议升级到 git 2.46 或更高版本,这样 gh 可以标记过期凭据,使其不会在其生命周期之外被缓存。在较旧版本的 git 上,gh 仍可正常工作,但会打印一次升级建议。
Good to know
需要了解的信息
- It’s opt-in and backward compatible. If you don’t opt in through the flag or configuration setting, nothing changes. Existing sign-ins and manually supplied tokens keep working exactly as before.
- You get whatever the server issues. --expiring requests refreshable credentials. If a server does not support them, gh falls back cleanly to a normal nonexpiring token.
- It will become the default over time. Expiring credentials are opt-in today. In a future release, we plan to make them the default for new sign-ins.
- 这是可选加入且向后兼容的。如果你未通过标志或配置设置选择加入,则没有任何变化。现有的登录方式和手动提供的令牌将继续像以前一样正常工作。
- 你将获得服务器颁发的任何内容。--expiring 请求可刷新的凭据。如果服务器不支持,gh 会干净地回退到普通的非过期令牌。
- 它将成为默认选项。目前,过期凭据是可选加入的。在未来的版本中,我们计划将其设为新登录的默认选项。
Things to watch out for
注意事项
Expiring access tokens are short-lived, so anything that captures a token once and reuses it later can break. Scripts and extensions that run gh auth token and store the result, or copy the token into another tool, will eventually be left holding an expired token.
过期访问令牌的生命周期较短,因此任何一次性捕获令牌并在后续重复使用的机制都可能出错。运行 gh auth token 并存储结果,或将令牌复制到其他工具中的脚本和扩展,最终可能会持有已过期的令牌。
If you consume the token outside of gh, read it fresh each time with gh auth token. This refreshes it when needed rather than caching the value. If a workflow depends on a token that stays valid for a long time, hold off on opting in for that workflow. When gh is configured as your git credential helper, it refreshes the credential for you, so git operations over HTTPS keep working.
如果你在 gh 之外使用令牌,请每次通过 gh auth token 重新读取。这会在需要时刷新令牌,而不是缓存其值。如果某个工作流依赖于长期有效的令牌,请暂缓为该工作流启用此功能。当将 gh 配置为 git 凭据助手时,它会自动为你刷新凭据,因此基于 HTTPS 的 git 操作仍可正常工作。
Who can use it
谁可以使用它
Expiring tokens are available on github.com and ghe.com today. GitHub Enterprise Server issues them starting with GHES 3.23. On earlier versions, opting in falls back to today’s nonexpiring behavior.
目前 github.com 和 ghe.com 已提供过期令牌。GitHub Enterprise Server 从 GHES 3.23 版本开始发行此类令牌。在更早的版本中,启用该功能会回退到当前的非过期行为。
For more on authenticating with GitHub CLI, including expiring tokens, see Authenticating with GitHub CLI.
有关使用 GitHub CLI 进行身份验证(包括过期令牌)的更多信息,请参阅“使用 GitHub CLI 进行身份验证”。
Install or upgrade to GitHub CLI v2.103.0 today to get started.
立即安装或升级到 GitHub CLI v2.103.0 即可开始使用。
Have feedback or found an issue? Open an issue in the cli/cli repository.
有反馈或发现问题?请在 cli/cli 仓库中提交 issue。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力