Skip to content

Add JWT multi-scheme support, Cognito package, and Testing package#53

Merged
Theauxm merged 1 commit into
mainfrom
feat/jwt-multi-scheme
May 14, 2026
Merged

Add JWT multi-scheme support, Cognito package, and Testing package#53
Theauxm merged 1 commit into
mainfrom
feat/jwt-multi-scheme

Conversation

@Theauxm
Copy link
Copy Markdown
Member

@Theauxm Theauxm commented May 13, 2026

Summary

  • Trax.Api.Auth.Jwt gains named-scheme overloads so hosts can register more than one JWT issuer side by side. Each named scheme exposes its own per-scheme policy ({name}-JwtPolicy) and joins the combined TraxAuthPolicy.
  • New AddTraxJwtDispatcher picks the validating scheme by inspecting the inbound token's iss claim. Unmapped issuers fall through to a reject handler that returns 401.
  • New Trax.Api.Auth.Jwt.Cognito package: UseCognito(region, userPoolId, clientId, tokenUse) builder helper and CognitoJwtPrincipalResolver that normalizes cognito:groups, cognito:username, and the federated identities array.
  • New Trax.Api.Auth.Jwt.Testing package: TestJwksServer (in-proc Kestrel host that serves a real JWKS endpoint) and TestTokenIssuer for minting RS256/HS256 tokens in integration tests.
  • JwtBuilder.CustomizeTokenValidation / CustomizeBearerOptions now chain (additive) instead of last-writer-wins so helpers can compose with consumer overrides.

Existing single-scheme callers and tests are untouched. The default-scheme overloads (AddTraxJwtAuth(authority, audience), AddTraxJwtAuth(Action<JwtBuilder>)) still register under JwtDefaults.SchemeName and produce the same DI bindings as before.

Coverage on the new code:

Package Line Branch
Trax.Api.Auth.Jwt 98.84% 96.77%
Trax.Api.Auth.Jwt.Cognito 98.92% 94.56%
Trax.Api.Auth.Jwt.Testing 98.31% 100.00%

178 new tests added on top of the existing 406 auth tests; all 1277 tests in the project pass.

Docs PR: TraxSharp/Trax.Docs#TBD

Test plan

  • CI green
  • dotnet test against the full Trax.Api solution
  • csharpier check clean

JwtAuthServiceCollectionExtensions gains named-scheme overloads so hosts
can register more than one JWT issuer side by side (e.g. Cognito for
mobile clients + internal HS256 for server-to-server). Each named scheme
gets its own per-scheme policy ({name}-JwtPolicy) and contributes to the
combined TraxAuthPolicy.

AddTraxJwtDispatcher registers a policy scheme that picks the validating
scheme by peeking at the inbound token's iss claim. Unmapped issuers get
forwarded to a built-in reject handler that produces a clean 401.

Trax.Api.Auth.Jwt.Cognito is a new package with UseCognito(region,
userPoolId, clientId) and a CognitoJwtPrincipalResolver that normalizes
cognito:groups (roles), cognito:username (display name), and the
federated identities array (identity_provider claim). Handles both ID
tokens (aud check) and access tokens (client_id check), plus token_use
validation.

Trax.Api.Auth.Jwt.Testing is a new package with TestJwksServer (in-proc
Kestrel host serving a real JWKS endpoint over loopback) and
TestTokenIssuer (fluent RS256/HS256 token builder). For use in
integration tests only.

CustomizeTokenValidation and CustomizeBearerOptions are now additive
(multiple calls chain in registration order) so helpers and consumers
can layer customizations.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

@Theauxm Theauxm merged commit f24a190 into main May 14, 2026
2 checks passed
@Theauxm Theauxm deleted the feat/jwt-multi-scheme branch May 14, 2026 00:58
@traxsharp
Copy link
Copy Markdown

traxsharp Bot commented May 14, 2026

This PR is included in version 1.29.0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant