Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak - What is the best way to get User's role

Tags:

keycloak

Hi I'm using Keycloak and I would like to know what is the best way to get User Role. I'm using a SPA written in ReactJS and it needs to know the user's role. Is there a Keycloak API to get this? or should I get it from the JWT token. Either way, what is the option that I should go with. Thank you.

like image 836
Darth Shirr Avatar asked Oct 07 '19 10:10

Darth Shirr


People also ask

How do I get the roles of a user in a Keycloak?

A user would have to be authenticated before seeing some application content. After successful authentication, access token would be given to client (can be application gateway or ui application) and then role can be extracted from it and used.

What are client roles in Keycloak?

Keycloak provides the concept of a client scope for this. Clients can define roles that are specific to them. This is basically a role namespace dedicated to the client. A token that provides identity information about the user.

How do you manage a role in a Keycloak?

You can either create a global (realm level) role, and easily access it from both clients, or you can create two identical roles in each client. Depending on your project scale, pick from what suits you best. Thank you, I got it - once again keycloak documentation is really weak in the area.


2 Answers

If you assigned role to a user, then this role is a claim inside JWT access token provided by Keycloak.

A user would have to be authenticated before seeing some application content.

After successful authentication, access token would be given to client (can be application gateway or ui application) and then role can be extracted from it and used.

like image 92
rok Avatar answered Oct 23 '22 04:10

rok


Yes, rest API is there to get user roles

GET /auth/admin/realms/{realm}/users/{user-uuid}/role-mappings/clients/{client-uuid}

you can find all the rest api of keycloak here:

https://www.keycloak.org/docs-api/5.0/rest-api/index.html

like image 40
niks Avatar answered Oct 23 '22 05:10

niks