This will create two environments: a development environment and a production environment.
Next, navigate to the policy editor section. By default, you should see an admin role already created. You just need to tick the view action we added, as it is not recognized by default. You need another role. This will be for users with only permission to read.
Click Create then Role and give it the name of user. Once created, you should see it in the policy editor and tick view in the user role you just created like so:
Click on Add users and then add, then add user. Fill in the details that will correspond to your users in the database.
Once that is done, navigate back to your project. In the Development For the environment for the bookstore project, click on the 3 dotted icon. You’ll see an option to copy the API key. Copy and save it somewhere, as you’ll be needing it for the project.
Setup the Database
Create a PostgreSQL database called bookstore. You’ll need to set up two tables:
users table: Stores user credentials and roles:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
role VARCHAR(50) NOT NULL
);
Go ahead and populate this, but make each user have a role of admin and user, respectively, and make sure they match the users added on Permit.io.
books table: Stores book details:
CREATE TABLE books (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
published_at DATE,
created_at TIMESTAMPTZ DEFAULT now()
);
You don’t need to populate this we’ll be doing that in the code.
Install Dependencies
You’ll need to install the following dependencies:
github.com/permitio/permit-golang: Provides tools for handling role-based access control (RBAC) and permission management with Permit.io in Go applications.
github.com/google/uuid: This provides functions to generate and work with universally unique identifiers (UUIDs).
github.com/gorilla/mux: Helps to Implement an HTTP request router and dispatcher for handling routes in a web application.
github.com/joho/godotenv: This loads environment variables from a
.env. file into the application, making it easier to manage configuration settings.github.com/lib/pq: This is Go’s Postgres driver for communicating with PostgreSQL databases.
golang.org/x/crypto: Implements supplementary cryptographic algorithms and libraries that are not included in Go's standard library.
To install these dependencies, you need to initialize initializes a new Go module. This is the starting point for dependency management in Go.
Run this command:
go mod init bookstore
Next, run this command:
go get github.com/google/uuid \
github.com/gorilla/mux \
github.com/joho/godotenv \
github.com/lib/pq \
github.com/permitio/permit-golang \
golang.org/x/crypto
This will install all the dependencies listed above.
Setup your PDP (Policy Decision Point) Container
To set up the
You'll see that the standard_user is restricted to viewing books only and cannot add, delete, or update a book.
Let's now log in using the admin_user to see what happens:
.
Conclusion
In this tutorial, we built a simple bookstore management app to implement role-based access control using Go, HTMX, and Permit.io. Authorization is fundamental aspect of application security, as it ensures that users can only access what they’re allowed to.
Implementing an effective access control model like RBAC or ABAC into your application would not only secure your application but also enhance its scalability and compliance.
SOCIAL SHARE CARD GENERATOR