# Decide locally

> Pull the realm's policies and its revocation feed into your own enforcement point, and decide every request in process with no call to the realm.

An enforcement point that decides locally keeps its own copy of the realm's policies and of everything the realm has
revoked, polls both, and decides each request in process. No request waits on the realm, and a short outage of the
realm doesn't stop your API. The price is freshness: a decision is as current as the last poll. This is how the
[gateway](/gateway/) works, and how you'd build your own.

Use it when a call to the realm per request is too slow or too fragile, and your policies decide from the request and
its token alone. For a policy that needs the caller's groups, which no token carries, [ask the realm](/gateway/decisions/ask-the-realm/).
The export and the revocation feed are available today, with or without the gateway.

## Before you begin

- **A client for your enforcement point**, with the scopes `policies:read` and `revocations:read`: registering it
  takes a realm administrator in a group holding `RealmOwner`, with the API.
- **A decision engine of your own** that decides by the rules on [Policies](/workforce-identity/authorization/policies/),
  or the gateway, which does.

## How it works

The enforcement point pulls two things from the realm, each with its own scope:

| Address | What it answers | Scope |
|---|---|---|
| `GET /policies/v1/export?policySet=<set>` | The set's active policies, in the shape the decision engine reads | `policies:read` |
| `GET /policies/v1/revocations` | Everything revoked that hasn't yet expired | `revocations:read` |

**The export** is `{ "result": [...], "resultCount": n, "comparators": {...}, "acrValues": {...} }`: the policies,
each set's comparator, and each `acr` value with the sign-in chain that earns it, so the enforcement point can ask for
a step-up (see [Step-up and re-authentication](/workforce-identity/applications/single-sign-on/step-up/)). The header `X-Policies-Excluded` counts the policies the realm
left out of the set. `policySet` may repeat; with none, every set is returned.

**The revocation feed** lists revoked tokens, refresh families, sessions, principals, client grants and
delegations: see [Revocation feed](/gateway/revocation/revocation-feed/).

Both answer an entity tag. Send `If-None-Match`, and an unchanged answer is a `304`.

## Set it up

1. **Register your enforcement point** as a client of its own, here `orders-pep`, that may read both:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=orders-pep" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "active",
           "tokenEndpointAuthMethod": "client_secret_basic",
           "clientSecret": "<a long random secret>",
           "grantTypes": ["client_credentials"],
           "scopes": ["policies:read", "revocations:read"],
           "defaultScopes": ["policies:read", "revocations:read"],
           "allowedResources": ["https://foobar.cvyuh.com/policies/v1"],
           "defaultAudience": "https://foobar.cvyuh.com/policies/v1" }'
   ```

   [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`. For a key in place of a secret, as the gateway
   uses, see [Choose how a client proves itself](/workforce-identity/guides/services/choose-client-authentication/).

2. **Get its token, and pull.** The token is good for five minutes; get a new one as it runs out:

   ```sh
   TOKEN=$(curl -s https://foobar.cvyuh.com/oauth2/token -u "orders-pep:<the secret>" \
     -d grant_type=client_credentials | jq -r .access_token)

   curl -i "https://foobar.cvyuh.com/policies/v1/export?policySet=default" -H "Authorization: Bearer $TOKEN"
   curl -i https://foobar.cvyuh.com/policies/v1/revocations -H "Authorization: Bearer $TOKEN"
   ```

   Keep each answer's `ETag`, and send it back as `If-None-Match` on the next poll.

3. **Choose how often it polls each.** Poll the revocation feed most often: a stale policy is only wrong if the policy
   changed, but a stale revocation list lets through a token known to be bad. The gateway polls policies every minute
   and revocations every 10 seconds.

4. **Decide every request** by the rules below.

## Good to know

Your enforcement point must decide by the rules on [Policies](/workforce-identity/authorization/policies/), as
the realm does. The checks around the decision are its own:

- **Verify every token yourself** with the realm's keys: its signature, its issuer, your audience, its type
  (`at+jwt`) and its expiry. Then check it against every list in the revocation feed.
- **Refuse when a set is empty or missing.** Nothing decided is nothing allowed.
- **Apply `X-Policies-Excluded`.** With a policy left out, an allow isn't an allow (`policies_excluded`).
- **Use the set's comparator** from `comparators`. A set missing from it, or a comparator you don't know, means
  refuse.
- **Treat groups as unknown.** A token carries none. A policy that needs them stays undecided: refuse, or ask the
  realm. The gateway asks.
- **Decide the resource as the API will read it.** Decide on the host and port you forward to, not the one the client
  named. Refuse a path the API might read differently: dot segments, empty segments, encoded letters or slashes.
- **Keep the last good copy** when a poll fails, and stop trusting the revocation feed when polls have failed for long
  enough.
- **Log your own decisions.** Decisions made locally aren't on the realm's audit trail.

## Limits and defaults

| What | Value |
|---|---|
| Policies, policy sets or resource types the export reads | Up to 10,000 of each; past that, the export is an error, never a partial set |
| The token to read the export and the feed | Audience `https://foobar.cvyuh.com/policies/v1`, scopes `policies:read` and `revocations:read` |
| How often the gateway polls | Policies every minute, revocations every 10 seconds |

## What your enforcement point sees when it fails

| Status | Why | What to do |
|---|---|---|
| `401` | The token is missing, expired or for another audience | Get a new token for `https://foobar.cvyuh.com/policies/v1` |
| `403` | The token lacks the scope | Add `policies:read` or `revocations:read` to the client |
| `503`, or no answer | The realm couldn't answer, or holds more policies than one export reads | Keep the last good copy |

## Audit record

Reading the export or the feed writes no record, and decisions made locally aren't on the realm's audit trail: log
them where your enforcement point runs. Changes to policies are recorded as `CREATED`, `PATCHED` and `DELETED`: see
[Change history](/audit-reports/audit-trail/change-history/).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Your export is empty | No active policy is linked to the set you named, or the set doesn't exist | Link the policies, and check `policySet` |
| `X-Policies-Excluded` isn't `0` | A policy in the set is incomplete, such as linked to no resource type | Link it to its type, or delete it |
| Your decisions disagree with the realm's | Your engine doesn't apply the set's comparator, or decides on another host than you forward to | Follow the checks above, one by one |

## Related

- [Revocation feed](/gateway/revocation/revocation-feed/): every list, and how long each entry stays.
- [Ask the realm](/gateway/decisions/ask-the-realm/): for decisions a token can't settle.
- [Run the gateway](/gateway/protect-apis/run-the-gateway/): the realm's own local enforcement point.
