I am trying to achieve fairly simple usecase of role based client application (VueJS multi-page applications) control using the keycloak.
As shown in image, I have three different roles and three different clients in single realm.
The arrow in the image represents which role can access which client.
So my main objectives are,
Viewer
should only be able to log-in to the Viewer Application
. If the same user tries to access the Operator Application
or Admin application
then keycloak should simply deny this user from doing so.Admin
and Operator
role. Users of Admin
role should be able to log-in to any of these application by keycloak.To achieve this usecase I tried following ways,
Authorization
. In the policies, I removed default policy that grant all users access to the client. And create a User
policy and Client
policy to restrict the access to client applicationGroup
based authorization policy. In this case, I created a group with client role and then assigned user to these groups. And enabled them from the Authorization
group policy.But, unfortunately none of this works. Meaning my user with Viewer
role can log-in to my admin application. Which is just strange.
Resource owners are allowed to manage permissions to their resources and decide who can access a particular resource and how. {project_name} can then act as a sharing management service from which resource owners can manage their resources.
The first step to enable Keycloak Authorization Services is to create the client application that you want to turn into a resource server. Click Clients. On this page, click Create client. Type the Client ID of the client.
To access the admin console, open http://localhost:8080/auth/admin/ in a browser. You will be redirected to the Keycloak login pages, where you can log in with the admin username and password you created in the previous section while installing Keycloak.
You can do this without extensions.
Access By Role
and select generic
as type.CONDITIONAL
is selected in the flow overview.Condition - User Role
, make it REQUIRED
and configure it:
Deny Access
and make it REQUIRED
as well.The final result should look similar to this:
This will deny access if the condition "admin-role-missing" is true.
You an also learn more from the docs: explicitly-deny-allow-access-in-conditional-flows
Also, don't forget to go to your client and select the flow in the authentication overrides.
I managed almost the same problem using KeyCloak extension SPI. After the deployment you will have additional configurable "execution" in authentication flows available, named "Validate User Role".
The auth flow then look's like :
This execution must be placed after the "Username Password Form" (or other form which authenticates user) or the authentication will fail.
The source code is here : https://github.com/ValentinChirikov/kc_user_role_validate_extension
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