Git Product home page Git Product logo

fastify-casbin-rest's Introduction

fastify-casbin-rest

Continuous Integration codecov npm version

A plugin for Fastify that adds support for Casbin RESTful model.

It depends and builds on top of fastify-casbin and provides an opinionated approach to model an authorization scheme based on a RESTful model using Casbin Node.js APIs within a Fastify application.

Install

npm i casbin fastify-casbin fastify-casbin-rest

fastify-casbin must be registered in the Fastify instance

How it works

Once registered, the plugin use the Fastify instance decorated by fastify-casbin and will automatically enforce authorization rules to routes where the plugin is enabled.

It uses the default Casbin's sub, obj and act entities and extracts them automatically from the request.

When a rule is not satisfied, it returns a 403 Forbidden error by default.

All the options can be customized when registering the plugin.

API

The 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.

fastify.route({
  // ... other route options
  casbin: {
    rest: true
  }
})

Route options

This plugin introduces new route option casbin.rest. It can be either a true value (which enables default configuration) or an object. Supported object options:

Option Type Description Default
getSub Request => string or string Extracts sub from the request or constant Value from plugin options
getDom Request => string or string Extracts dom from the request or constant Value from plugin options
getObj Request => string or string Extracts obj from the request or constant Value from plugin options
getAct Request => string or string Extracts act from the request or constant Value from plugin options

Plugin options

The API exposed by this plugin is the configuration options:

Option Type Description Default
getSub Request => string Extracts sub from the request r => r.user
getDom Request => string Extracts dom from the request undefined
getObj Request => string Extracts obj from the request r => r.url
getAct Request => string Extracts act from the request r => r.method
onDeny (Reply, { sub, obj, act, dom }) => any Invoked when Casbin's enforce resolves to false Returns a 403 Forbidden error
onAllow (Reply, { sub, obj, act, dom }) => any Invoked when Casbin's enforce resolves to true noop
log (Fastify, Request, { sub, obj, act, dom }) => void Invoked before invoking Casbin's enforce Logs using fastify.log.info
hook 'onRequest', 'preParsing', 'preValidation', 'preHandler' Which lifecycle to use for performing the check 'preHandler'

Note that extraction rules defined within route options take precedence over the rules defined in the plugin options. If getDom is not set either on a route nor on a plugin level, enforcer is invoked with (sub, obj, act). If getDom is set, enforcer is invoked with (sub, dom, obj, act).

Examples

A working example can be found in the examples folder.

The example below uses fastify-jwt to authenticate users and extract user information from the request. It uses sample REST model and policy files.

const fastify = require('fastify')()

// register jwt plugin
fastify.register(require('fastify-jwt'), {
  secret: 'some secret'
})

// register casbin plugin
fastify.register(require('fastify-casbin'), {
  modelPath: 'rest_model.conf', // the model configuration
  adapter: 'rest_policy.csv' // the adapter
})

// register and configure casbin-rest plugin
fastify.register(require('fastify-casbin-rest'), {
  getSub: r => r.user.payload.username
})

// decorate Fastify instance with authenticate method
fastify.decorate('authenticate', async function (request, reply) {
  try {
    await request.jwtVerify()
  } catch (err) {
    reply.send(err)
  }
})

// sample login endpoint which always authenticates the user
fastify.post('/login', async request => {
  return fastify.jwt.sign({ payload: { username: 'alice' } })
})

fastify.get(
  '/protected',
  {
    // ensure user is authenticated
    preValidation: [fastify.authenticate],
    // enable fastify-casbin-rest plugin on this route, override default "getObj" rule
    casbin: {
      rest: {
        getSub: request => request.userId,
        getAct: 'read'
      },
    }
  },
  async () => `You're in!`
)

License

Licensed under MIT License

fastify-casbin-rest's People

Contributors

bredikhin avatar davideroffo avatar dependabot[bot] avatar gilach avatar grantmorrison avatar hails avatar ilteoood avatar kibertoad avatar mahenrique94 avatar marco-ippolito avatar mazuh avatar melkornemesis avatar optic-release-automation[bot] avatar ramonmulia avatar sameer-coder avatar simoneb avatar wilkmaia avatar williamlines avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastify-casbin-rest's Issues

Allow manual checking to cover multiple routes

Expose

routeOptions.preHandler = async (request, reply) => {
const sub = options.getSub(request)
const obj = options.getObj(request)
const act = options.getAct(request)
fastify.log.info({ sub, obj, act }, 'Invoking casbin enforce')
if (!(await fastify.casbin.enforce(sub, obj, act))) {
await options.onDeny(reply, sub, obj, act)
}
}
}
})
as a fastify decorator so that it can be invoked manually even on routes which are not explicitly enabled for the plugin

Allow user to customize which hook to attach the automatic check to

Currently, preHandler is used.

A more sensible default would be onRequest, which is early enough to guarantee that no unnecessary operations are done on a request that's eventually going to be forbidden by authorization rules.

Clearly, if that becomes the default, any logic which needs to happen before authorization also needs to happen earlier in the request lifecycle. The examples and example application need to change to do authentication earlier (onRequest VS preValidation)

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Enable NPM Provenance

Integration with beta NPM Provenance integration when publishing new versions to NPM registry.

Release pending!

Pending commits since release v3.0.0

Based on the following commits, a minor release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

  • 8ca5a61 ci: update notify-release action permissions (#101)
  • 241af66 ci: update check-linked-issues job permissions (#100)
  • 2841e8f switch the org for optic-release-automation-action (#99)
  • 780c6f0 chore(deps-dev): bump tsd from 0.27.0 to 0.28.0 (#98)
  • dfcad43 chore(deps-dev): bump typescript from 4.9.5 to 5.0.2 (#97)
  • 885ccec chore(deps-dev): bump tsd from 0.26.1 to 0.27.0 (#96)
  • 2421fea chore(deps-dev): bump tsd from 0.25.0 to 0.26.1 (#95)
  • 274abe8 feat: update notify-release config (#93)
  • d7f4145 chore(deps-dev): bump tsd from 0.24.1 to 0.25.0 (#92)
  • c8208be chore(deps-dev): bump sinon from 14.0.2 to 15.0.0 (#91)
  • 70d4ee3 fix: filter ci executions against pushes to master only (#90)
  • 946ff23 chore(deps): bump nearform/optic-release-automation-action from 3 to 4 (#89)
  • 250dc2c chore: release trigger added to notify-release workflow (#87)
  • a4be7e4 chore: changed nvmrc file content (#86)
  • dd31735 chore(deps-dev): bump tsd from 0.23.0 to 0.24.0 (#85)
  • e385dfd chore: removed optional github token (#84)
  • 9bde1d3 chore(deps-dev): bump tsd from 0.22.0 to 0.23.0 (#83)
  • 96a5735 chore(deps): bump fastify-plugin from 3.0.1 to 4.1.0 (#82)
  • b4e0fe9 chore(deps-dev): bump tsd from 0.21.0 to 0.22.0 (#79)
  • c6851b8 chore(deps): bump nearform/optic-release-automation-action from 2 to 3 (#78)

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Release pending!

Pending commits since release v3.1.1

Based on the following commits, a patch release is recommended.

Unreleased commits have been found which are pending release, please publish the changes.

If you close the issue as Not Planned

  • The notification will be snoozed for 7 days, starting when the issue is closed. After this period has passed, a new notification issue will be created the next time this action is run.

Issue generated by github-actions-notify-release.

Support four-part checks

For ACL-style checks it might be more convenient to separate object-type and object-id parts of the check (casbin seems to support that). Currently this can be worked around by concatenating both of them into single object entity, but it is not exactly elegant.

Support custom enforce loggers

Instead of executing default fastify.log.info({ sub, obj, act }, 'Invoking casbin enforce'), it should be possible to invoke custom function with { sub, obj, act}, e. g. to implement some kind of a basic audit functionality.

Release pending!

Pending commits since release v2.1.0

Unreleased commits have been found which are pending release, please publish the changes.

  • 4e00e24 Bump http-errors from 1.8.1 to 2.0.0 (#58)
  • 346cbf8 chore(deps-dev): bump @types/node from 17.0.45 to 18.0.0 (#76)
  • 954a719 chore: updates for fastify from 3.29.0 to 4.0.2 (#74)
  • 94c6e60 chore(deps-dev): bump tsd from 0.20.0 to 0.21.0 (#73)
  • 4c8bb95 chore(deps-dev): bump sinon from 13.0.2 to 14.0.0 (#69)
  • 491ba37 chore(deps-dev): bump standard from 16.0.4 to 17.0.0 (#68)
  • 809299e chore: set setup-node action version to v3 (#67)
  • c04ca52 Bump tsd from 0.19.1 to 0.20.0 (#66)
  • 0c4c1f0 Bump actions/setup-node from 3.0.0 to 3.1.0 (#65)
  • 353831f Bump tap from 15.2.3 to 16.0.0 (#64)
  • b868ffc Bump actions/checkout from 2 to 3 (#63)
  • 9917610 Bump actions/setup-node from 2 to 3.0.0 (#62)
  • b9d8326 Bump sinon from 12.0.1 to 13.0.0 (#61)
  • 40141e1 fix: updating pull request event (#60)
  • 5085859 chore: create .github/workflows/check-linked-issues.yml
  • c031ec7 chore: use major version of notify release action
  • b4b5bbe Bump nearform/github-action-notify-release from 1.2.7 to 1.2.8 (#59)
  • 71e50b7 Bump @types/node from 16.11.14 to 17.0.0 (#57)
  • 96c471d Update fastify-casbin version of the example to 2.1.0 (#55)
  • a80947b Bump fastify/github-action-merge-dependabot from 2.7.1 to 3.0.2 (#56)
  • f56c125 Bump fastify/github-action-merge-dependabot from 2.7.0 to 2.7.1 (#53)
  • 1c9957c Bump fastify/github-action-merge-dependabot from 2.6.0 to 2.7.0 (#52)
  • 0258aa6 Bump actions/setup-node from 2.4.1 to 2.5.0 (#51)
  • 104f10b Bump tsd from 0.18.0 to 0.19.0 (#50)
  • 39c931c Bump fastify/github-action-merge-dependabot from 2.5.0 to 2.6.0 (#49)
  • afd6c6a Bump sinon from 11.1.2 to 12.0.0 (#48)
  • 78496dd Bump actions/checkout from 2.3.5 to 2.4.0 (#47)
  • 7192358 Bump actions/checkout from 2.3.4 to 2.3.5 (#46)
  • 869d952 Bump tsd from 0.17.0 to 0.18.0 (#45)
  • 3eff5c4 Bump actions/setup-node from 2.4.0 to 2.4.1 (#44)

Issue generated by github-actions-notify-release.

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.