Giter Club home page Giter Club logo

nestjs-cerbos's Introduction

Cerbos NestJS Demo

This demo project demonstrates how to use Cerbos in NestJS as an interceptor to validate requests based on policies defined in Cerbos.

Run the project

To run the project, check package.json for prebuilt run configurations, by default NestJS offers npm run start:dev for development purposes. On top of that there also are Cerbos policies included in the repository, and to use this you can run the npm run cerbos:start. Both are combined into the single npm run start:devcerbos.

Demo Request

The demo includes an Document request, you can do a GET on http://localhost:3000/document/1.

Provide the authorization header with either user or admin as a value for getting a successful response, or anything else to get a rejected response. The success state depends on the author of the documents/*.

There are 3 documents defined in the src/db.ts file,

  • the document/1 can only be accessed by the admin user
  • the document/2 can be accessed by user or admin
  • the document/3 is for not-the-current-user but can be accessed by admin.

The Cerbos policy is validated in src/document/document.cerbos.interceptor.ts file.

The response is of course currently hardcoded in the /src/document/document.controller.ts file, as this is for demonstration purposes.

Note! You should NOT use authentication as demonstrated, we recommend using a JWT Guard in NestJS

NestJs example

This demo project demonstrates how to use Cerbos in NestJS as an interceptor to validate requests based on policies defined in Cerbos.

Table of Contents

Overview

Cerbos is an open-source authorization-as-a-service option for allowing decoupled access control in your software. It allows writing human-readable policy definitions that serve as context-aware access control policies for your application resources.

Cerbos works with any identity provider services like Auth0, Okta, FusionAuth, Clerk, Magic, WorkOS or even your own, bespoke directory system.

Our NestJs.js application will provide an API that uses Cerbos for authorization, to decide what actions are available on which resources for a given user.

The policies is defined in the cerbos/policies directory. Each policy is authored in the a very human-readable format which you can learn more about at the Cerbos Policy documentation site, and for the demo revolves around access to a contacts resource.

Tech Stack

  • Cerbos
  • NestJs - A progressive Node.js framework for building efficient, reliable and scalable server-side applications.
  • RxJs - Reactive Extensions Library for JavaScript

How to Run the Example

1. Clone the repository and install the dependencies

git clone https://github.com/cerbos/nestjs-cerbos.git

Then cd into the project directory and run npm install to install the dependencies.

npm install 

Alternatviely you could use yarn or pnpm or anything that runs npm scripts

2. Run the project

To run the project, check package.json for prebuilt run configurations, by default NestJS offers npm run start:dev for development purposes. On top of that there also are Cerbos policies included in the repository, and to use this you can run the npm run cerbos:start. Both are combined into the single npm run start:devcerbos.

3. Check out the example implementation

The demo includes an Document request, you can do a GET on http://localhost:3000/document/1.

Provide the authorization header with either user or admin as a value for getting a successful response, or anything else to get a rejected response. The success state depends on the author of the documents/*.

There are 3 documents defined in the src/db.ts file,

  • the document/1 can only be accessed by the admin user
  • the document/2 can be accessed by user or admin
  • the document/3 is for not-the-current-user but can be accessed by admin.

The Cerbos policy is validated in src/document/document.cerbos.interceptor.ts file.

The response is of course currently hardcoded in the /src/document/document.controller.ts file, as this is for demonstration purposes.

Note! You should NOT use authentication as demonstrated, we recommend using a JWT Guard in NestJS

4. Make changes to your Cerbos Policies

Your showcase is now running a local version of Cerbos, and you can easily make changes to the Policies and learn more about Cerbos and all its features.

You can use the existing set of E2E tests to test different policies and changes to existing requirements by running npm run test:e2e.

Commands

  • npm run cerbos:start - Starts the docker instance of cerbos server.
  • npm run start - Start the nestJs application in development mode.
  • npm run start:devcerbos - Starts both docker and the nestjs application

Learn More

To learn more about Clerk.dev and NestJs, take a look at the following resources:

nestjs-cerbos's People

Contributors

renovate[bot] avatar alexolivier avatar dependabot[bot] avatar honzikec avatar zelig880 avatar

Stargazers

Hamza HADJ AISSA avatar Ali Torki avatar Joseph Akayesi avatar Thams avatar Bruno Gomes avatar  avatar DJun avatar Muhamad Assar avatar

Watchers

 avatar Charith Ellawala avatar  avatar

nestjs-cerbos's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • fix(deps): update all non-major dependencies (@cerbos/grpc, @nestjs/schematics, @types/node, ts-jest)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

npm
package.json
  • @cerbos/core ^0.18.1
  • @cerbos/grpc ^0.18.1
  • @nestjs/common ^10.1.2
  • @nestjs/core ^10.1.2
  • @nestjs/platform-express ^10.1.2
  • reflect-metadata ^0.2.0
  • rimraf ^6.0.0
  • rxjs ^7.2.0
  • @nestjs/cli ^10.1.10
  • @nestjs/schematics ^10.0.1
  • @nestjs/testing ^10.1.2
  • @types/express ^4.17.13
  • @types/jest 29.5.12
  • @types/node ^20.0.0
  • @types/supertest ^6.0.0
  • @typescript-eslint/eslint-plugin ^6.0.0
  • @typescript-eslint/parser ^6.0.0
  • eslint ^8.0.1
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-prettier ^5.0.0
  • jest 29.7.0
  • prettier ^3.0.0
  • source-map-support ^0.5.20
  • supertest ^7.0.0
  • ts-jest 29.2.4
  • ts-loader ^9.2.3
  • ts-node ^10.0.0
  • tsconfig-paths 4.2.0
  • typescript ^5.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.