graphql-rule
Unopinionated rule based access / authorization / permission control for GraphQL type fields.
Inspired by RoModel and Firebase rules.
It actually has no dependencies to GraphQL. You can use it with plain js objects!
Supports Node.js >= 4.0.
Install
npm install --save graphql-rule
How It Works
graphql-rule
is simply an authorization layer between your data and data accessor (resolve
functions in GraphQL).
Without `graphql-rule`:
+-------------+ +------------+
| | | |
| | | |
| Accessor | +---> | Data |
| (GraphQL) | | |
| | | |
| | | |
+-------------+ +------------+
With `graphql-rule`:
+-------------+ +------------+ +------------+
| | | | | |
| | | | | |
| Accessor | +---> | Rules | +---> | Data |
| (GraphQL) | | | | |
| | | | | |
| | | | | |
+-------------+ +------------+ +------------+
You define access rules
for each property of your data (object), and it will
allow or disallow read to the property based on the predefined rules.
(only read rules are supported for now).
It is designed for access control in GraphQL, but it is not opinionated nor requires any dependencies. Thus, it can be used for all projects with or without GraphQL.
API - Rule.create(options)
; Rule
Basic Usage without GraphQL
; // create access control model for your dataconst Model = Rule; // create a wrapped instance of your data.const securedData = public: 'public data' secret: 'something secret' conditional: 5; securedDatapublic // 'public data' securedDatasecret // throws Error('Access denied'). securedDataconditional // throws Error('5 > 3'). // same access model for different data.const securedData2 = conditional: 1; securedData2conditional // 1 since 1 < 3.
User / Profile with Session
// set default `readFail`Rule; const UserRule = Rule; const ProfileRule = Rule; const session = userId: 'session_user_id' admin: false; const userData = id: 'user_id' email: 'user@example.com' password: 'secret' profile: name: 'John Doe' phone: '123-456-7890' ; // pass `session` as a second param to make it available as `$context`.const user = userData session; userid // 'user_id' useremail // `null` since not admin nor owner. userpassword // throws Error('Access denied'). userprofile // `ProfileRule` instance. accessible since authenticated. userprofilename // 'John Doe' userprofilephone // throws Error('Not authorized!') since not admin nor owner.
Integration with GraphQL
// Use `UserRule` and `ProfileRule` from the above example. const ProfileType = name: 'Profile' fields: name: type: GraphQLString phone: type: GraphQLString ; const UserType = name: 'User' fields: id: type: GraphQLID email: type: GraphQLString password: type: GraphQLString profile: type: ProfileType ; const schema = query: name: 'Query' fields: user: type: UserType args: id: type: GraphQLID { // `context` is passed as the third parameter of `resolve` function. // pass the `context` as the second parameter of `UserRule`. // Now your data is secured by predefined rules! return database; } ; app;
Integration with Mongoose & GraphQL
// Define Mongoose schemas.const UserModel = mongoose; const ProfileModel = mongoose; // Define access rules.const UserRule = Rule; const ProfileRule = Rule; // Define GraphQL Types.const ProfileType = name: 'Profile' fields: name: type: GraphQLString phone: type: GraphQLString ; const UserType = name: 'User' fields: id: type: GraphQLID email: type: GraphQLString password: type: GraphQLString profile: type: ProfileType ; // Define GraphQL Queries.const schema = query: name: 'Query' fields: user: type: UserType args: id: type: GraphQLID resolve: async { const userId = id; const user = await UserModel; const securedUser = user sessionContext; return securedUser; } ; // Express GraphQL middleware.app;
More $props and $context
const Model = Rule; const session = userId: 'user_id_1' admin: true; const model = authorId: 'user_id_1' adminField: 'adminFieldValue' authField: 'authFieldValue' guestField: 'guestFieldValue' ownerField: 'ownerFieldValue' notAllowedField: 'notAllowedFieldValue' nullField: 'nullFieldValue' undefinedField: 'undefinedFieldValue' // passed as $data session // passed as $context; model$propsisAdmin === true;model$propsisGuest === false;model$propsisAuthenticated === true;model$propsisOwner === true;model$propsauthId === 'user_id_1';model$propsauthorId === 'user_id_1'; // allowed to read by defaultRuledefault.read rulemodelauthorId === 'user_id_1'; // allowed to read since $props.isAdminmodeladminField === 'adminFieldValue'; // allowed to read since $props.isAuthenticatedmodelauthField === 'authFieldValue'; // not allowed to read since !$props.isGuestmodelguestField; // throws Error("Cannot access 'guestField'") // allowed to read since $props.isOwnermodelownerField === 'ownerFieldValue'; // not allowed to readmodelnotAllowedField; // throws Error('not allowed') // not allowed to read; returns nullmodelnullField === null; // rule is undefinedmodelundefinedField === undefined;
Even More Advanced Usage
Take a look at test file.
LICENSE
The MIT License (MIT)
Copyright (c) 2016 Joon Ho Cho
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.