UNPKG

4.46 kBMarkdownView Raw
1# RBAC
2A reusable package for permission based authorization with local or remote checks and express middleware.
3
4## Install
5```
6npm install --save @c8/rbac
7```
8
9## Usage
10Check the `/examples` folder.
11
12## API
13### const rbac = new Rbac(opts)
14Creates a new Rbac instance with the given options for local or remote authorization. It also provides an express middleware that can use information in the request (i.e. the authentication token or principal) in the authorization process.
15
16```js
17const rbac = new Rbac({
18 remoteAuth: {
19 url: 'http://www.example.com/authorize'
20 }
21})
22
23app.get('/',
24 rbac.express.authorizeRemote(['users:read']),
25 (req, res, next) => {
26 res.json({ message: 'You have acces to this awesome content!' })
27 })
28```
29
30* **`opts` {object}** - Options object.
31 * **`remoteAuth` {object} (Optional)** - Optional configuration object for allowing remote HTTP permission evaluation.
32 * **`headers` {object} (Optional)** - Optional headers to pass in the HTTP request.
33 * **`url` {string}** - Url for the HTTP request, required if `opts.remoteAuth` is set. The endpoint is expected to accept a JSON object with `permissions {array}` property and return 200 in case of success or different 200 in case of unauthorized. It can also return some claims about the principal (i.e. the user id) which will be merged with `req.user`, when called by the express middleware.
34 * **`checkPermission` {function}** - Callback function for local permission evaluation with the signature `function (id)` and returning a Promise resolving to the principal permissions array. **If `opts.remoteAuth` is not set, then this property is required.**
35 * **`getReqId` {function} (Optional)** - A callback with the signature `(req) => {}` that returns the principal ID from the HTTP request object. Defaults to `(req) => req.user.id`.
36
37### rbac.authorize(id, permissions)
38Checks if a given principal is authorized for any of the given permissions. Returns a Promise resolving to the principal being allowed the permission. This function can authorize the principal locally, for which you need to define the `checkPermission` callback in the instance options.
39 * **`id` {number}** - The principal id to be checked against the permissions.
40 * **`permissions` {array}** - The permissions to be checked against the principal.
41
42### rbac.authorizeRemote(permissions, headers)
43Checks if a given principal is authorized for any of the given permissions. Returns a Promise resolving to the principal being allowed the permission. The remote server can also return some claims about the principal, which will be returned in the Promise. This function can authorize the principal remotely, for which you need to define the `remoteAuth` object in the instance options.
44
45* **`permissions` {array}** - The permissions to be checked against the principal.
46* **`headers` {object} (Optional)** - Optional headers to pass in the HTTP request.
47
48### rbac.express.authorize(permissions)
49 Returns an express middleware function for checking if the principal who made the request is authorized for any of the given permissions. Parameters are the same as rbac.authorize, except for the `id` parameter which can be setup in the constructor options via the getReqId callback.
50
51### rbac.express.authorizeRemote(permissions)
52Returns an express middleware function for checking if the principal who made the request is authorized for any of the given permissions. Parameters are the same as rbac.authorizeRemote, except for the `headers` parameter which can be setup in the constructor options via the `remoteAuth.headers` callback. It will define the `authorization` header as the current request authorization header.
53
54## Tests
55
56The following commands are available:
57+ `coverage` for running code coverage with Istanbul (it shows the report at the bottom)
58+ `standard` for code style checks with Standardjs
59+ `test` for running Mocha tests
60
61## Versioning
62This module adheres to [semver](http://semver.org/) versioning. That means that given a version number MAJOR.MINOR.PATCH, we increment the:
63
641. MAJOR version when we make incompatible API changes,
652. MINOR version when we add functionality in a backwards-compatible manner, and
663. PATCH version when we make backwards-compatible bug fixes.
67
68Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
69
70## License
71The MIT License
72
73Copyright (c) 2016 C8 MANAGEMENT LIMITED
\No newline at end of file