npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sails-hook-rbac

v0.0.1

Published

Simple RBAC checker with support for context checks for Sails.js as a hook

Downloads

2

Readme

Sails hook RBAC


npm version


rbac2 hook for Sails v0.11 and up.

Allows you to set up RBAC2 rules in your API end-points

Installation

Install from NPM.

$ npm install sails-hook-rbac --save

Configuration options available

To configure this hook, you can create a file named config/rbac.js with the following options:

module.exports.rbac = {
  rbacRules: [
    { a: 'anonymous'}, 
    { a: 'normal', can: 'anonymous' },
    { a: 'admin', can: 'normal' }
    ],
  waitFor: [
    'custom',
    'session',
    'orm',
  ],
  helper: {
    path: 'services.RbacHelpers',
  },
  sessionObject: 'me',
  sessionObjectRolePath: 'role',
};

If you don't create the mentioned config file, it defaults to the above settings.

Implementation details

This hook extends the normal Sails routes configuration.

It lets you add an rbac attribute to your routes that defines the Role-Based Access Control for that particular route.

The hook uses the session object in the request object (by default, me, configured in sessionObject) to get the user's role.

The user's role attribute can be configured in sessionObjectRolePath. It can be a simple attribute (defaults to role), or it can be a path to that attribute, in case the value is nested within the session object.

In the default configuration, valid roles are normal and admin. There is a special role named anonymous that is assigned to all requests that don't have a user session.

All anonymous requests have a req.me.role = 'anonymous'

The rbac attribute in a Route definition is an object where it's attributes are role definitions.

Each of these 'roles' attributes are objects on their own, normally empty ones {}.

If a route needs to have a more advanced validation rule, a when attribute can be added.

This when attribute can be a function, or a string. If it is a function, it must match the following this signature:

when: function ( params, callback ) {
  const result = validateFunctionGoesHere(params.me, params.body);
  if( result ) {
    return callback(null, true); 
  }
  return callback(null, false);
}

where

  • params is an object with:
    • all attributes from the action: result from calling req.allParams()
    • me reference to req.me. This can be changed using the sessionObject configuration.
    • body reference to req.body
    • req reference to req
  • callback needs to be called to return from the validation. The callback signature callback(err, result) matches regular Node.js callback signatures. The result is treated as a boolean. If err is not null, then result is considered false.

A more detailed explanation can be found in rbac2 documentation.

if the when attribute is a string, it will call the service configured in the helper.path. For example:

when: 'isOwner'

would call sails.services.RbacHelpers.isOwner(params, next).

A can attribute can be added to a role definition, but a default one is generated by the hook. It is inferred from the controller and action names, or the route path. In the following example, the can attribute is set to put-post-update.

{
  'put /post/:postId': {
    controller: 'post',
    action: 'update',
    rbac: {
      'normal': {
        when: function isAuthor(params, next) {
          return UserService.isAuthor(params, next);
        }
      }
    }
  }
}

In this case, the when function calls the UserService.isAuthor function.

If no rbac is defined, a warning is shown in the console. That means, no RBAC will be checked for that route. One good example of this behavior is the 'get /' route.

If the rule has a route attribute, this route will be used instead of the 'original' one. In some cases, two routes conflict with each other, and the actual route needs to have a special regex. One such case is if one route has a more strict rule. For example:

  • the /group/tree and /group/:groupId routes. Normally, since /group/tree is listed first and before the more generic route, /group/tree should be hit if called. However, since /group/:groupId has a stricter rbac rule, and matches, the order no longer matters, and the server tries to use the stricter route. The solution is to make the matching of a more restrictive pattern, so that /group/tree does not match /group/:groupId using a regex that forces the groupId to be an id string.

Future

  • Support multiple roles

License

The MIT License (MIT)

Copyright (c) 2021 Luis Lobo Borobia

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.