Design · · 11 min

Keycloak Federation: Integrating Users from LDAP, Okta, Google, and Other Identity Providers

Keycloak integrates external identities two ways: User Federation syncs users from LDAP or AD, Identity Brokering proxies to Okta, Google and others.

Part 6 of 6Keycloak in productionWhole series
  1. 1. The login you wrote isn't the problem. The second one is.
  2. 2. Keycloak Login: Authorization Code + PKCE in MockMart
  3. 3. Keycloak M2M: Authenticating Services Without a User
  4. 4. Keycloak in Practice: 6 Real-World Integration Problems in an E-Commerce
  5. 5. Fine-Grained Authorization with OPA and Keycloak: Separating Authentication from Authorization
  6. 6. Keycloak Federation: Integrating Users from LDAP, Okta, Google, and Other Identity Providers
In this article

In many organizations, user identities are distributed across heterogeneous systems: an Active Directory holding thousands of employees, a social login provider for customers, an external IdP for business partners. Applications need a single authentication entry point, regardless of where users actually live.

Keycloak solves this with two distinct mechanisms: user federation and Identity Brokering. Both bring external identities into a Keycloak realm, but they work in fundamentally different ways. The choice between them depends on who controls credential storage.


The Problem: Fragmented Identities

In the introductory article we saw how a centralized identity provider eliminates identity silos. But there is an implicit assumption: that users are created inside Keycloak. In many real-world scenarios, that is not the case.

A typical setup:

Three completely different identity sources, three different protocols — but applications need to see a single JWT with a consistent structure. They do not want to know whether a user came from LDAP, Google, or Okta. They want a token signed by Keycloak, with the right claims.

Keycloak achieves this with two complementary strategies, described in the sections below.


Fig. 1 · Three sources, one token
Identity sources Active Directory, Google, Okta Employees in a corporate directory, customers with social login, consultants on a third-party IdP. Three different protocols.
↓ federation or brokering
Keycloak The point of convergence It delegates credential checking to the right source, then always signs the token itself with the realm's claims.
↓ a single JWT
Applications Internal app, customer portal, partner area They get tokens with the same structure. They do not know where the user came from, and they should not need to.
Changing identity source leaves the applications alone: the contract is the token, not the directory

Strategy 1: User Federation

The Concept

User Federation means Keycloak connects to an external user directory and uses it as if it were its own. Credentials are not copied: every login is delegated to the external system. Attributes (name, email, groups) are imported into Keycloak’s local database by default and synchronized periodically. It is possible to disable import and query the directory on every request, but the standard behavior includes a local cache.

The classic case is LDAP/Active Directory: the organization already has thousands of users in a directory and has no intention of migrating them. With user federation, Keycloak becomes a facade in front of that directory.

How It Works

User             Keycloak              LDAP / Active Directory
  |                  |                          |
  |-- login -------->|                          |
  |                  |-- bind (verify) -------->|
  |                  |<-- credentials ok -------|
  |                  |-- query attributes ----->|
  |                  |<-- name, email, groups --|
  |                  |                          |
  |                  | [creates local session,  |
  |                  |  issues JWT with claims  |
  |                  |  derived from LDAP]      |
  |                  |                          |
  |<-- JWT ---------|                          |

The key point: the user logs in on Keycloak, entering credentials on the Keycloak login page. But Keycloak does not validate those credentials against its own database — it forwards them to the external system. If LDAP confirms, Keycloak creates a session and issues a JWT just as it would for any local user.

From the application’s perspective, nothing changes. The JWT is identical to one issued for a user created directly in Keycloak. The difference is entirely behind the scenes.

Characteristics

When It Makes Sense

User Federation is the right choice when you control the external identity system and want Keycloak to use it as the source of truth for users. The typical scenario:

The fundamental point is that Keycloak is the only login page. The user never interacts directly with LDAP: they enter credentials on Keycloak, which handles communication with the directory.


Strategy 2: Identity Brokering

The Concept

Identity Brokering is a completely different mechanism. Instead of connecting to an external database, Keycloak delegates the entire login process to another identity provider (IdP). The user does not enter credentials on Keycloak: they are redirected to the external provider, authenticate there, and return with a token or assertion that Keycloak consumes.

The distinction is clear: with user federation, Keycloak receives credentials and verifies them by delegating to the external system. With Identity Brokering, Keycloak never touches credentials: it receives only the result of authentication performed elsewhere.

How It Works

User             Keycloak              External Provider
  |                  |                  (Google / Okta / another KC)
  |-- login -------->|                          |
  |                  |                          |
  |<-- redirect to --|                          |
  |   provider ------|                          |
  |                  |                          |
  |-- login directly at provider -------------->|
  |<-- authorization code (via redirect) -------|
  |                  |                          |
  |-- callback ----->|                          |
  |                  |-- exchange code -------->|
  |                  |<-- provider token -------|
  |                  |                          |
  |                  | [validates external      |
  |                  |  token, maps local user, |
  |                  |  creates session,        |
  |                  |  issues own JWT]         |
  |                  |                          |
  |<-- JWT ---------|                          |

The diagram shows the OIDC flow (Authorization Code). With SAML, the mechanism differs: the assertion travels directly via browser POST, without the server-to-server code-token exchange.

The difference is visible from the user experience: the user sees two login pages (or potentially just one, if they already have an active session on the external provider, making the redirect transparent). They are first redirected to the external provider, then return to Keycloak.

In this scenario, Keycloak never touches the user’s credentials. It receives a token from the external provider (via server-to-server exchange in the OIDC case, or an assertion via browser in the SAML case), validates it, and creates a local representation of the user in its own database. From that point on, it issues its own JWTs, signed with its own key, exactly as it does for any other user.

Supported Protocols

Identity Brokering works with several standard protocols:

What Happens to the Local User

When a user logs in for the first time through an external broker, Keycloak executes the First Broker Login Flow: a configurable flow that decides what to do with the received identity. The main strategies:

This means a single Keycloak user can have multiple federated identities linked: they log in with Google from their phone, with corporate credentials from their laptop, and Keycloak knows it is the same person. Applications always see the same sub in the JWT.

Concrete Examples

Social Login (Google): the simplest case. The user clicks “Sign in with Google” on the Keycloak login page, is redirected to Google, authorizes, and returns. Keycloak receives the OIDC token from Google, creates (or updates) the local user, and issues its own JWT. The application does not know — and does not care — that the user authenticated via Google.

Enterprise Federation (Okta): a business partner manages their own users in Okta. Instead of creating duplicate accounts in Keycloak, Okta is configured as an external identity provider. When a partner consultant accesses the portal, they are redirected to Okta, log in with their corporate credentials, and return. For applications, they are a Keycloak user like any other.

Multi-Keycloak (KC brokering toward another KC): a multi-region or multi-business-unit scenario. Each unit has its own Keycloak with its own users. A central Keycloak acts as a broker: when a user from another region accesses, they are redirected to their region’s Keycloak, log in, and return to the central Keycloak with a token that is consumed and re-issued. Applications see a single Keycloak and a single token format.

                  Central KC (broker)
                 /         |          \
         KC Italy      KC Germany    KC Spain
         500 users      800 users    300 users

Each regional KC manages its own users. The central KC duplicates nothing: it brokers toward the right one based on the email domain, the user’s choice, or any routing logic configured.

When It Makes Sense

Identity Brokering is the right choice when identity is managed by a third party and you do not have direct access to credential storage. Typical scenarios:


Federation vs Brokering: The Differences

AspectUser FederationIdentity Brokering
Where credentials liveIn the external system (LDAP/AD)In the external provider (Okta, Google, another KC)
Who authenticatesKeycloak validates credentials against the external systemThe external provider authenticates directly
Login pageOnly KeycloakRedirect to the external provider
What the user seesA single login page (Keycloak)Potentially two (Keycloak + provider)
Protocol toward the externalLDAP bind / queryOIDC, SAML, OAuth2
Local user in KCImported by default; optional with Import Users = OFFCreated at first login (required for the session)
Does Keycloak touch credentials?Yes — receives them and forwards themNo — sees only the resulting token/assertion
Typical use caseExisting corporate directorySocial login, external partners, multi-IdP

The practical rule is straightforward:


Combining Both Strategies

The two strategies are not mutually exclusive. In a realistic enterprise scenario, it is common to find both active in the same Keycloak realm:

Corporate LDAP  --[User Federation]-->
Google          --[Identity Broker]--> Realm "production"  -->  Uniform JWT for all apps
Okta (Partner)  --[Identity Broker]-->

Employees log in with LDAP credentials on the Keycloak page. Customers click “Sign in with Google” and are redirected. Partner consultants are redirected to Okta. But in the end, the application always receives a Keycloak-signed JWT with the same structure, the same claims, the same format.

This is the real value of federation: Keycloak becomes the convergence point. No matter how many identity sources exist — applications see only one.


Conclusion

Keycloak provides two mechanisms for integrating external identities, and the distinction is clear:

In practice, the two mechanisms combine freely in the same realm. The result is a single authentication point for all applications, regardless of where users live.

The next article in the series moves from concepts to implementation: we will see how to build the Authorization Code flow with PKCE in a real application.


Useful resources:

Francesco Montelli
Francesco Montelli

Freelance Software Engineer. I design, build and automate software: from architecture to the build, to the processes that keep quality high over time. From scratch or on systems grown rigid over time.

Follow me on LinkedIn →

Related articles

Edit on GitHub

Facing the same problem in your system?

Tell me how your system looks today: in half an hour we figure out where to start.

A 30-minute call

You pick the day and time, free.

Book →

A LinkedIn message

Even just to get to know each other.

Write me →

An email

If you prefer to explain at your own pace.

francesco@montelli.dev

In person

At DevRomagna or the next conference.

Where I speak →