{"id":20940057,"url":"https://github.com/nearform/fastify-casbin-rest","last_synced_at":"2025-05-13T22:32:12.053Z","repository":{"id":37410480,"uuid":"309979762","full_name":"nearform/fastify-casbin-rest","owner":"nearform","description":"A plugin for Fastify that adds support for Casbin's REST model","archived":false,"fork":false,"pushed_at":"2024-12-10T18:30:15.000Z","size":109,"stargazers_count":15,"open_issues_count":4,"forks_count":3,"subscribers_count":91,"default_branch":"master","last_synced_at":"2025-05-13T12:51:31.262Z","etag":null,"topics":["casbin","fastify"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nearform.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-04T11:15:48.000Z","updated_at":"2025-04-24T19:07:29.000Z","dependencies_parsed_at":"2023-02-17T09:01:31.523Z","dependency_job_id":"7d9d0d71-73da-4ff2-92f7-c3b60dc9bc77","html_url":"https://github.com/nearform/fastify-casbin-rest","commit_stats":{"total_commits":114,"total_committers":20,"mean_commits":5.7,"dds":"0.35964912280701755","last_synced_commit":"4037d9d9ef5821be3f72a9dbb6f8412e2fcc85ef"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearform%2Ffastify-casbin-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearform%2Ffastify-casbin-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearform%2Ffastify-casbin-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearform%2Ffastify-casbin-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nearform","download_url":"https://codeload.github.com/nearform/fastify-casbin-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036820,"owners_count":22003658,"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":["casbin","fastify"],"created_at":"2024-11-18T23:08:44.220Z","updated_at":"2025-05-13T22:32:09.848Z","avatar_url":"https://github.com/nearform.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastify-casbin-rest\n\n![Continuous Integration](https://github.com/nearform/fastify-casbin-rest/workflows/ci/badge.svg)\n[![codecov](https://codecov.io/gh/nearform/fastify-casbin-rest/branch/master/graph/badge.svg?token=BwsVjvYSJb)](https://codecov.io/gh/nearform/fastify-casbin-rest)\n[![npm version](https://badge.fury.io/js/fastify-casbin-rest.svg)](https://badge.fury.io/js/fastify-casbin-rest)\n\nA plugin for [Fastify](http://fastify.io/) that adds support for [Casbin](https://casbin.org/) RESTful model.\n\nIt depends and builds on top of [fastify-casbin](https://github.com/nearform/fastify-casbin) and provides an opinionated approach to model an authorization scheme based on a RESTful model using [Casbin Node.js APIs](https://github.com/casbin/node-casbin) within a Fastify application.\n\n## Install\n\n```\nnpm i casbin fastify-casbin fastify-casbin-rest\n```\n\n\u003e `fastify-casbin` must be registered in the Fastify instance\n\n## How it works\n\nOnce registered, the plugin use the Fastify instance decorated by `fastify-casbin` and will automatically enforce authorization rules to routes where the plugin is enabled.\n\nIt uses the default Casbin's `sub`, `obj` and `act` entities and extracts them automatically from the request.\n\nWhen a rule is not satisfied, it returns a `403 Forbidden` error by default.\n\nAll the options can be customized when registering the plugin.\n\n## API\n\nThe plugin must be explicitly enabled on individual routes via route options. The plugin will have no effect on routes on which it is not enabled.\n\n```js\nfastify.route({\n  // ... other route options\n  casbin: {\n    rest: true\n  }\n})\n```\n\n### Route options\n\nThis plugin introduces new route option `casbin.rest`. It can be either a `true` value (which enables default configuration) or an object.\nSupported object options:\n\n| Option   | Type                            | Description                                  | Default                     |\n| -------- | ------------------------------- | -------------------------------------------- | --------------------------- |\n| `getSub` | `Request =\u003e string` or `string` | Extracts `sub` from the request or constant  | Value from plugin options   |\n| `getDom` | `Request =\u003e string` or `string` | Extracts `dom` from the request or constant  | Value from plugin options   |\n| `getObj` | `Request =\u003e string` or `string` | Extracts `obj` from the request or constant  | Value from plugin options   |\n| `getAct` | `Request =\u003e string` or `string` | Extracts `act` from the request or constant  | Value from plugin options   |\n\n### Plugin options\n\nThe API exposed by this plugin is the configuration options:\n\n| Option   | Type                                                       | Description                                       | Default                         |\n| -------- | ---------------------------------------------------------- | ------------------------------------------------- | ------------------------------- |\n| `getSub` | `Request =\u003e string`                                        | Extracts `sub` from the request                   | `r =\u003e r.user`                   |\n| `getDom` | `Request =\u003e string`                                        | Extracts `dom` from the request                   |  undefined                      |\n| `getObj` | `Request =\u003e string`                                        | Extracts `obj` from the request                   | `r =\u003e r.url`                    |\n| `getAct` | `Request =\u003e string`                                        | Extracts `act` from the request                   | `r =\u003e r.method`                 |\n| `onDeny` | `(Reply, { sub, obj, act, dom }) =\u003e any`                   | Invoked when Casbin's `enforce` resolves to false | Returns a `403 Forbidden` error |\n| `onAllow`| `(Reply, { sub, obj, act, dom }) =\u003e any`                   | Invoked when Casbin's `enforce` resolves to true  | noop                            |\n| `log`    | `(Fastify, Request, { sub, obj, act, dom }) =\u003e void`       | Invoked before invoking Casbin's `enforce`        | Logs using fastify.log.info     |\n| `hook`   | `'onRequest', 'preParsing', 'preValidation', 'preHandler'` | Which lifecycle to use for performing the check   | `'preHandler'`                  |\n\nNote that extraction rules defined within route options take precedence over the rules defined in the plugin options.\nIf `getDom` is not set either on a route nor on a plugin level, enforcer is invoked with `(sub, obj, act)`.\nIf `getDom` is set, enforcer is invoked with `(sub, dom, obj, act)`.\n\n## Examples\n\nA working example can be found in the [examples](examples) folder.\n\nThe example below uses [fastify-jwt](https://github.com/fastify/fastify-jwt) to authenticate users and extract user information from the request.\nIt uses sample REST [model](examples/fastify-casbin-rest-example/config/rest_model.conf) and [policy](examples/fastify-casbin-rest-example/config/rest_policy.csv) files.\n\n```js\nconst fastify = require('fastify')()\n\n// register jwt plugin\nfastify.register(require('fastify-jwt'), {\n  secret: 'some secret'\n})\n\n// register casbin plugin\nfastify.register(require('fastify-casbin'), {\n  modelPath: 'rest_model.conf', // the model configuration\n  adapter: 'rest_policy.csv' // the adapter\n})\n\n// register and configure casbin-rest plugin\nfastify.register(require('fastify-casbin-rest'), {\n  getSub: r =\u003e r.user.payload.username\n})\n\n// decorate Fastify instance with authenticate method\nfastify.decorate('authenticate', async function (request, reply) {\n  try {\n    await request.jwtVerify()\n  } catch (err) {\n    reply.send(err)\n  }\n})\n\n// sample login endpoint which always authenticates the user\nfastify.post('/login', async request =\u003e {\n  return fastify.jwt.sign({ payload: { username: 'alice' } })\n})\n\nfastify.get(\n  '/protected',\n  {\n    // ensure user is authenticated\n    preValidation: [fastify.authenticate],\n    // enable fastify-casbin-rest plugin on this route, override default \"getObj\" rule\n    casbin: {\n      rest: {\n        getSub: request =\u003e request.userId,\n        getAct: 'read'\n      },\n    }\n  },\n  async () =\u003e `You're in!`\n)\n```\n\n## License\n\nLicensed under [MIT License](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnearform%2Ffastify-casbin-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnearform%2Ffastify-casbin-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnearform%2Ffastify-casbin-rest/lists"}