Skip to content
GatewayMarkdown

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 at http://gateway.internal:8080.
  1. Start the gateway where only your proxy reaches it, with the settings in Gateway configuration. JWT_AUDIENCE is your API’s audience. The gateway doesn’t forward checks, so BACKEND doesn’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"
  2. If clients call your API at an origin other than its audience, set PUBLIC_ORIGIN to that origin: a bound token’s proof names the address the client called.

  1. Send the check. For each request, your proxy calls the gateway with the request’s own method, at /ext_authz followed by the request’s path, and these headers:

    HeaderWhat 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 400
    x-forwarded-client-certThe client’s certificate, for certificate-bound tokens, with the forwarded-certificate setting on
    POST /ext_authz/orders/42 HTTP/1.1
    Host: gateway.internal:8080
    x-forwarded-host: api.example.com
    Authorization: DPoP eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCJ9…
    DPoP: eyJ0eXAiOiJkcG9wK2p3dCIsImFsZyI6IkVTMjU2IiwiandrIjp7…

    The request is decided on the host you send and the path, so write policies’ resources as *://*:*/orders/*, as the examples do.

  2. 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.

  3. 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.

  1. On 200, forward, naming the caller. Remove any x-agent-id the client sent, and copy the check’s x-agent-id onto the request to your API: it’s the verified caller.

  2. Copy DPoP-Nonce back 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 meet 401 use_dpop_nonce each time a nonce ages out.

  3. On anything else, answer with the check’s answer. Give the client the check’s status, its WWW-Authenticate and DPoP-Nonce headers 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.

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:

  1. Give your proxy a client of its own in the realm, confidential, marked exchangesAudience, with the token exchange grant, your API’s audience in servesResources and each API it calls in allowedResources: the calls are on Token swap, with your proxy’s client in place of floor.

  2. After an allowed check, exchange the caller’s token at https://foobar.cvyuh.com/oauth2/token, naming the API you’re forwarding to as resource, and send the new token to your API in place of the caller’s.

  3. 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.

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.

You send through your proxyYou get
GET /orders/42 with no token401, with the gateway’s Bearer and DPoP challenges
GET /orders/42 with a token a policy allowsYour API’s answer. Your API sees x-agent-id set to the caller
DELETE /orders/42 that no policy allows403, 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.

SymptomCauseFix
Every check is 400No x-forwarded-hostSend the request’s host in it
Every check is 403 no_matching_policyYour proxy sends the gateway’s own host, or a path with your proxy’s route prefixSend the host the client called, and the path your API serves
An MCP check is refused 400 though the call is fineThe check carries no body, and the headers-only setting is offSend the body, or turn the setting on as above
Every bound token is 401 invalid_dpop_proofThe proof names the address the client called, which isn’t the audience’s originSet PUBLIC_ORIGIN on the gateway

More symptoms are in Troubleshooting.

Turn off your proxy’s external-authorization check, and stop the gateway (docker rm -f gateway). Your proxy forwards as it did before.