• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GraphQL rule / access control / auth / security / authorization / permission

graphql-rule

Build Status Coverage Status npm version Dependency Status License

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)

import Rule from 'graphql-rule';

Rule.create({
  // [REQUIRED] Unique rule model name.
  // Used for child field type specification.
  name: string = null,
  
  // Base class for newly created and returned rule model class.
  // Base class must extend {Model} from 'graphql-rule'.
  base: ?class = class<Model>,
  
  // Define dynamic property getters.
  // Can be accessed via `model.$props.propName`
  // Each property is lazily initialized upon the first access and cached for performance.
  // Useful for something expensive to calculate and is accessed over and over by multiple fields.
  props: {
    [propName: string]: (model: Model) => any,
  } = {},
  
  // Define default rule for fields in this model.
  defaultRule: {
    // `preRead` is checked before accessing or calling a field.
    // Useful for failing fast before accessing field that can be expensive to calculate such as field that initiates network calls
    // If `false` or `preRead(model, key)` returns falsy value, the access to field will fail immediately.
    // [default=true]
    preRead: boolean | (model: Model, key: string) => boolean | Promise<boolean>,
    
    // `read` is checked after accessing or calling a field.
    // Useful for passing/failing based on the field value.
    // If `false` or `read(model, key, value)` returns falsy value, the access to field will fail immediately.
    // [default=true]
    read: boolean | (model: Model, key: string, value: any) => boolean | Promise<boolean>,
    
    // `readFail` is used when either `preRead` or `read` returns falsy value.
    // If it is not a function, it is used as a final value for the failed field.
    // If it is function, the returend value is used as a final value for the failed field.
    // It can throw an error, if throwing an error is a desired upon unauthorized access to a field.
    // [default=null]
    readFail: any | (model: Model, key: string, value: ?any) => any,
  } = {preRead: true, read: true, readFail: null},
  
  // Define access rule for fields in this model.
  rules: {
    [fieldName: string]: {
      // See `defaultRule.preRead`
      preRead,
      
      // See `defaultRule.read`
      read,
      
      // See `defaultRule.readFail`
      readFail,
      
      // If specified, field value will be wrapped as an instance of the specified Model class.
      type: string | class<Model> = null,
      
      // Whether the field returns a list of Model instances.
      // Used together with `type` above.
      list: boolean = false,
      
      // Filter function for list.
      // Used together with `list` above.
      readListItem: (listItem: FieldModel, model: Model, key: string, list: [FieldModel]) => boolean
      
      // Whether the field is a function method.
      method: boolean = false,
      
      // Whether to cached the final value for the field.
      // Only applied if `method: false` above.
      cache: boolean = true,
    },
  },
  
  // Interfaces to inherit static and prototype properties and methods from.
  // Useful if you have common props / field rules / etc.
  interfaces: [class<Model>] = [],
})

Basic Usage without GraphQL

import Rule from 'graphql-rule';

// create access control model for your data
const Model = Rule.create({
  // name for this access model
  name: 'Model',

  // define access rules
  rules: {
    // allow access to `public` property.
    public: true,

    secret: {
      // disallow access to `secret` property.
      read: false,

      // throw an error when read is disallowed.
      readFail: () => { throw new Error('Access denied'); },
    },

    conditional: {
      // access raw data via `$data`.
      // conditionally allow access if `conditional` <= 3.
      read: (model) => model.$data.conditional <= 3,

      readFail: (model) => { throw new Error(`${model.$data.conditional} > 3`); },
    },
  },
});


// create a wrapped instance of your data.
const securedData = new Model({
  public: 'public data',
  secret: 'something secret',
  conditional: 5,
});

securedData.public // 'public data'

securedData.secret // throws Error('Access denied').

securedData.conditional // throws Error('5 > 3').


// same access model for different data.
const securedData2 = new Model({conditional: 1});

securedData2.conditional // 1 since 1 < 3.

User / Profile with Session

// set default `readFail`
Rule.config({
  readFail: () => { throw new Error('Access denied'); },
});

const UserRule = Rule.create({
  name: 'User',

  // props are lazily initialized and cached once initialized.
  // accessible via `model.$props`.
  props: {
    isAdmin: (model) => model.$context.admin,

    isAuthenticated: (model) => Boolean(model.$context.userId),

    isOwner: (model) => model.$data.id === model.$context.userId,
  },

  rules: {
    // Everyone can read `id`.
    id: true,

    email: {
      // allow access by admin or owner.
      read: (model) => model.$props.isAdmin || model.$props.isOwner,

      // returns null when read denied.
      readFail: null,
    },

    // No one can read `password`.
    password: false,

    profile: {
      // Use `Profile` Rule for `profile`.
      type: 'Profile',

      // allow access by all authenticated users
      read: (model) => model.$props.isAuthenticated,

      readFail: () => { throw new Error('Login Required'); },
    },
  },
});

const ProfileRule = Rule.create({
  name: 'Profile',

  rules: {
    name: true,

    phone: {
      // Access `UserRule` instance via `$parent`.
      read: (model) => model.$parent.$props.isAdmin || model.$parent.$props.isOwner,

      readFail: () => { throw new Error('Not authorized!'); },
    },
  },
});


const session = {
  userId: 'session_user_id',
  admin: false,
};

const userData = {
  id: 'user_id',
  email: '[email protected]',
  password: 'secret',
  profile: {
    name: 'John Doe',
    phone: '123-456-7890',
  },
};

// pass `session` as a second param to make it available as `$context`.
const user = new UserRule(userData, session);

user.id // 'user_id'

user.email // `null` since not admin nor owner.

user.password // throws Error('Access denied').

user.profile // `ProfileRule` instance. accessible since authenticated.

user.profile.name // 'John Doe'

user.profile.phone // throws Error('Not authorized!') since not admin nor owner.

Integration with GraphQL

// Use `UserRule` and `ProfileRule` from the above example.

const ProfileType = new GraphQLObjectType({
  name: 'Profile',
  fields: {
    name: { type: GraphQLString },
    phone: { type: GraphQLString },
  }
});

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    id: { type: GraphQLID },
    email: { type: GraphQLString },
    password: { type: GraphQLString },
    profile: { type: ProfileType },
  }
});

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      user: {
        type: UserType,
        args: {
          id: { type: GraphQLID }
        },
        resolve: (_, args, session) => {
          // `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.getUser(args.id).then((user) => new UserRule(user, session));
        },
      }
    }
  })
});

app.use('/graphql', graphqlHTTP((request) => ({
  schema: schema,
  // pass session data as `context`.
  // becomes available as third parameter in field `resolve` functions.
  context: request.session,
})));

Integration with Mongoose & GraphQL

// Define Mongoose schemas.
const UserModel = mongoose.model('User', new mongoose.Schema({
  email: String,
  password: String,
  profile: {
    type: ObjectId,
    ref: 'Profile',
  },
}));

const ProfileModel = mongoose.model('Profile', new mongoose.Schema({
  name: String,
  phone: String,
}));


// Define access rules.
const UserRule = Rule.create({
  name: 'User',
  props: {
    isAdmin: (model) => model.$context.admin,
    isOwner: (model) => model.$data.id === model.$context.userId,
  },
  rules: {
    id: true,
    email: {
      preRead: (model) => model.$props.isAdmin || model.$props.isOwner,
      readFail: () => { throw new Error('Unauthorized'); },
    },
    password: false,
    profile: {
      type: 'Profile',
      preRead: true,
    },
  },
});

const ProfileRule = Rule.create({
  name: 'Profile',
  rules: {
    name: true,
    phone: {
      preRead: (model) => model.$parent.$props.isAdmin || model.$parent.$props.isOwner,
      readFail: () => null,
    },
  },
});


// Define GraphQL Types.
const ProfileType = new GraphQLObjectType({
  name: 'Profile',
  fields: {
    name: { type: GraphQLString },
    phone: { type: GraphQLString },
  }
});

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    id: { type: GraphQLID },
    email: { type: GraphQLString },
    password: { type: GraphQLString },
    profile: { type: ProfileType },
  }
});


// Define GraphQL Queries.
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      user: {
        type: UserType,
        args: {
          id: { type: GraphQLID }
        },
        resolve: async (_, {id}, sessionContext) => {
          const userId = new ObjectId(id);
          const user = await UserModel.findById(userId).populate('profile').exec();
          const securedUser = new UserRule(user, sessionContext);
          return securedUser;
        },
      }
    }
  })
});


// Express GraphQL middleware.
app.use('/graphql', graphqlHTTP((request) => ({
  schema: schema,
  context: request.session,
})));

More $props and $context

const Model = Rule.create({
  name: 'Model',
  props: {
    // context.admin
    isAdmin: (model) => Boolean(model.$context.admin),

    // !context.userId
    isGuest: (model) => !model.$context.userId,

    // !props.isGuest
    isAuthenticated: (model) => !model.$props.isGuest,

    // context.userId
    authId: (model) => model.$context.userId,

    // data.authorId
    authorId: (model) => model.$data.authorId,

    // props.authorId === props.authId
    isOwner: (model) => model.$props.authorId === model.$props.authId,
  },
  defaultRule: {
    // read allowed by default
    read: (model) => true,

    // throws an error when read not allowed
    readFail: (model, key) => { throw new Error(`Cannot access '${key}'`); },
  },
  rules: {

    // use defaultRule settings
    authorId: {},

    // read allowed only if `props.isAdmin`
    adminField: (model) => model.$props.isAdmin,

    // above is equivalent to:
    adminField: {
      read: (model) => model.$props.isAdmin,
    },

    // read allowed only if `props.isAuthenticated`
    authField: (model) => model.$props.isAuthenticated,

    // read allowed only if `props.isGuest`
    guestField: (model) => model.$props.isGuest,

    // read allowed only if `props.isOwner`
    ownerField: (model) => model.$props.isOwner,

    notAllowedField: {
      read: (model) => false,
      readFail: (model, key) => { throw new Error('not allowed'); },
    },

    nullField: {
      read: (model) => false,
      readFail: (model) => null,
    },
  },
});

const session = {
  userId: 'user_id_1',
  admin: true,
};

const model = new 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.$props.isAdmin === true;
model.$props.isGuest === false;
model.$props.isAuthenticated === true;
model.$props.isOwner === true;
model.$props.authId === 'user_id_1';
model.$props.authorId === 'user_id_1';

// allowed to read by defaultRuledefault.read rule
model.authorId === 'user_id_1';

// allowed to read since $props.isAdmin
model.adminField === 'adminFieldValue';

// allowed to read since $props.isAuthenticated
model.authField === 'authFieldValue';

// not allowed to read since !$props.isGuest
model.guestField; // throws Error("Cannot access 'guestField'")

// allowed to read since $props.isOwner
model.ownerField === 'ownerFieldValue';

// not allowed to read
model.notAllowedField; // throws Error('not allowed')

// not allowed to read; returns null
model.nullField === null;

// rule is undefined
model.undefinedField === 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.

More Repositories

1

tsdef

TypeScript common pattern shortcut definitions / utility gist library
TypeScript
591
star
2

tscpaths

Replace absolute paths to relative paths after typescript compilation
TypeScript
266
star
3

react-native-google-sign-in

React Native Wrapper for Latest Google Sign-In OAuth SDK / API
Objective-C
211
star
4

graphql-input-number

A configurable custom input number type for GraphQL with sanitization and validation.
JavaScript
101
star
5

react-native-linkedin-sdk

React Native Wrapper for Latest LinkedIn Mobile SDK for Sign-In / Auth and API Access.
Objective-C
40
star
6

batchloader

BatchLoader is a utility for data fetching layer to reduce requests via batching written in TypeScript. Inspired by Facebook's DataLoader. Works great with GraphQL, MongoDB, Mongoose, etc.
TypeScript
35
star
7

firebase-encode

Encode and decode firebase key string to escape and unescape unsafe characters (.$[]#/).
JavaScript
34
star
8

proptypes-parser

PropTypes parser / generator for React and React Native components with GraphQL-like syntax.
JavaScript
29
star
9

react-native-mail-compose

React Native library for composing email. Wraps MFMailComposeViewController for iOS and Intent for Android.
Java
27
star
10

react-native-message-compose

React Native library for composing Message / SMS / Text. Wraps MFMessageComposeViewController for iOS and Intent for Android.
Java
24
star
11

graphql-scalar

Configurable custom GraphQL Scalars (string, number, date, etc) with sanitization / validation / transformation in TypeScript.
TypeScript
23
star
12

vcard-generator

VCard v4.0 rfc6350 compliant generator from JSON.
JavaScript
21
star
13

graphql-input-string

A configurable custom input string type for GraphQL with sanitization and validation.
JavaScript
20
star
14

graphql-relay-connection

A GraphQL Relay connection with cursor based on any comparator. Can be used with ObjectId for MongoDB.
JavaScript
20
star
15

seri

JavaScript (Node.js) serializer / deserializer with support for classes.
JavaScript
19
star
16

tsapis

Various cloud services API response types documented and written in TypeScript
TypeScript
16
star
17

country-calling-code

Country calling codes based on countrycode.org
JavaScript
15
star
18

react-native-router-relay-todo

React Native Todo App with react-router-native and relay
JavaScript
10
star
19

auth0-react-native

React Native port of Auth0 SDK (auth0/auth0.js)
JavaScript
10
star
20

react-hat

A higher order component for react-helmet that auto fills favicon, og, twitter meta tags for SEO.
JavaScript
9
star
21

s3-policy-v4

Amazon AWS S3 Upload Policy Generator with Signature Version 4
JavaScript
8
star
22

angular-smart-scroll

Smart infinite scroll directive for Angular.js that is customizable to support nested, horizontal, vertical, and/or bidirectional scrolls
JavaScript
7
star
23

underscore-node

Underscore for Node.js without unnecessary cross-browser checks
JavaScript
7
star
24

romodel

A lightweight and unopinionated model-based data accessor / enhancer library
JavaScript
6
star
25

ts-jutil

TypeScript library of simple utility functions
TypeScript
6
star
26

graphql-fetcher

A GraphQL data fetcher using fetch API.
JavaScript
5
star
27

iso-country-codes

ISO Country Codes including Alpha-2, Alpha-3, and numeric codes
JavaScript
5
star
28

jexgrid

JavaScript Grid Solution
JavaScript
4
star
29

react-native-contact-picker

React Native wrapper for CNContactPickerViewController
Swift
4
star
30

rn-json-store

A thin wrapper around React Native's AsyncStorage to easily write and read JSON values.
JavaScript
4
star
31

graphql-complexity

Compute complexity of GraphQL Queries
JavaScript
3
star
32

basex-encoder

Encode / decode any base X to and from string or buffer written in TypeScript
TypeScript
3
star
33

jwt-node-decoder

Decodes JWT (JSON Web Token) and checks expiration date. A Node port of angular-jwt.
JavaScript
2
star
34

auth-perm

A simple level-based permission / authorization / access control
JavaScript
2
star
35

angular-facebook-api

A Facebook SDK wrapper service for Angular.js
JavaScript
2
star
36

fullcontact-card-reader

FullContact Card Reader API Wrapper for Node.js
JavaScript
2
star
37

firestore-backup-cronjob

TypeScript
2
star
38

s3-policy-middleware

Express Middleware for Amazon AWS S3 Upload Policy Generator with Signature Version 4
JavaScript
2
star
39

servertil

HTTP server / express related utilities library written in TypeScript
TypeScript
1
star
40

material-design-color-palette

Material design color palette meets TinyColor
JavaScript
1
star
41

tsxhr

A super lightweight XMLHTTPRequest wrapper written in TypeScript. No dependencies.
TypeScript
1
star
42

healthcheck-ok

A simple healthcheck middleware for express.js that sends 200.
JavaScript
1
star
43

tstf

CLI tools for useful TypeScript code transformations such as paths transforms.
TypeScript
1
star
44

restpi

REST API client for various third party cloud services
TypeScript
1
star
45

superwild

Extremely fast, optimized, compilable wildcard pattern matching without using RegExp, which is slow.
TypeScript
1
star
46

webtil

Tiny utility library functions to be used in browser written in TypeScript
TypeScript
1
star
47

sanidator

Sanitize and Validate JavaScript Objects
JavaScript
1
star
48

unagi

A collection of basic JavaScript utility functions intended for Node.js (V8). It is best optimized for performance with NO cross-browser compatibility.
JavaScript
1
star
49

formost

Event based reactive form field model
TypeScript
1
star
50

react-native-add-contact

WIP: NOT WORKING YET! Add contact to address book
Java
1
star
51

vcard3

vCard 3.0 iOS compliant
TypeScript
1
star
52

react-native-issues-solutions

This repo is for me to track current React Native issues and possible/best available solutions.
1
star
53

firestore-model

Firestore / Firebase object model schema wrapper
TypeScript
1
star
54

expjson

Super lightweight, fast, and optimized evaluate-able and compilable expressions in JSON written in TypeScript
TypeScript
1
star
55

ga-util

Google Analytics Wrapper written in TypeScript
TypeScript
1
star
56

ts-url

URL object parser written in TypeScript
TypeScript
1
star
57

brands

social media brands / branding resources
1
star
58

tshttpcode

HTTP Status Code enum in TypeScript optimized for tree shaking
TypeScript
1
star
59

swifty-js

Swift like property observer (willSet, didSet) pattern for JavaScript.
JavaScript
1
star
60

memprop

React optimization tool for memoizing (function / object) property to avoid unnecessary re-renders
TypeScript
1
star
61

blitline-s3

Post Blitline jobs that uploads to s3 and do polling for the jobs.
JavaScript
1
star
62

notil

Small utility library written in TypeScript to be used in Node.js environment using Node packages.
TypeScript
1
star
63

smartystreets-js

JS client for SmartyStreets API. Address autocomplete and verification.
JavaScript
1
star