Skip to content

Run an OpenID Connect provider on Better Auth

How Ostiary turns Better Auth's OAuth provider plugin into a self-hosted OpenID Connect server: discovery, clients, API scopes, signing keys, device flow.

Published on 7 min read

Better Auth is a TypeScript authentication framework that you usually embed in one application. Its OAuth provider plugin can also turn a Better Auth server into an authorization server that other applications sign in with. That is what Ostiary is: a Next.js app built on Better Auth and @better-auth/oauth-provider (version 1.7.7 in the repository today), set up as a standalone OpenID Connect provider with an admin console around it.

This post walks through that provider from the outside: what your apps need to connect, how tokens for your own APIs work, how signing keys rotate, and which parts Ostiary adds on top of the plugin.

Why a separate identity provider

When Better Auth lives inside an app, that app owns its users table and its sign-in pages. Ship a second app and you either duplicate both or share a database between them. An OpenID Connect provider takes the other route: one service holds the accounts and the sign-in screens, and every app delegates to it over a standard protocol. People sign in once and move between your apps, which is single sign-on.

Ostiary runs as its own deployment (apps/auth), with an optional admin console as a second app (apps/admin), both on one Postgres database. Your apps never import Ostiary code. They speak OpenID Connect Core 1.0 on top of OAuth 2.0 (RFC 6749). Ostiary describes itself as an OAuth 2.1 and OpenID Connect provider; OAuth 2.1 is an IETF draft that consolidates OAuth 2.0 and the security guidance published since.

The issuer and the discovery document

The issuer is the auth app's URL followed by /api/auth, for example https://auth.example.com/api/auth. As OpenID Connect Discovery 1.0 defines, the provider publishes its configuration under the issuer:

https://auth.example.com/api/auth/.well-known/openid-configuration

That document lists the authorization, token and UserInfo endpoints, the jwks_uri with the public signing keys, and the supported scopes and grant types. Most OIDC libraries only need the issuer: they fetch the document and configure themselves. Clients that read OAuth authorization server metadata (RFC 8414) instead, such as MCP clients, find it at https://auth.example.com/.well-known/oauth-authorization-server/api/auth.

Register an application and connect it

In the admin console, Applications registers an OAuth client: its name, its redirect URIs, and a client ID and secret. Only platform admins manage clients, and only from the console; Better Auth's own client endpoints are closed to everyone else. Native apps and CLIs that cannot keep a secret are registered as public clients and rely on PKCE.

There is no SDK to learn. With Auth.js in a Next.js app, the whole integration is one provider entry:

import NextAuth from "next-auth";

export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [
    {
      id: "ostiary",
      name: "Ostiary",
      type: "oidc",
      issuer: "https://auth.example.com/api/auth",
      clientId: process.env.OSTIARY_CLIENT_ID,
      clientSecret: process.env.OSTIARY_CLIENT_SECRET,
    },
  ],
});

The same three values (issuer, client ID, client secret) work with Better Auth's generic OAuth plugin, with openid-client on Node.js, or with AppAuth on iOS and Android. The Connect section of the home page shows the steps, and the connect guide covers every option.

Tokens for your own APIs

Signing people in is half of the job. The other half is letting apps call your APIs on their behalf. In Ostiary you register each API under APIs in the admin console, with an identifier (usually its URL) and the scopes clients may request for it. A client asks for a token for that API with the resource parameter of RFC 8707; the token's audience is then your API, which verifies the JWT against Ostiary's public keys. Access tokens carry the granted scopes and the user's role as a claim, and a token for one API never carries another API's scopes.

Each API has its own settings in the console:

  • Applications: every application can get tokens for an API by default. Choose Only linked applications to allow just the ones you tick; the others get invalid_target. Linked applications can also introspect the API's tokens.
  • Token lifetimes: shorter access and refresh token lifetimes than the defaults of 1 hour and 30 days.
  • Custom claims: a JSON object added to the API's tokens, for example {"tenant": "acme"}.
  • DPoP: require tokens bound to a key with DPoP (RFC 9449); the API must then check the DPoP proof.

New APIs and scopes reach the auth server within a minute, without a redeploy. To provision an instance from configuration instead, OAUTH_API_AUDIENCES and OAUTH_API_SCOPES declare them from the environment. Services that call an API on their own behalf use the client credentials grant, with scopes per client. How to protect an API with JWT access tokens walks through verifying these tokens in your API.

Signing keys, JWKS and rotation

ID tokens and JWT access tokens are signed with an Ed25519 key (the EdDSA algorithm of RFC 8037). The public keys are published as a JSON Web Key Set (RFC 7517) at https://auth.example.com/api/auth/jwks, the jwks_uri of the discovery document. Private keys are stored encrypted with BETTER_AUTH_SECRET, and the console never shows them.

The Signing keys page lists each key with its ID, algorithm, dates and status (current, still published for verification, or expired), and offers three controls:

  • Rotate now creates a key that signs every token from then on. The previous key stays in the JWKS for the grace period, so the tokens it signed keep verifying until they expire.
  • Automatic rotation, off by default, replaces the key every 30, 90, 180 or 365 days.
  • Grace period, 30 days by default, is how long a retired key stays published. Keep it longer than your longest token lifetime: 1 hour for access tokens, 10 hours for ID tokens.

Your apps need nothing special, as long as they pick keys by kid and fetch the JWKS again when they meet an unknown one. jose's createRemoteJWKSet, openid-client, Auth.js and Better Auth all do. Only apps that pin a single public key must be updated after a rotation.

Device sign-in for CLIs and TVs

Some clients cannot open a browser. For them Ostiary implements the device authorization grant (RFC 8628). The device shows a short code such as WDJB-MJHT; the user opens /device on Ostiary in a browser where they are signed in, checks the app and its scopes, and approves, while the device polls the token endpoint. You enable it per application with Device sign-in, for clients registered in the console, and the discovery document then lists the device_authorization_endpoint. The user always approves on that page, even for clients set to skip consent.

When an app asks for access, people see Ostiary's consent screen before they are sent back. Applications you register yourself can be set to Skip consent; clients that registered themselves never can. The built-in OpenID Connect scopes are openid, profile, email and offline_access (for refresh tokens), and the UserInfo endpoint returns the claims those scopes allow. Token introspection (RFC 7662) lets an API check a token on the server side; in Ostiary, an API's linked applications may introspect its tokens.

On their account dashboard, people see their connected applications, with each app's icon, its permissions in plain words, when they connected and when they last used it. Disconnect removes the consent and revokes their tokens for that app.

What Ostiary adds on top of the plugin

The protocol endpoints come from Better Auth's OAuth provider plugin. Around it, the Ostiary repository adds:

  • an admin console for applications, APIs, scopes, consents and signing keys, with settings stored in the database that reach the auth server within a minute;
  • sign-in, consent, account selection and device pages in 20 languages, in light and dark themes, with optional branding per application;
  • rate limits per client IP on sign-in, codes, password reset and the token endpoint, counted in Postgres so they hold on serverless hosts where each instance has its own memory;
  • an App usage page (tokens, users and consents per app over 30 days) and an audit log of admin actions;
  • limits on clients that register themselves, covered in OAuth for MCP clients and AI agents.

For the wider picture (passkeys, enterprise SSO, organizations), start with Ostiary as a self-hosted, open-source Auth0 alternative. For the sign-in side, passkeys in a self-hosted identity server explains how WebAuthn fits in.

Try it

The live demo shows the sign-in screens. To run your own provider, follow the deployment guide or the Deploy section, register an application, give your OIDC library the issuer, and your first app signs in through Ostiary.

Related articles

Ostiary is an open-source Auth0 alternative you host yourself, built on Better Auth: an OpenID Connect provider, passkeys, SSO and an admin console.
Register an API in Ostiary, request tokens with the resource parameter, and verify the JWT in your API: JWKS, audience, scopes, introspection and DPoP.
Move users from Auth0 to Ostiary with a dry run first. Imported bcrypt hashes keep working and become scrypt at sign-in, with no password reset.