fix: map email from preferred_username/upn for OIDC providers like En… - #4073
fix: map email from preferred_username/upn for OIDC providers like En…#4073Vansh98789 wants to merge 3 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe OIDC controller now extracts email values from standard and fallback claims in ID-token claims and userinfo. The user profile uses this helper. Tests cover precedence, arrays, Entra-specific claims, fallbacks, and missing values. ChangesOIDC email extraction
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@npm/src/controller/utils.ts`:
- Around line 277-291: Update extractOIDCEmail so each source’s email value is
normalized by selecting its first non-empty string, handling arrays before
falling back to userinfo. Preserve ID-token precedence for both scalar and array
claims, skip empty array entries, and add regression tests covering array
precedence and empty-first-entry behavior.
In `@npm/test/sso/extract_oidc_email.test.ts`:
- Around line 4-6: Remove the tap.teardown callback that calls process.exit(0)
in the test file. Allow Tap to finalize assertions, summaries, and failures
normally; do not replace it with another forced process exit.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c9d1bc56-803c-4a12-b45c-461c2f3e1bb8
📒 Files selected for processing (2)
npm/src/controller/utils.tsnpm/test/sso/extract_oidc_email.test.ts
|
Thanks @Vansh98789, the team will review your PR soon. |
Fixes #3916
This PR fixes the missing
emailattribute in the/api/oauth/userinfoendpoint for OIDC providers like Microsoft Entra ID (Azure AD).Root cause.
extractOIDCUserProfileinnpm/src/controller/utils.tsonly mapped the email fromidTokenClaims.emailoruserinfo.email:This line had three problems:
No fallback to a usable claim — Microsoft Entra ID often does not populate the standard
emailclaim at all. The email lives inpreferred_usernameorupn, which the old code never looked at, so the email stayedundefined.Blind fallback — when
idTokenClaims.emailwas not a string, it unconditionally useduserinfo.email, even if that was also missing (undefined) or an array (Entra sendsemailas["a@x.com", "b@x.com"]when multiple emails exist).No type guard on the fallback —
userinfo.emailcould beundefined,null, or an array and was assigned toprofile.claims.emailanyway.Fix. Added an
extractOIDCEmailhelper and wiredextractOIDCUserProfileto use it:
Priority | Check | Covers -- | -- | -- 1 | idTokenClaims.email (string, non-empty) | Standard OIDC (Google, Okta, etc.) 2 | userinfo.email (string, non-empty) | Standard OIDC 3 | email as array → take [0] | Entra ID multiple emails 4 | preferred_username (ID token, then userinfo) | Entra ID common case 5 | upn (ID token, then userinfo) | Entra ID last resort 6 | undefined | Nothing foundextractOIDCEmailtakes both sources and walks a priority ladder, returning the first value that is actually usable:To reproduce locally:
New unit tests
Checklist
My code follows the style guidelines of this project
I have performed a self-review of my own code and corrected any misspellings
I have commented my code, particularly in hard-to-understand areas
I have made corresponding changes to the documentation
My changes generate no new warnings
I have added tests that prove my fix is effective or that my feature works
New and existing unit tests pass locally with my changes
Summary by CodeRabbit
Bug Fixes
Tests