This custom mapper is based on UserAttributeMapper.java, which allows mapping attributes to token claims. The attributes can be obtained from one or more of the following elements:
- Users
- Realm Roles
- Client Roles
- Groups
- Roles associated with Groups
- You must compile and generate the jar file of the
keycloak-extensions.protocolmodule. - Put the generated jar file in your Keycloak provider folder and restart the server.
You can see a more detailed instructions on the official Keycloak documentation
- Login to the Keycloak Administration Console.
- Navigate to the
Client Scopessection. - Click on
Create client scope. - Enter the name, description, and the other information according your requirements. The
Protocolfield must be OpenID Connect. - Click on
Save.
-
After creating the client scope, click on the
Mapperstab. -
Click on
Add mapper by configuration. -
Click on the mapper with the name
Attribute from Users, Realm Roles, client roles or Groups. -
Enter the basic configuration:
Name,Attribute,Token Claim Nameand the others fields. -
You must select at least one of the following options:
Include user attributes,Include realm role attributes,Include client role attributes,Include group attributes.The option
Include roles associated in the groupworks only when selecting the optionInclude group attributes. It is important to note that since groups can have associated realm roles as well as client roles, when including the roles of the groups, it is necessary to additionally specify at least one of these options:Include realm role attributesandInclude client role attributes. -
Click on
Save.
- Navigate to the
Clientssection. - Select the client you want to configure.
- Go to the
Client Scopestab and select theSetupsub tab. - Click on the
Add client scopeand select the previous client scope configured.
- Navigate to the
Clientssection. - Select the client previously configured.
- Go to the
Client Scopestab and select theEvaluatesub tab. - Choose a user and click on
Generated access token. - You should see the claim configured according the OIDC General Attribute Mapper.