sails-hook-rbac
v0.0.1
Published
Simple RBAC checker with support for context checks for Sails.js as a hook
Downloads
2
Maintainers
Readme
Sails hook RBAC
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 toreq.me
. This can be changed using thesessionObject
configuration.body
reference toreq.body
req
reference toreq
- all attributes from the action: result from calling
callback
needs to be called to return from the validation. The callback signaturecallback(err, result)
matches regular Node.js callback signatures. The result is treated as a boolean. Iferr
is not null, thenresult
is consideredfalse
.
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.