Gateway-level JWT policy
Learn how to set up JWT authentication and claim-based authorization for all routes attached to a gateway.
This feature is an Enterprise-only feature that requires a Gloo Gateway Enterprise license.
Before you begin
Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
Add JWT policy to gateways
To protect all the routes that are attached to a gateway, create a JWT policy with a VirtualHostOption.
Create a VirtualHostOption with the details of the JSON Web Key Set (JWKS) server to use to verify the signature of JWTs in future protected requests.
Send a request to the httpbin app. Verify that your request is denied and that you get back a 401 HTTP response code, because all routes on the gateway now require a valid JWT token from the provider.
Example output:
Create an environment variable to save the JWT tokens for the users Alice and Bob. You can optionally create other JWT tokens by using the JWT generator tool.
Save the JWT token for Alice. Alice works in the
dev
team.Save the JWT token for Bob. Bob works in the
ops
team.
Send another request to the httpbin app. This time, you include Alice’s JWT token in the
Authorization
header. Because these JWT tokens were signed by the JWT issuer that is used in the JWT policy, the request now succeeds. Verify that you get back a 200 HTTP response code.Example output:
Repeat the request with Bob’s JWT token. Verify that the request succeeds with a 200 HTTP response code.
Example output:
Authorize access based on claims
You can use the claims in the JWT token to restrict access beyond basic authentication.
Create a RouteOption resource that extracts the
team
claim from the JWT token. The following example allows access to httpbin only if the JWT contains the"team": "dev"
claim.Send another request to the httpbin with the JWT token for Alice. Because the JWT matcher is set to
LIST_CONTAINS
, the request only succeeds if theteam: dev
claims is present in the JWT token. Because Alice’s JWT token includes that claim, the request succeeds.
Example output:
- Repeat the request with Bob’s JWT token. Because Bob’s token does not include the
team: dev
claim, the request is denied and a 403 HTTP response code is returned.
Example output:
Next steps
Good job protecting all the routes attached to the gateway.
Next, try out a guide that applies a JWT policy in more specific scenarios.