Skip to content

Pass redemption handler dependencies as a struct - #7220

Merged
jhrozek merged 2 commits into
mainfrom
tokenexchange-refactor-2-redemption-deps
Nov 23, 2026
Merged

jhrozek merged 2 commits into
mainfrom
tokenexchange-refactor-2-redemption-deps

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The JWT-bearer and ID-JAG redemption constructors take seven and ten positional parameters, and the factory's build closure takes eight. Every new dependency has to be threaded through every constructor, both factory closures and about 25 test call sites. Three planned changes each add one dependency (a per-client lifespan map, the authorization_details registry, and the subject resolver).

This PR collects the compose-time inputs in one issuanceDeps struct, so each of those changes adds one field instead of editing every signature.

  • issuanceDeps (new file) holds the inputs and owns the dependency checks and the per-issuer policy map.
  • newJWTBearerIssuanceHandler and newIDJAGIssuanceHandler take the struct. Both build on a shared newIssuanceCore, which filters trusted issuers by assertion form in one place.
  • jwtBearerGrantFactory's build closure takes the struct. The exported JWTBearerIssuanceFactory and IDJAGIssuanceFactory signatures do not change.
  • Tests build handlers through one fixture builder.

No behaviour changes: error texts, log lines and rejection reasons are the same as before.

Type of change

  • Refactoring (no behavior change)

Test plan

  • Unit tests: go test -race ./pkg/authserver/server/tokenexchange/... ./pkg/authserver/
  • Linting: golangci-lint and go vet on the same packages

New tests pin that both constructors reject bad dependencies with the same error, that the issuer-is-resource startup warning counts every trusted issuer (not only those accepting id_jag), and that both factories refuse to build without a shared validator.

Changes

File Change
issuance_deps.go New issuanceDeps struct with validate and policiesFor
jwt_bearer_handler.go Constructor and factory take the struct; new newIssuanceCore
id_jag_handler.go Constructor and factory closure take the struct
*_test.go Fixture builder testIssuanceDeps; call sites updated; new pinning tests

Special notes for reviewers

Adding a dependency later, before and after:

Planned change Before After
Per-client lifespan map both constructors, build closure, both factory closures, ~25 test call sites one struct field, the factory closure, the tests that care
authorization_details registry same same
Subject resolver same same

newJWTBearerHandler (the two-argument test seam) is left as is.

The JWT-bearer and ID-JAG redemption constructors took seven and ten
positional parameters, and the factory's build closure took eight, so
every new dependency touched every constructor, both factory closures
and about 25 test call sites.

Collect the compose-time inputs in an issuanceDeps struct. Both
constructors now take it and name their assertion form through a shared
newIssuanceCore, which filters the trusted issuers once. The exported
factory signatures, error texts and log lines are unchanged.

Tests build handlers through one fixture builder. New tests pin that
both constructors refuse bad dependencies with the same error, that the
issuer-is-resource warning reads the unfiltered issuer list, and that
both factories pass their inputs through to the handler.

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 23, 2026 08:58
@github-actions github-actions Bot added the size/L Large PR: 600-999 lines changed label Nov 23, 2026
rdimitrov
rdimitrov previously approved these changes Nov 23, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. This is a pure plumbing refactor and I found no behaviour change.

Design: Collapsing the long constructor and factory argument lists into one dependencies struct is made at the right place, jwt_bearer_handler.go, which builds the shared core that both the plain jwt-bearer and ID-JAG handlers use. All constructors involved are unexported, and both exported factories reach them through jwtBearerGrantFactory, so every entry point is covered and exported signatures are unchanged. Validation order and error strings, per-form policy filtering, and the ID-JAG audience warning (which still reads the unfiltered issuer list) all match the old behaviour, so nothing changes on upgrade. The struct also makes the planned extra dependencies cheap to add.

The one weak spot (not blocking): dependency validation is split between deps.validate() and newJWTBearerHandler, and the comment says a new field needs a check in one of the two. Moving the validator and token-endpoint checks into validate() would remove that trap.

🤖 Reviewed with Claude Code

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.62%. Comparing base (122fdef) to head (c4b29ed).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7220      +/-   ##
==========================================
- Coverage   81.62%   81.62%   -0.01%     
==========================================
  Files         902      903       +1     
  Lines       91736    91738       +2     
==========================================
- Hits        74882    74879       -3     
- Misses      16849    16854       +5     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 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.

Dependency checks were split: newJWTBearerHandler checked the validator
and token endpoint, issuanceDeps.validate checked the rest. A new field
could be checked in one place and missed in the other.

Move the validator and endpoint checks into a shared helper that both
newJWTBearerHandler and issuanceDeps.validate call. newIssuanceCore now
validates once, up front. Check order and error texts are unchanged.

Co-authored-by: Mecatl <[email protected]>
@github-actions github-actions Bot added size/L Large PR: 600-999 lines changed and removed size/L Large PR: 600-999 lines changed labels Nov 23, 2026
@jhrozek

jhrozek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@rdimitrov I took your validation point. The validator and token-endpoint checks now sit in a shared helper that both newJWTBearerHandler and issuanceDeps.validate() call, so validate() covers every field and newIssuanceCore validates once up front. Check order and error texts are the same, and the existing same-error tests pass unchanged. It's in c4b29ed on top of the first commit. That push probably dismissed your approval, so could you take another look when you get a chance?

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of c4b29ed: still approve. The new commit only moves the validator and token-endpoint checks into checkValidatorAndEndpoint, called by both newJWTBearerHandler and issuanceDeps.validate().

This closes the one weak spot from my earlier approval: all dependency checks now live in validate(), so a new field has one place to be checked. The check order (validator, endpoint, consumer, then the fosite dependencies) and the error texts are unchanged. newIssuanceCore validates once, up front, and builds the handler with the same two fields newJWTBearerHandler set. The design is unchanged, and CI is green.

🤖 Reviewed with Claude Code

@jhrozek
jhrozek merged commit 740e467 into main Nov 23, 2026
47 checks passed
@jhrozek
jhrozek deleted the tokenexchange-refactor-2-redemption-deps branch November 23, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Large PR: 600-999 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants