Classify token-endpoint refusals by OAuth error code, not by 401

Merged
JE
2mo ago
+752-135

bc3 is moving a body-authenticated invalid_client from a bare 401 to a 400 (RFC 6749 §5.2, RFC 9110 §15.5.2). A Basic-auth invalid_client stays 401 with a Basic challenge. TypeScript, Ruby and Python treated a token error as auth_required only when it was a 401 or invalid_grant. After the bc3 change, a refused client would show up as api_error.

The rule, in every SDK with OAuth

ResponseCode
error is invalid_grant, invalid_client, unauthorized_client or access_denied, on any statusauth_required
401, whatever the body (OAuth error object, other JSON, HTML, empty)auth_required
anything elseapi_error (with the status)

This is pinned in conformance/oauth-token: fixtures 08–17 are new, and 04/05 now name their code. Go, TypeScript, Python, Ruby and Rust run those fixtures directly; Kotlin mirrors them in OAuthTest.kt. The schema now requires code on every reject fixture, so a harness can't pass just because the call failed. It also accepts rawBody for non-JSON responses. The rule is written up in SPEC §16, "Token Endpoint Error Classification".

The SDKs didn't agree before this

The brief assumed Go and Kotlin already classified by error code. They didn't:

  • Go oauth.Exchanger: every non-200 came back as an untyped fmt.Errorf, so callers couldn't classify it at all. It now returns a typed *basecamp.Error. When the body had no error member, the old message also echoed the raw body. Now it is just token request failed with status N, as SPEC §9 requires: only error/error_description may be rendered.
  • Go AuthManager refresh: every non-200 was api_error, a 401 included. That error reaches callers through AccessToken's proactive refresh. It now uses the same rule and renders only the parsed OAuth fields (the old message echoed the whole body). An unreadable body is now refused before classification. The rule comes from the internal package go/pkg/basecamp/internal/oautherror (basecamp and basecamp/oauth can't import each other).
  • Kotlin: every non-2xx was Auth, including 5xx and 304. Auth now carries the real status through a new internal constructor; the public constructor's signature is unchanged.
  • Rust: already classified by code, with the four codes above plus any 401. It mapped other 400/422 responses to validation; those are now api_error. It still maps 429 to rate_limit (retryable) and treats a 5xx as a retryable api_error. No fixture pins either of those, and the SPEC section records both.
  • TypeScript / Ruby / Python: the set was only invalid_grant, and a 401 with a non-JSON body became a parse-failure api_error before the classifier ran. Fixtures 12 and 13 showed that.

Decisions

  • access_denied is in the set (the brief listed three codes). Rust already included it, SPEC §16 maps the device flow's access_denied to auth_required, and signing in again is the right response wherever the token endpoint returns it.
  • Any 401 is auth_required, even with a parseable code outside the set (fixture 14). The brief only required this for a 401 without a parseable OAuth body. Narrowing it would have moved, for example, a 401 invalid_request from auth_required to api_error in every SDK, with nothing gained.
  • The device flow is unchanged. Every SDK's poll classifies by error code (authorization_pending, slow_down, access_denied, expired_token, the 429 pair) and doesn't check for 401, so moving invalid_client from 401 to 400 doesn't change its outcome. Unknown codes stay a terminal api_error, as SPEC §16 specifies.
  • Revoke: no SDK has an OAuth revoke call.
  • Not touched: the Ruby OauthTokenProvider and Python OAuthTokenProvider/async variant are legacy and Launchpad-only. They raise their auth error for any refused refresh, so invalid_client on a 400 is already auth there.

Failing first

On the old code the new fixtures failed 5 each in TypeScript, Ruby and Python (08, 09, 11, 12, 13) and all 10 in Go. Rust failed on 15, Kotlin on its mirror test plus the 304 test, and Go AuthManager on 7 of 10 table cases. Everything passes on this branch.


Summary by cubic

Classifies token-endpoint refusals by OAuth error code, not by 401, across all SDKs, so a body-authenticated invalid_client answered with a 400 (RFC 6749 §5.2) surfaces as auth_required rather than api_error.

The rule

  • invalid_grant, invalid_client, unauthorized_client, or access_denied, on any status, is auth_required.
  • Any 401 is auth_required, whatever its body.
  • Everything else is api_error carrying the status.

Per-SDK changes

  • TypeScript: a body-less or non-JSON refusal is classified by status alone, never sent through the body reader (which failed closed on a null body without a Content-Length) or turned into a parse-failure api_error.
  • Go: oauth.Exchanger returns typed *basecamp.Error; AuthManager refresh classifies via the shared internal/oautherror package. Both render only the RFC 6749 error/error_description or the status, never the raw refusal body, which can echo what was sent (SPEC §9).
  • Kotlin: no longer raises Auth for every non-2xx (5xx and 304 are now api_error), and Auth now carries the refusal's actual HTTP status instead of a hard-coded 401.
  • Rust: other 400/422 responses drop the validation mapping; 429 stays rate_limit and 5xx a retryable api_error.
  • Ruby and Python: non-JSON 401 bodies no longer become a parse-failure api_error.

New conformance fixtures 08–17 and a schema requirement that rejections name their code pin the rule across SDKs.