Roles in a Keycloak token
Keycloak has two kinds of role. Realm roles belong to the whole realm, such as admin or order-manager. Client roles belong to one client, such as orders:read on an orders-api client. Users get roles directly, through groups, or through composite roles that contain other roles.
When Keycloak issues an access token, it writes the roles the client is allowed to see into two claims. Your API reads those claims to decide what the caller may do. If a role is not in the token, your API cannot see it, whatever the admin console shows for the user.
Where the roles are in the token
"realm_access": {
"roles": ["default-roles-acme", "offline_access", "uma_authorization", "order-manager"]
},
"resource_access": {
"orders-api": { "roles": ["orders:read", "orders:write"] },
"account": { "roles": ["manage-account", "view-profile"] }
}| Claim | Holds |
|---|---|
realm_access.roles | Realm roles, as a flat list |
resource_access.<client-id>.roles | Client roles, grouped by the client that owns them |
scope | The scopes granted to the token; not roles, but often confused with them |
groups | Group paths, only if a group membership mapper adds them |
Composite roles are expanded: if a user has a composite role, the token lists the composite and every role it contains.
What this viewer does
- Lists realm roles with a short note for the roles every realm has.
- Lists client roles in a table, client by client.
- Answers “does this token have role X?” and says whether it is a realm role, a client role or a group.
- Shows the realm (from
iss), the client (azp) and the user. - Gives every role as one line,
realm:roleorclient:role, ready to copy into a test or a ticket.
It also shows a groups claim and a custom roles claim if a mapper added them, and the token’s scopes.
Step by step
- Paste a Keycloak access token.
- Read the realm roles and the client roles tables.
- Type a role name in the search box to see whether the token carries it, and where.
- Copy the full list if you need it elsewhere.
Default roles every realm has
Each realm has a default role named default-roles-<realm>, which Keycloak gives to every new user. It is a composite role. By default it contains the realm roles offline_access and uma_authorization, and the account client’s roles view-profile and manage-account.
offline_accessallows the user’s apps to request offline tokens with theoffline_accessscope.uma_authorizationis used by User-Managed Access.view-profileandmanage-accountlet the user use the account console.
Because the account client roles are in the token, Keycloak also adds account to the token’s audience. You can change what the default role contains under Realm settings, User registration.
How Keycloak decides which roles go in
The roles client scope
The realm_access and resource_access claims are written by mappers in the built-in roles client scope. A client that does not have this scope assigned gets tokens without them. Check under Clients, then your client, then Client scopes.
Full scope allowed
Each client has a dedicated scope with a Full scope allowed switch, on its Scope tab. When it is on, every role the user has can appear in the token. When it is off, only roles listed in the client’s scope mappings appear. Turning it off keeps tokens small and limits what a leaked token grants.
Access token, ID token, userinfo
The role mappers add roles to the access token. Adding them to the ID token or the userinfo response is a separate switch on each mapper. That is why an ID token often has no roles.
Try it without an app
In the admin console, open the client, then Client scopes, then Evaluate. Choose a user and generate an example access token. Paste it here to see exactly which roles that client would receive for that user.
Use cases
A user gets 403 from an API
Paste the token the API rejected and search for the role the API requires. If it is missing, the problem is in Keycloak’s configuration, not in the API.
Writing authorization rules
See the exact shape your code must read: client id keys under resource_access, role names as they appear, and whether you need realm or client roles.
Reviewing least privilege
A token that lists roles for many clients the app never calls is a sign that Full scope allowed is on. Each extra role is something a leaked token could be used for.
Why a role is missing
| Check | Where in the admin console |
|---|---|
| The user has the role, directly or through a group | Users, the user, Role mapping |
The client has the roles client scope | Clients, the client, Client scopes |
| The role is allowed by the client’s scope | Clients, the client, Client scopes, dedicated scope, Scope |
| The mapper adds roles to this kind of token | Client scopes, roles, Mappers |
| The token is new | Get a fresh token; old tokens keep old roles |
Reading roles in your API
Once the token is verified, reading roles is a matter of walking two claims. Accept that either may be missing.
const realmRoles = token.realm_access?.roles ?? [];
const apiRoles = token.resource_access?.['orders-api']?.roles ?? [];
const canWrite = apiRoles.includes('orders:write');Many frameworks have Keycloak or OpenID Connect integrations that map these claims to their own role model. Check which claim they read: some default to realm roles only.
Composite roles, worked through
Suppose a realm role order-manager is a composite that contains the client roles orders:read and orders:write on orders-api. A user who has only order-manager gets a token with order-manager in realm_access.roles and both client roles under resource_access.orders-api, provided the client may see them.
This is why the roles in a token can be more than the roles you assigned directly. The admin console shows the expanded set on the user’s Role mapping tab when you turn off the filter that hides inherited roles.
Roles compared with scopes
Roles say what the user may do. Scopes say what the client asked to be allowed to do on the user’s behalf. An API can check both: the token’s scope must include the operation, and the user must have the role for it. In Keycloak, client scopes can also limit roles through their own Scope tab, which ties the two together.
Security notes
Roles in a token are only trustworthy after the signature is verified. Anyone can write admin into a payload; only the realm’s key can sign it. Verify with the signature verifier and check iss and aud before you rely on any role.
Role changes do not reach tokens already issued. A user whose role you removed keeps it until their access token expires, which is one reason to keep access tokens short-lived.
Best practices
- Prefer client roles for permissions specific to one API, and realm roles for broad ones.
- Turn off Full scope allowed and map only the roles each client needs.
- Check roles in the API from verified tokens only.
- Keep large group and role lists out of access tokens; they make tokens big.