Mapping external identity provider claims to Keycloak roles and groups

Identity provider mappers turn upstream claims and SAML attributes into Keycloak roles, groups and user attributes at each login. This page covers mapper types, sync modes, and how to keep authorization decisions consistent across brokered sign-ins.

On this page

Why Keycloak identity provider mappers decide who gets access

When a user signs in through an upstream provider such as Microsoft Entra ID, Google or AD FS, that provider already holds group or role data, but Keycloak does not automatically turn it into local authorization. Keycloak identity provider mappers are the rules that translate upstream claims and SAML attributes into Keycloak roles, groups and user attributes. Without them, a brokered user can authenticate successfully and still hold no permissions in the realm, or hold permissions copied from a claim nobody meant to trust. The mapper layer is therefore where access decisions for brokered users actually get made, so it deserves the same review as the client configuration itself.

In the brokering flow, Keycloak receives the upstream token or assertion, runs the identity provider's mappers during the first login and on later logins according to each mapper's sync mode, and then writes the results onto the local user. Teams usually need mappers when the upstream directory is the system of record for roles, when several applications depend on the same group membership, or when they want a hardcoded default such as a baseline role for every brokered user. Where the logic ends up is a design choice with three main homes: Keycloak realm roles and client roles, Keycloak groups, or user attributes that applications read from the token. Application code is the fourth option, and it is the one most likely to drift from what the identity provider assigned.

Before building any mapper, the reader should export a sample of real upstream claims or assertions for a test account in each relevant group and record exactly which claim names, value formats and group identifiers arrive. Group claims often carry object identifiers rather than display names, and claim names differ between OIDC and SAML providers, so a mapper written against a guessed name will silently match nothing. The reader should also decide, for each target role or group, which system owns the assignment, because two systems writing the same grant will produce access that nobody can explain during an access review.

The main risk is that mapper outcomes are only checked at login time. If an upstream group is removed, the local grant may persist until the sync mode, the next login or a separate deprovisioning process removes it, so the reader should confirm how each mapper behaves when a claim disappears. Teams should also confirm with their identity provider's documentation how group overage or claim limits are handled, since a missing group claim can look like a revoked role. Treat the mapper configuration as an access control artifact: document each rule, its owner and the test that proves it still works after an upgrade.

Mapper types and when each fits

This table compares the identity provider mappers Keycloak offers for brokered logins, showing what each one reads, what it writes and when it is the right choice. Read each row as a decision: start with the question of whether the value comes from the upstream token or is the same for every user.

Mapper typeInput it readsOutput it writesRight choice when
Hardcoded roleNothing from the upstream token; a role you pick in the mapper configurationA fixed realm or client role on every brokered userEvery user from this provider should get the same baseline access, such as a read-only role for a partner provider
Hardcoded groupNothing from the upstream token; a group you pick in the mapper configurationMembership in one Keycloak group for every brokered userA provider serves one population, and group-based policies or client scopes should apply to all of them
Claim-to-roleA claim in the upstream OIDC ID token or userinfo response, matched by name and valueRealm or client roles, only for users whose claim value matchesThe upstream system already expresses entitlements as claim values, such as an app role or a security group identifier
Claim-to-groupA claim in the upstream OIDC token, matched by name and valueMembership in a Keycloak group for matching usersAccess is managed as groups, and you want group membership to follow the upstream directory on each login
SAML attribute-to-roleAn attribute in the SAML assertion, matched by attribute name and valueRealm or client roles for matching usersThe upstream identity provider sends roles or groups as SAML attributes rather than as OIDC claims
Attribute mapper (user attribute)A claim or SAML attribute from the upstream tokenA Keycloak user attribute, not a role or groupYou need values such as department, cost center or employee ID for claims, token mappers or downstream systems, but no authorization decision
Sync mode on each loginThe mapper's current input on every brokered loginAdds or removes the mapped grant depending on the configured sync modeUpstream removals must revoke access in Keycloak, so verify each mode before relying on it for authorization

Choosing sync modes so roles follow the upstream source

The sync mode on an identity provider mapper decides whether a role or group granted from an upstream claim keeps following that upstream source after the first login. In import mode, the mapper applies when the brokered account is first created and then stops governing that grant, so a group removed upstream leaves the role in place in Keycloak until someone removes it by hand. In force mode, the mapper reapplies its result on every login, so a role that came from upstream is removed when the claim no longer supports it. Legacy mode exists for compatibility with earlier behavior. Before you choose it for a new realm, confirm its exact effect for your Keycloak version in the Keycloak server administration guide.

The main risk is stale local grants. Under import mode, an upstream group removal does nothing on its own, so access can outlive the reason it was granted. Under force mode, the risk runs the other way: a local administrator who grants a role directly can have it removed at the next login if a mapper on the same provider controls that role and the upstream claim does not include the user. For illustrative purposes, a support engineer given a realm role by hand in a force-mode realm may lose it silently after signing in, and the admin who made the change may never be told. Decide which source owns each role. Roles produced by a mapper should not also be assigned by hand, and roles that only local administrators manage should not be targeted by any mapper.

Test the sync mode with a user who is removed from the upstream group, not only with a user who is added. Sign in as that user after the change and check three things: the role or group in the Keycloak admin console, the claims in the resulting access token, and whether a locally granted role was kept or removed. Then check existing sessions. A token already issued keeps its claims until it expires or is refreshed, so the lifetime of your access and refresh tokens determines how long a removed group still appears to grant access. Record the result for each mapper and each provider, and repeat the test after any change to the mapper, the sync mode or the provider's login settings.

How to configure claim-based mappers step by step

This sequence takes a claim or SAML attribute from an upstream provider to a verified Keycloak role or group, so each mapping is confirmed on a real login before anything depends on it.

  1. Capture the upstream claim or attribute

    Sign in once through the identity provider with a test account, then inspect the ID token, userinfo response or SAML assertion to record the exact claim or attribute name, its nesting, and the value format (for example, a group object ID, a group display name, or a list versus a single string). Use a test account in each upstream group you plan to map, since the values you need to match come from the upstream side and not from Keycloak. Record the findings in a short mapping sheet owned by the team that controls the upstream provider. The step is done when every source value you plan to match is written down exactly as it arrives, with its format noted.

  2. Create the identity provider mapper

    In the Keycloak admin console, open the realm, go to Identity providers, select the upstream provider, and add a mapper. Choose the mapper type that fits the source: a claim-based mapper for a claim or attribute value, or a hardcoded mapper when every user from that provider should receive the same role or group. Set the claim name and the expected value exactly as recorded in the previous step, and choose the sync mode. Use import mode when you only want attributes applied at first login, or legacy or force mode when Keycloak should reapply the mapping on each login and remove roles that no longer match. Save the mapper. The step is done when the mapper appears in the provider's mapper list with the settings you intended.

  3. Create or import target roles and groups

    Create the realm roles, client roles and groups that the mapper will assign, or import them from the design document. Keep names stable and avoid encoding upstream display names into Keycloak role names, because a rename upstream would then break authorization. Decide which roles are realm roles and which belong to a client, since the mapper form asks for the target. Confirm that each target role exists exactly once and that no older role with a similar name is still in use by applications. The step is done when every target named in the mapper exists in the realm or client, and the list matches the mapping sheet.

  4. Confirm the mapper target and attribute settings

    Open each mapper again and check the target fields: the role or group selected, the user attribute name if you are mapping to an attribute, and any options that control whether the value is treated as a single value or multiple values. Compare these against the target list from the previous step. Where a value needs transformation, such as a case difference or a prefix in the upstream group name, handle it in the upstream configuration or in a documented custom mapper rather than assuming Keycloak will normalize it. The step is done when a reviewer who did not build the mapper can read each row and state which upstream value produces which Keycloak target.

  5. Sign in with test accounts in each upstream group

    Create or select one test account in each upstream group, plus one account in no mapped group. Sign in through the Keycloak login page using the brokered provider, then sign out. Repeat for each account so that every mapping path runs at least once. Watch for first-login behavior: an existing Keycloak user may not receive a mapping change until the next login, depending on the sync mode. Record the outcome of each sign-in. The step is done when each test account has produced a login result that matches the mapping sheet, including the account with no mapped group.

  6. Verify roles and groups on the user detail page

    In the Keycloak admin console, open the Users section, find each test account, and check the Role mapping and Groups tabs. Confirm that the expected roles and groups are present and that unexpected roles are absent. Then sign in again with the same account and check that the result holds. If you chose a sync mode that removes roles, change the upstream group membership of one test account, sign in again, and confirm the role is removed. The step is done when each test account's assignments match the mapping sheet on two consecutive logins, and the removal test behaves as the chosen sync mode promises.

  7. Verify the token issued to a client

    Register or use a test client that reads roles or groups from the token, then request a token through a test sign-in for each account and decode it. Check that the role claims or group claims contain the expected values, that the claim names match what the application reads, and that roles from other clients or realms are not included unless intended. Confirm that the token lifetime and any client scope restrictions do not hide the claim the application needs. Save the decoded token samples with the mapping sheet. The step is done when each test token contains exactly the authorization data the mapping sheet specifies, and the application team has confirmed that the claim names they read are the ones issued.

  8. Record the mapping and schedule a regression test

    Store the mapping sheet, mapper configuration export and test results in the team's configuration repository or change record. Note the upstream owner who must tell you when a group name or claim format changes, and define a regression test that repeats the sign-ins from the earlier steps after each Keycloak upgrade and each upstream change. The step is done when the mapping is documented, the owner is named, and the regression test has been run once against the current configuration.

Keeping authorization decisions consistent across applications

Once several upstream providers feed one Keycloak realm, the same person can receive different roles depending on which login path, client or group name they used. The cards below cover the places where that drift usually starts and the check to run against each one before go-live.

Mappers that disagree across providers

When two upstream providers both feed the same realm, each identity provider mapper writes its own roles and groups. The same person can end up with a role from one provider and none from another. Keep one target role per business meaning, and test the same test user through every provider to compare the resulting roles side by side.

Keycloak docs

Client scope mappers add claims unevenly

Protocol mappers in client scopes decide which claims reach each application's tokens. One application may get a groups claim while another reads roles from a different claim name, so the same grant is enforced differently. Inventory every client scope that adds role or group claims, and decode a token from each client to confirm the claim names and values match.

Group names differ between Entra ID and Keycloak

An Entra ID group identified by its object ID or display name rarely matches a Keycloak group name one for one. A mapper that matches on the wrong string grants nothing, or grants the wrong group. Agree on a naming convention and a mapping table owned by the identity team, and test that each mapped group produces the expected Keycloak group membership after login.

Default roles grant more than intended

Default roles are assigned to every new user in a realm, and composite roles can pull in additional client roles. A role meant for a narrow partner group can end up on every account through a default composite. Review the default role composition, remove client roles that only one application needs, and test a newly created user with no mapped groups to see what they can access.

Users keep grants after leaving a group

If a mapper only adds roles at login and the sync mode does not remove them, a user who leaves an upstream group keeps the Keycloak role until something else clears it. Choose the sync mode for each mapper deliberately, then test removal: take a test user out of the upstream group, log in again, and confirm the role is gone from the user and from the tokens issued afterward.

Claim formats that break mappers in practice

The most common reason a mapper silently assigns nothing is that the upstream claim does not contain what the mapper expects. Group claims are often object IDs rather than display names. Microsoft Entra ID, for example, can emit group identifiers in a groups claim unless the application registration is configured to emit group names or sAMAccountName-style values. Before writing a claim-based mapper, ask the upstream administrator for one real token or assertion from a test user who belongs to each group you plan to map, and compare the values against what Keycloak will receive. Also confirm whether the upstream provider truncates or replaces large group lists. Overage handling is provider-specific: when a user belongs to too many groups, the provider may omit the groups claim and point to a Graph endpoint instead, so a mapper built on the claim would see nothing for exactly the users with the most access.

Multi-valued claims and SAML attributes cause a different class of failure. A claim that carries a list in one token may carry a single string for a user with one group, and a mapper written for one shape can mis-assign roles for the other. Confirm whether each attribute is multi-valued and how the upstream encodes it. In SAML, the attribute name and its NameFormat must match exactly what the mapper is configured to read, and the NameID format decides which value identifies the user at first login. A persistent or transient NameID changes how accounts link later, so agree the NameID choice with the upstream administrator before go-live. Finally, many providers send optional claims or attributes only when the relying application is explicitly configured to request them. A claim that appears in testing may disappear in production if a different application registration or attribute release policy is used, so record the exact configuration the upstream team applied and ask them to confirm it in writing.

How mapper design differs by upstream provider

Entra ID is the richest source of authorization data among the providers covered here, but what arrives depends on how the app registration and token are configured. Group membership can arrive as object IDs rather than display names, and large group sets can be replaced by a link that points to Graph instead of a list in the token. App roles, which you define on the app registration and assign to users or groups, are usually the cleaner signal for application authorization. Map on stable values and confirm what the token contains for your tenant before writing mappers.

  • Group claims format: Check whether the groups claim carries object IDs or names in your configuration, and map to those exact values.
  • Overage behavior: Plan for users whose group count exceeds the token limit, where the claim is missing and a fallback is needed.
  • App roles: Prefer app roles defined on the registration for application permissions, since they are versioned with the app.
  • Tenant scope: Restrict which tenant can sign in before mapping, so claims from unexpected tenants never reach role logic.

For setup steps, see the sibling topic on Microsoft Entra ID as an identity provider in Keycloak. Confirm the claim shapes in a test tenant before you rely on them.

Security and operational considerations for mapped access

Mappers turn upstream claims into roles, so the security of mapped access depends on what you trust upstream, who can edit the mappers and how you notice when a login gets more access than it should. The cards below cover those controls and the checks to run after each change.

Trust only validated upstream providers

A claim mapper is only as trustworthy as the identity provider that issues the claim. Map roles only from providers you control, own the tenant or directory behind them, and have validated which claims they actually emit. Confirm who can add groups or assign app roles upstream, because anyone who can do that upstream can also gain access in Keycloak. Document each provider and the claims it is allowed to drive, and remove mappers for providers that no longer need to grant access.

Keycloak docs

Protect the admin console that edits mappers

Anyone who can edit an identity provider mapper can change who receives which role, so the admin console needs the same protection as the realm itself. Keep the admin console and Administration REST API on a separate hostname or context path from public frontend URLs, restrict network access to administrators, and require strong authentication for admin accounts. Review which accounts hold realm administration rights and remove standing access that is no longer needed.

Production guide

Change control for mapper edits

Treat mapper edits as configuration changes with a review step. Record the current mapper settings before each change, have a second person review the claim name, the value matched and the target role or group, and apply changes in a non-production realm first. Keep the configuration in version control where your team can, so a rollback restores a known state. Test with a user from each affected group before you release the change.

Audit logging of role changes

Confirm that administrative events are captured for mapper creation, edits and deletion, and that role and group membership changes are logged with the acting administrator. Send those logs to a store that administrators cannot quietly edit, and set retention to match your audit requirements. Check that a test change produces a log entry you can attribute, because a gap in this trail makes later access reviews hard to complete.

Monitor for unexpected role assignments

Watch for sign-ins that receive roles or groups outside the expected set for that provider. Alert on new role assignments from a mapper, on users gaining high-privilege roles, and on sudden changes in the number of members of a privileged group. Compare the roles issued at login with the roles your applications expect, since a mismatch often shows up in the application before it shows up in Keycloak. Agree in advance who receives these alerts and how quickly they respond.

Mapper changes apply at the next login

A mapper change affects the next login, not sessions that already exist. A user who keeps an active session can keep the roles from the earlier login until the session ends, the token expires or the mapper runs again. Plan changes that remove access accordingly, by ending affected sessions or shortening token lifetimes where the risk requires it. After each change, verify with a test account that the new role appears on the next login, that removed roles disappear, and that applications reject tokens that should no longer grant access.

Login flow from upstream claim to local role

The flow starts when a user chooses an upstream provider on the Keycloak login page and the browser is redirected to that provider. The provider returns an ID token or SAML assertion to Keycloak, which runs the identity provider mappers configured for that provider. Depending on the sync mode, the mappers set the local user's roles, groups and attributes, and the client then receives those values in the tokens Keycloak issues. Check the sync mode first, because it decides whether a role removed upstream is also removed locally at the next login.

Upstream claims pass through mappers before the client receives local roles and groups.

Limits to confirm in your environment

Mapper behavior, token contents and failure modes depend on the Keycloak version you deploy and on what your upstream provider actually sends. Treat each item below as a check to run in a test realm, and use the Keycloak server administration guide as the reference for each one.

Mapper types and sync behavior

Confirm which mapper types your Keycloak version offers for the provider you are brokering, such as hardcoded role, hardcoded group, claim-to-group and attribute importers, and what each one does on first login versus later logins. Check the sync mode setting for each identity provider, because import mode and legacy mode behave differently when a role or group is removed upstream. Run a login test that removes a group upstream and confirm whether the Keycloak role is revoked.

Keycloak docs

Token size with many groups

Upstream providers can send long group lists, and Keycloak copies mapped roles and groups into the tokens it issues. Large tokens can break cookies, proxies or header limits in downstream applications. Test with a user who belongs to the largest group set you expect, measure the size of the ID and access tokens, and decide whether to filter groups by prefix or use a group-to-role map instead of passing every group through.

Upstream provider unreachable

Brokered login depends on the upstream provider for each sign-in, and role changes only reach Keycloak when a user logs in again under the chosen sync mode. Confirm what users see when the provider is down, whether an existing Keycloak session keeps its roles, and how long a removed upstream role remains active. Write this down as an operating rule so support teams and application owners know what to expect during an outage.

Claims your upstream actually sends

Claim names differ by provider and by tenant configuration. A Microsoft Entra ID tenant may omit group claims or replace them with group identifiers when the group count is high, and a SAML identity provider may send attributes under different names or NameID formats. Decode a real token or inspect a real SAML response from a test user, compare what arrives in Keycloak's brokered login logs with your mapper expressions, and confirm the claim names with the upstream administrator before go-live.

Consistency across applications

Roles mapped in Keycloak are only as consistent as the clients that read them. Confirm which client scopes and protocol mappers include realm roles, client roles and groups in each application's tokens, and whether each application checks the same claim. Record the expected claim set per client so a mapper change in one realm does not silently change authorization in another.

Questions teams ask before mapping upstream groups

What does a mapping project cost and what drives the effort?

Effort follows the number of upstream groups and claims, how many upstream identity providers feed the realm, and how many Keycloak clients depend on the resulting roles. Each upstream provider adds its own claim format, group semantics and test cases, so a single Microsoft Entra ID tenant with a short role list is far smaller than several providers with nested groups. Ask any partner for a quote that lists each provider, the group inventory size, the number of clients that consume roles, the sync mode for each mapper, and the test matrix. Fees are paid for analysis, build, testing and handover. Some costs recur, such as reviewing mappers after upstream group changes, and these should be stated separately from the one-time build.

How long does a typical mapping project take?

Duration depends mostly on scope: how many upstream providers are involved, how many groups need a decision about mapping, and whether the upstream administrators can answer questions quickly. A single provider with a documented group model usually moves faster than a project that must reconcile overlapping directories or roles defined by several business units. The most common delays come from waiting on upstream access and from unresolved decisions about which group is authoritative. Ask for a timeline tied to the written scope, with milestones for the group inventory, the mapper design, the test accounts and the cutover. Do not accept a duration that is not linked to the list of providers, groups and clients.

What must our team supply before the work starts?

Three inputs unblock most mapping work. First, administrative access to the upstream identity provider, such as an app registration or SAML configuration with permission to view claims, group definitions and token settings. Second, a complete group inventory that names each upstream group, its purpose, its owner and the Keycloak role or group it should map to. Third, test accounts in each upstream provider covering the common cases: a user in one group, a user in several groups, a user in no mapped group and a user who has left a group. Also confirm which Keycloak clients need which roles, because mappers are only useful when the consuming applications read them consistently.

Can our own team build claim-based mappings without a partner?

Yes, a capable Keycloak team can configure hardcoded and claim-based mappers, because the administration console and the Administration REST API expose the same settings. The work becomes harder when group claims arrive in a format the upstream provider does not send by default, when group overage or pagination behavior must be handled, or when sync modes must be chosen for several providers at once. Check internal skills in OpenID Connect, SAML assertion structure and Keycloak's identity provider settings before deciding. A reasonable test is whether your team can explain, for each role a user receives, which upstream claim produced it and when that role is removed.

How do we change mappers after go-live without breaking sign-in?

Treat mapper changes as configuration changes that affect authorization, and test them before they reach production. Keep the mapper definitions in version control if your deployment manages Keycloak configuration as code, and review every change against the group inventory. Be careful with sync mode: the setting determines whether a role granted by an upstream group is added, updated or removed on each login, so a change can revoke access for users who sign in next. Test with the same accounts used at launch, confirm the claims reaching each client, and communicate upstream group renames before they happen. Keep a rollback plan, because an incorrect mapper may only show up when a user next logs in.

When is it worth bringing in a partner for mapping?

Bring in a partner when mapping decisions affect several identity providers, regulated access rules or many applications at once, or when the internal team lacks time to test every upstream case. A partner is also useful when an audit needs evidence that role assignments are consistent and reviewable. DeployOpen plans, implements, tests and documents self-hosted platforms in infrastructure you control, and prepares the configuration evidence that compliance teams review. It does not issue audit reports or certifications. A written scope lets you compare a partner with your own team on the same list of providers, groups, clients and tests.

How DeployOpen scopes and delivers identity provider mappers

DeployOpen treats identity provider mappers as an authorization design problem first and a configuration task second. We agree the claims, roles and groups in writing, build the mappers in your Keycloak environment, test each login path, and hand over the configuration with the reasoning behind it.

How we scope

  • We inventory the claims or SAML attributes each upstream provider sends, including tenant, group and role values.
  • We document which Keycloak roles, groups and user attributes each upstream value should produce, and who approves that mapping.
  • Engagements are fixed-scope, with the mapping list, deliverables and timeline agreed in writing before build work starts.

How we build

  • We configure hardcoded and claim-based mappers for each identity provider, with the sync mode chosen for each attribute.
  • We test first login, returning login and a changed upstream group for each provider and client in a non-production realm.
  • We check that roles reaching tokens match what each application reads, so authorization decisions stay consistent across clients.

How we hand over

  • You receive a mapping register listing each source claim, target role or group, sync mode and the owner of each rule.
  • We document the test cases, their results and the steps to repeat them when an upstream provider changes its claim format.
  • We walk your identity and application teams through the configuration so they can review and change mappings safely.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers, including review after each Keycloak upgrade.
  • We can retest mappers after upstream provider changes, new applications or new group structures, under the agreed scope.
  • Ask us to confirm how your Keycloak version handles mapper behavior before any upgrade, so authorization changes are planned rather than discovered.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.