GraphQL authorization made easy
Homepage Repository npm Download
npm install graphql-acl-service@2.0.1
GraphQL authorization made easy
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Install it via npm
npm install graphql-acl-service
A step by step series of examples that tell you how to get a development env running
Say what the step will be
git clone https://gitlab.com/gotoar/graphql-acl-service.git
npm install
You can find an example in the examples folder, or a boilerplate at this repo
Let's see an example of a configuration object and then explain what each thing does
const Service = require('graphql-acl-service')
const controller = require('./route/to/controller')
const CustomException = require('./route/to/custom/exception')
const exampleConfig = {
exception: Error,
is: {
role_field: 'role',
privileged: ['super', 'admin']
},
protected: ['id'],
resolvers: {
yourself: async (is, user, raw) => is.Role('admin') && user.uid !== raw.customer_id
},
methods: [
{
name: 'batchFetch',
method: controller.success
},
{
name: 'create',
method: controller.create,
rules: [
{ type: 'privileged', message: 'Only admins' },
{ type: 'yourself', message: 'You can only create users for yourself', exception: CustomException }
],
sanitize: true
}
],
fields: {
invite: [
{ type: 'privileged', message: 'Only admins' }
],
users: [
{ type: 'privileged', message: 'Only admins' }
]
}
}
const ExampleService = new Service({ role: 'super' }, exampleConfig)
As explained in the concepts section, when a rule returns true an exception is thrown. If the rule has an exception set, it will throw that one, but if there isn't, it will default to the one defined in the root of the config object
is provides helper methods that allow us to check whether the user is privileged or not, or if the role equals a value. In order to do that we need to declare the role field and the values that a privileged user will have. If we want to check for a specific role we can do so creating a resolver like this:
{
...
only_super: async (is, user, raw) => !is.Role('super'),
...
}
Maybe we don't want our users populating some data, eg.: an autogenerated id. So we can define those inside an array and those fields will be stripped from the payload
If you want to protect a method with a custom rule first you have to define it inside resolvers. This will allow to use the same rule to protect many methods while not repeating code. From the resolver you can access everything in the scope of the file and four parameters passed to the function in the following order:
An array of objects containing the method resolver, rules and method related configuration. The method configuration object consists of the following fields (Some are required and some are optional)
[
...
{ type: 'privileged', message: 'You need to be an admin or a superuser', exception: Error },
...
]
const method = (arg1, arg2) => console.log(arg1, arg2)
You can set it in the args aray like this
{
...
args: ['id', 'name'],
...
}
And when the method is called with a payload like this
{
id: '1',
name: 'tom',
age: '21'
}
The method will be called this way
method('1', 'tom')
If you want to pass an arg and the whole payload you can do so by setting the arg with the wildcard *
{
...
args: ['id', '*'],
...
}
An object setting the rules to run for that field
npm run test
You can check linting with this command
npm run lint
No dependencies
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting merge requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details