fix(auth): stop resending a dead OAuth refresh token

Description

Once GitLab rejects a refresh token with invalid_grant, glab keeps it and sends the same refresh on every command. An agent polling through glab with an expired login turns that into a steady stream of 400s. In one report, about 200 glab api calls a minute got the user's IP blocked at GitLab.com's edge ("elevated 400 error rates"), which cut off every client on that address.

Now, when adopt() finds no token rotated in by another process, glab deletes the rejected refresh token, but only if the stored copy is still the one GitLab rejected. Later commands fall back to the access-token-only auth source. Both its Header() and the Credential() branch now refuse a token whose recorded expiry has passed and tell the user to run glab auth login, so the request never goes out. Credential() keeps the failed to refresh the OAuth2 token prefix on that error, which the Duo CLI matches on to show its "session expired" message. That covers glab auth credential-helper, the Git credential helper, cluster graph, and opentofu init.

The stored expiry applies only to the stored token. An OAuth token from the environment (OAUTH_TOKEN, or GITLAB_TOKEN with GLAB_IS_OAUTH2=true) isn't judged against it, so a fresh environment token still works on a host whose stored login died. glab auth status now names a rejected refresh token as one cause of its "no refresh token is stored" warning, since this change creates that state.

A dead session now costs one failed refresh in total, not one per command.

Out of scope, for client-go follow-ups:

  • gitlaboauth2.NewOAuth2Config leaves AuthStyle unset, so x/oauth2 retries a failed refresh with the credentials in the body, and that one failed refresh is two requests. Now that it happens once per session, pinning a style here isn't worth the risk without live testing.
  • A 429 is retried five times, using RateLimit-Reset but ignoring Retry-After. Rack::Attack sends RateLimit-Reset, so only edge 429s are affected.

Context: https://gitlab.slack.com/archives/C085M5071LG/p1793173786419569 (internal)

https://gitlab.com/gitlab-org/cli/-/work_items/8693

How has this been tested?

TestToken_ForgetsRejectedRefreshToken, TestToken_ForgetsRejectedRefreshToken_FileBacked, TestToken_KeepsRefreshTokenRotatedAfterRejection, TestOAuth2AccessTokenOnlyAuthSource_Header, TestClient_Credential_expiredAccessTokenOnly, and TestNewClientFromConfig_EnvTokenIgnoresStoredExpiry are new, and each fails with the fix reverted. They pass under go test -race alongside internal/oauth2, internal/api, internal/config, and internal/commands/auth/.... lefthook run pre-push passes except lychee, which errors when a push has no Markdown files.

Not tested against a live instance. To check: log in with OAuth, revoke glab under User settings > Applications, then run glab api user twice. The first run should fail with invalid_grant and the second should fail locally, with no request to /oauth/token. Running glab auth credential-helper twice should also fail both times with failed to refresh the OAuth2 token, not return the expired access token.

Edited by Lucas Charles

Merge request reports

Loading
Loading