Kanishk Kaushik - May 07, 2025
π Role-Based Access Control in Your App with Keycloak Authorization
Managing who can access what in your application is a fundamental requirement for any secure system. With Keycloak, an open-source identity and access management tool, you can go beyond simple role checks and implement a fine-grained authorization system using RBAC (Role-Based Access Control). In this blog, weβll explore how to protect your application endpoints using Keycloak Authorization Services.
π What is Role-Based Authorization?
Role-Based Authorization means access to resources is granted based on the roles assigned to a user. For example:
- A user with the admin role can create and delete users.
- A user with the viewer role can only read data.
RBAC allows us to define clear and manageable access control policies for users without hardcoding them into our application logic.
π‘οΈ Keycloak Authorization Concepts
Hereβs how Keycloak helps implement RBAC using the following core concepts:
β Resources
Resources represent what you are protecting (e.g., endpoints, data objects).
Example: /teams, /users/{id}
π Scopes
Scopes define what actions can be performed on a resource.
Example: view, create, delete, update
πͺͺ Permissions
Permissions map roles or policies to resources and scopes. Example:
adminβ/teams+create,deleteviewerβ/teams+view
π Policies
Policies determine who is allowed to access what. Policies can be based on roles, group membership, client IP, time, or custom JS logic.
π οΈ Small Example: Protect /teams Endpoint
Suppose we have an API endpoint /teams and want only users with the team-admin role to access it.
-
Create Role
In Keycloak Admin Console:
Go toClients > Your Client > Roles
Add role:team-admin -
Create Resource
Go toAuthorization > Resources:- Name:
teams-endpoint - URI:
/teams - Scopes:
view,create
- Name:
-
Create Scope
UnderAuthorization > Scopes, create:viewcreate
-
Create Policy
UnderAuthorization > Policies:- Type: Role-based
- Name:
Team Admin Policy - Apply to role:
team-admin
-
Create Permission
UnderAuthorization > Permissions:- Name:
Teams Access - Resources:
teams-endpoint - Scopes:
view,create - Apply Policy:
Team Admin Policy
- Name:
π§ͺ How to Enforce Access in Your App
1. Get Access Token (Client Credentials)
curl --location --request POST 'http://<KEYCLOAK_HOST>/realms/<realm>/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=<client_id>' \
--data-urlencode 'client_secret=<client_secret>'2. Request RPT Token (UMA Authorization)
curl --location --request POST 'http://<KEYCLOAK_HOST>/realms/<realm>/protocol/openid-connect/token' \
--header 'Authorization: Bearer <access_token>' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:uma-ticket' \
--data-urlencode 'audience=<client_id>' \
--data-urlencode 'permission=/teams#create'If the user has the right role (team-admin), youβll get a valid RPT (Requesting Party Token). Otherwise, the server responds with an error.
3. Protect Endpoint in Your Backend
In your app (e.g., in Go, Python, Node.js), verify the JWT token and optionally check for permissions in the RPT token.
For example, in Go:
// decode and verify RPT JWT
// check if it contains permission: /teams with scope createπ§ͺ Discovering Protected Resources
You can also discover resources via:
GET /auth/realms/<realm>/authz/protection/resource_set?uri=/teams&deep=true
Authorization: Bearer <PAT>This allows your app or API gateway to dynamically check which permissions apply to a URI.
π Final Thoughts
Keycloak makes it easy to manage RBAC and enforce it with its powerful Authorization Services. By clearly defining resources, scopes, roles, and permissions, you can protect your application endpoints dynamically and securely.
Weβve successfully applied this to our own app for team and project creation, ensuring that only users with the admin role can perform create operations β all without hardcoding roles or permissions in our application code.
Next time you’re building a secure app, consider handing off your authorization logic to Keycloak β and focus more on building features, not access checks.
Need a walkthrough? Contact your Customer Success Manager or email us at info@bluefunda.com.