Keycloak - prevent assignment of roles

Viewed 46

I am working on service to service authentication using the client credentials flow. I am using keycloak as the authorization server.

Let's say, there are two services S1 and S2.
Every service will allow only certain other services to call its APIs. It might also happen that two different APIs of one service might permit different set of callers.

I have created roles. I have created scopes and mapped the roles to the scope. When I am passing the scope in the get token call (/realms/realm_name/protocol/openid-connect/token), I am getting the token with roles that are mapped to the passed scope.

What I am not able to figure out is how do I restrict the assignment of different roles to different services?

Only service owner or admin should be able to login to keycloak and should be able to assign the roles corresponding to his/her service to other services. He should not be able to do anything with roles not meant for his service.

Eg: S1 has owner O1 (a user in keycloak). S1 has three APIs A1, A2 and A3. To call A1 API, caller should have R1 role, and to call A2 API, caller should have R2 role.

Now if service S2 wants to call S1's API A1, only admin or user O1 should be able to attach role R1 to S2's client in keycloak (or add role R1 to scope attached to service client S2). This might be related to limited access to perform different operations in keycloak.

1 Answers

If I understood your request correctly you can try to use client finer permission. Run keycloak with the following flag:

-Dkeycloak.profile.feature.admin_fine_grained_authz=enabled

Then in your client you can choose which user can do what with the client:

enter image description here

For example, you can client on the permission view and then create a policy based on the user, and then configure the rest accordingly.

You can in the Keycloak official documentation read more about Managing policies.

Related