[PR #7865] [MERGED] fix(oauth-provider): customIdTokenClaims should override standard claims #15845

Closed
opened 2026-04-13 10:15:40 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7865
Author: @gustavovalverde
Created: 2/8/2026
Status: Merged
Merged: 3/3/2026
Merged by: @himself65

Base: canaryHead: fix/id-token-custom-claims-overwrite


📝 Commits (3)

  • ff63ffa fix(oauth-provider): customIdTokenClaims should override standard claims
  • a6e52e4 test(oauth-provider): improve test assertions for customIdTokenClaims
  • 3f64dce Merge branch 'canary' into fix/id-token-custom-claims-overwrite

📊 Changes

2 files changed (+138 additions, -1 deletions)

View changed files

📝 packages/oauth-provider/src/token.test.ts (+137 -0)
📝 packages/oauth-provider/src/token.ts (+1 -1)

📄 Description

Summary

customIdTokenClaims values are overwritten by standard userNormalClaims in the id_token payload due to incorrect spread order in createIdToken().

The userInfoEndpoint already has the correct order ({ ...baseUserClaims, ...additionalInfoUserClaims }), but createIdToken() had them reversed ({ ...customClaims, ...userClaims }), making custom claims ineffective when they overlap with standard profile claims like given_name or family_name.

Fix

Swap the spread order in createIdToken() so custom claims take precedence over auto-derived standard claims, matching the existing userInfoEndpoint behavior.

Test

Added a test that configures customIdTokenClaims returning custom given_name, family_name, and custom_field, then verifies the id_token contains the custom values rather than the auto-derived ones.

Fixes #7864


Summary by cubic

Fixes id_token claim precedence so customIdTokenClaims override standard profile claims. Matches userInfo endpoint behavior.

  • Bug Fixes
    • Build id_token payload as {...userClaims, ...customClaims} so custom claims win (e.g., given_name, family_name).
    • Expanded tests: verify override and keep name/sub intact; add assertions for client_secret, redirect_uris, redirect URL, and auth code.

Written for commit 3f64dce12c. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7865 **Author:** [@gustavovalverde](https://github.com/gustavovalverde) **Created:** 2/8/2026 **Status:** ✅ Merged **Merged:** 3/3/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/id-token-custom-claims-overwrite` --- ### 📝 Commits (3) - [`ff63ffa`](https://github.com/better-auth/better-auth/commit/ff63ffa4b5263426268f294263b6e280e37a8430) fix(oauth-provider): customIdTokenClaims should override standard claims - [`a6e52e4`](https://github.com/better-auth/better-auth/commit/a6e52e472ed769ffe56efa403bea4fad790e8f24) test(oauth-provider): improve test assertions for customIdTokenClaims - [`3f64dce`](https://github.com/better-auth/better-auth/commit/3f64dce12cc911ded4a9536dafadb2af519377d8) Merge branch 'canary' into fix/id-token-custom-claims-overwrite ### 📊 Changes **2 files changed** (+138 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/oauth-provider/src/token.test.ts` (+137 -0) 📝 `packages/oauth-provider/src/token.ts` (+1 -1) </details> ### 📄 Description ## Summary `customIdTokenClaims` values are overwritten by standard `userNormalClaims` in the id_token payload due to incorrect spread order in `createIdToken()`. The `userInfoEndpoint` already has the correct order (`{ ...baseUserClaims, ...additionalInfoUserClaims }`), but `createIdToken()` had them reversed (`{ ...customClaims, ...userClaims }`), making custom claims ineffective when they overlap with standard profile claims like `given_name` or `family_name`. ## Fix Swap the spread order in `createIdToken()` so custom claims take precedence over auto-derived standard claims, matching the existing `userInfoEndpoint` behavior. ## Test Added a test that configures `customIdTokenClaims` returning custom `given_name`, `family_name`, and `custom_field`, then verifies the id_token contains the custom values rather than the auto-derived ones. Fixes #7864 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes id_token claim precedence so customIdTokenClaims override standard profile claims. Matches userInfo endpoint behavior. - **Bug Fixes** - Build id_token payload as {...userClaims, ...customClaims} so custom claims win (e.g., given_name, family_name). - Expanded tests: verify override and keep name/sub intact; add assertions for client_secret, redirect_uris, redirect URL, and auth code. <sup>Written for commit 3f64dce12cc911ded4a9536dafadb2af519377d8. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:15:40 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#15845