Case study 02 · SaaS · HR & benefits

Multi-tenant B2B2C identity for a benefits platform

Employers bring their own login (OIDC, SAML or none), HR systems drive joiners, movers and leavers over SCIM, and a custom guard blocks cross-tenant account takeover. 24 end-to-end tests.

24end-to-end tests
3tenant login types
5architecture decision records

The problem

A benefits platform serves many employers. Each one has its own identity provider (or none), its own HR system, and people who join, move and leave every day. Brokers work across several employers. The platform must keep tenants strictly apart, let only eligible employees in, and cut leavers off immediately, without a custom integration per customer.

Approach

Perkhub (fictional) runs on Keycloak Organizations, one per employer:

flowchart TD
    start["Email entered"] --> d{"Domain matches an<br/>employer IdP?"}
    d -- "acme.test" --> oidc["Acme (OIDC)"]
    d -- "globex.test" --> saml["Globex (SAML)"]
    d -- "no IdP" --> local["Password + TOTP<br/>(invited)"]
    oidc --> guard["Tenant-boundary guard"]
    saml --> guard
    guard -- "domain + provisioned + member + enabled" --> ok["Signed in"]
    guard -- "otherwise" --> deny["Refused"]
  • Tenant onboarding as code. A declarative tenants.json is applied idempotently: organizations, verified domains, IdP links, broker memberships.
  • SCIM 2.0 provisioning service. Each employer’s HR system gets its own OAuth client; the tenant comes from the token, never the request. Joiners are created (with an invite email where the employer has no IdP), movers are synced, leavers are disabled and logged out.
  • No just-in-time accounts. A custom first-login authenticator only links an IdP login to an existing, enabled member of that IdP’s own organization, in that organization’s domain.
  • BFF portal with tenant- and role-scoped authorization. Employees see their employer’s plans, HR admins see their own staff, and brokers see aggregate figures only.

Key decisions

Decision Why
Organizations in one realm (not a realm per tenant) Onboarding is data; brokers can belong to several tenants; scales to hundreds of employers
HR feed is the source of truth; IdPs only authenticate Eligibility comes from HR records, not from whoever can log in at the employer
Tenant-boundary guard before auto-link Stops one employer’s IdP from claiming another tenant’s user
BFF with server-side tokens Benefits data is sensitive; no tokens in the browser

What testing revealed

  • Cross-tenant account takeover is the default risk. Without the guard, an Acme IdP account with the email [email protected] would be auto-linked to Eve’s Globex account. The guard blocks it, and a test proves it.

  • Keycloak’s Organizations API needs manage-realm, even for reads. The provisioning service needs broader rights than intended. This is documented as an accepted risk with compensating controls, not hidden.

  • Back-channel logout is not enough for leavers. With two active sessions, Keycloak removed both but notified the app only once, so a leaver stayed signed in on the second device. Fixed with a session liveness check, and tested on two devices.

  • Recreated employer accounts. When an employer deletes and recreates an account, the IdP sends a new subject for the same email. Keycloak failed with a generic error. Re-linking by email would be a takeover risk, because the address may now belong to someone else. The guard now refuses with a clear message and routes the case to an admin verification procedure. Found during manual testing; now covered by a regression test.

Results

24 end-to-end tests with a headless browser that handles identity-first login, OIDC and SAML brokering, invite emails, password setup and TOTP enrolment. They cover SCIM tenant isolation, eligibility, cross-tenant attacks, role and tenant authorization, and the full joiner/mover/leaver lifecycle.

← All case studies