Skip to content

feat: add explicit OAuth token refresh support - #143

Merged
babakks merged 8 commits into
mainfrom
babakks/add-refresh-token-support
Oct 20, 2026
Merged

babakks merged 8 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
Comment thread README.md Outdated
Comment thread oauth_device.go Outdated
Comment thread api/access_token.go Outdated
babakks and others added 5 commits October 20, 2026 13:46
RFC 6749 section 6 lets a server omit refresh_token from a refresh
response, meaning the previous refresh token stays valid. Returning an
empty RefreshToken would make callers that persist the result lose a
usable refresh token, so fall back to the one passed in RefreshOptions
and leave enforcing rotation to the application.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Rotation is optional per the OAuth spec, so the README now attributes
it to GitHub and notes that Refresh falls back to the previous refresh
token when the server does not return a new one.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Both flows added offline_access to the requested scopes in the same
way, so move that logic onto Flow as requestedScopes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
ExpiresIn and RefreshTokenExpiresIn were int seconds, so every caller
had to convert them before doing time arithmetic. Both fields are new
and unreleased, so switch them to time.Duration now. The wire format is
still parsed as seconds, and device.DeviceCode.ExpiresIn is unchanged
since it is already part of the released API.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
The lint workflow installs the latest stable Go, now 1.27.3, whose
export data golangci-lint v2.13.2 cannot decode, so every package
failed typecheck. v2.14.0 is built with Go 1.27.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
@babakks
babakks merged commit 7efb8e6 into main Oct 20, 2026
28 checks passed
@babakks
babakks deleted the babakks/add-refresh-token-support branch October 20, 2026 15:49
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