Hive Gateway supports Authentication and Authorization using JSON Web Tokens (JWT).
A JSON Web Tokens (JWT) is a signed token containing arbitrary information,
commonly used for authentication. By being signed by the issuer of the token, it can be verified
that the token is valid and has not been tampered with.
Hive Gateway provides a plugin to easily integrate JWT into your API, allowing you to easily
validate, decode and use the token (for identity and authorization).
Once you have the JWT token extract and validated, the JWT claims (and optionally, the full token)
are injected to the Hive Gateway execution context, and forwarded to upstream GraphQL subgraphs,
using the extensions field.
How to use?
Here’s a minimal example for configuring the JWT plugin with a local signing key, and looking for
the token in the authorization header:
gateway.config.ts
import { createInlineSigningKeyProvider, defineConfig, extractFromHeader,} from "@graphql-hive/gateway";const signingKey = "my-secret-key";export const gatewayConfig = defineConfig({ jwt: { // Look and extract for the token in the 'authorization' header, with the 'Bearer' prefix. tokenLookupLocations: [ extractFromHeader({ name: "authorization", prefix: "Bearer" }), ], // Decode and validate the token using the provided signing key. signingKeyProviders: [createInlineSigningKeyProvider(signingKey)], // Forward the verified token payload to the upstream GraphQL subgraphs. forward: { payload: true, }, },});
import { defineConfig, createInlineSigningKeyProvider, createRemoteJwksSigningKeyProvider, extractFromHeader, extractFromCookie } from '@graphql-hive/gateway'export const gatewayConfig = defineConfig({ jwt: { // Forward the extracted token and claims to the upstream GraphQL subgraphs. forward: { payload: true, // optional, default is "true" token: false, // optional, default is "false" extensionsFieldName: "jwt", // optional, default is "jwt" }, // Configure your signing providers: either a local signing-key or a remote JWKS are supported. signingKeyProviders: [ createInlineSigningKeyProvider(signingKey), createRemoteJwksSigningKeyProvider({ jwksUri: 'https://example.com/.well-known/jwks.json' }) ] // Configure where to look for the JWT token: in the headers, or cookies. // By default, the plugin will look for the token in the 'authorization' header only. tokenLookupLocations: [ extractFromHeader({ name: 'authorization', prefix: 'Bearer' }), extractFromCookie({ name: 'auth' }), ], // Configure your token issuers/audience/algorithms verification options. // By default, the plugin will only verify the HS256/RS256 algorithms. // Please note that this should match the JWT signer issuer/audience/algorithms. tokenVerification: { issuer: 'http://my-issuer.com', audience: 'my-audience', algorithms: ['HS256', 'RS256'], }, // The plugin can reject the request if the token is missing or invalid (doesn't pass JWT `verify` flow). // By default, the plugin will reject the request if the token is missing or invalid. reject: { missingToken: true, invalidToken: true, } }})
The JWT token and payload can be forwarded to the upstream GraphQL subgraphs, using the extensions
field of the request body.
This workflow can allow you to easily delegate the authentication process to Hive Gateway, and allow
the subgraphs to deal only with the user identity and authorization.
To pass the full token payload, you can use the forwarded.claims option:
The JWT plugin will inject the decoded token and payload into the context of Hive Gateway.
You can use the injected payload with other plugins, to implement things like authorization or
user-identity based logic.
For example, with a plugin like Operation Field Permissions, you can use the jwt property of the
context to access the decoded JWT token, and decide what permissions to allow to the user based on
identity or token claims:
gateway.config.ts
import { useOperationFieldPermissions } from "@envelop/operation-field-permissions";import { defineConfig } from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ // ... jwt: { // ... }, plugins: () => [ useOperationFieldPermissions({ getPermissions: (context) => { const { jwt } = context; // Check based on identity / user-id. if (jwt?.payload?.sub === "123") { return new Set(["Query.*"]); } // Check based on token payload if (jwt?.payload?.role === "admin") { return new Set(["Query.*"]); } // Default permissions return new Set(["Query.greetings"]); }, }), ],});
Allowing Introspection
If you want to allow introspection queries to be executed without a JWT token, you can use the
operation field permissions plugin to allow only introspection queries when no JWT token ispresent.
This way, you can still introspect the schema and explore the API without a token, while protecting
the rest of the API with JWT authentication.
gateway.config.ts
import { useOperationFieldPermissions } from "@envelop/operation-field-permissions";import { defineConfig } from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ jwt: { ...options, reject: { // allow requests with missing token to introspect the schema missingToken: false, }, }, plugins: () => [ useOperationFieldPermissions({ getPermissions: (context) => { const { jwt } = context; if (!jwt) { // allow only introspection if no token is present return new Set([ "Query.__schema", "Query.__type", "Query.__typename", ]); } // allow everything when token is present, or you can return a different // set of permissions based on the token like in the previous example return "*"; }, }), ],});
In upstream GraphQL subgraphs
The JWT token and claims are forwarded to the upstream GraphQL subgraphs, using the extensions
field.
To access the JWT token and claims in your upstream service resolvers/execution, you can use the
extensions field of the incoming GraphQL request.
If you are using GraphQL-Yoga for your upstream
subgraph implementation, you can use a built-in utility for extracting it for you in an easy way:
yoga-subgraph.ts
import { useForwardedJWT } from "@graphql-hive/gateway";const myYogaSubgraphServer = createYoga({ schema: mySchema, plugins: [ useForwardedJWT({ // The name of the field in the extensions object, default is "jwt" extensionsFieldName: "jwt", // The name of the field to inject into the local context object, default is "jwt" extendContextFieldName: "jwt", }), ],});
With this plugin configured, you should be able to just access context.jwt in your subgraphs, just
like you would in the gateway.
This makes the process of integrating JWT easier, and streamlined across the whole flow of
execution.
With Apollo-Server, you can access the forwarded claims/token, using a custom Apollo-Server plugin
that extracts extensions and injects it into the context:
apollo-subgraph.ts
import { ApolloServer, ApolloServerPlugin } from "@apollo/server";const extractJwtPlugin = { async requestDidStart({ request, contextValue }) { contextValue.jwt = request.extensions?.jwt; },} satisfies ApolloServerPlugin<{ jwt?: { payload: Record<string, any> } }>;const server = new ApolloServer({ // Now, in your schema resolvers, you can access the JWT token and claims using `context.jwt`. plugins: [extractJwtPlugin], // ...});
Other implementations for GraphQL subgraph servers can also access the JWT token and claims, by
looking at the extensions field of the incoming request.
The extensions field of the incoming request will contain the JWT token and claims, injected by
Hive Gateway, following this structure:
The plugin can be configured to look for the JWT token in different locations:
By default, the plugin will look for the token in the authorization header. You can configure the
plugin to look for the token in a different header or with a different prefix.
The prefix is being validated along with the token (for example: Bearer my-token).
When using multiple providers, the plugin will try to use the first available signing key.
import { createInlineSigningKeyProvider, createRemoteJwksSigningKeyProvider, defineConfig,} from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ // ... jwt: { // ... signingKeyProviders: [ // In case your remote provider is not available, the plugin will try use the inline provider. createRemoteJwksSigningKeyProvider({ jwksUri: "https://example.com/.well-known/jwks.json", }), createInlineSigningKeyProvider(process.env.MY_JWT_SECRET), ], },});
Token Verification
The plugin verification process can be customized to match the JWT token issuer, audience, and
algorithms.
Note that the verification options should match the JWT signer’s configuration.
In case you want to handle the error yourself, you can set
reject: { missingToken: false, invalidToken: false } and handle the error in your resolvers. The
context.jwt will be undefined in case of missing or invalid token.
Granular Protection using Auth Directives (@authenticated, @requiresScopes and @policy)
Configuration
By default, the JWT plugin protects the whole schema. If you want to use a granular protection by
using Federation directives such as @authenticated, @requiresScopes and @policy, you can use
the Generic Auth plugin to have a granular protection using with or without JWT.
With the following configuration, you can use the JWT plugin to extract the token and claims, and
then use the Generic Auth plugin to protect the schema with the Federation directives:
import { defineConfig } from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ // ... jwt: { // You have to disable the default rejection of the JWT plugin reject: { missingToken: false, invalidToken: false, }, }, genericAuth: { // Then set generic auth plugin to use granular mode mode: "protect-granular", // Set where to extract the payload resolveUserFn: (ctx) => ctx.jwt?.payload, // If you want to continue execution even if some fields are rejected rejectUnauthenticated: false, },});
Protect a field using a field @authenticated
In your GraphQL schema SDL, you can add @authenticated directive to your fields.
By default, the plugin will try to extract available scopes for the current payload from scope
property which is expected to be a string like read:user read:admin. However you can customize
this behavior by providing a custom extractScopes function.
{ validateUser, mode: 'protect-granular', // Set where to extract the payload resolveUserFn: ctx => ctx.jwt?.payload, extractScopes: jwtPayload => jwtPayload?.scopes // Expected to return an array of strings}
You can also apply AND or OR logic to the scopes:
extend schema @link( url: "https://specs.apollo.dev/federation/v2.5" import: ["@requiresScopes"] )type Query { # This field requires the user to have `read:user` OR `read:admin` scopes me: User! @requiresScopes(scopes: [["read:user"], ["read:admin"]]) # This field requires the user to have `read:user` AND `read:admin` scopes protectedField: String @requiresScopes(scopes: [["read:admin", "read:user"]]) publicField: String}
@policy directive to fetch the roles from a policy service
You can use the @policy directive to fetch the roles from a policy service. Here’s an example of
how you can use it: