[PR #8118] [MERGED] fix(sso): ensure consistent OIDC discovery across APIs #24657

Closed
opened 2026-04-15 22:29:57 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8118
Author: @hlhc
Created: 2/23/2026
Status: Merged
Merged: 2/28/2026
Merged by: @Bekacru

Base: canaryHead: fix/sso-discovery


📝 Commits (5)

  • 3d4d92b test(sso): add OIDC SSO tests with defaultSSO array configuration
  • d70a479 fix(sso): ensure consistent OIDC discovery across APIs
  • f658653 test(sso): add tests for ensureRuntimeDiscovery function
  • d6214d8 fix(sso): include authorizationEndpoint check in needsRuntimeDiscovery function
  • 8f92d4e test(sso): add tests for authorizationEndpoint hydration and discovery logic

📊 Changes

5 files changed (+444 additions, -39 deletions)

View changed files

📝 packages/sso/src/oidc.test.ts (+276 -0)
📝 packages/sso/src/oidc/discovery.test.ts (+94 -2)
📝 packages/sso/src/oidc/discovery.ts (+35 -4)
📝 packages/sso/src/oidc/index.ts (+1 -0)
📝 packages/sso/src/routes/sso.ts (+38 -33)

📄 Description

Summary

This PR standardizes OIDC discovery logic across all SSO-related APIs. Previously, some endpoints only updated the authorizationEndpoint during discovery, while others replaced more fields but sometimes omitted the JWKS URL or other endpoints. This led to subtle inconsistencies and potential runtime errors.

With this PR:

  • All APIs now use the same ensureRuntimeDiscovery utility.
  • Discovery always hydrates all relevant OIDC endpoints (authorizationEndpoint, tokenEndpoint, userInfoEndpoint, jwksEndpoint, etc.).
  • Removes duplicated or partial discovery logic.
  • Adds tests for ensureRuntimeDiscovery covering all major scenarios.
  • Adds tests for defaultSSO /w oidcConfig

Why

  • Prevents bugs caused by incomplete or inconsistent OIDC config hydration.
  • Ensures all endpoints behave the same way when provider metadata changes.
  • Fixes my problem with OIDC failing because discovery was not invoked with defaultSSO.

Checklist

  • All affected APIs use ensureRuntimeDiscovery
  • All endpoints hydrate the full OIDC config
  • Tests added for defaultSSO scenarios
  • pnpm typecheck, pnpm lint, and targeted vitest tests pass

Summary by cubic

Standardizes OIDC discovery across all SSO APIs using ensureRuntimeDiscovery, hydrating authorization, token, userinfo, JWKS, and tokenEndpointAuthentication for consistent, reliable flows. Fixes sign-in/callback failures when authorizationEndpoint is missing and makes defaultSSO OIDC flows work predictably.

  • Refactors

    • Added ensureRuntimeDiscovery, exported from OIDC; used in sign-in and callback to merge hydrated endpoints into config.
    • Preserves existing config (clientId, clientSecret, pkce, scopes) and only sets defaults when missing.
  • Bug Fixes

    • needsRuntimeDiscovery now checks authorizationEndpoint alongside tokenEndpoint and jwksEndpoint.
    • Consistent discovery + trusted-origin checks across APIs; DiscoveryError mapped to API errors and callback redirects.
    • Added tests for ensureRuntimeDiscovery, authorizationEndpoint hydration when missing, and defaultSSO flows (providerId, domain match, explicit endpoints skip discovery).

Written for commit 8f92d4e6cd. 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/8118 **Author:** [@hlhc](https://github.com/hlhc) **Created:** 2/23/2026 **Status:** ✅ Merged **Merged:** 2/28/2026 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/sso-discovery` --- ### 📝 Commits (5) - [`3d4d92b`](https://github.com/better-auth/better-auth/commit/3d4d92b1758dd28429e7bb75a9e685730cda3a81) test(sso): add OIDC SSO tests with defaultSSO array configuration - [`d70a479`](https://github.com/better-auth/better-auth/commit/d70a479850f823118309a55bb398994ec2ab2616) fix(sso): ensure consistent OIDC discovery across APIs - [`f658653`](https://github.com/better-auth/better-auth/commit/f6586537b6fc8def98ccc6a25e8d707f4fe33c28) test(sso): add tests for ensureRuntimeDiscovery function - [`d6214d8`](https://github.com/better-auth/better-auth/commit/d6214d87bbc5907f9ca239ed04579bb0639a06d9) fix(sso): include authorizationEndpoint check in needsRuntimeDiscovery function - [`8f92d4e`](https://github.com/better-auth/better-auth/commit/8f92d4e6cd3fb0077b79da883d9cf44d98fb78b2) test(sso): add tests for authorizationEndpoint hydration and discovery logic ### 📊 Changes **5 files changed** (+444 additions, -39 deletions) <details> <summary>View changed files</summary> 📝 `packages/sso/src/oidc.test.ts` (+276 -0) 📝 `packages/sso/src/oidc/discovery.test.ts` (+94 -2) 📝 `packages/sso/src/oidc/discovery.ts` (+35 -4) 📝 `packages/sso/src/oidc/index.ts` (+1 -0) 📝 `packages/sso/src/routes/sso.ts` (+38 -33) </details> ### 📄 Description ## Summary This PR standardizes OIDC discovery logic across all SSO-related APIs. Previously, some endpoints only updated the `authorizationEndpoint` during discovery, while others replaced more fields but sometimes omitted the JWKS URL or other endpoints. This led to subtle inconsistencies and potential runtime errors. ## With this PR: - All APIs now use the same ensureRuntimeDiscovery utility. - Discovery always hydrates all relevant OIDC endpoints (authorizationEndpoint, tokenEndpoint, userInfoEndpoint, jwksEndpoint, etc.). - Removes duplicated or partial discovery logic. - Adds tests for `ensureRuntimeDiscovery` covering all major scenarios. - Adds tests for `defaultSSO` /w `oidcConfig` ## Why - Prevents bugs caused by incomplete or inconsistent OIDC config hydration. - Ensures all endpoints behave the same way when provider metadata changes. - Fixes my problem with OIDC failing because discovery was not invoked with `defaultSSO`. ## Checklist - [x] All affected APIs use ensureRuntimeDiscovery - [x] All endpoints hydrate the full OIDC config - [x] Tests added for defaultSSO scenarios - [x] pnpm typecheck, pnpm lint, and targeted vitest tests pass <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Standardizes OIDC discovery across all SSO APIs using ensureRuntimeDiscovery, hydrating authorization, token, userinfo, JWKS, and tokenEndpointAuthentication for consistent, reliable flows. Fixes sign-in/callback failures when authorizationEndpoint is missing and makes defaultSSO OIDC flows work predictably. - **Refactors** - Added ensureRuntimeDiscovery, exported from OIDC; used in sign-in and callback to merge hydrated endpoints into config. - Preserves existing config (clientId, clientSecret, pkce, scopes) and only sets defaults when missing. - **Bug Fixes** - needsRuntimeDiscovery now checks authorizationEndpoint alongside tokenEndpoint and jwksEndpoint. - Consistent discovery + trusted-origin checks across APIs; DiscoveryError mapped to API errors and callback redirects. - Added tests for ensureRuntimeDiscovery, authorizationEndpoint hydration when missing, and defaultSSO flows (providerId, domain match, explicit endpoints skip discovery). <sup>Written for commit 8f92d4e6cd3fb0077b79da883d9cf44d98fb78b2. 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-15 22:29:57 -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#24657