SAML SSO with Okta

A guide to integrating SAML Single Sign-On (SSO) with Better Auth, featuring Okta

This guide walks you through setting up SAML Single Sign-On (SSO) with your Identity Provider (IdP), using Okta as an example. For every SAML option, see SAML providers and the SSO reference.

What is SAML?

SAML (Security Assertion Markup Language) is an XML-based standard for exchanging authentication and authorization data between an Identity Provider (IdP) (e.g., Okta, Azure AD, OneLogin) and a Service Provider (SP) (in this case, Better Auth).

In this setup:

  • IdP (Okta): Authenticates users and sends assertions about their identity.
  • SP (Better Auth): Validates assertions and logs the user in.up.

Step 1: Create a SAML Application in Okta

  1. Log in to your Okta Admin Console

  2. Navigate to Applications > Applications

  3. Click "Create App Integration"

  4. Select "SAML 2.0" as the Sign-in method

  5. Configure the following settings:

    • Single Sign-on URL: Your Better Auth callback endpoint (e.g., http://localhost:3000/api/auth/sso/saml2/sp/acs/sso). Note: sso is your providerId
    • Audience URI (SP Entity ID): Your Better Auth metadata URL (e.g., http://localhost:3000/api/auth/sso/saml2/sp/metadata)
    • Name ID format: Email Address or any of your choice.
  6. Download the IdP metadata XML file and certificate

IdP-initiated SSO: To let users start from the IdP dashboard, point the Single Sign-on URL at /api/auth/sso/saml2/sp/acs/{providerId} and set saml: { allowIdpInitiated: true } in the sso() options. It is off by default, because anyone with an account at the IdP could then sign a victim into the attacker's account. The setting applies to every SAML provider. Enable it only when the IdP requires it; see IdP-initiated SSO.

Step 2: Configure Better Auth

Here’s an example configuration for Okta in a dev environment:

const ssoConfig = {
  defaultSSO: [{
    domain: "localhost:3000", // Your domain
    providerId: "sso",
    samlConfig: {
      // SP Configuration
      issuer: "http://localhost:3000/api/auth/sso/saml2/sp/metadata",
      entryPoint: "https://trial-1076874.okta.com/app/trial-1076874_samltest_1/exktofb0a62hqLAUL697/sso/saml",
      // IdP Configuration
      idpMetadata: {
        entityID: "https://trial-1076874.okta.com/app/exktofb0a62hqLAUL697/sso/saml/metadata",
        singleSignOnService: [{
          Binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect",
          Location: "https://trial-1076874.okta.com/app/trial-1076874_samltest_1/exktofb0a62hqLAUL697/sso/saml"
        }],
      },
      cert: `-----BEGIN CERTIFICATE-----
MIIDqjCCApKgAwIBAgIGAZhVGMeUMA0GCSqGSIb3DQEBCwUAMIGVMQswCQYDVQQGEwJVUzETMBEG
...
[Your Okta Certificate]
...
-----END CERTIFICATE-----`,
      // Better Auth generates the SP metadata from `issuer` and the ACS URL.
      // If you supply your own `spMetadata.metadata`, its entityID becomes the SP entity ID and the default audience.
    }
  }]
}

Step 3: Multiple Default Providers (Optional)

You can configure multiple SAML providers for different domains:

const ssoConfig = {
  defaultSSO: [
    {
      domain: "company.com",
      providerId: "company-okta",
      samlConfig: {
        // Okta SAML configuration for company.com
      }
    },
    {
      domain: "partner.com", 
      providerId: "partner-adfs",
      samlConfig: {
        // ADFS SAML configuration for partner.com
      }
    },
    {
      domain: "contractor.org",
      providerId: "contractor-azure",
      samlConfig: {
        // Azure AD SAML configuration for contractor.org
      }
    }
  ]
}

Explicit: Pass providerId directly when signing in. Domain fallback: Matches based on the user’s email domain. e.g. user@company.com → matches company-okta provider.

Step 4: Initiating Sign-In

You can start an SSO flow in three ways:

1. Explicitly by providerId (recommended):

// Explicitly specify which provider to use
await authClient.signIn.sso({
  providerId: "company-okta",
  callbackURL: "/dashboard"
});

2. By email domain matching:

// Automatically matches provider based on email domain
await authClient.signIn.sso({
  email: "user@company.com",
  callbackURL: "/dashboard"
});

3. By specifying domain:

// Explicitly specify domain for matching
await authClient.signIn.sso({
  domain: "partner.com",
  callbackURL: "/dashboard"
});

Important Notes:

  • The example uses localhost:3000 - adjust URLs for your environment
  • For production, always use proper IdP providers like Okta, Azure AD, or OneLogin
  • The ACS URL is derived automatically from {baseURL}/sso/saml2/sp/acs/{providerId}. The post-login destination is controlled by callbackURL in signIn.sso()

Step 5: Dynamically Registering SAML Providers

For dynamic registration, you should register SAML providers using the API. See the SSO Plugin Documentation for detailed registration instructions.

Example registration:

await authClient.sso.register({
  providerId: "okta-prod",
  issuer: "https://your-domain.com",
  domain: "your-domain.com",
  samlConfig: {
    // Your production SAML configuration
  }
});

Additional Resources