Skip to content

An example application of integrating Cerbos with an Express server using Prisma as the ORM.

License

Notifications You must be signed in to change notification settings

cerbos/express-prisma-cerbos

Repository files navigation

express-prisma-cerbos

An example application of integrating Cerbos with an Express server using Prisma as the ORM. Cerbos helps you super-charge your authorization implementation by writing context-aware access control policies for your application resources. Author access rules using an intuitive YAML configuration language, use your Git-ops infrastructure to test and deploy them and, make simple API requests to the Cerbos PDP to evaluate the policies and make dynamic access decisions.

This is based on the REST API Express example from Prisma.

Dependencies

Getting Started

  1. Start up the Cerbos PDP instance docker container. This will be called by the express app to check authorization.
cd cerbos
./start.sh
  1. Install node dependencies
npm install
  1. Setup Prisma and seed the database
npx prisma migrate dev --name init
  1. Start the express server
npm run dev

Seed Users

The Prisma seed command will create the following users in the database. Authentication is done via Basic authentication using the following credentials. The Role and Department is loaded from the database after successful authentication.

ID Username Password Role Department
1 alice supersecret Admin IT
2 john password1234 User Sales
3 sarah asdfghjkl User Sales
4 geri pwd123 User Marketing

Policies

This example has a simple CRUD policy in place for a resource kind of contact - like a CRM system would have. The policy file can be found in the cerbos/policies folder here.

Should you wish to experiment with this policy, you can try it in the Cerbos Playground.

The policy expects one of two roles to be set on the principal - admin and user and an attribute which defines their department as either IT, Sales or Marketing.

These roles are authorized as follows:

Action Role: User Role: Admin
read Only if department is Sales Y
create Only if department is Sales Y
update Only if they own the contact being accessed Y
delete Only if they own the contact being accessed Y

Example Requests

Get all contact

As a Sales user => 200 OK

curl -i http://john:password1234@localhost:3000/contacts

As a Marketing user => 403 Unauthorized

curl -i http://geri:pwd123@localhost:3000/contacts

Get a contact

As a Sales user => 200 OK

curl -i http://john:password1234@localhost:3000/contacts/1

As a Marketing user => 403 Unauthorized

curl -i http://geri:pwd123@localhost:3000/contacts/1

About

An example application of integrating Cerbos with an Express server using Prisma as the ORM.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •