Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/auth/uat_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ func synchronizeStoredTokenClock(ctx context.Context, httpClient *http.Client, o
return nil
}

// refreshOnce performs one token exchange and classifies its retry and storage action.
// Only HTTP 429 bypasses body decoding; other statuses retain the existing classification.
func refreshOnce(ctx context.Context, httpClient *http.Client, endpoint string, opts UATCallOptions, stored *StoredUAToken, proofKey *dpop.Key, allowClockRecovery bool) refreshResult {
payload, err := json.Marshal(refreshRequest{
GrantType: "refresh_token",
Expand Down Expand Up @@ -497,6 +499,18 @@ func refreshOnce(ctx context.Context, httpClient *http.Client, endpoint string,
defer resp.Body.Close()
logHTTPResponse(resp)

// A rate-limited request was rejected without consuming the refresh token.
// Inspect the status before decoding: the endpoint may return plain text.
if resp.StatusCode == http.StatusTooManyRequests {
return refreshResult{
action: refreshRetryAndPreserve,
err: errs.NewAPIError(errs.SubtypeRateLimit,
"token refresh rate limited (HTTP 429)").
WithCode(http.StatusTooManyRequests).
WithRetryable(),
}
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return refreshResult{
Expand Down
74 changes: 73 additions & 1 deletion internal/auth/uat_client_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
)

type refreshHTTPTestStep struct {
status int
body string
err error
markWritten bool
Expand Down Expand Up @@ -72,6 +73,8 @@ func refreshHTTPResponse(req *http.Request, body string) *http.Response {
}
}

// scriptedRefreshClient returns a transport that replays the configured responses and errors.
// Each step can override the HTTP status or simulate a request written before a failure.
func scriptedRefreshClient(t *testing.T, steps []refreshHTTPTestStep, calls *atomic.Int32) *http.Client {
t.Helper()
return &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
Expand All @@ -95,7 +98,11 @@ func scriptedRefreshClient(t *testing.T, steps []refreshHTTPTestStep, calls *ato
if step.err != nil {
return nil, step.err
}
return refreshHTTPResponse(req, step.body), nil
response := refreshHTTPResponse(req, step.body)
if step.status != 0 {
response.StatusCode = step.status
}
return response, nil
})}
}

Expand Down Expand Up @@ -353,6 +360,8 @@ func TestRefreshDPoPTokenRecoversClockAndKeepsBinding(t *testing.T) {
}
}

// TestRefreshFailureDeterminesStoredTokenDisposition checks retries, typed errors, and stored tokens.
// A later rate limit must not erase the uncertainty of an earlier written request.
func TestRefreshFailureDeterminesStoredTokenDisposition(t *testing.T) {
sentinel := errors.New("transport unavailable")
sharedNetworkErr := errs.NewNetworkError(errs.SubtypeNetworkTransport,
Expand All @@ -365,10 +374,48 @@ func TestRefreshFailureDeterminesStoredTokenDisposition(t *testing.T) {
wantCategory errs.Category
wantSubtype errs.Subtype
wantRetryable bool
wantCode int
wantNeedAuth bool
wantCause error
wantInvalidResponseCause bool
}{
{
name: "plain text rate limits preserve after retry",
steps: []refreshHTTPTestStep{
{status: http.StatusTooManyRequests, body: "ratelimit triggered"},
{status: http.StatusTooManyRequests, body: "ratelimit triggered"},
},
wantCalls: 2,
wantPreserved: true,
wantCategory: errs.CategoryAPI,
wantSubtype: errs.SubtypeRateLimit,
wantRetryable: true,
wantCode: http.StatusTooManyRequests,
},
{
name: "JSON rate limits take precedence over unknown error codes",
steps: []refreshHTTPTestStep{
{status: http.StatusTooManyRequests, body: `{"code":123456,"error":"slow_down"}`},
{status: http.StatusTooManyRequests, body: `{"code":123456,"error":"slow_down"}`},
},
wantCalls: 2,
wantPreserved: true,
wantCategory: errs.CategoryAPI,
wantSubtype: errs.SubtypeRateLimit,
wantRetryable: true,
wantCode: http.StatusTooManyRequests,
},
{
name: "rate limit does not undo an earlier uncertain refresh",
steps: []refreshHTTPTestStep{
{err: sharedNetworkErr, markWritten: true},
{status: http.StatusTooManyRequests, body: "ratelimit triggered"},
},
wantCalls: 2,
wantCategory: errs.CategoryAuthentication,
wantSubtype: errs.SubtypeTokenMissing,
wantNeedAuth: true,
},
{
name: "policy challenge stops and preserves",
steps: []refreshHTTPTestStep{{
Expand Down Expand Up @@ -454,6 +501,9 @@ func TestRefreshFailureDeterminesStoredTokenDisposition(t *testing.T) {
t.Fatalf("token = %+v, want nil on refresh failure", token)
}
problem := requireRefreshProblem(t, err, tt.wantCategory, tt.wantSubtype, tt.wantRetryable)
if tt.wantCode != 0 && problem.Code != tt.wantCode {
t.Fatalf("code = %d, want %d", problem.Code, tt.wantCode)
}
if tt.wantNeedAuth {
if !IsNeedUserAuthorizationError(err) {
t.Fatalf("error = %v, want need-user-authorization sentinel", err)
Expand Down Expand Up @@ -490,6 +540,28 @@ func TestRefreshFailureDeterminesStoredTokenDisposition(t *testing.T) {
}
}

// TestRefreshSucceedsAfterRateLimit verifies that a rejected attempt can be retried with the same credential.
func TestRefreshSucceedsAfterRateLimit(t *testing.T) {
setupStoredTokenTest(t)
stored := newRefreshTestToken()
if err := SetStoredToken(stored); err != nil {
t.Fatal(err)
}
var calls atomic.Int32
client := scriptedRefreshClient(t, []refreshHTTPTestStep{
{status: http.StatusTooManyRequests, body: "ratelimit triggered"},
{body: `{"access_token":"access-new","refresh_token":"refresh-new","expires_in":120,"refresh_token_expires_in":600}`},
}, &calls)
token, err := GetValidAccessToken(context.Background(), client, newRefreshTestOptions(stored))
if err != nil || token == nil || token.AccessToken != "access-new" || calls.Load() != 2 {
t.Fatalf("refresh = (%+v, %v, %d calls), want success after one retry", token, err, calls.Load())
}
current := mustGetStoredToken(t, stored.AppId, stored.UserOpenId)
if current == nil || current.RefreshToken != "refresh-new" {
t.Fatalf("stored token = %#v, want rotated credential", current)
}
}

func TestRefreshDoesNotOverwriteNewerGeneration(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading