Skip to content

refactor(auth): align JWT claim parsing across microservices and amber - #4896

Merged
Yicong-Huang merged 3 commits into
apache:mainfrom
Yicong-Huang:refactor/align-jwt-parser
May 4, 2026
Merged

Yicong-Huang merged 3 commits into
apache:mainfrom
Yicong-Huang:refactor/align-jwt-parser

Conversation

@Yicong-Huang

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

Two JWT parsing paths drifted apart, and the codebase had two JwtConsumer instances built with byte-identical config:

  • common/auth/JwtParser (microservices) read 5 claims and dropped googleAvatar — even though JwtAuth.jwtClaims writes it on every token.
  • amber/.../UserAuthenticator read those 5 plus comment, accountCreation — neither of which the issuer ever writes, so always null in real tokens. Also dropped googleAvatar.

Consolidate into a single JwtParser.claimsToSessionUser(JwtClaims): SessionUser in common/auth. Make JwtParser.parseToken reuse JwtAuth.jwtConsumer (one consumer instance left in the codebase). Rewrite UserAuthenticator.authenticate as a 1-line delegate so amber and the microservices produce identical SessionUser objects from the same token.

UserAuthenticator itself stays in amber — it's a Dropwizard 1.3 Authenticator[JwtContext, SessionUser] adapter for the toastshaman library. Once amber's Dropwizard upgrade unblocks the move to the common JwtAuthFilter, this whole adapter goes away.

Side robustness fix: jose4j returns Long for integer claims after JSON round-trip but setClaim keeps Integer in memory. Widening userId via getClaimValue("userId", classOf[Number]).intValue() makes directly-built and parsed claims behave the same.

Any related issues, documentation, discussions?

Closes #4895

How was this PR tested?

JwtParserSpec (new, 4 cases): asserts every issued claim is populated (including googleAvatar), the non-issued User slots stay null, an end-to-end round-trip via JwtAuth.jwtToken -> JwtParser.parseToken reconstructs the user, and a structurally invalid token returns empty.

sbt Auth/test 11/11 green; AccessControlService, ConfigService, FileService, ComputingUnitManagingService, WorkflowExecutionService all compile clean; Auth/scalafmtCheckAll and Auth/scalafixAll --check clean.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Opus 4.7)

@codecov-commenter

codecov-commenter commented May 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 42.41%. Comparing base (332d4f4) to head (597832e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4896      +/-   ##
============================================
+ Coverage     42.38%   42.41%   +0.03%     
- Complexity     2154     2155       +1     
============================================
  Files           954      954              
  Lines         34694    34675      -19     
  Branches       3629     3629              
============================================
+ Hits          14705    14709       +4     
+ Misses        19083    19060      -23     
  Partials        906      906              
Flag Coverage Δ
access-control-service 39.53% <ø> (ø)
amber 42.86% <100.00%> (+0.07%) ⬆️
computing-unit-managing-service 0.00% <ø> (ø)
config-service 0.00% <ø> (ø)
file-service 33.24% <ø> (ø)
workflow-compiling-service 47.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aglinxinyuan

Copy link
Copy Markdown
Contributor

Why are we dropping googleAvatar? Are you sure it's not being used?

@aglinxinyuan

Copy link
Copy Markdown
Contributor

Why are we dropping googleAvatar? Are you sure it's not being used?

Nvm, it's not used on the backend.

@Yicong-Huang
Yicong-Huang marked this pull request as draft May 4, 2026 04:22
Two parsing paths drifted apart:

- common/auth/JwtParser (used by JwtAuthFilter in 4 microservices and
  by a couple of amber resources directly) read 5 claims and dropped
  googleAvatar even though JwtAuth.jwtClaims writes it into every
  issued token.
- amber/.../UserAuthenticator (used by amber's toastshaman-based
  filter) read those 5 plus comment + accountCreation, neither of
  which the issuer ever writes — so they were always null in real
  tokens. It also dropped googleAvatar.

Two JwtConsumer instances were also built with byte-identical builder
chains (same secret, same clock-skew/exp/sub config) — pure copies in
JwtParser and JwtAuth.

Consolidate:

1. Extract JwtParser.claimsToSessionUser(JwtClaims): SessionUser as
   the single source of truth for "verified claims -> SessionUser".
   Reads the full set the issuer writes, including googleAvatar.
2. JwtParser.parseToken now reuses JwtAuth.jwtConsumer instead of
   building its own. The codebase has exactly one JwtConsumer left.
3. UserAuthenticator.authenticate becomes a 1-line delegate to
   claimsToSessionUser, dropping the dead comment / accountCreation
   reads.

Robustness: jose4j returns Long after JSON round-trip but setClaim on
in-memory JwtClaims keeps Integer; widen userId via
getClaimValue("userId", classOf[Number]).intValue() so directly-built
claims and parsed claims behave the same.

Tests: 4-case JwtParserSpec covers the populated/null field surface
and a JwtAuth.jwtToken -> JwtParser.parseToken round-trip.

Closes apache#4895
JwtParserSpec gains 8 cases for the verification life cycle:
- token signed with wrong secret (signature mismatch)
- expired beyond clock-skew window
- missing subject claim
- missing exp claim (consumer requires it)
- expired within the 30s clock-skew window (still accepted)
- expired past the clock-skew window (rejected)
- tampered signature segment
- tampered payload segment

Plus a "life cycle" suite covering distinct-user round-trip and
re-parse stability.

UserAuthenticatorSpec (new, 3 cases) covers the amber-side adapter
end-to-end: real JwtAuth.jwtToken -> JwtAuth.jwtConsumer.process ->
UserAuthenticator.authenticate, plus missing-required-claim paths
that exercise the catch in the adapter.
@Yicong-Huang
Yicong-Huang force-pushed the refactor/align-jwt-parser branch from f69afaa to 59f95fa Compare May 4, 2026 04:33
@Yicong-Huang
Yicong-Huang marked this pull request as ready for review May 4, 2026 04:43
@Yicong-Huang
Yicong-Huang enabled auto-merge (squash) May 4, 2026 04:43
@Yicong-Huang
Yicong-Huang merged commit 786a920 into apache:main May 4, 2026
17 checks passed
@Yicong-Huang
Yicong-Huang deleted the refactor/align-jwt-parser branch May 4, 2026 05:41
yangzhang75 pushed a commit to yangzhang75/texera that referenced this pull request Jun 22, 2026
apache#4896)

### What changes were proposed in this PR?

Two JWT parsing paths drifted apart, and the codebase had two
`JwtConsumer` instances built with byte-identical config:

- `common/auth/JwtParser` (microservices) read 5 claims and dropped
`googleAvatar` — even though `JwtAuth.jwtClaims` writes it on every
token.
- `amber/.../UserAuthenticator` read those 5 plus `comment`,
`accountCreation` — neither of which the issuer ever writes, so always
`null` in real tokens. Also dropped `googleAvatar`.

Consolidate into a single `JwtParser.claimsToSessionUser(JwtClaims):
SessionUser` in `common/auth`. Make `JwtParser.parseToken` reuse
`JwtAuth.jwtConsumer` (one consumer instance left in the codebase).
Rewrite `UserAuthenticator.authenticate` as a 1-line delegate so amber
and the microservices produce identical `SessionUser` objects from the
same token.

`UserAuthenticator` itself stays in amber — it's a Dropwizard 1.3
`Authenticator[JwtContext, SessionUser]` adapter for the toastshaman
library. Once amber's Dropwizard upgrade unblocks the move to the common
`JwtAuthFilter`, this whole adapter goes away.

Side robustness fix: jose4j returns `Long` for integer claims after JSON
round-trip but `setClaim` keeps `Integer` in memory. Widening `userId`
via `getClaimValue("userId", classOf[Number]).intValue()` makes
directly-built and parsed claims behave the same.

### Any related issues, documentation, discussions?

Closes apache#4895

### How was this PR tested?

`JwtParserSpec` (new, 4 cases): asserts every issued claim is populated
(including `googleAvatar`), the non-issued `User` slots stay `null`, an
end-to-end round-trip via `JwtAuth.jwtToken -> JwtParser.parseToken`
reconstructs the user, and a structurally invalid token returns empty.

`sbt Auth/test` 11/11 green; `AccessControlService`, `ConfigService`,
`FileService`, `ComputingUnitManagingService`,
`WorkflowExecutionService` all compile clean; `Auth/scalafmtCheckAll`
and `Auth/scalafixAll --check` clean.

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Opus 4.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Align JwtAuthFilter parsing across microservices and amber, deduplicate JwtConsumer

3 participants