<?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 Dineshtyagi on Medium]]></title>
        <description><![CDATA[Stories by Dineshtyagi on Medium]]></description>
        <link>https://medium.com/@dineshtyagi567?source=rss-d8f47673e02d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*05jrJ3h7TefMkQut</url>
            <title>Stories by Dineshtyagi on Medium</title>
            <link>https://medium.com/@dineshtyagi567?source=rss-d8f47673e02d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:07:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dineshtyagi567/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[Implementing Relationship Based Access Control (ReBac) using Node and Oso]]></title>
            <link>https://medium.com/@dineshtyagi567/implementing-relationship-based-access-control-rebac-using-node-and-oso-0c64274d5af6?source=rss-d8f47673e02d------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c64274d5af6</guid>
            <category><![CDATA[node]]></category>
            <category><![CDATA[rebac]]></category>
            <category><![CDATA[oso]]></category>
            <category><![CDATA[typecript]]></category>
            <category><![CDATA[authorization]]></category>
            <dc:creator><![CDATA[Dineshtyagi]]></dc:creator>
            <pubDate>Sun, 03 May 2026 01:11:10 GMT</pubDate>
            <atom:updated>2026-05-03T01:11:10.361Z</atom:updated>
            <content:encoded><![CDATA[<p>Modern applications require complex authorization, that’s where Relationship-Based Access Control (ReBAC) comes in.</p><p>I went through this research paper: <a href="https://dl.acm.org/doi/10.1145/1943513.1943539">Relationship-based access control: protection model and policy language</a> in order to understand, learn, and implement it. Here’s everything I learned along the way.</p><h3>The Paper</h3><p>The paper proposes a type of access control characterized by the relationships between users and resources, and control policies based on those relationships. This allows authorization logic to take into account the context of the relationships, giving a high level of control over how resources are accessed.</p><h3>Why I Implemented It</h3><p>While job hunting, I recently came across an assignment where I had to implement ReBAC. I searched what it was, found it genuinely interesting, and here we are. I was also inspired by a friend on Twitter to go through an actual research paper.</p><h3>Tech Stack</h3><ul><li><strong>Node.js</strong></li><li><strong>TypeScript</strong></li><li><strong>Oso Cloud</strong></li></ul><h3>What is Relationship-Based Access Control?</h3><p>A simple way to visualize it:</p><p>Student → Courses → Professors</p><ul><li><strong>Relationship 1:</strong> A student is enrolled in a course</li><li><strong>Relationship 2:</strong> A course is taught by a professor</li></ul><p>We can combine these relationships to answer queries like “Which professor teaches which student?” — even though there’s no direct relationship between them. What we’re really talking about here is <strong>transitivity</strong>:</p><pre>A =&gt; B<br>B =&gt; C<br>∴ A =&gt; C</pre><p>Importantly, it doesn’t just track whether a relationship exists, it also tracks the type of that relationship. That distinction gives us more context to work with.</p><p>Some important components of ReBac are:</p><ol><li><strong>User</strong></li><li><strong>Resource</strong></li><li><strong>Resource Owner</strong></li><li><strong>Relationship Identifiers</strong></li><li><strong>Context</strong></li></ol><h3>Context in ReBAC</h3><p>An important concept in ReBAC is <strong>context</strong>. The context includes all the relationships in the network, which are matched against policies to arrive at an authorization decision.</p><p>The <strong>viewer</strong> must be in a specific kind of relationship with the <strong>owner</strong> in order to access a resource.</p><p>Associated with every resource is an <strong>access control policy</strong>, a ternary predicate (3 inputs):</p><ol><li><strong>Owner</strong></li><li><strong>Viewer</strong></li><li><strong>A Social Network</strong> (the graph of relationships between users, modeled using Relationship Identifiers)</li></ol><p>This predicate returns a boolean.</p><p>Notably, each context might result in a <strong>different authorization decision</strong> even if the resource, user, and owner are the same. For example: a close relative of a patient wants to access a medical record, when they opt to <em>view</em>, they’re authorized; when they opt to <em>edit</em>, they’re not.</p><p>There are 3 sources an access control policy can come from:</p><ol><li><strong>Mandatory</strong> — set by the sysadmin on all resources</li><li><strong>Discretionary</strong> — set by the resource owner</li><li><strong>Policy Vocabulary</strong> — predefined policies users can choose from</li></ol><p>The <strong>social network</strong> is the graph of users and resources, recording the relations articulated in a given context.</p><h3>How Authorization Works</h3><p>Authorization is achieved by traversing relationships in the social network. Here’s the flow:</p><ol><li>The viewer attempts to access a resource</li><li>The system looks up the <strong>policy predicate</strong> associated with the resource and its owner</li><li>The <strong>effective social network</strong> (the context) is derived</li><li>The system applies the predicate to the owner, viewer, and social network to arrive at an authorization decision</li></ol><p>Two important rules govern this:</p><ul><li><strong>Rule 1:</strong> Only the social network aspect of the context is significant</li><li><strong>Rule 2:</strong> Effective social networks of descendant contexts can inherit relationships defined in ancestor contexts, a child context contains <em>no fewer</em> relationships than its parent</li></ul><p>The root context is the root node of the contextual hierarchy tree. The extends relation defines this hierarchy. A tree-shaped hierarchy corresponds to the nested structure of relationship scopes:</p><blockquote>c1 extends c2 if and only if c1 is either c2 or one of the descendants of c2.</blockquote><h3>The Implementation</h3><p>The paper references an EHR (Electronic Health Records) system as its example, so that’s what I implemented in TypeScript, using Oso Cloud.</p><p>Github Repository — <a href="https://github.com/Dinesht04/rebac-ts-implementation"><strong>rebac-ts-implementation</strong></a></p><figure><img alt="Tree representation of the EHR Model" src="https://cdn-images-1.medium.com/max/393/1*zZeY5yqIY72Bi4ZiJV1EOg.png" /></figure><h3>Step 1: Define the Rules in Polar</h3><p>Oso uses its own policy language called <strong>Polar</strong>. Here’s the schema I defined in Oso’s rules editor:</p><pre>actor Physician {}<br><br>actor User {<br>  relations = {<br>    emergencyContact: User<br>  };<br>}<br>resource Institution {<br>  roles = [&quot;nurse&quot;, &quot;admin&quot;];<br>}<br>resource Case {<br>  permissions = [&quot;edit&quot;, &quot;view&quot;];<br>  roles = [&quot;editor&quot;, &quot;viewer&quot;];<br>  relations = {<br>    institution: Institution,<br>    patient: User,<br>    doctor: Physician<br>  };<br>  &quot;edit&quot; if &quot;editor&quot;;<br>  &quot;view&quot; if &quot;viewer&quot;;<br>  &quot;viewer&quot; if &quot;editor&quot;;<br>  # admin permissions<br>  &quot;editor&quot; if &quot;admin&quot; on &quot;institution&quot;;<br>  # patient permissions<br>  &quot;viewer&quot; if &quot;patient&quot;;<br>  # emergency contact permissions<br>  &quot;viewer&quot; if &quot;emergencyContact&quot; on &quot;patient&quot;;<br>  # doctor permissions<br>  &quot;editor&quot; if &quot;doctor&quot;;<br>  # nurse permissions<br>  &quot;editor&quot; if &quot;nurse&quot; on &quot;institution&quot;;<br>}<br>resource Treatment {<br>  permissions = [&quot;edit&quot;, &quot;view&quot;];<br>  roles = [&quot;editor&quot;, &quot;viewer&quot;];<br>  relations = {<br>    case: Case,<br>    patient: User,<br>    physician: Physician<br>  };<br>  &quot;edit&quot; if &quot;editor&quot;;<br>  &quot;view&quot; if &quot;viewer&quot;;<br>  &quot;viewer&quot; if &quot;editor&quot;;<br>  &quot;editor&quot; if &quot;editor&quot; on &quot;case&quot;;<br>  &quot;viewer&quot; if &quot;editor&quot;;<br>  &quot;editor&quot; if &quot;physician&quot;;<br>  &quot;viewer&quot; if &quot;patient&quot;;<br>  &quot;viewer&quot; if &quot;emergencyContact&quot; on &quot;patient&quot;;<br>}</pre><h3>Step 2: Initialize the Project</h3><p>Set up a new TypeScript project and add your Oso Cloud API key to .env:</p><p>OSO_KEY=&quot;your_api_key&quot;</p><p>Then initialize the Oso client:</p><pre>import dotenv from &#39;dotenv&#39;<br>import { Oso } from &#39;oso-cloud&#39;;<br>import { addCases, addInstitutionStaff, addPatientsAndEmergencyContacts, treatmentsPatient1, treatmentsPatient2 } from &#39;./data.js&#39;;<br>import type { DefaultPolarTypes } from &#39;oso-cloud/dist/src/helpers.js&#39;;<br><br>dotenv.config()<br>const apiKey = process.env.OSO_KEY as string;<br>const oso = new Oso(&#39;https://cloud.osohq.com&#39;, apiKey)</pre><h3>Step 3: Add Facts (Roles &amp; Relations)</h3><p>For this example, we use two types of facts — has_role and has_relation:</p><pre>// User to User relationship (emergency contact)<br>await oso.insert([&quot;has_relation&quot;, {type: &quot;User&quot;, id: &quot;patient_1&quot;}, &quot;emergencyContact&quot;, {type: &quot;User&quot;, id: &quot;contact_1&quot;}]);<br>// Resource to Resource relationship (Case belongs to Institution)<br>await oso.insert([&quot;has_relation&quot;, {type: &quot;Case&quot;, id: &quot;case_b&quot;}, &quot;institution&quot;, {type: &quot;Institution&quot;, id: &quot;general_hospital&quot;}]);<br>// Resource to Actor relationship (Case belongs to patient)<br>await oso.insert([&quot;has_relation&quot;, {type: &quot;Case&quot;, id: &quot;case_b&quot;}, &quot;patient&quot;, {type: &quot;User&quot;, id: &quot;patient_2&quot;}]);</pre><p>Note: This doesn’t include all the insertions, check github repository for all.</p><h3>Step 4: Authorize</h3><p>The authorize function returns a boolean:</p><pre>// Dr. Strange tries to edit a case they&#39;re not assigned to<br>let isAllowed = await oso.authorize(<br>  { type: &#39;Physician&#39;, id: &#39;dr_strange&#39; }, // Actor<br>  &#39;edit&#39;,                                   // Action<br>  { type: &#39;Case&#39;, id: &#39;case_a&#39; }            // Resource<br>);<br>console.log(isAllowed); // false — not assigned to dr_strange</pre><pre>// An emergency contact tries to view a treatment<br>isAllowed = await oso.authorize(<br>  { type: &#39;User&#39;, id: &#39;contact_1&#39; },<br>  &#39;view&#39;,<br>  { type: &#39;Treatment&#39;, id: &#39;treat_a1&#39; }<br>);<br>console.log(isAllowed); // true — legitimate emergency contact</pre><pre>// An emergency contact tries to view a treatment<br>isAllowed = await oso.authorize(<br>  { type: &#39;User&#39;, id: &#39;contact_1&#39; },<br>  &#39;view&#39;,<br>  { type: &#39;Treatment&#39;, id: &#39;treat_a1&#39; }<br>);<br>console.log(isAllowed); // true - legitimate emergency contact</pre><p>All permissions and roles are derived from the Polar schema defined in the Oso Cloud console. (Oso also has a neat section where you can inspect all facts and query logs — really helpful for debugging.)</p><p>The full code is on GitHub: <strong>rebac-ts-implementation</strong></p><h3>References</h3><ul><li><a href="https://www.osohq.com/docs/reference/sdks/overview"><strong>Oso Typesript SDK</strong></a></li><li><a href="https://www.osohq.com/post/relationship-based-access-control-rebac-in-node-js-with-oso-cloud"><strong>Oso Blog Node.js</strong></a></li><li><a href="https://dl.acm.org/doi/10.1145/1943513.1943539">Relationship-based access control: protection model and policy language</a></li></ul><p>If you have questions or feedback, feel free to reach out!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c64274d5af6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hosting a NODE Websocket back-end on Render]]></title>
            <link>https://medium.com/@dineshtyagi567/hosting-a-node-websocket-back-end-on-render-eeebca9aab8e?source=rss-d8f47673e02d------2</link>
            <guid isPermaLink="false">https://medium.com/p/eeebca9aab8e</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[render]]></category>
            <category><![CDATA[websocket]]></category>
            <dc:creator><![CDATA[Dineshtyagi]]></dc:creator>
            <pubDate>Sat, 26 Jul 2025 01:13:37 GMT</pubDate>
            <atom:updated>2025-07-26T01:13:37.171Z</atom:updated>
            <content:encoded><![CDATA[<p>While I was trying to deploy my node back-end on render last weekend, I ran into an issue. Everything was working fine on localhost, but the Render Server’s endpoint showed an upgrade-needed message.</p><p>I was very confused and after hours of searching for answers on medium, reddit, etc. I stumbled upon the forums of Render where users who experienced the same issues shared their distress. There I found a post with almost 2 likes, which had a reply — !! THE SOLUTION !! .</p><p>USE ‘<a href="https://www.npmjs.com/package/express-ws">express-ws</a>’ . While their answer was very vague and I didn’t want to go down that rabbit hole, I looked at my pockets and was reminded of my dire financial conditions, and a need for Free server hosting, so this rabbit hole it was!</p><p>Integrating previous code to a new server was really simple to be honest.</p><p>My previous code written in Plain ‘ws’ library is below</p><pre>import { WebSocketServer } from &#39;ws&#39;;<br>import WebSocket from &#39;ws&#39;;<br>import dotenv from &#39;dotenv&#39;;<br>dotenv.config();<br><br>// @ts<br>const PORT: number = parseInt(&lt;string&gt;process.env.PORT, 10) || 3000<br><br><br>if (PORT) {<br>  const wss = new WebSocketServer({ port: PORT });<br>  console.log(`WebSocket server started on ws://localhost:${PORT}`);<br><br>  let userCount = 0;<br><br>  wss.on(&#39;connection&#39;, (ws: WebSocket) =&gt; {<br>    console.log(&#39;Client connected!&#39;);<br>    userCount++;<br>    ws.on(&#39;message&#39;, async (message) =&gt; {<br>      const msgStr: string = message.toString();<br>      const msg: message = JSON.parse(msgStr);<br>      console.log(msg);<br>    }<br>    ws.on(&#39;close&#39;, () =&gt; {<br>      removeClientFromRooms(ws);<br>      console.log(&#39;Client disconnected.&#39;);<br>    });<br>  });<br>} else {<br>  console.log(&#39;Error: PORT Undefined&#39;);<br>}</pre><p>Integrating it with ‘express-ws’ was really easy, just had to make an express app and add an endpoint for the websocket using ‘.ws’ instead of ‘.get’</p><pre>import WebSocket from &#39;ws&#39;;<br>import dotenv from &#39;dotenv&#39;;<br><br>dotenv.config();<br><br>var express = require(&#39;express&#39;)<br><br>const app = express();<br>var expressWs = require(&#39;express-ws&#39;)(app);<br><br>// @ts<br>const PORT: number = parseInt(&lt;string&gt;process.env.PORT, 10) || 3000<br><br>app.get(&#39;/&#39;, function(req: any, res: Response) {<br>  console.log(&#39;Get Req&#39;)<br>});<br><br><br>app.ws(&#39;/&#39;, function(ws :WebSocket, req:any) {<br><br>  ws.on(&#39;open&#39;,(ws: WebSocket)=&gt;{<br>    console.log(&#39;Client connected!&#39;);<br><br>  })<br><br>  ws.on(&#39;message&#39;, async (message) =&gt; {<br>    const msgStr: string = message.toString();<br>    const msg: message = JSON.parse(msgStr);<br>    console.log(message);<br>  })<br>  <br>});<br><br>app.listen(PORT,()=&gt;{<br>  console.log(&#39;Listening on PORT:&#39;,PORT)<br>});</pre><p>Hope this helps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eeebca9aab8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>