An open API service indexing awesome lists of open source software.

https://github.com/chronark/access

E2E Typed Access Control
https://github.com/chronark/access

Last synced: about 1 year ago
JSON representation

E2E Typed Access Control

Awesome Lists containing this project

README

          


@chronark/access-*


E2E Typed Access Control


A minimal library for access control. It is designed to be used together with
opaque access tokens by providing a simple interface to define roles with
different access permissions and verifying requests to resources.

- Fully typed
- Zero dependencies
- Serializable to store in a database

## Install

```
npm i @chronark/access-policies
```

## Usage

```ts

type Resources = {
team: ["create", "invite" /* more */];
joke: ["create", "read", "vote"];
};

/**
* User or team id
*/
type TenantId = string;

/**
* Just some uuid
*/
type JokeId = string;

type ResourceIdentifier = `${TenantId}-${keyof Resources}-${JokeId}`;

/**
* Allow the policy owner to create jokes inside the vercel tenant
*/
const createJokesInVercel = new Policy({
resources: {
joke: {
"vercel-joke-*": ["create"],
},
},
});

/**
* Or
*
* Allow the policy owner to read and vote on all jokes across all tenants
*/
const readAndVoteJokesInAllTeams = new Policy({
resources: {
joke: {
"*-joke-*": ["read", "vote"],
},
},
});

/**
* Verifying a request
*/
const { valid, error } = readAndVoteJokesInAllTeams.validate("joke:read", "vercel-joke-jokeId")

```