-
Notifications
You must be signed in to change notification settings - Fork 88
Add opt-in refresh token support #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hpsin
wants to merge
2
commits into
cli:main
Choose a base branch
from
hpsin:hpsin-solid-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Changelog | ||
|
|
||
| ## Unreleased | ||
|
|
||
| ### Added: opt-in support for expiring access tokens and refresh tokens | ||
|
|
||
| GitHub OAuth apps can issue access tokens that expire after 8 hours along with a refresh token valid | ||
| for 6 months. This release adds support for that flow. It is **opt-in and fully backwards | ||
| compatible** — existing applications continue to receive non-expiring tokens with no code changes. | ||
|
|
||
| - `Flow.RequestRefreshToken` opts an authorization into expiring tokens by requesting the | ||
| `offline_access` scope, in both Device flow and Web application flow. Also available as | ||
| `device.WithRefreshToken()` and `webapp.WithRefreshToken()` for callers using those packages | ||
| directly. | ||
| - `api.AccessToken` now records `ExpiresIn`, `ExpiresAt`, `RefreshTokenExpiresIn`, and | ||
| `RefreshTokenExpiresAt`, with `IsExpired()` and `CanRefresh()` helpers. | ||
| - `api.Refresh` exchanges a refresh token for a new token. A rejected refresh token is reported as | ||
| `api.ErrRefreshTokenInvalid`. | ||
| - `oauth.TokenSource` holds a token and refreshes it on demand, with an `OnRefresh` callback for | ||
| persisting rotated credentials. | ||
| - `oauth.NewHTTPClient` returns an `http.Client` that attaches the token and, on a rejected request, | ||
| refreshes once and retries once. | ||
|
|
||
| Servers that do not support expiring tokens ignore the request and return a non-expiring token with | ||
| no refresh token, so applications must not assume a refresh token was issued. | ||
|
|
||
| See the "Expiring access tokens" section of the README for an adoption guide. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestFormResponse_AccessToken_expiry(t *testing.T) { | ||
| now := time.Date(2026, 8, 18, 12, 0, 0, 0, time.UTC) | ||
| timeNow = func() time.Time { return now } | ||
| t.Cleanup(func() { timeNow = time.Now }) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| values url.Values | ||
| wantExpiresIn int | ||
| wantExpiresAt time.Time | ||
| wantRefreshExpiresIn int | ||
| wantRefreshTokenExpiresAt time.Time | ||
| }{ | ||
| { | ||
| name: "expiring token with refresh token", | ||
| values: url.Values{ | ||
| "access_token": []string{"ATOKEN"}, | ||
| "refresh_token": []string{"RTOKEN"}, | ||
| "expires_in": []string{"28800"}, | ||
| "refresh_token_expires_in": []string{"15897600"}, | ||
| }, | ||
| wantExpiresIn: 28800, | ||
| wantExpiresAt: now.Add(28800 * time.Second), | ||
| wantRefreshExpiresIn: 15897600, | ||
| wantRefreshTokenExpiresAt: now.Add(15897600 * time.Second), | ||
| }, | ||
| { | ||
| name: "server without expiring token support", | ||
| values: url.Values{ | ||
| "access_token": []string{"ATOKEN"}, | ||
| "token_type": []string{"bearer"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "unparseable expiry is ignored", | ||
| values: url.Values{ | ||
| "access_token": []string{"ATOKEN"}, | ||
| "expires_in": []string{"soon"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "refresh expiry ignored without refresh token", | ||
| values: url.Values{ | ||
| "access_token": []string{"ATOKEN"}, | ||
| "refresh_token_expires_in": []string{"15897600"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := FormResponse{values: tt.values}.AccessToken() | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got.ExpiresIn != tt.wantExpiresIn { | ||
| t.Errorf("ExpiresIn = %d, want %d", got.ExpiresIn, tt.wantExpiresIn) | ||
| } | ||
| if !got.ExpiresAt.Equal(tt.wantExpiresAt) { | ||
| t.Errorf("ExpiresAt = %v, want %v", got.ExpiresAt, tt.wantExpiresAt) | ||
| } | ||
| if got.RefreshTokenExpiresIn != tt.wantRefreshExpiresIn { | ||
| t.Errorf("RefreshTokenExpiresIn = %d, want %d", got.RefreshTokenExpiresIn, tt.wantRefreshExpiresIn) | ||
| } | ||
| if !got.RefreshTokenExpiresAt.Equal(tt.wantRefreshTokenExpiresAt) { | ||
| t.Errorf("RefreshTokenExpiresAt = %v, want %v", got.RefreshTokenExpiresAt, tt.wantRefreshTokenExpiresAt) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAccessToken_IsExpired(t *testing.T) { | ||
| now := time.Date(2026, 8, 18, 12, 0, 0, 0, time.UTC) | ||
| timeNow = func() time.Time { return now } | ||
| t.Cleanup(func() { timeNow = time.Now }) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| token *AccessToken | ||
| want bool | ||
| }{ | ||
| {name: "nil token", token: nil, want: false}, | ||
| {name: "non-expiring token", token: &AccessToken{Token: "A"}, want: false}, | ||
| {name: "valid token", token: &AccessToken{ExpiresAt: now.Add(time.Hour)}, want: false}, | ||
| {name: "expired token", token: &AccessToken{ExpiresAt: now.Add(-time.Second)}, want: true}, | ||
| {name: "within leeway counts as expired", token: &AccessToken{ExpiresAt: now.Add(30 * time.Second)}, want: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := tt.token.IsExpired(); got != tt.want { | ||
| t.Errorf("IsExpired() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAccessToken_CanRefresh(t *testing.T) { | ||
| now := time.Date(2026, 8, 18, 12, 0, 0, 0, time.UTC) | ||
| timeNow = func() time.Time { return now } | ||
| t.Cleanup(func() { timeNow = time.Now }) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| token *AccessToken | ||
| want bool | ||
| }{ | ||
| {name: "nil token", token: nil, want: false}, | ||
| {name: "no refresh token", token: &AccessToken{Token: "A"}, want: false}, | ||
| {name: "refresh token without expiry", token: &AccessToken{RefreshToken: "R"}, want: true}, | ||
| {name: "unexpired refresh token", token: &AccessToken{RefreshToken: "R", RefreshTokenExpiresAt: now.Add(time.Hour)}, want: true}, | ||
| {name: "expired refresh token", token: &AccessToken{RefreshToken: "R", RefreshTokenExpiresAt: now.Add(-time.Hour)}, want: false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := tt.token.CanRefresh(); got != tt.want { | ||
| t.Errorf("CanRefresh() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAppendOfflineAccess(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []string | ||
| want []string | ||
| }{ | ||
| {name: "empty", input: nil, want: []string{"offline_access"}}, | ||
| {name: "appends", input: []string{"repo"}, want: []string{"repo", "offline_access"}}, | ||
| {name: "dedupes", input: []string{"repo", "offline_access"}, want: []string{"repo", "offline_access"}}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := AppendOfflineAccess(tt.input) | ||
| if len(got) != len(tt.want) { | ||
| t.Fatalf("got %v, want %v", got, tt.want) | ||
| } | ||
| for i := range got { | ||
| if got[i] != tt.want[i] { | ||
| t.Fatalf("got %v, want %v", got, tt.want) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAppendOfflineAccess_doesNotMutateInput(t *testing.T) { | ||
| // A caller's Scopes slice with spare capacity must not be written through. | ||
| input := make([]string, 1, 4) | ||
| input[0] = "repo" | ||
|
|
||
| _ = AppendOfflineAccess(input) | ||
|
|
||
| if got := input[:cap(input)]; got[1] != "" { | ||
| t.Errorf("input slice was mutated: %q", got[1]) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems inverted - if the RT_expires_at is 0 that seems like there's no RT and therefore it can't be refreshed.