Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
graphql-auth
Advanced tools
๐ Authentication and authorization middleware for GraphQL.
graphql-auth
is a very simple middleware that easily integrates with any GraphQL server that follows the GraphQL API for resolvers.
graphql-auth
exports a single function (middleware) withAuth
. This function takes two parameters, the first is scope
(if any) for authorization, and the second is the callback
to call when auth checking is complete. Let's look at an example:
import withAuth from 'graphql-auth';
const resolvers = {
Query: {
users: withAuth(['users:view'], (root, args, context) => { ... }),
...
}
}
The way this works is withAuth
looks for a special auth
property on the context
of the resolver. It expects the auth
property to have two properties of its own:
isAuthenticated
to tell if the user is logged inscope
scope of the logged in user (optional)This allows you to use any form of authentication already supported by common frameworks like express
and hapi
. Here is an example in Hapi.js:
import { graphqlHapi } from 'graphql-server-hapi';
import { makeExecutableSchema } from 'graphql-tools';
import typeDefs from './type-defs';
import resolvers from './resolvers';
const register = function(server, options, next) {
const executableSchema = makeExecutableSchema({
resolvers,
typeDefs,
});
server.register(
[
{
register: graphqlHapi,
options: {
path: '/graphql',
graphqlOptions: request => ({
pretty: true,
schema: executableSchema,
context: {
auth: {
isAuthenticated: request.auth.isAuthenticated,
scope: request.auth.credentials
? request.auth.credentials.scope
: null,
},
},
}),
},
},
],
error => {
if (error) return next(error);
next();
}
);
};
register.attributes = {
name: 'graphql-api',
version: '1.0.0',
};
export default register;
For more in depth examples take a look at the
graphql-auth-examples
repo.
yarn add graphql-auth
Without scope:
import withAuth from 'graphql-auth';
const resolvers = {
Query: {
users: withAuth((root, args, context, info) => { ... }),
...
}
}
With scope:
import withAuth from 'graphql-auth';
const resolvers = {
Query: {
users: withAuth(['users:view'], (root, args, context, info) => { ... }),
...
}
}
With dynamic scope:
import withAuth from 'graphql-auth';
const resolvers = {
Query: {
users: withAuth(
(root, args, context, info) => { /* return scope based on resolver args */ },
(root, args, context, info) => { ... }),
...
}
}
Thanks goes to these wonderful people (emoji key):
artgibson ๐ป | HaNdTriX ๐ป ๐ | Pascal Birchler ๐ป | Andrรฉas Hanss ๐ป ๐ |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
๐ Authentication and authorization middleware for GraphQL.
The npm package graphql-auth receives a total of 75 weekly downloads. As such, graphql-auth popularity was classified as not popular.
We found that graphql-auth demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.ย It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.