# Requests and refusals

> What a caller sees when the gateway refuses it, how a client finds where to sign in, and what your API receives when a request is allowed.

With the [gateway](/gateway/) in front of your API, a refused request never reaches it, and the refusal
tells the caller what would help: where to get a token, which scopes would pass, or that a stronger sign-in is needed.
A client that has never met your API can find its way from the first `401` to a working token without anything
configured by hand. An allowed request reaches your API with the verified caller named in a header.

## Before you begin

- **The gateway running** in front of your API: see [Run the gateway](/gateway/protect-apis/run-the-gateway/). Its
  image isn't published yet: ask your Cvyūh contact for it.
- Nothing to set in the realm. On the gateway, `SCOPES` and `DPOP_REQUIRED` change what a refusal offers: see
  [Gateway configuration](/gateway/configuration/#tokens-and-callers).

## How it works

### What callers see

A client calls your API through the gateway with its token:

```http
GET /orders/42 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCJ9.eyJzdWIiOiI…
```

A request with no token is refused, and the answer offers both ways to present one, with the address of the API's
metadata:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
WWW-Authenticate: DPoP algs="ES256 PS256 RS256", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
```

A token the policies don't allow is `403`, with an `insufficient_scope` challenge naming the scopes that would pass
where a policy says so. A request a stronger or more recent sign-in would pass is
`401 insufficient_user_authentication`, with the `acr_values` or `max_age` needed, so the app can ask the user to step up: see
[Step-up and re-authentication](/workforce-identity/applications/single-sign-on/step-up/).

### Resource metadata

The metadata address in every challenge is public, and names the realm that protects the API:

```json
{
  "resource": "https://api.example.com",
  "authorization_servers": ["https://foobar.cvyuh.com/oauth2"],
  "bearer_methods_supported": ["header"],
  "dpop_bound_access_tokens_required": false,
  "dpop_signing_alg_values_supported": ["ES256", "PS256", "RS256"],
  "scopes_supported": ["orders:read"]
}
```

`resource` is your API's audience, and `scopes_supported` appears only when you give the gateway the scopes to name
(`SCOPES`, in [Gateway configuration](/gateway/configuration/)). When the
audience has a path, such as `https://gw.example.com/mcp`, the metadata is at
`/.well-known/oauth-protected-resource/mcp`.

### What your API sees

The request, at your API's own address, with `x-agent-id` set to the token's `sub`: the user, agent or client the
token is about. A client can't set that header itself; any it sends is removed. With
[token swap](/gateway/protect-apis/token-swap/) on, the request also carries a fresh token meant for your API in place of the
caller's.

## Good to know

- **Paths are decided as your API reads them.** A path your API might read differently is refused
  (`400 path_not_canonical`), so an escaped letter can never reach a path a policy denies.
- **The host is your API's.** A request is decided on the host and port it will be forwarded to, never the one the
  client named.

## Limits and defaults

| What | Default | Can it change? |
|---|---|---|
| The verified caller's header | `x-agent-id` | No |
| Challenges on a missing token | `Bearer` and `DPoP` | `DPoP` alone with `DPOP_REQUIRED` |
| Scopes the metadata names | None | `SCOPES` |
| Proof algorithms offered | `ES256`, `PS256`, `RS256` | No |

## Reference

### Errors

| Status and reason | What it means |
|---|---|
| `400 path_not_canonical` | The path has dot segments, empty segments, a backslash or an encoding your API might decode differently |
| `401 missing_token` | No token. Both `Bearer` and `DPoP` are offered (`DPoP` alone with `DPOP_REQUIRED`) |
| `401 invalid_token` | The token doesn't verify, isn't for this API, or isn't presented as its binding requires |
| `401 invalid_dpop_proof` | A bound token's proof is missing or doesn't hold: see [Bound tokens at the gateway](/gateway/protect-apis/bound-tokens/) |
| `401 use_dpop_nonce` | The proof needs a fresh nonce, given in `DPoP-Nonce` |
| `401 token_revoked` | The token, a token it was exchanged from, its family, its session or its client grant was revoked, or it was issued before its subject or an actor was revoked |
| `401 agent_revoked` | Its agent, or an agent acting in it, was stopped, or the user took that agent back |
| `401 insufficient_user_authentication` | A stronger or more recent sign-in would pass, with the `acr_values` or `max_age` needed |
| `403 no_policies_loaded` | The gateway holds no policies for this request |
| `403 policies_excluded` | A policy in the set is incomplete, so the realm left it out, and the set allows nothing until it's fixed |
| `403 policy_denied`, `403 no_matching_policy` | A policy refuses the request, or none allows it |
| `403 upstream_unavailable` | The realm had to decide and couldn't be asked |
| `403 exchange_refused` | With `UPSTREAM_AUDIENCE`, the realm refused to swap the caller's token. A `503` means it couldn't be asked |
| `503 revocations_stale` | The revocation list hasn't been refreshed for three intervals: see [Fails closed](/gateway/protect-apis/run-the-gateway/#fails-closed) |
| `503 dpop_proofs_full` | The gateway is holding too many recent proofs. Try again shortly |
| `502`, `503`, `504` | Allowed, but your API failed, refused the connection, or didn't answer within 60 seconds |

On an MCP path ([MCP tool calls](/gateway/mcp-servers/mcp-tool-calls/)), a `403` carries its reason as a JSON-RPC error, with the same status and
challenge.

## Audit record

The gateway's refusals aren't on the audit trail yet. Each is a warning line in the gateway's own log, with the path,
the caller when the token verified, and the reason: see [Logs and tracing](/gateway/configuration/#logs-and-tracing).
A decision the gateway asks the realm for is on the trail as `POLICY_DECIDED`.

## Troubleshooting

A refusal you didn't expect is in [Troubleshooting](/gateway/protect-apis/run-the-gateway/#troubleshooting), by its
status and reason.

## Related

- [Run the gateway](/gateway/protect-apis/run-the-gateway/): how each request is checked, and its settings.
- [Step-up and re-authentication](/workforce-identity/applications/single-sign-on/step-up/): asking the user for a stronger sign-in.
- [Get started with Gateway](/gateway/get-started/): an allowed and a refused call, end to end.
