I am currently experimenting with Keycloak as OICD/OAuth provider and planned to use it for authorization based on roles in my application. For that I need the roles to apply for a session to be contained in generated access tokens.
My plan was the following:
So far it’s all possible except that the interesting “roles” claim in generated access tokens contains all roles of the user and not only the roles that are assigned to the requested scopes.
So my question is:
Example:
We have the following scope-role-mappings:
| scope | roles |
|---|---|
| read | r_read |
| write | r_write |
And a user with the roles “r_read” and “r_write”.
When the user logs in with the “read” scope requested I would expect that the “roles” array in the generated access token only contains the “r_read” role. Instead it currently also contains the “r_write” role.
Update:
After some more digging I think I was mislead by the documentation of the "Scope"-Tab in the "Client Scopes" detail-Configuration (or misunderstood it):
Scope mappings allow you to restrict which user role mappings are included within the access token requested by the client.
The assigned roles set there only control which role is required so that the scope can be applied/added to the scope claim of the access token.
The possibilities I currently see are:
Yes, you can. Easy way:
Create needed scope in Clients Scopes menu section [/auth/admin/master/console/#/realms/<your realm>/client-scopes] name it: read (for example)
You can turn off options Display On Consent Screen & Include In Token Scope. We don't need it
After scope saving go to the MAPPER tab (of the scope read)
CREATE buttonHardcoded Role; Name = r_read ; Role=r_readread) to Optional Client Scopes of your main client [/auth/admin/master/console/#/realms/<your realm>/clients/<your client UUID>/client-scopes/setup-scopes]read)realm_access.roles section of your access token will look something like that: "realm_access": {
"roles": [
"offline_access",
"uma_authorization",
"r_read"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With