Skip to content

Use shared pkg/oauthproto helpers in tokenexchange#5212

Open
jhrozek wants to merge 1 commit intomainfrom
oauth-refactor-prep-3
Open

Use shared pkg/oauthproto helpers in tokenexchange#5212
jhrozek wants to merge 1 commit intomainfrom
oauth-refactor-prep-3

Conversation

@jhrozek
Copy link
Copy Markdown
Contributor

@jhrozek jhrozek commented May 7, 2026

Summary

Switch pkg/auth/tokenexchange to consume the shared OAuth grant-helper primitives in pkg/oauthproto that landed in earlier prep work (#5082, #5168). Removes a duplicated HTTP client, hand-rolled redaction, and a handful of //nolint:gosec directives that were suppressing rules gosec does not actually emit.

WHY:

  • oauthproto.Redact and oauthproto.DefaultHTTPClient are now consumed by every grant path. Keeping a duplicate inside tokenexchange invites drift — consolidating early means future helper changes propagate uniformly across token exchange, DCR, and the auth-server flows.
  • The four //nolint:gosec // G117 annotations on token/secret struct fields and one // G706 annotation in middleware.go were suppressing nothing. gosec does not emit those rule IDs at those positions. .claude/rules/go-style.md forbids //nolint directives that do not suppress a confirmed false positive.
  • 22 defer server.Close() sites in exchange_test.go are foot-guns under t.Parallel: the parent test can return before parallel subtests finish, closing the server prematurely. .claude/rules/testing.md calls this out — converted to t.Cleanup(server.Close).

WHAT:

  • HTTP client: the nil-fallback now routes to oauthproto.DefaultHTTPClient(). Local defaultHTTPClient and defaultHTTPTimeout deleted.
  • Redaction: the three String() methods (exchangeRequest, response, clientAuthentication) call oauthproto.Redact instead of branching on empty/non-empty manually. redactedPlaceholder and emptyPlaceholder constants deleted.
  • //nolint:gosec cleanup: 4 in exchange.go, 1 in middleware.go.
  • Tests: defer server.Close()t.Cleanup(server.Close) across 22 sites.

This is an isolated step in a larger refactor. The request-plumbing swap (createTokenExchangeRequestoauthproto.NewFormRequest, executeTokenExchangeRequestoauthproto.DoTokenRequest) is left for a follow-up so each step bisects to a single concern. maxResponseBodySize stays local for now with a TODO — the shared constant is unexported because it is consumed only by oauthproto.DoTokenRequest, which will replace executeTokenExchangeRequest in that follow-up.

Type of change

  • Refactoring (no behavior change)

Test plan

  • Unit tests (task test)
  • Linting (task lint-fix)

Does this introduce a user-facing change?

No.

Switch tokenexchange to the shared grant-helper primitives landed
earlier:

- HTTP client: delete the local defaultHTTPClient var and
  defaultHTTPTimeout const; route the nil-client fallback through
  oauthproto.DefaultHTTPClient(). Every grant now picks up the same
  process-wide transport and connection pool.

- Redaction: delete the local redactedPlaceholder and emptyPlaceholder
  constants; the three String() methods (exchangeRequest, response,
  clientAuthentication) now call oauthproto.Redact(value), which
  replaces four hand-rolled "if empty/else redacted" branches with
  one expression each.

- Pre-existing annotation cleanup: four //nolint:gosec // G117
  directives on sensitive struct-tag fields (AccessToken,
  RefreshToken, ClientSecret x2) were suppressing a rule gosec does
  not actually flag. Delete them per the go-style.md rule against
  //nolint directives that do not suppress a confirmed false
  positive. middleware.go had one //nolint:gosec // G706 in the same
  state; same treatment.

- Test hygiene: convert every defer server.Close() in
  exchange_test.go (22 sites) to t.Cleanup(server.Close). Every
  affected site runs t.Parallel either directly or via subtests;
  .claude/rules/testing.md calls out defer as a foot-gun there
  because the parent test function can return before parallel
  subtests finish, closing the server prematurely.

maxResponseBodySize stays local with a TODO referencing the
follow-up that replaces executeTokenExchangeRequest with
oauthproto.DoTokenRequest, at which point the local constant
disappears. createTokenExchangeRequest, executeTokenExchangeRequest,
parseTokenExchangeResponse are untouched in this commit for the
same reason: isolating the HTTP-client swap from the request-plumbing
rewrite makes a future bisect point at one concern at a time.
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label May 7, 2026
@jhrozek jhrozek requested a review from tgrunnagle May 7, 2026 10:57
@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

❌ Patch coverage is 25.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.75%. Comparing base (182a025) to head (8abbe21).

Files with missing lines Patch % Lines
pkg/auth/tokenexchange/exchange.go 25.00% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5212   +/-   ##
=======================================
  Coverage   67.74%   67.75%           
=======================================
  Files         608      608           
  Lines       62212    62198   -14     
=======================================
- Hits        42148    42143    -5     
+ Misses      16891    16881   -10     
- Partials     3173     3174    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants