We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
When deploying a GraphQL schema, the admin user can set a
# Dgraph.Authorization
line at the bottom of the schema to specify how JWT
tokens present in the HTTP header requests are extracted, validated and used.
This line must start with the exact string # Dgraph.Authorization
and be at
the bottom of the schema file.
To configure how Dgraph should handle JWT token for /graphql
endpoint add a
line starting with # Dgraph.Authorization
and with the following parameters at
the very end of your GraphQL schema.
The Dgraph.Authorization
object uses the following syntax:
Dgraph.Authorization
object contains the following parameters:
Header
name of the header field used by the client to send the token.
Don’t use Dg-Auth
, X-Auth-Token
or Authorization
headers which are
used by Dgraph for other purposes.
Namespace
is the key inside the JWT that contains the claims relevant to
Dgraph authorization.
Algo
is the JWT verification algorithm which can be either HS256
or
RS256
.
VerificationKey
is the string value of the key, with newlines replaced with
\n
and the key string wrapped in ""
:
VerificationKey
contains the public key
string.VerificationKey
is the secret
key.JWKURL
/JWKURLs
is the URL for the JSON Web Key sets. If you want to pass
multiple URLs, use JWKURLs
as an array of multiple JWK URLs for the JSON Web
Key sets. You can only use one authentication connection method, either JWT
(Header
), a single JWK URL, or multiple JWK URLs.
Audience
is used to verify the aud
field of a JWT, which is used by
certain providers to indicate the intended audience for the JWT. When doing
authentication with JWKURL
, this field is mandatory.
ClosedByDefault
, if set to true
, requires authorization for all requests
even if the GraphQL type doesn’t specify rules. If omitted, the default
setting is false
.
When the # Dgraph.Authorization
line is present in the GraphQL schema, Dgraph
uses the settings in that line to
These claims are accessible to any @auth schema directives (a GraphQL schema directive specific to Dgraph) that are associated with GraphQL types in the schema file.
See the RBAC rules and [Graph traversal rules](./graphtraversal-rules for details on how to restrict data access using the @auth directive on a per-type basis.
To not only accept but to require the JWT token regardless of @auth directives
in your GraphQL schema, set option “ClosedByDefault” to true in the
# Dgraph.Authorization
line.
Dgraph.Authorization
is fully configurable to work with various authentication
providers. Authentication providers have options to configure how to generate
JWT tokens.
Here are some configuration examples.
In your clerk dashboard, Access JWT Templates
and create a template for
Dgraph.
Your template must have an aud
(audience), this is mandatory for Dgraph when
the token is verified using JWKURL.
Decide on a claim namespace and add the information you want to use in your RBAC rules.
This example uses the https://dgraph.io/jwt/claims
namespace and is retrieving
the user current organization, role (Clerk has currently two roles admin
and
basic_member
) and email.
This is our JWT Template in Clerk:
In the same configuration panel
Configure your Dgraph GraphQL schema with the following authorization
Note that
You can select the header to receive the JWT token from your client app,
X-Dgraph-AuthToken
is a header authorized by default by Dgraph GraphQL API to
pass CORS requirements.
To use a single JWK URL:
To use multiple JWK URL:
Using HMAC-SHA256 token in X-My-App-Auth
header and authorization claims in
https://my.app.io/jwt/claims
namespace:
Using HMAC-SHA256 token in X-My-App-Auth
header and authorization claims in
https://my.app.io/jwt/claims
namespace:
The value of the JWT header
is expected to be in one of the following forms:
Bare token.
For example:
A Bearer token, e.g., a JWT prepended with Bearer
prefix (including
space).
For example:
If ClosedByDefault is set to true, and the JWT is not present or if the JWT
token does not include the proper audience information, or is not properly
encoded, or is expired, Dgraph replies to requests on /graphql
endpoint with
an error message rejecting the operation similar to:
Error messages
aud
value doesn’t match
with the audience”We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
When deploying a GraphQL schema, the admin user can set a
# Dgraph.Authorization
line at the bottom of the schema to specify how JWT
tokens present in the HTTP header requests are extracted, validated and used.
This line must start with the exact string # Dgraph.Authorization
and be at
the bottom of the schema file.
To configure how Dgraph should handle JWT token for /graphql
endpoint add a
line starting with # Dgraph.Authorization
and with the following parameters at
the very end of your GraphQL schema.
The Dgraph.Authorization
object uses the following syntax:
Dgraph.Authorization
object contains the following parameters:
Header
name of the header field used by the client to send the token.
Don’t use Dg-Auth
, X-Auth-Token
or Authorization
headers which are
used by Dgraph for other purposes.
Namespace
is the key inside the JWT that contains the claims relevant to
Dgraph authorization.
Algo
is the JWT verification algorithm which can be either HS256
or
RS256
.
VerificationKey
is the string value of the key, with newlines replaced with
\n
and the key string wrapped in ""
:
VerificationKey
contains the public key
string.VerificationKey
is the secret
key.JWKURL
/JWKURLs
is the URL for the JSON Web Key sets. If you want to pass
multiple URLs, use JWKURLs
as an array of multiple JWK URLs for the JSON Web
Key sets. You can only use one authentication connection method, either JWT
(Header
), a single JWK URL, or multiple JWK URLs.
Audience
is used to verify the aud
field of a JWT, which is used by
certain providers to indicate the intended audience for the JWT. When doing
authentication with JWKURL
, this field is mandatory.
ClosedByDefault
, if set to true
, requires authorization for all requests
even if the GraphQL type doesn’t specify rules. If omitted, the default
setting is false
.
When the # Dgraph.Authorization
line is present in the GraphQL schema, Dgraph
uses the settings in that line to
These claims are accessible to any @auth schema directives (a GraphQL schema directive specific to Dgraph) that are associated with GraphQL types in the schema file.
See the RBAC rules and [Graph traversal rules](./graphtraversal-rules for details on how to restrict data access using the @auth directive on a per-type basis.
To not only accept but to require the JWT token regardless of @auth directives
in your GraphQL schema, set option “ClosedByDefault” to true in the
# Dgraph.Authorization
line.
Dgraph.Authorization
is fully configurable to work with various authentication
providers. Authentication providers have options to configure how to generate
JWT tokens.
Here are some configuration examples.
In your clerk dashboard, Access JWT Templates
and create a template for
Dgraph.
Your template must have an aud
(audience), this is mandatory for Dgraph when
the token is verified using JWKURL.
Decide on a claim namespace and add the information you want to use in your RBAC rules.
This example uses the https://dgraph.io/jwt/claims
namespace and is retrieving
the user current organization, role (Clerk has currently two roles admin
and
basic_member
) and email.
This is our JWT Template in Clerk:
In the same configuration panel
Configure your Dgraph GraphQL schema with the following authorization
Note that
You can select the header to receive the JWT token from your client app,
X-Dgraph-AuthToken
is a header authorized by default by Dgraph GraphQL API to
pass CORS requirements.
To use a single JWK URL:
To use multiple JWK URL:
Using HMAC-SHA256 token in X-My-App-Auth
header and authorization claims in
https://my.app.io/jwt/claims
namespace:
Using HMAC-SHA256 token in X-My-App-Auth
header and authorization claims in
https://my.app.io/jwt/claims
namespace:
The value of the JWT header
is expected to be in one of the following forms:
Bare token.
For example:
A Bearer token, e.g., a JWT prepended with Bearer
prefix (including
space).
For example:
If ClosedByDefault is set to true, and the JWT is not present or if the JWT
token does not include the proper audience information, or is not properly
encoded, or is expired, Dgraph replies to requests on /graphql
endpoint with
an error message rejecting the operation similar to:
Error messages
aud
value doesn’t match
with the audience”