Set up SAML and OIDC SSO for your customers
Enterprise customers expect to log in through their own identity provider. Supporting SAML 2.0 and OpenID Connect turns a login flow into a tenant-level integration. This guide shows how to expose SSO in your application using Bastionary as the self-hosted control plane.
Define the system boundary
A reliable SSO integration separates three concerns: identity proof, tenant membership, and entitlements. The identity provider proves who the user is. Your application decides which organizations the user belongs to and what they can do.
Model these objects explicitly. A minimal schema looks like this:
CREATE TABLE memberships (
id UUID PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id),
org_id UUID NOT NULL REFERENCES organizations(id),
role TEXT NOT NULL,
status TEXT NOT NULL,
UNIQUE (user_id, org_id)
);
Keep the SSO logic focused on authentication. Do not put billing rules, seat counts, or feature flags inside the SAML or OIDC callback. Handle those after you know which user and tenant the session belongs to. This separation prevents every new enterprise requirement from becoming custom glue code.
Choose SAML or OIDC for each customer
SAML 2.0 is still common with Microsoft Entra ID and Okta enterprise apps. OpenID Connect is simpler, JSON-based, and preferred by newer providers. Ask each customer which protocol their identity provider supports.
- SAML: exchange XML metadata, validate signatures, map NameID to a stable user identifier.
- OIDC: exchange discovery document, validate ID tokens, map standard claims such as
sub,email, andname.
Store each tenant's identity provider settings separately. Never reuse the same certificate or client secret across tenants. Keep a mapping of tenant to federation settings so support can see which identity provider a customer uses without reading raw XML.
Configure the identity provider
Create an application registration in the customer's identity provider and enter your service provider details. Provide these values to the customer's admin:
- Assertion Consumer Service URL:
https://auth.example.com/sso/saml/acs - Entity ID:
bastionary-example - Reply URL / redirect URI for OIDC:
https://auth.example.com/sso/oidc/callback - Name ID format:
emailAddressorpersistent
Import the identity provider metadata or discovery document into Bastionary so the service can validate tokens and retrieve public keys automatically.
Validate SAML signatures
Parse the signed <saml:Assertion> or response, check the signature against the imported certificate, verify the recipient and audience match your service, and enforce a short assertion lifetime. Reject unsigned assertions.
Validate OIDC tokens
Fetch the JSON Web Key Set from the discovery document, verify the ID token signature and issuer, confirm the nonce and state values match the login request, and reject tokens that have expired.
Map users and roles from claims
Do not create users blindly. Decide what happens when a new NameID or sub arrives:
- Look up the user by the federated identifier.
- If no match exists, create a user and membership in the matching tenant.
- Map group membership from the identity provider to application roles, but keep role assignment auditable.
- Disable or suspend users when the identity provider no longer lists them.
Example JavaScript for processing an OIDC callback:
async function handleOidcCallback(tokenSet, tenantId) {
const { sub, email, name } = tokenSet.claims();
let user = await findUserByExternalId(sub);
if (!user) {
user = await createUser({ email, name, externalId: sub });
}
const membership = await ensureMembership(user.id, tenantId, 'member');
await createSession(user.id, membership.org_id);
}
Run this inside a transaction so the user, membership, and session are created atomically. If anything fails, redirect the user to an error page instead of leaving a partially created account.
Build the self-service setup UI
Your customers should be able to configure SSO without opening a support ticket. Expose a settings page that accepts their metadata and maps attributes. Required fields:
- Protocol selection: SAML or OIDC.
- Identity provider metadata URL or raw XML for SAML; issuer and discovery URL for OIDC.
- Attribute mapping for email, full name, and groups.
- A test login button that validates the connection before enabling it.
Store configuration under the tenant and run a validation job that fetches metadata and checks signatures before the customer enables the connection. Show clear error messages when validation fails, because the customer's admin will be debugging the identity provider side.
Test before going live
Validate every path end to end in a non-production tenant:
- Initiate login from your application and from the identity provider dashboard.
- Log in with an existing user, a new user, and a user removed from the identity provider.
- Verify role changes in the identity provider propagate to your application.
- Confirm the logout flow terminates sessions on both sides.
- Check that error states produce actionable logs for both your team and the customer admin.
After go-live, keep audit logs of every SSO event. They are the first place you will look when an enterprise admin reports a login problem. Set up dashboards for failed login attempts and unexpected claim values.
Next steps
- Enable SCIM provisioning so user lifecycle changes are pushed automatically.
- Document your attribute mapping and tenant onboarding steps for customer admins.
- Add admin controls that let customers disable SSO and fall back to local credentials safely.
- Schedule a quarterly review of identity provider certificates and client secrets.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.