How to Implement RBAC in Astro Framework

How to Implement RBAC in Astro Framework

June 20, 2024 (4mo ago)

devrelmarketingdeveloperbeginners
This Article was originally published on Permit.io

Astro is a modern, open-source web framework designed for building high-performance, content-driven websites. It allows the creation of fast and efficient websites like blogs, marketing pages, and e-commerce sites. As your web application grows, managing user access becomes an inseparable part of its functionality—you need to be able to decide who can do what in your web application once they log in.

A simple way to do that is to implement a Role-Based Access Control (RBAC) authorization layer into your Astro web application. This allows you to assign users roles, with each role having access to only specific segments and actions in the web app.

As Astro doesn’t provide us with built-in RBAC capabilities, we will implement them using Permit.io, - an authorization tool that allows you to easily configure permissions and roles for your Astro app through a simple, intuitive interface.

In this guide, we'll create a demo blog application with Server-Side Rendering (SSR) and Static Site Generation (SSG) capabilities, incorporating varying degrees of access control using RBAC. We’ll have an “Admin” role who can Read, Update and Delete blogs and an Employee role who can only read them.

image - 2024-06-20T174659.491.png

Note that this guide requires you to be at least somewhat familiar with Astro and JavaScript.

Why do we need Authorization for an Astro Application :

After a user logs in to our Astro app using our authentication solution of choice, meaning we have successfully confirmed their identity, we now need to make the decision of what they can do inside our app. Not every user should have access to any of the app's functions.

Creating and managing authorization logic from scratch can be time-consuming. Permit.io simplifies this process by providing an authorization layer with an intuitive interface, separating authorization policies from application code. This ensures consistent access controls, eases management and reduces the burden on your codebase.

Now that we understand the why let’s get to how to implement authorization into your Astro app:

Setup an Astro Project :

To get started, let's create a new Astro project. To save you time, we’ve got a basic Astro starting app that you can use to follow along.

  1. Make sure you have Node.js and npm installed on your machine. You can download them from the official Node.js website.
  2. Next, open up your code editor and clone the starter code by running the following code :
 git clone git@github.com:permitio/Astro-Framework-RBAC-Example.git

This will create a new Astro project with the default settings.

  1. After creating the project, navigate to the project directory by running:
 cd Astro-Framework-RBAC-Example
  1. Next, install the required dependencies by running:
npm install
  1. Now we’ll create a .env file in the root directory to store the secret keys for our projects. It should look something like this:
PERMIT_TOKEN="your permit token”

With that, we’ve set up our astro code.

Configure a Basic RBAC Policy with Permit.io:

Now that we’ve set up Astro let’s use Permit.io to create a basic RBAC policy for our project.

To get started, you'll first need to create an account. Sign up on Permit.io and create a new workspace for your team or organization. This will allow you to invite team members and take advantage of all the collaboration features Permit.io has to offer.

After logging in, we’ll create a new workspace for this project. For this tutorial, we are keeping the workspace name as blog-app:

image - 2024-06-20T174712.043.png

To begin, we will create roles. Roles facilitate the grouping of permissions, which can then be assigned to users or other entities.

  1. Navigate to the Policy page and select Create a Role.
  2. Add the following roles, specifying the actions as detailed below:
  • Admin
  • Employee

image - 2024-06-20T174728.199.png

image - 2024-06-20T174744.607.png

Next, let’s create an Admin role:

  1. Go to the Roles section under Policy.
  2. Click Create Role.
  3. Fill in the details as shown below.

image - 2024-06-20T174808.622.png

Similarly, let’s create the Employee role:

  1. Go to the Roles section under Policy.
  2. Click Create Role.
  3. Fill in the details as shown below.

image - 2024-06-20T174841.349.png

Now that our roles are created let’s create a resource. A resource is the thing our Roles will be able to perform actions on.

  1. Navigate to the Resources section and click Create a Resource.
  2. Create the resource as shown below and assign it with Actions read, update, and delete
  3. Assign it two roles, Admin and Employee

image - 2024-06-20T174851.335.png

Next, let’s create our policy:

  1. Go to the Policy Editor.
  2. Check all the checkboxes as shown below.
  3. Save the changes.

image - 2024-06-20T174911.856.png

Finally, go to the Directory section and create a new tenant:

image - 2024-06-20T174928.242.png

To create a new tenant, we need to fill in the name and description of the tenant as shown below :

image - 2024-06-20T174945.144.png

Now let’s create a user :

  1. Go to the permit directory
  2. Click on add user :

image - 2024-06-20T174954.510.png

  1. Fill in the fields as shown below :

image - 2024-06-20T175008.947.png

  1. Click on Save

  2. Look in the permit directory, you should be able to find your user there.

    image - 2024-06-20T175025.452.png

That's all it took for us to create a Role-Based Access Control (RBAC) model that we can use in our applications. Permit.io provides us with a clean and easy-to-use user interface, which enhances the overall Software Development Life Cycle by allowing us to integrate authorization into our code smoothly.

Adding User to our application :

To demonstrate the role-based access control features, Let’s add user details to our application :

  1. Go to src/utils/user.json file :
  2. Next, enter the details of the previously created user and save the file:

Note: We're using a dummy user for this example. In a real application, you'd implement authentication and sync users with Permit.io using the syncUser API.

Setting Up Environment variables :

Before we can see our app in action, we need to set up our environment variables.

For Permit.io configuration, ensure you are in the Default Project section and in the production environment.

image - 2024-06-20T175105.012.png

  1. Go to Dashboard > Settings > API Keys and find the secret production key in the Environment API Keys section.
  2. Copy the key and paste it in the .env file:
PERMIT_TOKEN=”your permit token”

With our environment variables configured, we can now see how our application looks. In the root directory, open your terminal and execute:

npm run dev

This will spin up our Astro website on http://localhost:4321

image - 2024-06-20T175135.107.png

Demo of our blog application :

Demo of user with employee permissions :

In this demonstration, we will be showing how our employees can only read blogs but cannot edit or delete them :

  • Read Blogs: The user with an employee role can read the Blogs.
  • Restricted from Editing Blogs: The user cannot modify existing Blog posts.
  • Restricted from Deleting Todos: The user does not have permission to delete Blogs.

Vid 1: Demo of user with employee permissions

Updating our user to admin :

Next, we will demonstrate how to change the user's role from Employee to Admin using Permit.io. This process involves navigating to the Permit.io dashboard and updating the user's role assignment.

Watch the video below to see how the role change is performed:

Vid 2: Updating our user to admin

Demo of Admin user with all the permissions:

After updating the role to Admin, the user now has full permissions within the application. As an Admin, the user can:

  • Update Blogs Title: Modify existing Blogs Title.
  • Delete Blogs: Remove Blogs from the list.
  • Read Blogs: Read existing Blogs.

The following video demonstrates the enhanced capabilities of the Admin role:

Vid 3: Demo of Admin user with all the permissions

Understanding Authorization Flow in an Astro Application :

As you already know by now authorization is preventing users from doing what they are not allowed to do, like deleting data or creating unwanted data in our application.

Let’s look at how we’ve implemented authorization in our Astro Blog application and go through the code :

image - 2024-06-20T175259.926.png

The diagram above shows the overall flow of authorization in our application, let’s take a closer look

Getting permissions from Permit:

First, we’ll instantiate the class Permit as shown below :

image - 2024-06-20T175321.894.png

Then we’ll receive id and operation parameters through query params which will help us get permissions data from permit.io:

image - 2024-06-20T175332.144.png

After getting the data, we are going to use the permit.check() API to check for permissions. This takes the following parameters:

  • user: The key of the user
  • action: The name of the operation, such as create, update, read, or delete.
  • resource: A string or object that represents the resource the user is trying to interact with. This could refer to a type of resource or a specific instance of a resource.
  • context (optional): An optional object that can be used to provide additional data for policy evaluation.

The permit.check() function returns a boolean value.

image - 2024-06-20T175352.669.png

Congrats! By using Permit, we’ve just successfully implemented role-based access control (RBAC) in your Astro application

Conclusion:

In this tutorial, we learned how to set up and configure Permit and integrate a basic RBAC policy using Permit’s intuitive and simple interface in our Astro applications.

Now that you’ve learned how to configure a basic policy in Permit, you can improve your applications' security by leveraging it to real use cases in your application.

It is always recommended to continue with ReBAC and ABAC which will provide a more fine-grained authorization to your applications. For that and more, we recommend you to continue reading our learning materials, such as the difference between RBAC and ABAC, and adding ABAC to your application with Permit.io.

Want to learn more about implementing authorization? Got questions? Reach out to us in our Slack community.