Validate JWTs at the edge without changing your API
Protect one API route by verifying a bearer access token’s signature, issuer, audience and expiry before it reaches the app.
An API gateway can validate a JWT before your backend sees the request. On Gregale, put a JWT rule on the exact method and path you want to protect, and use @app to cover all of the app’s public hostnames. The rule verifies the signature with the issuer's public keys, matches the issuer and audience, and rejects expired tokens. Add it on Hobby or above. JWT rule reference
Decide what the edge checks
Use the issuer URI your identity provider puts in iss, its HTTPS JWKS URL, the provider's signing algorithm, and an audience issued for your API. Use access tokens meant for this API, not ID tokens meant for a browser client. Require expiration. A shared issuer alone does not tell you the token belongs to your application.
A passing JWT identifies the caller. Keep tenant, object and operation authorization in the backend. Keep auth on alternate paths that do not use the public edge, such as internal service calls. Don't remove another authentication layer from a real app just because the demo is intentionally narrow.
Set up a disposable issuer and app
The starter includes a tiny Node service that serves only synthetic data and a public JWKS. A local signing key is generated for the test; it is not uploaded. Run these commands from the starter’s edge-jwt directory with Node 24, jq, the Gregale CLI on your PATH, and an explicitly exported GREGALE_TOKEN permitted to create and deploy apps in a disposable workspace. Signing in to the CLI does not export that variable.
export GREGALE_API_URL=https://api.gregale.dev
scratch=$(mktemp -d)
node demo.mjs stage "$scratch/app" "$scratch/keys"
Deploy only the staged app. Keep the private keys on your machine. The provisioner rejects extra files, links, changed fixture code and any non-public key material before requesting an app. It builds a separate temporary upload directory from the three checked files. It then chooses a new random slug, requires an HTTP 201 create receipt, and checks the exact app ID before opening the fixture or uploading code. It never adopts an existing app. The fixture contains no private data even before the JWT rule is enabled.
# Stop here if provisioning fails. Do not reuse an existing app name.
node provision.mjs "$scratch/app" >"$scratch/app-receipt.json"
After that command succeeds, load the verified slug and origin from the receipt:
APP=$(jq -er '.slug' "$scratch/app-receipt.json")
ORIGIN=$(jq -er '.origin' "$scratch/app-receipt.json")
gregale --json release traffic status "$APP"
Record the 100%-traffic deployment ID. The public JWKS must be readable without credentials; never open a real app to run the example.
Require a JWT for one route
Add the rule only to GET /api/report. The @app matcher covers that route on the platform and custom public hosts; it does not cover internal service calls. Keep the exact JWKS path reachable without a token, or the gateway cannot read the public key.
gregale edge rule create --app "$APP" --kind jwt \
--match-host @app --match-path /api/report --match-method GET \
--jwt-issuer "$ORIGIN" --jwt-jwks-url "$ORIGIN/.well-known/jwks.json" \
--jwt-algorithm RS256 --jwt-audience gregale-report-demo --jwt-require-exp
The example uses the app as its own deterministic demo key host, not as a real identity provider. In production, get the issuer and key URL from your identity provider's official configuration. Take care with trailing slashes: the rule issuer must match the token's exact issuer.
Verify the rule with real HTTP requests
node demo.mjs check "$ORIGIN" "$scratch/app" "$scratch/keys"
gregale --json release traffic status "$APP"
The verifier checks that the public key on the test host matches the local fixture before sending any tokens. It refuses redirects and prints only status codes. The expected outcomes are:
| Request | Expected result |
|---|---|
| Correct signature, issuer, audience and lifetime | 200, exact synthetic report JSON |
| Expired token | 401 from the edge |
| Token for another audience | 401 from the edge |
| No bearer token | 401 from the edge |
Check that the serving deployment ID is unchanged. This is a live check of the result; a rule trace can show which policy matches, but does not fetch JWKS or verify a token.
If the valid token returns 503, check that the gateway can read the HTTPS JWKS and that the key's kid and algorithm match. If all requests return 401, including the valid one, confirm the fixture's app-wide authentication isn't intercepting the request, then verify the exact issuer and audience. Don't relax validation on a real app to make a test pass.
When done, delete only the newly created disposable app, confirm it is gone, and remove the local signing key. No production identity provider, credentials or customer data are needed.
What we verified on Gregale
On a disposable Gregale app, we fetched the fixture’s public JWKS and got the exact report JSON with a valid token. The expired token, wrong-audience token, and missing token each got an edge 401. The test used only throwaway signing keys and the disposable app was removed.
Questions
- Does edge JWT verification replace backend authorization?
No. The edge verifies the token. Keep tenant, record and action authorization in your application.