Carry a token refusal's OAuth error and Retry-After, and type device authorization's 429

Merged
JE
2mo ago
+529-29

Stacked on #971, which made the Go token-endpoint refusals typed; this builds on that typed error, and GitHub will retarget it to main when #971 merges.

What

A token-endpoint or device-authorization refusal now carries what the server said, on *basecamp.Error:

  • OAuthError and OAuthErrorDescription: the RFC 6749 error and error_description. Both are bounded the way the message is, and nothing else from the body is rendered (SPEC §9).
  • RetryAfter: the wait from Retry-After, parsed by the client's own §6 parser. ParseRetryAfter exports that parser so the oauth package reads the header the same way.
  • A 429 is rate_limit in the Exchanger and in AuthManager refresh. Rules 1 and 2 still come first, so invalid_grant on any status is still auth_required. This is the same refinement Rust already makes.

RequestDeviceAuthorization gets the same treatment. A 4xx body's error and error_description go into the typed fields and the message. A 429 is rate_limit with its RetryAfter, and everything else stays api_error with its status. Nothing becomes auth_required there, because the refusal is of the login being started, and "sign in again" doesn't fix it.

Why

A Basecamp CLI user locked themselves out. Two containers held copies of one login. The second refresh revoked it, and a scheduled job then resent the revoked token every two minutes. bc3's abuse tracker escalated to a 4-hour block on every OAuth endpoint for that client and address, device authorization included. Through the SDK:

  • the refresh 429 came back as api_error with no wait, so the caller's only option was to try again into the block;
  • the device login said device authorization failed with status 429, with no reason and no time.

The CLI also detected invalid_grant by matching "token error: invalid_grant" in the message. With OAuthError it can match the code.

Decisions

  • Go only. The CLI is the consumer that needs this. SPEC §16 records the Go behavior the same way it already records Rust's 429 refinement and other "Go only, today" rules. The other SDKs keep their current classification.
  • Device authorization reads a 4xx body; it used to read none. The old code classified every non-2xx before reading, so a stalled body couldn't turn into a retryable transport failure. That guarantee still holds: a body that can't be read, or is over the cap, leaves the status to classify alone, and cancellation still wins. A 3xx or 5xx is still classified without a read. The cost is that a stalled 4xx body now takes up to the request timeout before it fails as an api_error, instead of failing at once.
  • New fields on basecamp.Error, not a new error type. Callers already classify with errors.As(*basecamp.Error). Extra fields keep that working and reach the AuthManager path, which can't import oauth.

Testing

  • New tests failed first on behavior, not just compilation: the field was added before the logic. TestExchanger_Refresh_CarriesOAuthErrorAndRetryAfter and TestAuthManager_Refresh_CarriesOAuthErrorAndRetryAfter got api_error/429 with RetryAfter = 0 and an empty OAuthError. TestRequestDeviceAuthorization_CarriesOAuthErrorAndRetryAfter got the bare status 429 message. All pass now, along with the existing suites.
  • TestExchanger_Refresh_BoundsOAuthErrorFields: a 10 KB error or error_description is truncated in the typed fields as well.
  • make check passes (macOS)

Summary by cubic

A refused token refresh or device authorization in the Go SDK now carries the server's RFC 6749 error code, description, and Retry-After wait on *basecamp.Error, so callers act on the server's own verdict instead of a bare status.

  • A 429 is now rate_limit in the Exchanger and AuthManager refresh, matching Rust's classification.
  • Device authorization reads a 4xx body's error and error_description into the typed fields and message; a 429 is rate_limit with its wait, everything else stays api_error.
  • ParseRetryAfter exports the client's Retry-After parser so the oauth package reads the header the same way.
  • OAuthError and OAuthErrorDescription are bounded like the message; the composed message is bounded too.
  • An unreadable or oversized refusal keeps its status-based class and Retry-After, but a caller's own cancellation during a body read surfaces as cancellation, not a refusal.