Reputation: 2103
Under Realm Settings -> User Profile I added some custom attributes
I want these to be returned as part of the JWT upon successful login. I tried to map them the way I mapped roles (Client - Client Scopes - dedicated - add mapper) but they aren't being included in the JWT after login.
How do I get these in there?
Upvotes: 2
Views: 3141
Reputation: 51443
The User Profile
is about the attribute scheme and enforcing constraints on it. However, for those attributes to show up on the token, besides the user attribute mapper, the user to whom the token is being acquired on behalf of needs to have those attributes set.
One can set those attributes by going to Users, selecting the user in question, and then switching to tab Attributes and adding the attributes (e.g., "Department" : "lol"). Or require the user to manually do so by forcing the update of the profile for example.
A demo example using the new Keycloak API:
Declarative User Profile is Technology Preview and is not fully supported. This feature is disabled by default.
To enable start the server with --features=preview or --features=declarative-user-profile
Then go to:
"User Profile Enabled"
to ON
(if not yet done)Save
buttonSwitch to the tab User Profile
:
Create attribute
Save
buttonNow on the client that will be used for authentication do:
Client Scopes
Configure a new Mapper
User Attribute
Save
buttonNow, either you or your users need to fill up their "Department attribute" in order for that attribute to show up on the token.
a) To manually add the attribute on behalf of the user:
Users
Attributes
Now if you request a token on behalf of the user above using the client to which we have created the User Attribute
mapper, you should see the attribute Department
on the access token. Something like:
✻ Payload
{
"exp": ...,
"iat": ...,
"jti": ..",
"iss": "...",
"sub": "...",
"typ": "...",
"azp": "test_client",
"session_state": ...",
"acr": "1",
"scope": "email profile",
"sid": "...",
"Department": "a",
"email_verified": ...,
"name": "a a",
"preferred_username": "test_user",
"given_name": "a",
"family_name": "a"
}
b) To enforce the users to add their mandatory attributes:
Authentication
Required Actions
Update Profile
to ON
If the user now logs in into kc_dns/realms/{REALM_NAME}/account
for instance, you should get something like:
After the user adds the attribute and press Submit
. Now if you request a token on behalf of the user above using the client to which we have created the User Attribute
mapper, you should see the attribute Department
on the access token.
Upvotes: 4