{"id":15371614,"url":"https://github.com/charlesread/fastify-acl-auth","last_synced_at":"2025-04-15T14:05:20.078Z","repository":{"id":55076589,"uuid":"122015591","full_name":"charlesread/fastify-acl-auth","owner":"charlesread","description":"ACL-like authorization for Fastify apps","archived":false,"fork":false,"pushed_at":"2021-01-12T18:42:47.000Z","size":100,"stargazers_count":9,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-02T22:35:31.582Z","etag":null,"topics":["acl","authorization","fastify"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/charlesread.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-19T03:33:29.000Z","updated_at":"2024-11-18T13:30:49.000Z","dependencies_parsed_at":"2022-08-14T11:20:23.999Z","dependency_job_id":null,"html_url":"https://github.com/charlesread/fastify-acl-auth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-acl-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-acl-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-acl-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlesread%2Ffastify-acl-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charlesread","download_url":"https://codeload.github.com/charlesread/fastify-acl-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248758418,"owners_count":21156957,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["acl","authorization","fastify"],"created_at":"2024-10-01T13:48:05.374Z","updated_at":"2025-04-15T14:05:20.048Z","avatar_url":"https://github.com/charlesread.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastify-acl-auth\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/charlesread/fastify-acl-auth.svg)](https://greenkeeper.io/)\n[![Build Status](https://travis-ci.org/charlesread/fastify-acl-auth.svg?branch=master)](https://travis-ci.org/charlesread/fastify-acl-auth)\n[![Coverage Status](https://coveralls.io/repos/github/charlesread/fastify-acl-auth/badge.svg?branch=master)](https://coveralls.io/github/charlesread/fastify-acl-auth?branch=master)\n[![NPM downloads](https://img.shields.io/npm/dm/fastify-acl-auth.svg?style=flat)](https://www.npmjs.com/package/fastify-acl-auth)\n\nACL-like authorization for [*fastify*](https://fastify.io) apps.\n\nWith *fastify-acl-auth* you can secure routes with roles, like **admin**, **superuser**, or **user:write**.  Then you just tell the plugin how to determine which roles a user has, and you're set.  You can also:\n\n* Specify any/all functionality (allow if user has any of these roles, allow if users has all of these roles, for example)\n* Specify a hierarchy of roles (\"admins\" are clearly \"users\" too, so let them through without explicitly letting \"admins\" through, for example)\n* Easily use *fastify-acl-auth* as an authentication strategy with [*fastify-auth*](https://www.npmjs.com/package/fastify-auth), or anything else really\n\n\u003c!-- toc --\u003e\n\n- [Usage](#usage)\n  * [Simple Example](#simple-example)\n  * [Using a Hierarchy](#using-a-hierarchy)\n- [API](#api)\n  * [`options`](#options)\n  * [`aclFactory([options])`](#aclfactoryoptions)\n- [Use with _fastify-auth_](#use-with-_fastify-auth_)\n\n\u003c!-- tocstop --\u003e\n\n## Usage\n\n**NOTE:** If you're not familiar with [scoping in *fastify*](https://www.fastify.io/docs/master/Plugins/) this plugin isn't going to make much sense to you.  I'd highly recommend making sure that you're solid with this concept before proceeding.\n\n**ANOTHER NOTE:**  _fastify-acl-auth_ needs to have a way to know what roles a user has, right?  By default it assumes that you have a session provider available at `request.session` (and that roles are available at `request.session.credentials.roles`, which you can easily change).  In many examples I simulate this with a request decorator (`fastify.decorateRequest('session', { ... })`), I recommend [*fastify-server-session*](https://www.npmjs.com/package/fastify-server-session) in practice.\n\nYou can use *fastify-acl-auth* in a few ways, ways that depend on how you want to structure your application and leverage *fastify*'s scoping.\n\n### Simple Example\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nconst aclFactory = require('fastify-acl-auth')\n\nconst credentials = {\n  id: 'bc965eb1-a8a4-4320-9172-726e9a7e83c9',\n  username: 'cread',\n  roles: 'vendor'\n}\n\nfastify.decorateRequest('session', {credentials})\n\nfastify.register(function (fastifyScope, opts, next) {\n  fastifyScope.register(\n    aclFactory(\n      {\n        allowedRoles: ['customer']\n      }\n    )\n  )\n  // 403\n  fastifyScope.get('/customers', function (request, reply) {\n    return reply.send('/customers')\n  })\n  next()\n})\n\nfastify.register(function (fastifyScope, opts, next) {\n  fastifyScope.register(\n    aclFactory(\n      {\n        allowedRoles: ['vendor']\n      }\n    )\n  )\n  // 200\n  fastifyScope.get('/vendors', function (request, reply) {\n    return reply.send('/vendors')\n  })\n  next()\n})\n\nfastify.listen(8080, function (err) {\n  if (err) throw err\n  console.log('listening on %s', fastify.server.address().port)\n})\n\n```\n\n### Using a Hierarchy\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nconst aclFactory = require('fastify-acl-auth')\n\nconst hierarchyAcl = aclFactory({hierarchy: ['user', 'admin', 'superuser']})\n\nconst credentials = {\n  id: 'bc965eb1-a8a4-4320-9172-726e9a7e83c9',\n  username: 'cread',\n  roles: 'admin'\n}\n\nfastify.decorateRequest('session', {credentials})\n\nfastify.register(function (fastifyScope, opts, next) {\n  fastifyScope.register(\n    hierarchyAcl,\n    {\n      allowedRoles: ['user']\n    }\n  )\n  // 200, because 'admin' \u003e 'user' in hierarchy\n  fastifyScope.get('/user', function (request, reply) {\n    return reply.send('/user')\n  })\n  next()\n})\n\nfastify.register(function (fastifyScope, opts, next) {\n  fastifyScope.register(\n    hierarchyAcl,\n    {\n      allowedRoles: ['admin']\n    }\n  )\n  // 200\n  fastifyScope.get('/admin', function (request, reply) {\n    return reply.send('/admin')\n  })\n  next()\n})\n\nfastify.register(function (fastifyScope, opts, next) {\n  fastifyScope.register(\n    hierarchyAcl,\n    {\n      allowedRoles: ['superuser']\n    }\n  )\n  // 403\n  fastifyScope.get('/superuser', function (request, reply) {\n    return reply.send('/superuser')\n  })\n  next()\n})\n\nfastify.listen(8080, function (err) {\n  if (err) throw err\n  console.log('listening on %s', fastify.server.address().port)\n})\n\n```\n\n## API\n\n_fastify-acl-auth_ exports a factory function; a function that _makes_ the plugin that you'll use.\n```js\nconst aclFactory = require('fastify-acl-auth')\n``` \n### `options`\n\n`options` is a simple object with the following properties:\n\n| Property | Default | Type | Notes |\n| --- | --- | --- | --- |\n| `actualRoles` | `request.session.credentials.roles` |  `Array`, `string`, `[async] function` | Since `fastify-acl-auth` is all about comparing what roles a user _actually_ has to what a route _allows_ then this property is pretty important.  This property can be an `Array` of roles (`string`s), a role itself (`string`), or an `[async] function` that returns an `Array` of roles (`string`s). |\n| `allowedRoles` | `[]` | `Array`, `string`, `[async] function`  | ^ that whole thing.  Except this property tells `fastify-acl-auth` which roles are _allowed_ for a route or routes. ([scoping!!!](https://www.fastify.io/docs/latest/Plugins/)) |\n| `any` | `true` | `boolean` | If `true` a `200` will be returned if `actualRoles` contains _any_ of the roles in `allowedRoles`, `403` otherwise. |\n| `all` | `false` | `boolean` | If `true` a `200` will be returned [iff](https://en.wikipedia.org/wiki/If_and_only_if) `actualRoles` contains _ALL_ of the roles in `allowedRoles`, `403` otherwise. |\n| `hierarchy` | `undefined` | `Array` | An `Array` that specifies the privilege hierarchy of roles in order of ascending privilege. For instance, suppose we have `hierarchy: ['user', 'admin', 'superuser]`, `allowedRoles : ['admin']`, and `actualRoles: ['superuser]` configured for a route.  A user with the `superuser` role will be able to access that route because the `superuser` role is of higher privilege than the `user` and `admin` roles, as specified in the hierarchy. |\n| `pathExempt` | `undefined` | `Array` | An `Array` that specifies the path patterns that should be exempt from enforcement; `['/login', '/callback**']` for example.  Uses the NPM module `url-pattern` internally for URL pattern matching. |\n\n### `aclFactory([options])`\nThis will create an instance of `fastify-acl-auth`.  It can be used with `fastify.register()` just like any other plugin.\n\n```js\nfastify.register(aclFactory([options]), [options])\n```\n\nNope, that's not a typo, `options` is there twice;  `aclFactory([options])` is setting the options of your _plugin instance_, whereas passing `options` during _registration_ is setting, or overriding, the _plugin instance_ options for _that registration_ of the plugin instance.  So you can create an instance of `fastify-acl-auth` and \"carry it around with you\" for later use.  Passing `options` _when you register_ the plugin will _override_ the `options` set when creating the plugin instance with the factory function.\n\nLots of words, right?  This architecture really comes from the architecture (really talking about scoping here) of `fastify` itself, and should make sense with a [basic knowledge of scoping](https://www.fastify.io/docs/latest/Plugins/).  It's actually very logical when it sinks in.\n\n## Use with _fastify-auth_\nAll of the actual logic that used in _fastify-acl-auth_ is contained in `lib/auth.js`, it exports a function with signature `function(actualRoles, allowedRoles[, options])` that simply returns a `boolean`, which can be used _anywhere_.\n\n```js\nconst auth = require('fastify-acl-auth/lib/auth')\nauth(['user'], ['admin','user'], {any: true})\n// true\nauth(['foo'], ['bar','baz'], {any: true})\n// false\nauth(async function () {return ['admin']}, ['user'], {hierarchy: ['user', 'admin']})\n// true\n// et cetera\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlesread%2Ffastify-acl-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharlesread%2Ffastify-acl-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlesread%2Ffastify-acl-auth/lists"}