Skip to content

Commit 90f46c0

Browse files
authored
docs: add AuthGuardian JWT guide (hasura#3958)
1 parent 5387ba0 commit 90f46c0

9 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
.. meta::
2+
:description: Integrate AuthGuardian JWT with Hasura
3+
:keywords: hasura, docs, guide, authentication, auth, jwt, integration
4+
5+
.. _auth_guardian_jwt:
6+
7+
OneGraph's AuthGuardian JWT Integration with Hasura GraphQL engine
8+
==================================================================
9+
10+
.. contents:: Table of contents
11+
:backlinks: none
12+
:depth: 1
13+
:local:
14+
15+
In this guide, we will walk through how to set up AuthGuardian JWT to work with the Hasura GraphQL engine.
16+
17+
About AuthGuardian
18+
^^^^^^^^^^^^^^^^^^
19+
20+
`AuthGuardian <https://www.onegraph.com/docs/auth_guardian.html>`__
21+
is a free service by `OneGraph <https://www.onegraph.com/>`__ that allows you to both add sign-on with dozens of services like GitHub, Twitch, Stripe, Salesforce, and more, and also to easily visually describe authentication and authorization rules for your app, API, or service.
22+
23+
It will generate JWTs compatible with any service that supports JWTs - like Hasura! And on top of that, AuthGuardian has built-in support for generating Hasura roles to make working with Hasura's permission system as easy as possible.
24+
25+
Step 1: Create an account
26+
^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
Sign into `OneGraph <https://www.onegraph.com/>`__ with either GitHub or an email/password.
29+
30+
On the left sidebar, chose "Auth Services" and then "AuthGuardian", and we're ready to start!
31+
32+
Step 2: Add an auth rule
33+
^^^^^^^^^^^^^^^^^^^^^^^^
34+
35+
AuthGuardian works with two basic concepts: ``rules`` and ``effects``.
36+
37+
``Effects`` are useful for modifying the JWT during sign-in. AuthGuardian supports four ``effects``:
38+
39+
- Set the ``user_id`` for Hasura in the JWT
40+
- Set the default Hasura user role in the JWT
41+
- Set the allowed Hasura user roles in the JWT
42+
- Set session variables for Hasura to use in the JWT
43+
44+
These can all be mixed and matched when creating Hasura-compatible JWTs to fully cover your app's permission settings.
45+
46+
The second concept, ``rules`` determines whether the ``effects`` are allowed to be added to the JWT. The rules might look like:
47+
48+
- "If this user is logged into Twitch.tv"
49+
- "If this user has an email on Salesforce that belongs to my organization's domain"
50+
- "If this user is a member of my GitHub organization"
51+
52+
53+
Let's see how to use these ``rules`` and ``effects`` in action!
54+
55+
Option 1: Set Hasura ``user_id``
56+
--------------------------------
57+
Let's say you're using GitHub as the primary login for your Hasura app, and that you want to use their GitHub ``user_id`` as a primary key:
58+
59+
- For the section "When the user on", select ``GitHub`` -> ``login status`` -> ``is true`` ("When this user is logged into GitHub")
60+
- For the section "Then", choose ``On hasura set user id``.
61+
- Add set the user id in the "built-in value" field to ``GITHUB_USER_ID``
62+
- Click the ``save`` button on the right hand side.
63+
64+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-user-id.png
65+
:alt: Set an AuthGuardian JWT with a user id
66+
67+
Option 2: Set the default Hasura role
68+
-------------------------------------
69+
Next, you want to assign a *default* role for all users, even those who haven't logged in with GitHub yet.
70+
71+
Add a new rule (the ``+`` button in the top-left), and fill it out:
72+
73+
- For the section "When the user is on", select ``Always`` (this will *always* pass, even if the user isn't logged into any service).
74+
- For the section "Then", choose ``On hasura set default role``.
75+
- Add the default role, e.g. ``user``.
76+
- Click the ``save`` button on the right hand side.
77+
78+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-default-role.png
79+
:alt: Set an AuthGuardian JWT with default role
80+
81+
.. note::
82+
83+
Note that AuthGuardian knows that Hasura requires that the ``default role`` *also* appear in the list of ``x-hasura-allowed-roles``, and added it in both places automatically.
84+
85+
Option 3: Allow additional Hasura roles
86+
---------------------------------------
87+
Now, you want to restrict access to some data in Hasura so that only you and your teammates can read it. We'll use Hasura's permissions to restrict data to those who have an ``admin`` role, and use AuthGuardian's rules to set that role in the JWT to people who belong to your GitHub organization:
88+
89+
- For the section "When the user is on", select ``GitHub`` -> ``is member of organization named`` -> ``<your org name, e.g. AcmeCo>`` ("When this user is a member of AcmeCo on GitHub")
90+
- For the section "Then", choose ``On hasura add roles``.
91+
- Click on "Add" and add an additional role, e.g. ``admin``.
92+
- Click the ``save`` button on the right hand side.
93+
94+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-additional-role.png
95+
:alt: Set an AuthGuardian JWT with additional roles
96+
97+
Option 4: Set a session variable
98+
--------------------------------
99+
Hasura can use **session variables** for all sorts of `powerful cases <https://docs.hasura.io/1.0/graphql/manual/auth/authorization/roles-variables.html#dynamic-session-variables>`_. AuthGuardian also supports setting these in your JWT!
100+
101+
Let's say we want to restrict access to some super-interesting data in our Hasura backend to users who have starred a particular GitHub repository:
102+
103+
- For the section "When the user is on", select ``GitHub`` -> ``has starred a repository with a full name of`` -> ``hasura/graphql-engine`` ("When this user has starred the 'hasura/graphql-engine' on GitHub")
104+
- For the section "Then", choose ``On hasura set session variable``.
105+
- Add your session variable name ``is-our-biggest-fan`` and value to JSON ``true``.
106+
- Click the ``save`` button on the right hand side.
107+
108+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-session-variable.png
109+
:alt: Set an AuthGuardian JWT with session variables for Hasura to use
110+
111+
.. note::
112+
113+
AuthGuardian knows where to place Hasura session variables in the JWT, and also knows to prefix the variables with ``x-hasura-`` automatically.
114+
115+
You're all done! Now whenever a user hits your Hasura API they'll always have a default role of ``user``, and:
116+
117+
- Their Hasura user-id will match their (permanent) GitHub user id if they're logged into GitHub.
118+
- They'll be allowed to use the ``admin`` permissions if they're a member of your GitHub organization.
119+
- They'll have a session variable of ``x-hasura-is-our-biggest-fan: true`` if they've starred ``hasura/graphql-engine`` on GitHub.
120+
121+
Nice!
122+
123+
124+
Step 3: Generate Hasura's JWT config to securely verify your new tokens
125+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126+
Next we'll configure Hasura to verify our new JWTs - don't worry, AuthGuardian also has built-in support for that!
127+
128+
- On the left sidebar, click on "JWT Settings" and scroll down to "Configuration generator".
129+
- Choose either "Hasura" or "Hasura on Heroku".
130+
131+
The generated config has the following structure:
132+
133+
.. code-block:: json
134+
135+
{
136+
"type": "RS256",
137+
"jwk_url": "https://serve.onegraph.com/app/35bcf98c-1df0-4644-a453-bf06a1349449/.well-known/jwks.json",
138+
"claims_format": "json"
139+
}
140+
141+
- Add the generated config as a value for the environment variable ``HASURA_GRAPHQL_JWT_SECRET`` or for the ``--jwt-secret`` server flag.
142+
143+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-config-generator.png
144+
:alt: AuthGuardian lets you copy/paste the required JWT configuration for either Hasura or Hasura-on-Heroku
145+
146+
Step 4: Test the config and tokens
147+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
148+
When configuring your permissions in Hasura, it's useful to be able to quickly generate test tokens to make sure everything works as you expect.
149+
150+
- Copy the JWT that you created in step 2.
151+
- On the left sidebar, click on "JWT settings" and scroll down to "Generate signed token".
152+
- Paste the copied JWT (or optionally write your own JSON if you want to test alternative scenarios).
153+
- Copy the signed token and add it as a header in the Hasura console.
154+
155+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-generate-test-jwt.png
156+
:alt: Use the JWT-signer form to quickly sign any JSON and test in the Hasura console
157+
158+
- In GraphiQL, try out queries to test that the integration works as expected by adding an ``Authorization`` header, with a value of ``Bearer <the-copied-JWT-text>``. Hasura's GraphiQL will recognize this header, show you its content, and confirm whether it recognizes the JWT as securely signed.
159+
160+
.. thumbnail:: ../../../../img/graphql/manual/guides/auth-guardian-test-jwt.png
161+
:alt: Test AuthGuardian JWT
162+
163+
Next Steps
164+
^^^^^^^^^^
165+
AuthGuardian supports much more, including the ability to eject your rules as a pair of GraphQL query and JavaScript function so you can customize the auth as necessary. To read more about it, please visit the `AuthGuardian docs <https://www.onegraph.com/docs/>`__.

Diff for: docs/graphql/manual/guides/integrations/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Guides: Integration/migration tutorials
1111
:local:
1212

1313
- :doc:`auth0-jwt`
14+
- :doc:`auth-guardian-jwt`
1415
- :doc:`aws-cognito`
1516
- :doc:`apollo-subscriptions`
1617
- `Blog: Move from firebase to realtime GraphQL on Postgres <https://hasura.io/blog/firebase2graphql-moving-from-firebase-to-realtime-graphql-on-postgres-4d36cb7f4eaf>`__
@@ -24,5 +25,6 @@ Guides: Integration/migration tutorials
2425
:hidden:
2526

2627
Auth0 JWT Integration <auth0-jwt>
28+
OneGraph's AuthGuardian JWT Integration <auth-guardian-jwt>
2729
Using AWS Cognito for authentication <aws-cognito>
2830
Subscriptions using apollo-client <apollo-subscriptions>
Loading
Loading
37.8 KB
Loading
Loading
Loading
58.3 KB
Loading
44.1 KB
Loading

0 commit comments

Comments
 (0)