Organization claims and groups in Keycloak tokens

Keycloak can put organization context into tokens so a SaaS application knows which tenant a user is acting for. This page covers the claim mapper, groups and roles, token size, and the isolation checks the application must still make.

On this page

Passing organization context in Keycloak tokens for SaaS applications

A multi-tenant SaaS team needs every request to carry the tenant a signed-in user belongs to. In Keycloak, the organization claim is the token content that tells the application which organization the user is acting for, so the application can select the right data and settings. Teams need it once one Keycloak realm serves several customers and a single user may belong to more than one of them. Before building on it, confirm the exact claim name, its shape (a single value or a map keyed by organization), and whether it appears in the ID token, the access token or both in the Keycloak version you run, since these details have changed across releases and the current documentation is the authority.

The Organizations feature is what makes the claim possible. It manages member organizations, the users in them and the email domains that route people to the right organization. The mapper that writes organization data into tokens is configured per client, so decide which clients need tenant context and which do not. Organization groups and roles can add further tenant-scoped authorization data, but each addition increases token size, and a user who belongs to many organizations can produce a large token. Test the largest realistic membership for your users early, and confirm how your client libraries handle the claim format, because a parser that expects one value will fail on a list.

The token only carries context. It does not enforce isolation. Keycloak can tell your application that a user is a member of organization A, but your application must still check that every query, file access and API call is limited to the tenant named in the request and that the user holds a role there. Treat the claim as an input to authorization, validate it against the verified signature and issuer on every request, and never accept a tenant identifier from the URL or request body without checking it against the token. Write tests that attempt cross-tenant access with a valid token from another organization, because that is the failure a tenant boundary most often hides.

Which token model fits your tenant design

When each user belongs to exactly one customer organization, the simplest model works well. The token can carry the organization identifier as a single claim, and the application uses that claim as the tenant key on every request. Because the claim describes one tenant, the token stays small and the application does not need a second lookup to learn where the user works.

  • Organization claim as tenant key: Map the organization alias or ID into the access token with the organization claim mapper, and make the application reject requests whose tenant does not match the stored record.
  • Roles inside the tenant: Keep application roles in the token only when they are few and stable. Large role lists grow the token on every login.
  • Group membership for fine detail: Use organization groups for team structure inside one customer, and confirm how your chosen Keycloak version exposes them in tokens before you depend on them.
  • Application-owned data: Store tenant-specific settings, feature flags and entitlements in the application database, not in the token, so changes take effect without a new login.
  • Illustrative case: A vendor serving one company per user puts the organization claim in the token and loads that customer's plan from its own database on each request.

Before committing, confirm with a test realm that the claim appears in the token types your application actually receives.

Organization claim, group and role options compared

This table compares the four ways to carry tenant context into a Keycloak token or application: the organization claim mapper, organization groups, realm and client roles, and a lookup at request time. Read each row against your user count, how many organizations a typical user belongs to, and how quickly membership changes must reach the application. The main takeaway is that tokens are cheap to read but go stale, while lookups stay fresh but add a dependency on Keycloak for every request.

OptionWhen it fitsCost in token sizeFreshness and coupling
Organization claim mapperApps that need to know which organization the user is signed in under, with a small number of memberships per userGrows with each organization the user belongs to, so users in many organizations get larger tokensUpdated at each token issue; the app reads it locally, but coupling to the claim name and structure is high
Organization groupsTenant-scoped permissions where each organization has its own set of groups that the app maps to access levelsAdds one entry per group the user holds, so group design should stay coarseChanges apply at the next token refresh; the app must agree on the group naming convention
Realm rolesGlobal roles shared across all tenants, such as platform support or billing staffSmall and stable, since the same few roles apply to every tenantLow churn, but reusing realm roles for tenant-specific access mixes tenants and makes isolation harder to audit
Client rolesPermissions that belong to one application and do not vary by tenantAdds entries scoped to the client, which can grow if many clients are included in the tokenCoupled to the client definition; changes need an update to the client and its mappers
Lookup through the Admin REST API or application databaseUsers with very large numbers of organizations, or data that must reflect a change immediatelyKeeps the token small because membership is not embeddedFreshest data, but adds a network call or query on each request and depends on the application's own sync logic

Setting up the organization claim in a Keycloak realm

This sequence turns Keycloak's Organizations feature into a tenant claim in access and ID tokens that your SaaS application can read and enforce. Each step ends with a check you can run before the application depends on the claim.

  1. Enable the Organizations feature in the realm

    A realm administrator turns on the Organizations feature for the realm that will serve your tenants, then confirms that an Organizations entry appears in the admin console navigation. Keycloak's server administration guide documents the Organizations feature, so check the feature state against the version you run before you build on it. Do this in a non-production realm first so you can see how the console and tokens behave before any tenant data exists.

  2. Create organizations and register their domains

    Create one organization per customer or business unit, give each a name and alias that match your provisioning records, and add the email domains that belong to it. Domains matter because they drive membership matching and later identity-first login. The check is a list view in which each organization shows its expected domains and no domain appears under two organizations, since overlapping domains make membership ambiguous.

  3. Add members and test the membership model

    Add existing users to their organizations, and decide with your identity team whether a user can belong to several organizations, which is common for consultants, partners and shared administrators. Confirm in the admin console that each user's organization list matches the business record. Keep this list as the reference for the token tests in later steps.

  4. Add the organization claim to the client scope

    Create or edit a client scope that your application client requests, and add an Organization mapper or a protocol mapper that emits the organization membership into the access token or ID token. Set the claim name your application expects, and restrict the mapper to the clients that need tenant context. Save the mapper configuration in a file or exported realm so the setting can be reviewed and reapplied during upgrades.

  5. Add groups or roles for in-tenant permissions

    If your application needs permissions inside a tenant, such as tenant administrator or billing viewer, create groups or client roles inside the organization and map them to the token only where the application reads them. Keep roles that apply across all tenants in a separate client, so a tenant administrator role cannot be confused with a platform role. The check is that a group or role appears in the token only for members of the organization that holds it.

  6. Decode a token for a single-organization user

    Sign in as a test user who belongs to exactly one organization and capture the access token and ID token from the application's authorization flow. Decode them with a JWT inspection tool in a test environment only, and confirm that the organization claim contains the expected organization identifier or alias and no other tenants. Compare the output with the membership list from step three, and record the token size so you have a baseline.

  7. Test a multi-organization user and enforce isolation

    Sign in as a user who belongs to several organizations and confirm that the token lists each membership the mapper is configured to emit, with no membership missing or extra. Then send requests for one tenant's data using that user and check that the application rejects requests where the tenant in the path or request does not match an organization in the token. A passing result shows the claim is correct and that the application, not the token alone, enforces tenant isolation.

Keeping tokens small for users in many organizations

A user who belongs to many organizations can make every token larger than it should be. If the organization claim, group paths and roles for every membership are written into each access token, the token grows with the user's membership count rather than with the tenant the user is working in right now. Include only the current or requested organization in the token. Most SaaS applications need one tenant context per request, so the token should carry that organization and nothing else from the membership list. Decide early how the client requests a specific organization, and make the application reject requests where the organization in the token does not match the resource being accessed.

Do not embed full membership lists or every group a user holds across tenants. Move that detail to a lookup: the application or a backend service can query Keycloak's Admin REST API for memberships when it needs a full picture, or it can keep its own membership table synchronized from Keycloak events. Lookups add a call and some caching logic, so decide which data is needed on every request and which is needed only on administrative screens. Keep the claim mapper configuration reviewed as part of change control, since a single added mapper that copies a multi-valued attribute can change token size for every user at once.

Measure real token sizes in your own environment before you set limits. Generate tokens for test users with the largest expected membership counts, record their encoded size, and include the cookies, headers and any extra claims your applications add. Then confirm the header size limits at each reverse proxy, load balancer and API gateway in the path, because a token that works in a direct test can fail once it passes through a proxy with a smaller request header limit. Record those limits in the platform's configuration, and retest after any change to mappers, client scopes or proxy settings.

How the organization claim reaches the application

The flow starts when a user signs in to a Keycloak realm, where the organization mapper adds the tenant context to the token. The token passes through the gateway to the SaaS API, which validates the signature against Keycloak's published keys and reads the claim. The application then checks that the tenant in the claim owns the resource being accessed. The trust boundary sits between Keycloak and the application: the claim is only as trustworthy as the signature check and the tenant check that follow it.

Tenant claim flows from Keycloak through the gateway to the application's tenant check.

Enforcing tenant isolation in the application, not only in the token

A token that says which organization a user belongs to is only an input. The application still has to decide whether that claim is trustworthy and whether it matches the tenant on each resource. The cards below cover the checks that connect the token to the data the application serves.

Verify signature and issuer first

Before reading any organization claim, the application must validate the token signature against the realm's published keys and confirm the issuer matches the expected realm URL. A token from another realm, or one signed by an unexpected key, must be rejected before its claims are used. Check the audience as well, so a token issued for a different client cannot be replayed against your API. Confirm your token library enforces these checks by default and does not skip them for unverified tokens.

Keycloak docs

Compare claim with the resource tenant

Every request that touches tenant data should compare the organization claim with the tenant that owns the requested resource. The tenant is usually derived from the resource itself, such as a project ID or a URL path segment, and not from a value the client sends. A mismatch means the user is trying to reach another tenant's data and the request should fail. Keep this comparison in one shared authorization function so new endpoints cannot skip it.

Scope every database query by tenant

Filter queries on the tenant identifier taken from the validated token, and make the tenant column part of every primary lookup, update and delete. A query that fetches a record by ID alone can expose another tenant's record even when the endpoint checked the user's login. Where the database supports it, consider row-level security so the database enforces the filter even if application code omits it. Test the filter with a user from one tenant requesting IDs that belong to another.

Reject tokens without expected context

Decide which claims are mandatory for each API. If an endpoint requires an organization claim, a token that lacks it, carries an empty value, or lists several organizations without a selected tenant should be refused with a clear error. Do not fall back to a default tenant or to the first organization in a list, because that silently widens access. Document the required claims in your API contract so client teams know what their tokens must contain.

Log cross-tenant attempts

Record every request where the organization claim does not match the resource tenant, including the subject, the token's issuer, the requested resource and the endpoint. These events are the early signal of a broken client, a misconfigured mapper or an attempted data access. Send them to your central log store with a distinct event type so security teams can alert on them. Review the log volume after a rollout, since a single misconfigured client can produce a steady stream of mismatches.

Treat claims as data, own the design

A claim is data sent by a token issuer and read by your application. Keycloak can place organization information in the token, but it cannot know which of your resources belong to which tenant or which endpoints need which check. Your team must define the tenant model, the authorization rules and the tests that prove isolation holds. Ask your partner or internal reviewers to confirm this design in writing before build, and retest after every change to mappers or client scopes.

Questions teams ask before adding organization claims

What does adding organization claims to Keycloak tokens cost?

Cost depends on how many tenants the token design must serve, how many applications read the claim, and how much of the application code has to change to enforce tenant isolation. The main cost is engineering time for claim design, mapper configuration, testing across tokens, and application changes. Recurring cost comes from keeping mappers and tests aligned when a new Keycloak minor release arrives or a new application joins. Ask any quote to list the realm and client count in scope, which applications will consume the claim, whether application code changes are included, and who writes the verification tests. DeployOpen does not publish a price list; a quote follows from the written scope.

How long does an organization claims project take?

Duration follows scope. A single realm with one or two applications and a small, fixed claim set is a much smaller job than a multi-application SaaS platform where each application must reject tokens from other tenants. The schedule is driven by three things: how quickly the claim model is agreed, how many applications need changes, and how long test users and application owners take to respond. DeployOpen agrees timelines in writing as part of a fixed-scope engagement, so the timeline is set when the scope is set, not estimated here.

What must our team provide before the work starts?

Most delays come from missing inputs rather than from configuration. Plan to provide:

  • A realm plan that states whether tenants are organizations inside one realm or separate realms, and which realm holds which users.
  • Named application owners who can confirm how each application reads tenant context and who can test changes.
  • Test users who belong to one, several and no organizations, so token behavior can be checked for each case.
  • Access to a non-production Keycloak and to the applications under test.

Confirm the list of required inputs with your own team before a quote is signed.

Can our internal team add organization claims without outside help?

Yes, if your team already runs Keycloak in production, has administrators who know its client scopes and protocol mappers, and owns the applications that will read the claims. The harder parts are usually application-side: deciding where tenant checks belong, writing tests that prove one tenant cannot read another tenant's data, and keeping token size under control as memberships grow. Teams without that experience often bring in a partner for the design review and the isolation tests, then run the configuration themselves. The split can be written into the scope.

How should we scope a fixed-scope engagement for this work?

Start with the list of applications that will consume tenant context and the claim each one needs. Then define the claim model: which organization attributes appear in the token, whether group or role data is included, and which values are used for the check in each application. Next, agree what counts as done: named token test cases, the isolation tests run against each application, and the documentation handed over. Ask for deliverables, exclusions and the process for changes in writing. Changes found during testing should go through a written change request so the scope and quote stay accurate.

What makes organization tokens grow too large?

Token size grows with the number of organizations a user belongs to and with how much data each mapper places in the token. A user in many organizations can produce a claim that is large enough to cause header-size problems at proxies and gateways. The usual fixes are to include only the organization the request is for, to move detail to a lookup in the application, and to keep group and role mappers narrow. Confirm the largest membership count you expect, the gateways between Keycloak and the application, and the header limits each one enforces before choosing a design.

Does adding a tenant claim alone isolate tenant data?

No. A claim in the token tells the application which tenant the user is acting for, but the application must check that claim on every request and must scope every database query to it. Keycloak can issue the claim correctly and the application can still leak data if it trusts a tenant identifier from the URL or request body. Isolation work therefore includes application code review, authorization tests with users from different tenants, and checks that the claim is validated against the token signature and issuer. Ask any partner how those tests will be run and what evidence they will hand over.

How DeployOpen scopes and delivers this work

DeployOpen starts with the tenant model, because the claim and group design depends on it. Before any mapper is configured, the team agrees in writing which tenant identifier the application will trust, whether tenants are modeled as organizations inside one realm or as another structure, which organization attributes and groups become claims, and which roles are granted per tenant versus globally. The scope also records the token size expectations for users who belong to many organizations, the claim names the application code will read, and the applications that receive each token. Most engagements are fixed-scope, so these decisions, the deliverables and the timeline are written down before implementation begins. Ask the partner to show the claim inventory as a table that the application team signs off on.

Implementation happens in your own Keycloak, on your infrastructure, using your naming conventions and change process. DeployOpen configures the organization claim mapper, the group and role mappers, and the client scopes that control which claims each application receives, then exports the configuration so it can be reviewed and reproduced in your environment. Where the configuration is managed as code, the work is delivered in that form. Confirm with the partner which Keycloak version and deployment method the build targets, since those determine how mappers and realm exports behave during later upgrades and how the work is promoted between environments.

Isolation is tested with the application team, not only with Keycloak. DeployOpen builds test accounts that belong to several organizations, and checks that each token carries only the tenant context the application is meant to trust, that a user cannot change the tenant identifier by editing a request, and that the application rejects tokens whose tenant does not match the requested resource. Findings are fixed in the Keycloak configuration or the application contract, then retested. Changes after go-live, such as a new claim for a new product area or a change to group structure, can be handled through a retainer covering ongoing support, upgrades and continuous testing. Ask what the retainer includes, how change requests are estimated, and who in your team approves changes to claims that carry authorization decisions.

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.