Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f5c79efc-51d8-4913-8961-701522797f9e
Signed-off-by: Babak K. Shandiz <babakks@github.com>
20fbfd1 to
cde372e
Compare
There was a problem hiding this comment.
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
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.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
b9a715f to
4beee3a
Compare
williammartin
left a comment
There was a problem hiding this comment.
LGTM. Left a few non-blocking notes on things we might come back to once the cli/cli stack has landed.
| return nil, err | ||
| } | ||
|
|
||
| return token, nil |
There was a problem hiding this comment.
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.
| Refresh tokens are single use. A successful refresh invalidates the previous access token and refresh token, so | ||
| applications must persist the returned replacements. |
There was a problem hiding this comment.
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."
| oa.ClientID, oa.Scopes, device.WithAudience(oa.Audience)) | ||
| scopes := oa.Scopes | ||
| if oa.RequestRefreshToken { | ||
| scopes = withOfflineAccess(scopes) |
There was a problem hiding this comment.
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.
| Scope string | ||
| // The number of seconds from issuance until Token expires. Zero means the server did not return | ||
| // expiration metadata. | ||
| ExpiresIn int |
There was a problem hiding this comment.
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.


Description
Adds explicit OAuth token refresh support, consumed by the refreshable (short-lived) token stack in
cli/cli.Refreshfunction (refresh.go) exchanges a refresh token at the token endpoint for a new access and refresh token pair. It comes withRefreshOptionsand a distinctErrRefreshTokenInvalid, so callers can tell a permanently rejected refresh token from a transient failure and avoid pointless retries.api.AccessTokengainsExpiresInandRefreshTokenExpiresIn, parsed from the token response, so callers know when the access and refresh tokens expire. Zero means the server returned no expiry metadata.Flow.RequestRefreshTokenopts an authorization into an expiring access token plus a refresh token by adding theoffline_accessscope. 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.gocovers the refresh exchange (success, empty and rejected refresh tokens, error mapping);access_token_test.goandoauth_test.gocover expiry parsing and theoffline_accessopt-in. The README and runnable examples are updated. Also bumps workflow action versions.