Docs
Launch GraphOS Studio

Authentication and authorization

Control access to your GraphQL API


Your API probably needs to control which users can see and interact with the various data it provides.

  • Authentication is determining whether a given user is logged in, and subsequently determining which user someone is.
  • Authorization is then determining what a given user has permission to do or see.

💡 TIP

can now provide authentication and authorization for your entire . While it may make sense to re-apply auth checks at a level or in a monolith graph, the Apollo Router has built and provided standard JWT checks that can be setup with a simple YAML configuration and enforce this in a central location for all subgraphs:

Putting authenticated user info in your contextValue

NOTE

In the examples below, we use top-level

calls to start our server asynchronously. If you'd like to see how we set this up, check out the
Getting Started
guide for details.

Before we can correctly control access to data, we have to authenticate a user. There are many patterns for providing authentication credentials, including HTTP headers and JSON web tokens.

The example below extracts a user token from the HTTP Authorization header included in each request. It then fetches the corresponding user object for that token and adds it to

that's passed to every executing . Each resolver can then use this object to determine what data the user has access to.

import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
interface MyContext {
// we'd define the properties a user should have
// in a separate user interface (e.g., email, id, url, etc.)
user: UserInterface;
}
const server = new ApolloServer<MyContext>({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
// Note: This example uses the `req` argument to access headers,
// but the arguments received by `context` vary by integration.
// This means they vary for Express, Fastify, Lambda, etc.
// For `startStandaloneServer`, the `req` and `res` objects are
// `http.IncomingMessage` and `http.ServerResponse` types.
context: async ({ req, res }) => {
// Get the user token from the headers.
const token = req.headers.authorization || '';
// Try to retrieve a user with the token
const user = await getUser(token);
// Add the user to the context
return { user };
},
});
console.log(`🚀 Server listening at: ${url}`);
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
// Note: This example uses the `req` argument to access headers,
// but the arguments received by `context` vary by integration.
// This means they vary for Express, Fastify, Lambda, etc.
// For `startStandaloneServer`, the `req` and `res` objects are
// `http.IncomingMessage` and `http.ServerResponse` types.
context: async ({ req, res }) => {
// Get the user token from the headers.
const token = req.headers.authorization || '';
// Try to retrieve a user with the token
const user = await getUser(token);
// Add the user to the context
return { user };
},
});
console.log(`🚀 Server listening at: ${url}`);

Because your contextValue is generated separately for every new request, we don't have to worry about cleaning up user data at the end of an 's execution.

The specifics of retrieving a user will look different for each method of authentication, but the final part will look about the same every time. The authorization needs for your schema might require you to put nothing more than { loggedIn: true } into contextValue, but also may require an id or roles, like { user: { id: 12345, roles: ['user', 'admin'] } }.

In the next section, we'll look at ways to use the user information we now have to secure your schema.

Authorization methods

API-wide authorization

Once we have information about the user making a request, the most basic thing we can do is deny them the ability to execute a at all based on their roles. We'll start with this all-or-nothing approach to authorization because it's the most basic.

We should use this method only on highly restrictive environments that provide no public access to the API whatsoever, like an internal tool or an independent microservice that shouldn't be exposed to the public.

To perform this kind of authorization, we can modify the context function to throw a GraphQLError if the user isn't authenticated:

import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { GraphQLError } from 'graphql';
interface MyContext {
user: UserInterface;
}
const server = new ApolloServer<MyContext>({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
context: async ({ req }) => {
// get the user token from the headers
const token = req.headers.authorization || '';
// try to retrieve a user with the token
const user = getUser(token);
// optionally block the user
// we could also check user roles/permissions here
if (!user)
// throwing a `GraphQLError` here allows us to specify an HTTP status code,
// standard `Error`s will have a 500 status code by default
throw new GraphQLError('User is not authenticated', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
// add the user to the context
return { user };
},
});
console.log(`🚀 Server listening at: ${url}`);
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { GraphQLError } from 'graphql';
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
context: async ({ req }) => {
// get the user token from the headers
const token = req.headers.authorization || '';
// try to retrieve a user with the token
const user = getUser(token);
// optionally block the user
// we could also check user roles/permissions here
if (!user)
// throwing a `GraphQLError` here allows us to specify an HTTP status code,
// standard `Error`s will have a 500 status code by default
throw new GraphQLError('User is not authenticated', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
// add the user to the context
return { user };
},
});
console.log(`🚀 Server listening at: ${url}`);

The only difference from the basic context function is the check for the user. If no user exists or if lookup fails, the function throws an error and the corresponding is not executed.

In resolvers

API-wide authorization might be useful in specific instances, but more commonly, a API has at least a few that are public. For example, a news site might show article previews to anyone, but restrict the full body of articles to paying customers.

Luckily, offers very granular control over data. In , individual have the ability to check user roles and make decisions as to what to return for each user. In the previous sections, we saw how to attach user information to the contextValue object. In the rest of the article, we'll discuss how to use that object.

For our first example, let's look at a that's only accessible with a valid user:

users: (parent, args, contextValue) => {
// In this case, we'll pretend there is no data when
// we're not logged in. Another option would be to
// throw an error.
if (!contextValue.user) return null;
return ['bob', 'jake'];
};

This example is a in our schema named users that returns a list of users' names. The if check on the first line of the function looks at the contextValue generated from our request, checks for a user object, and if one doesn't exist, returns null for the whole .

One choice to make when building out our is what an unauthorized should return. In some use cases, returning null here is perfectly valid. Alternatives to this would be to return an empty array, [] or to throw an error, telling the client that they're not allowed to access that . For the sake of simplicity, we just returned null in this example.

Now let's expand that example a little further, and only allow users with an admin role to look at our user list. After all, we probably don't want just anyone to have access to all our users.

users: (parent, args, contextValue) => {
if (!contextValue.user || !contextValue.user.roles.includes('admin')) return null;
return contextValue.models.User.getAll();
};

This example looks almost the same as the previous one, with one addition: it expects the roles array on a user to include an admin role. Otherwise, it returns null. The benefit of doing authorization like this is that we can short-circuit our and not even call lookup functions when we don't have permission to use them, limiting the possible errors that could expose sensitive data.

Because our have access to everything in contextValue, an important question to ask is how much information we want in that object. For example, we don't need the user's id, name, or age (at least not yet). It's best to keep things out of the contextValue until they're needed, since they're easy to add back in later.

In data models

As our server gets more complex, there will probably be multiple places in the schema that need to fetch the same kind of data. In our last example, you may have noticed the return array was replaced with a call to contextValue.models.User.getAll().

As always, we recommend moving the actual data fetching and transformation logic from your to

or model objects that each represent a concept from your application: User, Post, etc. This allows you to make your a thin routing layer, and put all of your business logic in one place.

For example, a model file for User would include all the logic for operating on users, and might look something like this:

export const User = {
getAll: () => {
/* fetching/transformation logic for all users */
},
getById: (id) => {
/* fetching/transformation logic for a single user */
},
getByGroupId: (id) => {
/* fetching/transformation logic for a group of users */
},
};

In the following example, our schema has multiple ways to request a single user:

type Query {
user(id: ID!): User
article(id: ID!): Article
}
type Article {
author: User
}
type User {
id: ID!
name: String!
}

Rather than having the same fetching logic for a single user in two separate places, it usually makes sense to move that logic to the model file. You may have guessed, with all this talk of model files in an authorization article, that authorization is another great thing to delegate to the model, just like data fetching. You would be right.

Delegating authorization to models

You may have noticed that our models also exist on the contextValue, alongside the user object we added earlier. We can add the models to the context in exactly the same way as we did the user.

context: async ({ req }) => {
// get the user token from the headers
const token = req.headers.authentication || '';
// try to retrieve a user with the token
const user = getUser(token);
// optionally block the user
// we could also check user roles/permissions here
if (!user) throw new GraphQLError("you must be logged in to query this schema", {
extensions: {
code: 'UNAUTHENTICATED',
},
});
// add the user to the contextValue
return {
user,
models: {
User: generateUserModel({ user }),
...
}
};
},

Starting to generate our models with a function requires a small refactor, that would leave our User model looking something like this:

export const generateUserModel = ({ user }) => ({
getAll: () => {
/* fetching/transform logic for all users */
},
getById: (id) => {
/* fetching/transform logic for a single user */
},
getByGroupId: (id) => {
/* fetching/transform logic for a group of users */
},
});

Now any model method in User has access to the same user information that already had, allowing us to refactor the getAll function to do the permissions check directly rather than having to put it in the :

getAll: () => {
if (!user || !user.roles.includes('admin')) return null;
return fetch('http://myurl.com/users');
};

With custom directives

Another way to go about authorization is via . A directive is an identifier preceded by a @ character, optionally followed by a list of named , which can appear after almost any form of syntax in the or schema languages.

Check out this example of an authorization :

const typeDefs = `#graphql
directive @auth(requires: Role = ADMIN) on OBJECT | FIELD_DEFINITION
enum Role {
ADMIN
REVIEWER
USER
}
type User @auth(requires: USER) {
name: String
banned: Boolean @auth(requires: ADMIN)
canPost: Boolean @auth(requires: REVIEWER)
}
`;

The @auth can be called directly on specific or on a type. This nicely hides the authorization logic behind the directive implementation.

With your @auth in place, your now need to check the user's role against the role specified by the directive. One way to do this is using the mapSchema function from the

package to transform every in your schema. For an example of setting up -based permission checks, see the
mapSchema docs
.

Outside of GraphQL

If you're using a REST API that has built-in authorization, like with an HTTP header, you have one more option. Rather than doing any authentication or authorization work in the layer (in /models), it's possible to simply pass through the headers or cookies to your REST endpoint and let it do the work.

Here's an example:

src/server.ts
context: async ({ req }) => {
// pass the request information through to the model
return {
user,
models: {
User: generateUserModel({ req }),
...
}
};
},
src/models/user.ts
export const generateUserModel = ({ req }) => ({
getAll: () => {
return fetch('http://myurl.com/users', { token: req.headers.token });
},
});

If your REST endpoint is already backed by some form of authorization, this cuts down a lot of the logic that needs to get built in the layer. This can be a great option when building a GraphQL API over an existing REST API that has everything you need already built in.

Previous
Automatic persisted queries
Next
CORS
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company