Giter VIP home page Giter VIP logo

graphql-auth's Introduction

Authentication and authorization in GraphQL-based apps

One approach to authenticate and authorize actions in GraphQL-based servers built using GraphQL reference implementation.

GraphQL itself is authentication/authorization-agnostic, you can use anything you want to do this auth-part of your app. This repo just demonstrates how to work with already authenticated users on GraphQL-level operations

As for the use case this is simple todo list, nothing fancy.

Thanks to @charypar for help and valuable discussion while I was sorting that stuff out.

Requirements

All queries/mutations should be available only to authenticated users

This is done in two steps:

  1. Express middlewares that reject request if no Auth header is present (or its value is empty string) or authenticate user with provided token.
  2. GraphQL is called with 3rd parameter - context, that contains authenticated user. It is then passed down to actions to authorize user provided.

Each action requires user having certain permissions otherwise it will not be executed

This is implemented in actions themselves as this is part of core business-logic. Each action has guard on entry that checks whether user provided is allowed to execute it. Authenticated user is provided as first argument to action, so action already has everything it needs to make this check.

Implementation

graphql entry point usually takes two params: schema definition and query to execute. There is third parameter, I call it context that seems to be user-defined thing that is being passed down to all GraphQL operations (including resolve functions). This context param is populated with authenticated user instance, passed to graphql function and then passed down to action functions. It allows actions themselves to decide whether given user is allowed to execute them or not. If user is not allowed (has no roles required), error is thrown.

In the real world it could be promise-based flow, but here it just throws Error to keep things simple. This error is intercepted by GraphQL internals and returned to called as regular GraphQL error (as stated in spec).

The only thing I'm worried about is that reference implementation returns promise that is always resolved, no matter if query was executed successfully or error was thrown. Also potential errors are returned as string messages only. These two things make it quite hard to distinct different types of errors and react accordingly (e.g. by returning correct HTTP status).

Try it at home

  1. Install stuff with npm install
  2. Run server with npm run dev. It will start server and you should be interested in http://localhost:3000/api endpoint.
  3. Issue any of the following requests to GraphQL endpoint. They need to be POST requests with Content-Type header set to application/graphql.
  4. All requests must have Auth header with token value of the user you are executing this request as. For the list of users and their tokens see core/auth.js file

Get todo items on list

The following query will return list of all not yet completed items. It requires authenticated user (as mentioned above) with read role, otherwise it will return an error. Try it with different users and see how it works.

{
  items {
	id,
	title,
	completed
  }
}

You may add query param as items(includeCompleted: true) to include completed items too.

Mark item as completed

This one marks item with given ID (see core/store.js file) as completed. It requires authenticated user to with write role, otherwise it will return an error as well.

mutation completeItem {
  markItemAsCompleted(id: 2) {
	id,
	title,
	completed
  }
}

Add new item

This one adds new item and also requires authenticated userwith write role.

mutation addItemToList {
  addItem(title: "take some sleep") {
	id,
	title,
	completed
  }
}

graphql-auth's People

Contributors

mostr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

graphql-auth's Issues

Thank you! I need some advice!

Thank you for creating this!! I've been looking everywhere for something like this!

I hope you don't mind me opening up this issue, I just wanted to open up a channel of communication in getting some advice on authentication/auth with graphQL.

I have one opening question and that is would you recommend using a separate 'server' for checking if a token exists and if the user has those permissions?

Thank you again ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.