A Functional, Rule-based Authorisation Module for Deno

authz

A deno module that provides a functional interface for defining action, object, and field-level authorisation rules.

No dependencies, 100% test coverage.

Usage

Creating an authoriser

import {
  and,
  create,
} from "https://raw.githubusercontent.com/joelshepherd/authz/main/mod.ts";

const authoriser = create({
  action: {
    read: () => true,
    write: (context) => context.role === "writer",
  },
  object: {
    // read falls back to action-level
    write: and(
      (context) => context.role === "writer",
      (context, post) => context.user === post.user
    ),
  },
});

Using an authoriser

if (authoriser(context, "write")) {
  // user can perform write
}

if (authoriser(context, "write", object)) {
  // User can perform write on this object
}

if (authoriser(context, "write", object, "name")) {
  // User can perform write on this object's name field
}

Rule and other helpers

const allRules = and(...rules);
const anyRule = or(...rules);
const notRule = not(rule);

const boundAuthoriser = bind(context, authoriser);
boundAuthoriser("read"); // no need to specify the context anymore

Download Details:

Author: joelshepherd

Source Code: https://github.com/joelshepherd/authz

#deno #nodejs #javascript #node

A Functional, Rule-based Authorisation Module for Deno
2.00 GEEK