Secure a GraphQL API with JWT authentication🔗
How to setup an authentication by JWT on a GraphQL API using overblog/GraphQLBundle and lexik/jwt-authentication-bundle.
Installation🔗
This cookbook asume you already have a working GraphQL api.
1 |
|
Follow the documentation to generate and configure a public and a secret key.
Configuration🔗
Add a firewall on your GraphQL API;
1 2 3 4 5 6 7 8 9 10 11 12 |
|
If users must be authenticated on all your API, add the following access control:
1 2 3 4 5 |
|
If anonymous users can access some parts of your API:
1 2 3 4 5 |
|
Then require authentication on queries, mutations and fields by using the access
or the public
config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Note: Unauthorized users can see fields with access condition but can access to it whereas they can't see at all fields with public condition. Be aware that objects must have at least one public field (so at least one query and one mutation).
See overblog bundle security documentation for more options.
Authentication will require client to provide a valid JWT token through the Authorization
header:
1 |
|
Authentication🔗
You will need a way to provide valid JWT to your user.
The following example is an authentication by username and password providing JWT in case of success.
Create a route:
1 2 3 4 |
|
Then configure a firewall with a guard authenticator for this route:
1 2 3 4 5 6 7 8 9 10 11 |
|
Note: This firewall must be defined before api's firewall.
The UsernamePasswordGuardAuthenticator
authenticate the user based on the given username and password and return a JWT in response. Customize this authentication according to your needs.
GraphiQL🔗
If you are using GraphiQL through overblog/GraphiQLBundle you have to customize it a little in order to handle authentication.
First, add access control to GraphiQL:
1 2 3 4 5 6 |
|
Note: GraphiQL is generaly not available in production, so it's fine to grant access to anonymous users. If your api has an access control, be sure to put graphiql access control before.
GraphiQL template allow us to override headers that will be sent to GraphQL, just what we want to add our authentication header.
Before, you need to create a Twig filter generating JWT from a username:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Then add the JWT to headers in the template:
1 2 3 4 5 6 7 8 9 10 |
|
Finally we will configure GraphiQL to use this template.
1 2 3 |
|
Usage🔗
With this template, you can set the token you want to use with the token
parameter when accessing to GraphiQL:
1 |
|
Or set the username
parameter, a JWT will be generated for you:
1 |
|
Or use the default username (admin@example.com in our example):
1 |
|