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.protocol
module. - 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 Scopes
section. - Click on
Create client scope
. - Enter the name, description, and the other information according your requirements. The
Protocol
field must be OpenID Connect. - Click on
Save
.
-
After creating the client scope, click on the
Mappers
tab. -
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 Name
and 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 group
works 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 attributes
andInclude client role attributes
. -
Click on
Save
.
- Navigate to the
Clients
section. - Select the client you want to configure.
- Go to the
Client Scopes
tab and select theSetup
sub tab. - Click on the
Add client scope
and select the previous client scope configured.
- Navigate to the
Clients
section. - Select the client previously configured.
- Go to the
Client Scopes
tab and select theEvaluate
sub tab. - Choose a user and click on
Generated access token
. - You should see the claim configured according the OIDC General Attribute Mapper.