[PR #6131] [MERGED] feat(oauth-proxy): stateless mode compatibility #14714

Closed
opened 2026-04-13 09:36:00 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6131
Author: @bytaesu
Created: 11/20/2025
Status: Merged
Merged: 11/22/2025
Merged by: @Bekacru

Base: canaryHead: 2025-11-20/feat/stateless-oauth-proxy


📝 Commits (9)

  • 5ec1ec0 feat: oauth-proxy plugin stateless session mode support
  • 644329c test: add stateless session mode test cases
  • f28a189 refactor: hooks order before first
  • 829c3a4 chore: add comment on magic number
  • 4b844ba Update packages/better-auth/src/plugins/oauth-proxy/oauth-proxy.test.ts
  • 57d6005 fix: from weakmap to middleware context
  • d8bfc92 from redirect to location header
  • c5644ef fix: capture statepackage for type
  • 45611a8 fix: check ctx.body before using it

📊 Changes

4 files changed (+579 additions, -173 deletions)

View changed files

📝 packages/better-auth/src/plugins/oauth-proxy/index.ts (+309 -173)
📝 packages/better-auth/src/plugins/oauth-proxy/oauth-proxy.test.ts (+182 -0)
packages/better-auth/src/plugins/oauth-proxy/types.ts (+23 -0)
packages/better-auth/src/plugins/oauth-proxy/utils.ts (+65 -0)

📄 Description

This PR makes the OAuth Proxy Plugin to work in stateless mode.

  1. Replace the state parameter in the OAuth provider URL with an encrypted package
  2. Decrypt the package to recover the original state and the stateCookie value
  3. Temporarily switch to database mode and inject a virtual adapter so it returns the decrypted state cookie value
  4. Restore the original auth config (storeStateStrategy, skipStateCookieCheck, adapter) in the after-hook

Summary by cubic

Add stateless (cookie-based) support to the OAuth Proxy plugin so cross-origin OAuth works without server-side state. The flow now encrypts the OAuth state and state cookie into the provider URL and restores them safely during the callback.

  • New Features

    • Stateless mode: replace provider URL state with an encrypted package; decrypt on callback and restore the original state.
    • Temporary adapter injection to serve the decrypted state in database mode during callback; original config restored after.
    • Callback URL is proxied during sign-in and unwrapped on same-origin; cross-origin cookies forwarded via encrypted payload.
    • Robust cookie parsing via parseSetCookieHeader and secure cookie handling on HTTPS.
  • Refactors

    • Added utils for current URL resolution and proxy skip checks; introduced types for state package and config snapshots.
    • Reordered hooks: before for callbackURL rewrite/state handling, after for provider URL mutation, redirects, and config restore.
    • Expanded tests covering stateless flows and same-origin behavior.

Written for commit 45611a8cae. Summary will update automatically 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/6131 **Author:** [@bytaesu](https://github.com/bytaesu) **Created:** 11/20/2025 **Status:** ✅ Merged **Merged:** 11/22/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `2025-11-20/feat/stateless-oauth-proxy` --- ### 📝 Commits (9) - [`5ec1ec0`](https://github.com/better-auth/better-auth/commit/5ec1ec030ba78c4df806de851ee89f4eba22e496) feat: oauth-proxy plugin stateless session mode support - [`644329c`](https://github.com/better-auth/better-auth/commit/644329c996ca9e9913d424a9de7ed00332f7068b) test: add stateless session mode test cases - [`f28a189`](https://github.com/better-auth/better-auth/commit/f28a189f6b58f24f8abe9e558231b1f716ce0fd5) refactor: hooks order before first - [`829c3a4`](https://github.com/better-auth/better-auth/commit/829c3a4fe21841d9e434dcdc706b7a00d3ce9efe) chore: add comment on magic number - [`4b844ba`](https://github.com/better-auth/better-auth/commit/4b844ba1b411436db315c4e6ece6916058144664) Update packages/better-auth/src/plugins/oauth-proxy/oauth-proxy.test.ts - [`57d6005`](https://github.com/better-auth/better-auth/commit/57d6005e77ab00b291599e55c6b4e969e5e19ebc) fix: from weakmap to middleware context - [`d8bfc92`](https://github.com/better-auth/better-auth/commit/d8bfc92f4abe732e6f0a8c499a0c8c7b3589deff) from redirect to location header - [`c5644ef`](https://github.com/better-auth/better-auth/commit/c5644ef048b56105e0827283a85a266048d0459d) fix: capture statepackage for type - [`45611a8`](https://github.com/better-auth/better-auth/commit/45611a8caee6e0d9113d67a1df509f9dcb883298) fix: check ctx.body before using it ### 📊 Changes **4 files changed** (+579 additions, -173 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/oauth-proxy/index.ts` (+309 -173) 📝 `packages/better-auth/src/plugins/oauth-proxy/oauth-proxy.test.ts` (+182 -0) ➕ `packages/better-auth/src/plugins/oauth-proxy/types.ts` (+23 -0) ➕ `packages/better-auth/src/plugins/oauth-proxy/utils.ts` (+65 -0) </details> ### 📄 Description This PR makes the OAuth Proxy Plugin to work in stateless mode. 1. Replace the `state` parameter in the OAuth provider URL with an encrypted package 2. Decrypt the package to recover the original state and the stateCookie value 3. Temporarily switch to database mode and inject a virtual adapter so it returns the decrypted state cookie value 4. Restore the original auth config (storeStateStrategy, skipStateCookieCheck, adapter) in the after-hook <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Add stateless (cookie-based) support to the OAuth Proxy plugin so cross-origin OAuth works without server-side state. The flow now encrypts the OAuth state and state cookie into the provider URL and restores them safely during the callback. - **New Features** - Stateless mode: replace provider URL state with an encrypted package; decrypt on callback and restore the original state. - Temporary adapter injection to serve the decrypted state in database mode during callback; original config restored after. - Callback URL is proxied during sign-in and unwrapped on same-origin; cross-origin cookies forwarded via encrypted payload. - Robust cookie parsing via parseSetCookieHeader and secure cookie handling on HTTPS. - **Refactors** - Added utils for current URL resolution and proxy skip checks; introduced types for state package and config snapshots. - Reordered hooks: before for callbackURL rewrite/state handling, after for provider URL mutation, redirects, and config restore. - Expanded tests covering stateless flows and same-origin behavior. <sup>Written for commit 45611a8caee6e0d9113d67a1df509f9dcb883298. Summary will update automatically 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 09:36:00 -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#14714