Skip to content

feat: add explicit OAuth token refresh support - #143

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

babakks wants to merge 2 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
token, err := resp.AccessToken()
if err != nil {
var apiError *api.Error
if errors.As(err, &apiError) && apiError.Code == "bad_refresh_token" {
Comment thread README.md
Before using an expired token, exchange its refresh token and persist the returned token pair:

```go
if token.IsExpired() && token.CanRefresh() {
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.

2 participants