1.0.9 • Published 4 years ago

@grafee/shield v1.0.9

Weekly downloads
20
License
MIT
Repository
github
Last release
4 years ago

Grafee Shield - Access Control Module

Grafee is the easiest way to create an awesome GraphQL API.

Getting started

yarn add @grafee/shield

Technologies

  • graphql-shield - GraphQL Server permissions as another layer of abstraction!

API

createRule

To create a role for access control

Params: role, callback = false, options = { cache: "contextual" }

permissions.js

import { createRole } from "@grafee/shield";

const GUEST = "GUEST";

// If user ctx.user.role equals GUEST veriable
export const isGuest = createRole(GUEST);

export const isAuthenticated = createRole(GUEST, ({ role, ctx }) => {
  // role equals GUEST veriable
  return ctx.user.role !== role;
});

Usage:

resolvers.js

import { isGuest, isAuthenticated } from "./permissions";

export const Query = {
  feed: isGuest,
  users: isAuthenticated
};

createShield

To create shield middleware for GraphQL server

access.js

import { createShield } from "@grafee/shield";
import * as resolvers from "./resolvers";

export default createShield(resolvers);

Usage:

import { ApolloServer } from "@grafee/core";
import access from "./access";
import schema from "@/modules";

const middlewares = [access];

const application = ApolloServer({
  schema,
  middlewares,
  engine: {
    apiKey: process.env.ENGINE_API_KEY,
    schemaTag: "production"
  }
});

const port = process.env.PORT || 4040;
application.listen({
  port,
  host: "0.0.0.0"
});

console.log(`🚀  GraphQL: http://localhost:${port + application.path}`);
console.log(
  `🚀  Subscriptions: ws://localhost:${port + application.subscriptions}`
);

Logic Rules (or, and, not, chain)

and, or, not, chain

and, or and not allow you to nest rules in logic operations.

and rule

And rule allows access only if all sub rules used return true.

chain rule

Chain rule allows you to chain the rules, meaning that rules won't be executed all at once, but one by one until one fails or all pass.

The left-most rule is executed first.

or rule

Or rule allows access if at least one sub rule returns true and no rule throws an error.

not

Not works as usual not in code works.

import { or, and, not } from "@grafee/shield";
import { isAdmin, isAuthenticated, isEditor } from "./permissions";

export const Query = {
  frontPage: not(isAuthenticated),
  fruits: and(isAuthenticated, or(isAdmin, isEditor)),
  customers: and(isAuthenticated, isAdmin)
};

license MIT

MIT License

Copyright (c) 2019 Yakauleu Uladzislau

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.
1.0.9

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago