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

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.

<Aside type="caution" title="The gateway's image isn't published yet">
  Ask your Cvyūh contact for the gateway's container image before you start.
</Aside>

**Before you start:**

- The gateway's client is active in the realm, and a policy allows something, as in
  [Get started with Gateway](/gateway/get-started/). 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`.

## Run the gateway for checks

<Steps>

1. **Start the gateway where only your proxy reaches it**, with the settings in
   [Gateway configuration](/gateway/configuration/). `JWT_AUDIENCE` is your API's audience. The gateway doesn't
   forward checks, so `BACKEND` doesn't matter here.

   ```sh
   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.

</Steps>

## Have your proxy ask first

<Steps>

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:

   | Header | What it carries |
   |---|---|
   | `Authorization` | The client's token, as it came |
   | `DPoP` | The client's proof, when it sent one |
   | `x-forwarded-host` | The host the client called. Required: a check without it is refused `400` |
   | `x-forwarded-client-cert` | The client's certificate, for certificate-bound tokens, with the forwarded-certificate setting on |

   ```http
   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](#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.

</Steps>

## Pass on what the gateway says

<Steps>

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](/gateway/protect-apis/requests-and-refusals/).

</Steps>

## 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:

<Steps>

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](/gateway/protect-apis/token-swap/#set-it-up), 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.

</Steps>

## 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](/gateway/configuration/#mcp-servers)). 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

| 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](/gateway/configuration/#logs-and-tracing).

## 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](/gateway/protect-apis/run-the-gateway/#troubleshooting).

## 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

- [Run the gateway](/gateway/protect-apis/run-the-gateway/): the checks behind each verdict.
- [Token swap](/gateway/protect-apis/token-swap/): the exchange your proxy makes.
- [Bound tokens at the gateway](/gateway/protect-apis/bound-tokens/): proofs, nonces and certificates.
- [MCP tool calls](/gateway/mcp-servers/mcp-tool-calls/): what an MCP check decides.
