<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Padmakar Kasture on Medium]]></title>
        <description><![CDATA[Stories by Padmakar Kasture on Medium]]></description>
        <link>https://medium.com/@kasturepadmakar4u?source=rss-e71cb058e6c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*x-YvNel_mLQv09oY</url>
            <title>Stories by Padmakar Kasture on Medium</title>
            <link>https://medium.com/@kasturepadmakar4u?source=rss-e71cb058e6c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:25:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kasturepadmakar4u/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Resource And Scope Based Authorization in Keycloak]]></title>
            <link>https://medium.com/@kasturepadmakar4u/resource-and-scope-based-authorization-in-keycloak-1fdb90408e91?source=rss-e71cb058e6c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fdb90408e91</guid>
            <category><![CDATA[keycloak]]></category>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[access-control]]></category>
            <category><![CDATA[rbac-access-control]]></category>
            <dc:creator><![CDATA[Padmakar Kasture]]></dc:creator>
            <pubDate>Sun, 14 Jan 2024 09:02:05 GMT</pubDate>
            <atom:updated>2024-01-19T03:49:08.516Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*D5tFfu8nZAIWCrDOMfeGGA.jpeg" /></figure><p>in this article, we will go step by step and learn how can we achieve resources and scope based authorization in keycloak.</p><p><strong>why resource and scope-based authorization is necessary?</strong></p><p>genrally in industry role based access control is used in which they hardcode a role to a specific apis Exaple (Nest js)-</p><pre>import { Roles} from &#39;nest-keycloak-connect&#39;;<br>@Controller(&#39;/entity&#39;)<br>export class entityController {<br>  constructor(private readonly userService: UserService) {}<br><br>@Get()<br>@Roles(&#39;employee&#39;)<br> getpublic(): string {<br>    return `${this.userService.getHello()} from public`;<br>  }<br><br>@Post()<br>@Roles(&#39;admin&#39;)<br> getUser(): string {<br>    return `${this.userService.getHello()} from user`;<br>  }<br><br>}</pre><p>here only user with role “employee” can access the entity get api.</p><p>Problem with this approach is if we introduced a new role and need access of GET /entity for that role then we have to alter the code and if we alter the code it needs to be deployed and tested and then updated to the production that means we can not change authorization in running application or we have to assign employee role for that user which will give all access of employee to the user(but we want only for single api).</p><p>to solve this problem we have to implement dynamic authorization dependent upon <strong>API and method. </strong>we can do with the help of resource and scope.</p><p><strong>Let’s understand resources and scopes</strong></p><p><strong>Resource</strong>: A resource is any real-world entity that is used in the application.</p><p><strong>Scope: </strong>scope is the operation that we will perform on resource/entity</p><p>ex-</p><p>Post /user</p><p>Get /user</p><p>Post /profile</p><p>here user and profile an entity /Resource and GET, POST is a scope.</p><p>we can consider scope as create,read update,delete.</p><pre>import { Resource, Scopes } from &#39;nest-keycloak-connect&#39;;<br>@Controller(&#39;/entity&#39;)<br>@Resource(&#39;entity&#39;)<br>export class entityController {<br>  constructor(private readonly userService: UserService) {}<br><br>@Get()<br>@Scope(&#39;GET)<br> getEntity(): string {<br>    return this.entityService.read();<br>  }<br><br>@Post()<br>@Scope(&#39;POST&#39;)<br> createEntity(): string {<br>    return this.entityService.create();<br>  }<br><br>}</pre><p>thats have we fixed hard coded role problem at the backend side now let’s configure this in keycloak.</p><p><strong>Configurtion of Resources and Scopes in keycloak.</strong></p><p><strong>Step1</strong>: create a realm with your project name. i have created with my-application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w4a9fKqEQWjhqX3F1oaRMw.png" /></figure><p><strong>Step2</strong>: Go to clients</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RFYZy5ods09mBPWL4irH5Q.png" /></figure><p><strong>Step3</strong>: Create client with name backend-client</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kzneLsXIBp9CWAJ52JMPHg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QvJgZAL44IeOkw3JIGxLbQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q7qYBTgJA-6P8FPFT1fVig.png" /></figure><p><strong>Step 4: </strong>Go to Authorization tab on the top bar and choose affirmative from the settings tab</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dJtOQ5Nk9OpALvltU-XLsg.png" /></figure><p><strong>Step 5:</strong> Go to Authorization tab on the top bar and create scopes by clicking on create authorization scopes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wi4tiQ3cIZfS3vojZEhYZw.png" /></figure><p><strong>Step 6:</strong> create scopes with GET and POST</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RdSfvyZK_AM7gacMqrAJ9Q.png" /></figure><p><strong>Step6</strong>: create Resource entity in resource tab and assign or scopes to the entity</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M85Z5Eai4NsOEH1P1O5Nvg.png" /></figure><p>we have added our apis resource and scope data in keycoak now we will add roles .</p><p><strong>Step 7:</strong> creating admin and employee role</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ewssJBmYnulya6uLBAgwg.png" /></figure><p>let us create a policy and permission for the api</p><p>here policy is basically evaluation rule for the permission .</p><p><strong>step 8: </strong>create <strong>Role</strong> based policies.</p><p>clients → backend-client → authorization → policies → create-policy → Role based policy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5iQ7bFhfLnX8InFh7GboAw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iKoi3ixgxOrjVMUnAGhaeA.png" /></figure><p><strong>Step 9 :</strong> Create Permissions</p><p>here we will create get permission for employee and post permission for admin also we will assign respective role policy to the permission.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bn995pXPW4yst72fhdllfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZf5TQLiOqhme5PToSI1IA.png" /></figure><p><strong>Step 10: </strong>Create an user and assign admin role to user i have added user as dummy user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VvZGYHxDoWkITmqL3MWnmw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZnyCdiI0g7tC6f_IDpyWbw.png" /></figure><p><strong>Step 11:</strong> Evaluate Permission for dummyuser in authorization → evaluate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfRw5gbXUQyH11zzf5G7GQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ME7EooqJYyqq00KXGKiARw.png" /></figure><p>you will get DENY.</p><p>similarly check for post you will get Permit</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dn9-hc9FXL3DHwQjrc5rkg.png" /></figure><p>if you remove admin role and assign employee role to the user it will work accordingly</p><p><strong>Let’s revisit our problem that what if we introduced a new role and wanted to give permission to single api?</strong></p><p>in this scenario you have to create a<strong> new role → create a policy for that role → and add a new permission for that policy.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wPvrNrEgUB_rucaR8braYQ.png" /></figure><p>now assign newRole to user and evaluate you will get result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Jfx_q_WkjSRy9pXgybreA.png" /></figure><p>If you have any doubt feel free to connect on linkedin <a href="https://www.linkedin.com/in/padmakar-kasture">https://www.linkedin.com/in/padmakar-kasture</a></p><p>Thank you for reading this post. If you do have any questions please add as a comment. I will definitely answer your questions. If you like this post, give a clap .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fdb90408e91" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>