[PR #7382] [MERGED] refactor(oauth-provider): usage of ctx headers for endpoints #7272

Closed
opened 2026-03-13 13:30:20 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7382
Author: @felipeva
Created: 1/15/2026
Status: Merged
Merged: 1/15/2026
Merged by: @himself65

Base: canaryHead: refactor/oauth-client-header-usage


📝 Commits (3)

  • 8c6ad06 chore(oauth-provider): using ctx.headers instead of request.headers
  • 8de5276 chore(oauth-provider): formatting
  • 6d4d870 chore(oauth-provider): renaming test

📊 Changes

2 files changed (+13 additions, -13 deletions)

View changed files

📝 packages/oauth-provider/src/oauthClient/endpoints.ts (+10 -10)
📝 packages/oauth-provider/src/register.test.ts (+3 -3)

📄 Description

This PR updates the oauth-provider endpoints to use ctx.headers instead of ctx.request.headers, ensuring consistency with how session validation is handled throughout the authentication flow.

Some background:

While implementing the OAuth plugin, I discovered an issue with the documented approach for calling OAuth admin endpoints. Following the example from the documentation for updating an OAuth client:

await auth.api.adminUpdateOAuthClient({
  headers,
  body: {
    redirect_uris: [redirectUri],
    client_secret_expires_at: 0, 
    skip_consent: true, 
    enable_end_session: true, 
  }
});

This approach consistently throws a BAD_REQUEST error.

The root cause is that the endpoint validates the presence of a request object on the context:

if (!ctx.request) throw new APIError("BAD_REQUEST");

When headers are passed as a standalone object (as shown in the documentation), no request object exists on the context. Meanwhile, all session validation in these endpoints happens through getSessionFromCtx, which reads from ctx.headers directly rather than from ctx.request.headers.

Since session validation already operates on ctx.headers and this is how the context is populated when headers are provided directly, we can safely standardize on using ctx.headers throughout the oauth-provider endpoints. This eliminates the dependency on the request object and aligns the implementation with the documented API.


Also, fixed the test assertion for "should register client with metadata and other extra fields." The test was checking for properties that Zod's parse() method strips from the result, as they're not defined in the schema.


Summary by cubic

Standardized OAuth provider admin endpoints to use ctx.headers instead of ctx.request.headers. This aligns with session validation and fixes BAD_REQUEST errors when calling endpoints with a headers object.

  • Refactors

    • Read headers from ctx.headers for get, list, delete, update, and rotate client endpoints.
    • Pass ctx.headers to clientPrivileges.
    • Validate presence of ctx.headers (no request object required).
  • Bug Fixes

    • Updated register test: metadata fields are surfaced; extra fields not in the schema are stripped.

Written for commit 6d4d87024f. 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/7382 **Author:** [@felipeva](https://github.com/felipeva) **Created:** 1/15/2026 **Status:** ✅ Merged **Merged:** 1/15/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `refactor/oauth-client-header-usage` --- ### 📝 Commits (3) - [`8c6ad06`](https://github.com/better-auth/better-auth/commit/8c6ad06bc039979aaff5a0a1a51c2d595123299c) chore(oauth-provider): using ctx.headers instead of request.headers - [`8de5276`](https://github.com/better-auth/better-auth/commit/8de52763e269fc0a26f13d1ce9f27cf23a5807d7) chore(oauth-provider): formatting - [`6d4d870`](https://github.com/better-auth/better-auth/commit/6d4d87024f3f92b2133fbb6a3a3c2760ed37f1b7) chore(oauth-provider): renaming test ### 📊 Changes **2 files changed** (+13 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `packages/oauth-provider/src/oauthClient/endpoints.ts` (+10 -10) 📝 `packages/oauth-provider/src/register.test.ts` (+3 -3) </details> ### 📄 Description This PR updates the oauth-provider endpoints to use `ctx.headers` instead of `ctx.request.headers`, ensuring consistency with how session validation is handled throughout the authentication flow. ### Some background: While implementing the OAuth plugin, I discovered an issue with the documented approach for calling OAuth admin endpoints. Following the example from the documentation for [updating an OAuth client](https://www.better-auth.com/docs/plugins/oauth-provider): ```ts await auth.api.adminUpdateOAuthClient({ headers, body: { redirect_uris: [redirectUri], client_secret_expires_at: 0, skip_consent: true, enable_end_session: true, } }); ``` This approach consistently throws a `BAD_REQUEST` error. The root cause is that the endpoint validates the presence of a `request` object on the context: ```ts if (!ctx.request) throw new APIError("BAD_REQUEST"); ``` When headers are passed as a standalone object (as shown in the documentation), no `request` object exists on the context. Meanwhile, all session validation in these endpoints happens through `getSessionFromCtx`, which reads from `ctx.headers` directly rather than from `ctx.request.headers`. Since session validation already operates on `ctx.headers` and this is how the context is populated when headers are provided directly, we can safely standardize on using `ctx.headers` throughout the oauth-provider endpoints. This eliminates the dependency on the `request` object and aligns the implementation with the documented API. --- Also, fixed the test assertion for "should register client with metadata and other extra fields." The test was checking for properties that Zod's `parse()` method strips from the result, as they're not defined in the schema. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Standardized OAuth provider admin endpoints to use ctx.headers instead of ctx.request.headers. This aligns with session validation and fixes BAD_REQUEST errors when calling endpoints with a headers object. - **Refactors** - Read headers from ctx.headers for get, list, delete, update, and rotate client endpoints. - Pass ctx.headers to clientPrivileges. - Validate presence of ctx.headers (no request object required). - **Bug Fixes** - Updated register test: metadata fields are surfaced; extra fields not in the schema are stripped. <sup>Written for commit 6d4d87024f3f92b2133fbb6a3a3c2760ed37f1b7. 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-03-13 13:30:20 -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#7272