[PR #9118] [MERGED] feat(oauth-provider): add customTokenResponseFields and harden authorization code validation #16696

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/9118
Author: @gustavovalverde
Created: 4/11/2026
Status: Merged
Merged: 4/11/2026
Merged by: @gustavovalverde

Base: mainHead: feat/oauth-provider-token-refactor


📝 Commits (4)

  • 1e4fab0 feat(oauth-provider): customTokenResponseFields, Zod validation, createUserTokens refactor
  • e4e71ae chore: changeset to patch (no breaking/behavioral changes)
  • 1b92587 fix(oauth-provider): call customTokenResponseFields before token side effects, add docs
  • 8bbe740 fix(oauth-provider): make oauthAuthorizationQuerySchema non-exported (knip)

📊 Changes

6 files changed (+490 additions, -131 deletions)

View changed files

.changeset/oauth-provider-token-refactor.md (+9 -0)
📝 docs/content/docs/plugins/oauth-provider.mdx (+18 -0)
📝 packages/oauth-provider/src/token.test.ts (+303 -3)
📝 packages/oauth-provider/src/token.ts (+88 -128)
📝 packages/oauth-provider/src/types/index.ts (+31 -0)
📝 packages/oauth-provider/src/types/zod.ts (+41 -0)

📄 Description

Summary

Closes #8635 (auth_session passthrough for first-party app step-up).

Instead of hardcoding auth_session into the verification value and threading it through positional parameters, this PR builds the proper foundation:

  • Add customTokenResponseFields callback to OAuthOptions for injecting custom fields into token endpoint responses across all grant types. Standard OAuth fields cannot be overridden. Follows the same pattern as customAccessTokenClaims and customIdTokenClaims.
  • Refactor createUserTokens from 10 positional params to a typed CreateUserTokensParams options object. Make user optional to unify handleClientCredentialsGrant through the same code path (eliminating 75 lines of duplicated token response construction).
  • Replace raw JSON.parse + type cast in checkVerificationValue with Zod runtime validation. Invalid JSON and malformed verification values now consistently return invalid_verification errors instead of 500s.

Summary by cubic

Adds customTokenResponseFields to OAuthOptions to inject custom fields into token responses for all grant types; the callback runs before any token creation/rotation to avoid partial state. Authorization code verification now uses Zod validation, and token creation is refactored to a typed options object that also handles client_credentials.

  • New Features

    • customTokenResponseFields adds fields to token responses for all grant types; standard fields cannot be overridden.
    • Callback runs before token creation/rotation to prevent partial state on error.
  • Refactors

    • Replaced positional params with CreateUserTokensParams; user is optional so client_credentials uses the same builder.
    • Zod-based verificationValueSchema for authorization code values; malformed data returns invalid_verification instead of 500.
    • Token logic adapts to context: uses m2mAccessTokenExpiresIn for M2M; only issues ID/refresh tokens when a user is present and scopes allow.
    • Made oauthAuthorizationQuerySchema internal (non-exported).

Written for commit 8bbe7406b3. 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/9118 **Author:** [@gustavovalverde](https://github.com/gustavovalverde) **Created:** 4/11/2026 **Status:** ✅ Merged **Merged:** 4/11/2026 **Merged by:** [@gustavovalverde](https://github.com/gustavovalverde) **Base:** `main` ← **Head:** `feat/oauth-provider-token-refactor` --- ### 📝 Commits (4) - [`1e4fab0`](https://github.com/better-auth/better-auth/commit/1e4fab02defc3d2cbd31b64a578feabd8ff704b0) feat(oauth-provider): customTokenResponseFields, Zod validation, createUserTokens refactor - [`e4e71ae`](https://github.com/better-auth/better-auth/commit/e4e71aef9b89195a0c3e7d39454c8e189ba794f2) chore: changeset to patch (no breaking/behavioral changes) - [`1b92587`](https://github.com/better-auth/better-auth/commit/1b9258758c6255d9e421dee703c768b09500fd6f) fix(oauth-provider): call customTokenResponseFields before token side effects, add docs - [`8bbe740`](https://github.com/better-auth/better-auth/commit/8bbe7406b34b7e256fd59132fbdcb8c859e2fff6) fix(oauth-provider): make oauthAuthorizationQuerySchema non-exported (knip) ### 📊 Changes **6 files changed** (+490 additions, -131 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/oauth-provider-token-refactor.md` (+9 -0) 📝 `docs/content/docs/plugins/oauth-provider.mdx` (+18 -0) 📝 `packages/oauth-provider/src/token.test.ts` (+303 -3) 📝 `packages/oauth-provider/src/token.ts` (+88 -128) 📝 `packages/oauth-provider/src/types/index.ts` (+31 -0) 📝 `packages/oauth-provider/src/types/zod.ts` (+41 -0) </details> ### 📄 Description ## Summary Closes #8635 (auth_session passthrough for first-party app step-up). Instead of hardcoding `auth_session` into the verification value and threading it through positional parameters, this PR builds the proper foundation: - Add `customTokenResponseFields` callback to `OAuthOptions` for injecting custom fields into token endpoint responses across all grant types. Standard OAuth fields cannot be overridden. Follows the same pattern as `customAccessTokenClaims` and `customIdTokenClaims`. - Refactor `createUserTokens` from 10 positional params to a typed `CreateUserTokensParams` options object. Make `user` optional to unify `handleClientCredentialsGrant` through the same code path (eliminating 75 lines of duplicated token response construction). - Replace raw `JSON.parse` + type cast in `checkVerificationValue` with Zod runtime validation. Invalid JSON and malformed verification values now consistently return `invalid_verification` errors instead of 500s. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds `customTokenResponseFields` to `OAuthOptions` to inject custom fields into token responses for all grant types; the callback runs before any token creation/rotation to avoid partial state. Authorization code verification now uses Zod validation, and token creation is refactored to a typed options object that also handles `client_credentials`. - **New Features** - `customTokenResponseFields` adds fields to token responses for all grant types; standard fields cannot be overridden. - Callback runs before token creation/rotation to prevent partial state on error. - **Refactors** - Replaced positional params with `CreateUserTokensParams`; `user` is optional so `client_credentials` uses the same builder. - Zod-based `verificationValueSchema` for authorization code values; malformed data returns `invalid_verification` instead of 500. - Token logic adapts to context: uses `m2mAccessTokenExpiresIn` for M2M; only issues ID/refresh tokens when a user is present and scopes allow. - Made `oauthAuthorizationQuerySchema` internal (non-exported). <sup>Written for commit 8bbe7406b34b7e256fd59132fbdcb8c859e2fff6. 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:38:54 -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#16696