Skip to content

Fixes #32000: Mint IdP-shaped principal claims in JWTTokenGenerator [2.0] - #34968

Merged
mohityadav766 merged 1 commit into
2.0from
backport/2.0/pr-33725
Oct 12, 2026
Merged

mohityadav766 merged 1 commit into
2.0from
backport/2.0/pr-33725

Conversation

@mohityadav766

Copy link
Copy Markdown
Member

Describe your changes:

Fixes #32000

Backports #33725 (54f4e80ea3) to 2.0. On 2.0.x, a confidential OIDC login whose jwtPrincipalClaimsMapping points at a custom identity-provider claim (for example username:suffixed_name) fails every request with 401 Invalid JWT token, 'username' claim is not present. Since 2.0 (#26314) the callback hands the browser an OpenMetadata-minted session JWT, and JwtFilter re-resolves the user from it with that mapping, but the minted token never carried the mapped claim.

With this backport:

  • JWTTokenGenerator.init(AuthenticationConfiguration, JWTTokenConfiguration) records the principal-claim configuration.
  • Minted tokens carry the mapped username and email claims, plus the first configured order claim.
  • The algorithm-only init overload stays, @Deprecated(forRemoval = true), for downstream callers.

On main, #34967 goes further: confidential OIDC browsers get the identity provider's own ID token. This PR is the minimal fix for 2.0.x users.

Release branch: 2.0.

Type of change:

  • Bug fix
  • Improvement
  • New feature
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation

High-level design:

N/A — cherry-pick of #33725. Conflicts resolved:

  • JWTTokenGenerator: main's internal action-token helpers are not on 2.0 and are left out. The change itself matches main's diff.
  • JwtFilter: only the comment update. 2.0 keeps its own domain-enforcement condition; main's email-first branch is not on 2.0.
  • Dropped: hunks for InternalActionTokenSignerTest and SocketAddressFilterSpoofRevalidationBypassTest, which do not exist on 2.0.

All callers of init (startup, security-configuration reload, OpenMetadataOperations) pass the AuthenticationConfiguration.

Tests:

Use cases covered

Unit tests

  • I added unit tests for the new/changed logic.
  • Regression test: JWTTokenGeneratorTest#oidcSessionTokenResolvesToItsUserThroughCustomClaimMapping.
    • On unpatched 2.0 it fails with AuthenticationException: Invalid JWT token, 'username' claim is not present.
    • With the backport it passes.
  • Runs: 0 failures across 195 tests:
    • JWTTokenGeneratorTest 14
    • JwtFilterTest 33
    • SocketAddressFilterTest 8
    • AuthenticationCodeFlowHandlerTest 41
    • SecurityUtilTest 53
    • UserUtilTest 29
    • TestLoginServiceTest 11
    • MultiUrlJwkProviderTest 6
  • openmetadata-mcp compiles. spotless:check passes.

Backend integration tests

  • Not applicable: no REST endpoint changes. Integration tests not run locally for 2.0.

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

  • Not exercised against a live identity provider.
  • Personal access tokens: tokens minted before the upgrade do not carry the custom claim, so under such a mapping they still fail until regenerated. Session tokens are re-minted at login and refresh.

UI screen recording / screenshots:

Not applicable.

Checklist:

  • I have read the CONTRIBUTING document.

  • My PR title is Fixes <issue-number>: <short explanation>

  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.

  • I have commented on my code, particularly in hard-to-understand areas.

  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed. No schema changes.

  • For UI changes: I attached a screen recording and/or screenshots above. No UI changes.

  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

  • I have added a test that covers the exact scenario we are fixing. For complex issues, comment the issue number in the test for future reference.

…2.0]

Backport of #33725 (cherry-picked from 54f4e80) to the 2.0 line.

Since #26314 the confidential OIDC callback hands the browser an
OpenMetadata-minted session JWT, and JwtFilter resolves the user from it
with the IdP-oriented jwtPrincipalClaimsMapping. The minted token never
carried a custom mapped claim such as username:suffixed_name, so every
request failed with 401 "Invalid JWT token, 'username' claim is not
present". JWTTokenGenerator.init now takes the AuthenticationConfiguration
and minted tokens carry the mapped username/email claims (or the first
configured order claim). The algorithm-only init stays as a deprecated
overload for downstream callers.

Conflicts:
- JWTTokenGenerator: main's internal action-token helpers are not on 2.0;
  kept 2.0's code and took only the new init Javadoc.
- JwtFilter: took main's reworded comment on the OM-issued-token domain
  exemption, kept 2.0's enforcePrincipalDomain condition (main's
  allowedEmailDomains branch is not on 2.0).
- JWTTokenGeneratorTest: merged imports with the regression test below.
- InternalActionTokenSignerTest and
  SocketAddressFilterSpoofRevalidationBypassTest do not exist on 2.0;
  dropped their hunks.

Regression test (JWTTokenGeneratorTest):
oidcSessionTokenResolvesToItsUserThroughCustomClaimMapping mints a
session token the way the OIDC callback does under jwtPrincipalClaims
[suffixed_name, email, preferred_username, sub] and mapping
[username:suffixed_name, email:email], then resolves it the way JwtFilter
does. On unpatched 2.0 it fails with the 401 AuthenticationException
above.

(cherry picked from commit 54f4e80)
@mohityadav766
mohityadav766 requested a review from a team as a code owner October 8, 2026 19:03
@mohityadav766 mohityadav766 added backend skip-pr-checks Bypass PR metadata validation check labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added the safe to test Add this label to run secure Github workflows on PRs label Oct 8, 2026
Comment on lines +291 to +305
private Map<String, String> identityClaims(String userName, String email) {
String principal = nullOrEmpty(email) ? userName : email;
Map<String, String> claims = new LinkedHashMap<>();
claims.put(SUBJECT_CLAIM, userName);
claims.put(USERNAME, userName);
if (!nullOrEmpty(email)) {
claims.put(EMAIL_CLAIM, email);
}
claims.put(PREFERRED_USERNAME, principal);
putConfiguredPrincipalClaims(claims, userName, principal);
return claims;
}

/**
* A deployment may resolve principals from claims we do not otherwise mint ({@code upn}, {@code

@gitar-bot gitar-bot Bot Oct 8, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Bug: Email in preferred_username/first claim can resolve token to wrong user

identityClaims now puts the email in preferred_username. With no mapping configured, putConfiguredPrincipalClaims also writes the email into the first configured order claim, even when that claim is sub or username. SecurityUtil.findUserNameFromClaims resolves the user from the first matching order claim and keeps only the part before @. So when a deployment's order reads preferred_username or sub first, any token minted after this change resolves to the email's local part, not to user.getName(). Before this change those claims held the bare name and resolved correctly.

This breaks every user or bot whose name differs from its email local part. Example: a bot my-bot with email [email protected], or an admin-created user john with email [email protected]. After login, refresh, or token regeneration (including the regenerate-bot-tokens ops command), their new token resolves to svc or john.doe. The result is a 401 or user-not-found, or the request runs as a different existing user with that name. Bot tokens are especially exposed: findUserNameFromClaims skips the mapping for isBot tokens and always uses the order.

The new tests pass only because every fixture uses an email whose local part equals the username ([email protected]). Suggested fix: only write the email into these claims when its local part (lowercased) equals userName, and otherwise keep the bare name. Also add a test where the name and the email local part differ.

Use the email as the principal only when it resolves back to the same username:

private Map<String, String> identityClaims(String userName, String email) {
  final boolean isEmailConsistent =
      !nullOrEmpty(email)
          && email.split("@")[0].equalsIgnoreCase(userName);
  final String principal = isEmailConsistent ? email : userName;
  ...

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Oct 8, 2026

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 0 closed / 1 findings

🔴 High risk · Minting configured principal claims into session JWTs could resolve users incorrectly, potentially granting a token another user's permissions.

Backports OIDC session token claim-mapping fix to 2.0, but minted tokens incorrectly place email into preferred_username, sub, or first-order claims, causing 401s or token resolution to the wrong user when name and email local part differ. This breaks every user or bot whose username differs from their email local part—for example my-bot with email [email protected] resolves to svc instead. Bot tokens are especially exposed since findUserNameFromClaims skips mapping for them. Only write email to these claims when its local part matches the username, and add a test with differing name and email local part.

⚠️ Bug: Email in preferred_username/first claim can resolve token to wrong user

📄 openmetadata-service/src/main/java/org/openmetadata/service/security/jwt/JWTTokenGenerator.java:291-305 📄 openmetadata-service/src/test/java/org/openmetadata/service/security/JWTTokenGeneratorTest.java:100-111 📄 openmetadata-service/src/test/java/org/openmetadata/service/security/JWTTokenGeneratorTest.java:165-178

identityClaims now puts the email in preferred_username. With no mapping configured, putConfiguredPrincipalClaims also writes the email into the first configured order claim, even when that claim is sub or username. SecurityUtil.findUserNameFromClaims resolves the user from the first matching order claim and keeps only the part before @. So when a deployment's order reads preferred_username or sub first, any token minted after this change resolves to the email's local part, not to user.getName(). Before this change those claims held the bare name and resolved correctly.

This breaks every user or bot whose name differs from its email local part. Example: a bot my-bot with email [email protected], or an admin-created user john with email [email protected]. After login, refresh, or token regeneration (including the regenerate-bot-tokens ops command), their new token resolves to svc or john.doe. The result is a 401 or user-not-found, or the request runs as a different existing user with that name. Bot tokens are especially exposed: findUserNameFromClaims skips the mapping for isBot tokens and always uses the order.

The new tests pass only because every fixture uses an email whose local part equals the username ([email protected]). Suggested fix: only write the email into these claims when its local part (lowercased) equals userName, and otherwise keep the bare name. Also add a test where the name and the email local part differ.

Use the email as the principal only when it resolves back to the same username
private Map<String, String> identityClaims(String userName, String email) {
  final boolean isEmailConsistent =
      !nullOrEmpty(email)
          && email.split("@")[0].equalsIgnoreCase(userName);
  final String principal = isEmailConsistent ? email : userName;
  ...
🤖 Prompt for agents
Code Review: Backports OIDC session token claim-mapping fix to 2.0, but minted tokens incorrectly place email into `preferred_username`, `sub`, or first-order claims, causing 401s or token resolution to the wrong user when name and email local part differ. This breaks every user or bot whose username differs from their email local part—for example `my-bot` with email `[email protected]` resolves to `svc` instead. Bot tokens are especially exposed since `findUserNameFromClaims` skips mapping for them. Only write email to these claims when its local part matches the username, and add a test with differing name and email local part.

1. ⚠️ Bug: Email in preferred_username/first claim can resolve token to wrong user
   Files: openmetadata-service/src/main/java/org/openmetadata/service/security/jwt/JWTTokenGenerator.java:291-305, openmetadata-service/src/test/java/org/openmetadata/service/security/JWTTokenGeneratorTest.java:100-111, openmetadata-service/src/test/java/org/openmetadata/service/security/JWTTokenGeneratorTest.java:165-178

   `identityClaims` now puts the email in `preferred_username`. With no mapping configured, `putConfiguredPrincipalClaims` also writes the email into the first configured order claim, even when that claim is `sub` or `username`. `SecurityUtil.findUserNameFromClaims` resolves the user from the first matching order claim and keeps only the part before `@`. So when a deployment's order reads `preferred_username` or `sub` first, any token minted after this change resolves to the email's local part, not to `user.getName()`. Before this change those claims held the bare name and resolved correctly.
   
   This breaks every user or bot whose name differs from its email local part. Example: a bot `my-bot` with email `[email protected]`, or an admin-created user `john` with email `[email protected]`. After login, refresh, or token regeneration (including the `regenerate-bot-tokens` ops command), their new token resolves to `svc` or `john.doe`. The result is a 401 or user-not-found, or the request runs as a different existing user with that name. Bot tokens are especially exposed: `findUserNameFromClaims` skips the mapping for `isBot` tokens and always uses the order.
   
   The new tests pass only because every fixture uses an email whose local part equals the username (`[email protected]`). Suggested fix: only write the email into these claims when its local part (lowercased) equals `userName`, and otherwise keep the bare name. Also add a test where the name and the email local part differ.

   Fix (Use the email as the principal only when it resolves back to the same username):
   private Map<String, String> identityClaims(String userName, String email) {
     final boolean isEmailConsistent =
         !nullOrEmpty(email)
             && email.split("@")[0].equalsIgnoreCase(userName);
     final String principal = isEmailConsistent ? email : userName;
     ...

Review coverage

📋 Rules No rules evaluated

🧪 Functional validation Not enabled · Set up

🤖 Auto-approval Not enabled · Set up

Options

Display: compact → Counting what did not apply, without listing it.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@mohityadav766
mohityadav766 merged commit 0cf5851 into 2.0 Oct 12, 2026
110 of 114 checks passed
@mohityadav766
mohityadav766 deleted the backport/2.0/pr-33725 branch October 12, 2026 13:45

This branch was previously deployed

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

Labels

backend safe to test Add this label to run secure Github workflows on PRs skip-pr-checks Bypass PR metadata validation check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants