Skip to content

feat: add explicit OAuth token refresh support - #143

Open
babakks wants to merge 3 commits into
mainfrom
babakks/add-refresh-token-support
Open

babakks wants to merge 3 commits into
mainfrom
babakks/add-refresh-token-support

Conversation

@babakks

@babakks babakks commented Sep 14, 2026

Copy link
Copy Markdown
Member

Description

Adds explicit OAuth token refresh support, consumed by the refreshable (short-lived) token stack in cli/cli.

  • New Refresh function (refresh.go) exchanges a refresh token at the token endpoint for a new access and refresh token pair. It comes with RefreshOptions and a distinct ErrRefreshTokenInvalid, so callers can tell a permanently rejected refresh token from a transient failure and avoid pointless retries.
  • api.AccessToken gains ExpiresIn and RefreshTokenExpiresIn, parsed from the token response, so callers know when the access and refresh tokens expire. Zero means the server returned no expiry metadata.
  • Flow.RequestRefreshToken opts an authorization into an expiring access token plus a refresh token by adding the offline_access scope. Servers that do not support it simply return an ordinary non-expiring token with no refresh token.

How did you test this change?

Unit tests: refresh_test.go covers the refresh exchange (success, empty and rejected refresh tokens, error mapping); access_token_test.go and oauth_test.go cover expiry parsing and the offline_access opt-in. The README and runnable examples are updated. Also bumps workflow action versions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f5c79efc-51d8-4913-8961-701522797f9e
@babakks
babakks requested a review from a team as a code owner September 14, 2026 23:42
@babakks
babakks requested review from niik and a lite review from Copilot and removed request for Copilot September 14, 2026 23:42
Signed-off-by: Babak K. Shandiz <babakks@github.com>
@babakks
babakks force-pushed the babakks/add-refresh-token-support branch from 20fbfd1 to cde372e Compare September 14, 2026 23:46
Copilot AI lite review requested due to automatic review settings September 14, 2026 23:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Standard invalid_grant refresh failures are not mapped to ErrRefreshTokenInvalid, and the README example references unavailable API methods.

Get a fresh assessment by requesting another Copilot review.

Review tier: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Adds OAuth token refresh support, token expiry metadata, and offline_access opt-in for web and device flows.

Changes:

  • Adds refresh-token exchange and invalid-token error handling.
  • Parses access and refresh token expiry values.
  • Updates flows, documentation, examples, tests, and CI workflows.
File Reviewed change
refresh.go Implements token refresh exchanges.
refresh_test.go Tests refresh behavior and errors.
README.md Documents refresh usage.
oauth.go Adds refresh-token flow configuration.
oauth_webapp.go Adds offline_access to web scopes.
oauth_test.go Tests scope handling.
oauth_device.go Adds offline_access to device scopes.
examples_test.go Adds a refresh example.
api/​access_token.go Parses token expiry metadata.
api/​access_token_test.go Tests expiry parsing.
.github/​workflows/​lint.yml Updates lint workflow dependencies.
.github/​workflows/​ci.yml Updates CI actions and Go versions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread refresh.go Outdated
Comment thread README.md Outdated
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@williammartin
williammartin force-pushed the babakks/add-refresh-token-support branch from b9a715f to 4beee3a Compare October 2, 2026 10:12

@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.

LGTM. Left a few non-blocking notes on things we might come back to once the cli/cli stack has landed.

Comment thread refresh.go
return nil, err
}

return token, nil

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.

Non-blocking, something to revisit later: RFC 6749 §6 lets a server omit refresh_token from a refresh response, which means "keep using the old one". Right now that comes back as RefreshToken: "", so a caller that just persists the result (as the README suggests) would lose a still-valid refresh token. GitHub always rotates and cli/cli rejects an empty refresh token before persisting, so it's fine today. For a generic library I think we'd want something like:

if token.RefreshToken == "" {
	token.RefreshToken = opts.RefreshToken
}

and leave enforcing rotation to the app. That would also mean updating TestTokenRefresherRefreshOmittedRefreshToken in cli/cli.

Comment thread README.md
Comment on lines +91 to +92
Refresh tokens are single use. A successful refresh invalidates the previous access token and refresh token, so
applications must persist the returned replacements.

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.

Non-blocking: this describes GitHub's behaviour rather than OAuth in general. Rotation is optional per the spec. Might be worth rewording later to something like "GitHub rotates refresh tokens on every refresh, so always persist the refresh token returned by Refresh."

Comment thread oauth_device.go
oa.ClientID, oa.Scopes, device.WithAudience(oa.Audience))
scopes := oa.Scopes
if oa.RequestRefreshToken {
scopes = withOfflineAccess(scopes)

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, for later: the same scope selection is in oauth_webapp.go. Could move it onto Flow (e.g. oa.requestedScopes()) so both flows share it.

Comment thread api/access_token.go
Scope string
// The number of seconds from issuance until Token expires. Zero means the server did not return
// expiration metadata.
ExpiresIn int

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.

Something to think about later, not for this PR: time.Duration might be nicer for callers than int seconds (no time.Duration(x) * time.Second dance). Int matches the wire format and device.DeviceCode.ExpiresIn though, so happy to keep it for now and revisit after the cli/cli stack lands.

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