Put the gateway behind your proxy
Keep the proxy or API gateway you already run, have it ask the gateway before it forwards each request, and pass on what the gateway says.
APIRFC 9449, RFC 8693
Your APIs already sit behind a proxy you run, with its routes, its TLS and its rate limits, and you don’t want a
second proxy in the path. Keep it. Many proxies can ask an outside service whether to let a request through before
they forward it: point that check at the gateway’s ext_authz address, and your proxy gets the same verdict the
gateway would give inline, from the same policies, with the verified caller named. This guide is for the team that
runs the proxy.
Before you start:
- The gateway’s client is active in the realm, and a policy allows something, as in
Get started with Gateway. That takes a realm administrator in a group holding
RealmOwner, with the API. - Your proxy has an HTTP external-authorization check that can send the request’s method, path and chosen headers to another service, wait for its answer, and copy headers from that answer onto the request it forwards and onto the answer it gives the client.
- Your API’s audience is
https://api.example.com, and your proxy reaches the gateway athttp://gateway.internal:8080.
Run the gateway for checks
Section titled “Run the gateway for checks”-
Start the gateway where only your proxy reaches it, with the settings in Gateway configuration.
JWT_AUDIENCEis your API’s audience. The gateway doesn’t forward checks, soBACKENDdoesn’t matter here.Terminal window docker run -d --name gateway -p 8080:8080 \-v "$PWD/gateway-key.pem:/etc/gateway/key.pem:ro" \-e ISSUER=https://foobar.cvyuh.com/oauth2 \-e JWT_AUDIENCE=https://api.example.com \-e CLIENT_ID=floor \-e CLIENT_KEY=/etc/gateway/key.pem \-e POLICY_SET=default \"$GATEWAY_IMAGE" -
If clients call your API at an origin other than its audience, set
PUBLIC_ORIGINto that origin: a bound token’s proof names the address the client called.
Have your proxy ask first
Section titled “Have your proxy ask first”-
Send the check. For each request, your proxy calls the gateway with the request’s own method, at
/ext_authzfollowed by the request’s path, and these headers:Header What it carries AuthorizationThe client’s token, as it came DPoPThe client’s proof, when it sent one x-forwarded-hostThe host the client called. Required: a check without it is refused 400x-forwarded-client-certThe client’s certificate, for certificate-bound tokens, with the forwarded-certificate setting on POST /ext_authz/orders/42 HTTP/1.1Host: gateway.internal:8080x-forwarded-host: api.example.comAuthorization: DPoP eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCJ9…DPoP: eyJ0eXAiOiJkcG9wK2p3dCIsImFsZyI6IkVTMjU2IiwiandrIjp7…The request is decided on the host you send and the path, so write policies’
resourcesas*://*:*/orders/*, as the examples do. -
For an MCP server, also send the request’s body, up to 1 MiB, on the paths named in
MCP_PATHS: the gateway decides the call it makes. If your proxy can’t send the body, see MCP calls without the body. -
Give the check 5 seconds, and refuse on anything else. A check the realm has to decide can take up to 3 seconds. If the check times out or fails, your proxy refuses the request: a check that isn’t answered is never an allow.
Pass on what the gateway says
Section titled “Pass on what the gateway says”-
On
200, forward, naming the caller. Remove anyx-agent-idthe client sent, and copy the check’sx-agent-idonto the request to your API: it’s the verified caller. -
Copy
DPoP-Nonceback to the client. When you require nonces, an allowed check carries the client’s next nonce. Copy it from the check onto your answer to the client. A proxy that copies it only onto the request to your API leaves the client to meet401 use_dpop_nonceeach time a nonce ages out. -
On anything else, answer with the check’s answer. Give the client the check’s status, its
WWW-AuthenticateandDPoP-Nonceheaders and its body, so the client learns where to get a token, which scopes would pass, or that it must sign in again: see Requests and refusals.
Swap tokens at the realm
Section titled “Swap tokens at the realm”Your API should receive a token meant for it alone, never the caller’s. The gateway swaps only for requests it forwards itself, so behind your proxy, the proxy swaps:
-
Give your proxy a client of its own in the realm, confidential, marked
exchangesAudience, with the token exchange grant, your API’s audience inservesResourcesand each API it calls inallowedResources: the calls are on Token swap, with your proxy’s client in place offloor. -
After an allowed check, exchange the caller’s token at
https://foobar.cvyuh.com/oauth2/token, naming the API you’re forwarding to asresource, and send the new token to your API in place of the caller’s. -
Bound callers need a proof of your own. When the caller’s token is bound to a key, the realm swaps it only under your proxy’s own DPoP proof, and the new token is bound to your proxy’s key. A proxy that can’t make DPoP proofs can swap only unbound callers: refuse a bound caller rather than send its token on.
MCP calls without the body
Section titled “MCP calls without the body”If your proxy can’t send the body with the check, turn on the gateway’s headers-only setting (see
Gateway configuration). The gateway then decides a bodiless check on the MCP
headers alone, for tools/call, resources/read, prompts/get, the listings and the calls that pass on a token
alone. Turn it on only when your proxy itself refuses a request whose Mcp-Method and Mcp-Name disagree with its
body: otherwise a call could be decided as one tool and run as another.
Check it worked
Section titled “Check it worked”| You send through your proxy | You get |
|---|---|
GET /orders/42 with no token | 401, with the gateway’s Bearer and DPoP challenges |
GET /orders/42 with a token a policy allows | Your API’s answer. Your API sees x-agent-id set to the caller |
DELETE /orders/42 that no policy allows | 403, reason no_matching_policy, and your API never sees it |
The gateway’s log shows a warning line for the refusal: see Logs and tracing.
If it doesn’t work
Section titled “If it doesn’t work”| Symptom | Cause | Fix |
|---|---|---|
Every check is 400 | No x-forwarded-host | Send the request’s host in it |
Every check is 403 no_matching_policy | Your proxy sends the gateway’s own host, or a path with your proxy’s route prefix | Send the host the client called, and the path your API serves |
An MCP check is refused 400 though the call is fine | The check carries no body, and the headers-only setting is off | Send the body, or turn the setting on as above |
Every bound token is 401 invalid_dpop_proof | The proof names the address the client called, which isn’t the audience’s origin | Set PUBLIC_ORIGIN on the gateway |
More symptoms are in Troubleshooting.
Undo it
Section titled “Undo it”Turn off your proxy’s external-authorization check, and stop the gateway (docker rm -f gateway). Your proxy forwards
as it did before.
Related
Section titled “Related”- Run the gateway: the checks behind each verdict.
- Token swap: the exchange your proxy makes.
- Bound tokens at the gateway: proofs, nonces and certificates.
- MCP tool calls: what an MCP check decides.