Repository navigation
Sign in with your own OpenID Connect provider when self-hosting - #451
Merged
abhishekgahlot2 merged 2 commits intoNov 22, 2026
Merged
Conversation
A self-hosted install could offer only passwords and Google. Companies
that run Okta, Microsoft Entra ID, Keycloak or Authentik want people to
sign in with that instead.
Backend:
- OIDC_ISSUER, OIDC_CLIENT_ID and OIDC_CLIENT_SECRET (env.ts
oidcAuthConfig) turn on better-auth's generic OAuth provider under the
id "oidc", with the endpoints read from the issuer's discovery
document, the openid, email and profile scopes, and PKCE. All three or
none: a partial set, or an issuer that is not an http(s) URL, warns
and stays off, like the Google settings. OIDC_BUTTON_LABEL names the
button (default "SSO"). Nothing changes when they are absent.
- New accounts go through the same user-create hook as Google: an
invited address needs email_verified to be literally true, anyone
else answers to the sign-up policy. Linking an existing account needs
a verified address too. The display name falls back from name to the
given and family names, the username, then the address, since the
library refuses a sign-in without one.
- GET /api/auth/provider-config answers oidc: { label } or null; the
issuer and client never leave the server.
- canSignIn and the Slack welcome mail now count any provider that can
sign an existing address in (providerSignInEnabled), not only Google.
Frontend:
- The sign-in card shows "Sign in with <label>" above the password
form, next to Google when both are set. The two buttons share one
ProviderSignInButton. The desktop sign-in page hands off through the
same provider (genericOAuthClient on the desktop auth client).
- A failed Google or OpenID Connect sign-in in the browser now returns
to /login?via=provider instead of the library's own error page, and
the card says why: no account was created for that address, the
address was not confirmed for an existing account, or a generic retry
line. Only known codes are mapped; the query text is never shown.
Docs: a "Single sign-on with OpenID Connect" section on the auth page
with a Keycloak setup, the settings in deploy/compose/env.example,
compose.local.yaml and the backend README.
Fixes useagenthq#96
|
Thanks @officialasishkumar, merged. We added one commit on top: sign-up through a provider now needs an email the provider has verified, so an unverified claim can never hold a sign-in on someone else's address. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.
Fixes #96
A self-hosted install could offer passwords and Google, nothing else. This adds sign-in through the deployment's own OpenID Connect provider (Okta, Microsoft Entra ID, Keycloak, Authentik), set up from environment variables, with the same new-account rules as Google.
Settings
OIDC_ISSUER<OIDC_ISSUER>/.well-known/openid-configuration.OIDC_CLIENT_IDOIDC_CLIENT_SECRETOIDC_BUTTON_LABEL<label>"; default "SSO".<BETTER_AUTH_URL>/api/auth/oauth2/callback/oidc./api/auth/sign-in/oauth2answers 404.What changed
Backend
env.ts:oidcAuthConfigreads the settings. The discovery URL drops a trailing slash before appending the well-known path, so an issuer with a path such as Keycloak's/realms/acmekeeps it.OIDC_PROVIDER_IDis"oidc".providerSignInEnabledis Google or OpenID Connect.auth.ts: when OpenID Connect is configured, it registers better-auth'sgenericOAuthplugin with:openid email profilescopes and PKCEmapProfileToUserwith two jobs:email_verified: truemarks the address verified. A missing claim or the string"true"does not.nameto the given and family names, thenpreferred_username, then the address. Many providers sendnameonly once a person has filled in their names, and the library refuses a sign-in without one.user.create.beforehook, so the rules match Google's:signupRefusal: closed, orSIGNUP_OPENnarrowed by domain. A provider sign-in carries no invite code.GET /api/auth/provider-config: answersoidc: { label }ornull. The issuer and client stay on the server.canSignIn(which decides whether an invitation can ever be used) and the Slack welcome mail checkedgoogleAuthEnabled()only. They now useproviderSignInEnabled(), because an OpenID Connect sign-in links an existing address the same way.NO_WAY_INnames both providers.Frontend
Sign-in card (
auth-form.tsx): shows Sign in with<label>above the password form, next to Continue with Google when both are set. The "or" divider and "Sign-in is unavailable" now account for it.Buttons: both use one new
ProviderSignInButton.GoogleSignInButtonkeeps its props and markup.lib/auth.ts:getAuthConfigdecodesoidc(a missing or blank label reads as off), andsignInWithOidcposts to/api/auth/sign-in/oauth2.Desktop: the desktop sign-in page hands off through the same provider. The desktop auth client gains
genericOAuthClient(), and the Electron plugin already handles/sign-in/oauth2and/oauth2/callback/*.Refused sign-ins (Google too): a refused Google or OpenID Connect sign-in used to end on better-auth's error page, which redirected to
/?error=...and from there to/loginwith no explanation. Both web buttons now senderrorCallbackURL=/login?via=provider(keepingredirect_url). The card then maps the library's code to plain words:Only known codes are mapped; the query text is never shown.
Docs:
docs-site/docs/platform/auth-and-orgs.mdx, with the Keycloak setup I ran below. The section also corrects the route that advertises sign-in methods: it is/api/auth/provider-config, not/api/config.deploy/compose/env.example,compose.local.yamlandbackend/README.md.Across a release: there is no schema change.
oidcfield.oidcas off and shows no button.providerId: "oidc".Tests
backend/test/oidc-auth.test.tsdrives the real better-auth routes and database. A test signs in at/api/auth/sign-in/oauth2, then follows the provider's redirect back to/api/auth/oauth2/callback/oidcwith the state cookie and a code. Only the identity provider is fake, stubbed atfetch(discovery and token endpoints), so nothing reaches the network. It covers:ftp:, a query, a fragment) leave it offprovider-configanswersoidc: { label: "Acme SSO" }without the secret or issuer, andnullonce the settings are gone/sign-in/oauth2route (404)openid email profilescope, an S256 code challenge and the callback redirect URI, and the code is exchanged with a PKCE verifier.errorCallbackURLset, the refusal lands on/login?via=provider&error=Account_creation_is_disabledpreferred_username), anoidcaccount row, a session and its own workspaceemail_verified: false,"true"or a missing claimSIGNUP_ALLOWED_DOMAINSand admits one inside itcanSignInadmits an invitation to a password-less address only while OpenID Connect is onFrontend:
signup-policy.test.ts: the card with OpenID Connect only, with both providers, with no password form, and with no provider. The sign-up card stays password-only.oidc-sign-in-button.test.tsx: the button's label.lib/auth.test.ts: decodingoidc, andproviderErrorURL.verification-notice.test.ts: the provider notices, a craftederrornever echoed, anderrorwithoutviastill meaning a confirmation link.desktop-auth/page.test.ts: the desktop hand-off sendsproviderId: "oidc"with the desktop query and reports a refused start.Checks with Bun 1.3.14, backend tests on
pgvector/pgvector:0.8.6-pg16-bookworm:bun run typecheck(root): passescd backend && bun run test: 3623 pass, 28 fail. The same 28 tests fail on an unmodifiedmaincheckout on my machine: Codex runtime and sandbox config, T3 provider bridge, andrepo-prep, all spawning local processes. Then./test/slack.test.ts: 119 pass, 0 fail.cd frontend && bun run test: 2017 pass, 0 failcd frontend && bun run lint: 248 warnings and 22 infos, the same asmaincd frontend && bun run build: compiles, type-checks and generates every page. My machine has no Node, so Bun ran Next and crashed on exit after writing the output. Thenext startrun below served that build.cd docs-site && bunx blume validate: no broken linksbun test scripts/agent-docs.test.ts: 5 passEnd to end
I ran Keycloak 26.4 in Docker (
start-dev) with a realmacme, a confidential clientuseagentand two users with verified addresses,adaandgrace. Against it I ran a real backend withNODE_ENV=productionon its own Postgres and a production build of the frontend (next start), withOIDC_ISSUER=http://localhost:18080/realms/acmeandOIDC_BUTTON_LABEL="Acme Keycloak". The deployment was closed, and one workspace had a pending invitation forgrace@acme.test. I drove it with Playwright:/api/auth/provider-configanswered"oidc":{"label":"Acme Keycloak"}./loginshowed Sign in with Acme Keycloak above "or" and the email form./realms/acme/protocol/openid-connect/auth./login?via=provider&error=Account_creation_is_disabled. The card said "This server did not create an account for that address. Ask a workspace admin to invite it. An invitation needs an address your sign-in provider has confirmed."/agent/new, signed in.get-sessionreturnedgrace@acme.test, "Grace Hopper".email_verified = true, one account withprovider_id = 'oidc', and "Grace Hopper's workspace" with her as owner. The invitation was still pending, to accept from its link, as after a Google sign-in.Not run: the Electron desktop app itself. Its hand-off is covered by the unit test above, and better-auth's Electron plugin already handles the
/sign-in/oauth2and/oauth2/callback/*paths.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.