|
| 1 | +# exegesis-plugin-roles |
| 2 | + |
| 3 | +[](https://npmjs.org/package/exegesis-plugin-roles) |
| 4 | +[](https://travis-ci.org/exegesis-js/exegesis-plugin-roles) |
| 5 | +[](https://coveralls.io/r/exegesis-js/exegesis-plugin-roles) |
| 6 | +[](https://github.com/semantic-release/semantic-release) |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +Adds support for the "x-exegesis-roles" vendor extension, which adds support for |
| 11 | +restricting which operations are available to which users after they have been |
| 12 | +authenticated. Authenticators can optionally return "roles" for a user. |
| 13 | +"x-exegesis-roles" can be specified either as an array of "role" strings, or as |
| 14 | +an array of such arrays. |
| 15 | + |
| 16 | +For example: |
| 17 | + |
| 18 | +```yaml |
| 19 | +x-exegesis-roles: |
| 20 | + - a |
| 21 | + - b |
| 22 | +``` |
| 23 | +
|
| 24 | +would only allow access to an operation if a user has both the 'a' and 'b' |
| 25 | +role, or: |
| 26 | +
|
| 27 | +```yaml |
| 28 | +x-exegesis-roles: |
| 29 | + - [a] |
| 30 | + - [b, c] |
| 31 | +``` |
| 32 | +
|
| 33 | +would only allow access to an operation if a user has the 'a' role, or has |
| 34 | +both the 'b' and 'c' role. |
| 35 | +
|
| 36 | +"x-exegesis-roles" can be defined on the root OpenAPI object, in which case |
| 37 | +all operations in the document will require those roles. This can be overridden |
| 38 | +by specifying "x-exegesis-roles" in an individual operation. An empty array |
| 39 | +indicates a user requires no roles: |
| 40 | +
|
| 41 | +```yaml |
| 42 | +x-exegesis-roles: [] |
| 43 | +``` |
| 44 | +
|
| 45 | +If "x-exegesis-roles" is defined on an operation which has no security |
| 46 | +requirements defined, this will throw an error. |
| 47 | +
|
| 48 | +Roles do not apply to security schemes with the "oauth2" type; scopes apply |
| 49 | +there instead. |
| 50 | +
|
| 51 | +Allowed in: |
| 52 | +
|
| 53 | +* [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#oasObject) |
| 54 | +* [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#operationObject) |
| 55 | +
|
| 56 | +## Installation |
| 57 | +
|
| 58 | +```sh |
| 59 | +npm install exegesis-plugin-roles |
| 60 | +``` |
| 61 | + |
| 62 | +## Example |
| 63 | + |
| 64 | +Add this to your Exegesis options: |
| 65 | + |
| 66 | +```js |
| 67 | +import exegesisRolesPlugin from 'exegesis-roles-plugin'; |
| 68 | + |
| 69 | +options = { |
| 70 | + plugins: [ |
| 71 | + exegesisRolesPlugin({ |
| 72 | + // List of all allowed roles. If you try to use any roles that |
| 73 | + // aren't in this list in your document, an error will be thrown. |
| 74 | + allowedRoles: ['user', 'admin', 'ops'] |
| 75 | + }) |
| 76 | + ] |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +In your OpenAPI 3.x document: |
| 81 | + |
| 82 | +```yaml |
| 83 | +paths: |
| 84 | + '/kittens': |
| 85 | + get: |
| 86 | + description: Get a list of kittens |
| 87 | + security: |
| 88 | + - basicAuth: [] |
| 89 | + - oauth: ['readOnly'] |
| 90 | + post: |
| 91 | + description: Add a new kitten |
| 92 | + security: |
| 93 | + - basicAuth: [] |
| 94 | + - oauth: ['readWrite'] |
| 95 | + x-exegesis-roles: ['admin'] # Only users with the "admin" role may call this. |
| 96 | +``` |
| 97 | +
|
| 98 | +The "get" operation can only be executed if the request matches one of the two |
| 99 | +listed security requirements. The "post" operation can only be executed if |
| 100 | +the security requirements are matched, and the current "user" has the "admin" |
| 101 | +role. |
| 102 | +
|
| 103 | +Copyright 2018 Jason Walton |
0 commit comments