# RGE-1: Registry-Gated Entitlements

A draft design for a licensor-side gate that enforces content-access licences
against anchored [ADP-1](../adp-1/SPEC.md) declarations. The licence window is
written into the anchored grant. Access tokens are issued only while a live
grant exists, and no token outlives the window.

**Status: Draft v0.1.** Not normative, not a standard, not deployed, and not
independently reviewed. Every organisation, domain and licence identifier in
this directory is a placeholder, and every key is derived from a fixed test
seed.

- **[`SPEC.md`](./SPEC.md)**: the specification.
- **[`gate.mjs`](./gate.mjs)**: the reference gate. Node.js, no dependencies.
- **[`test-vectors.json`](./test-vectors.json)**: signed and anchored grant
  bundles, 20 issuance scenarios with the outcome each must produce, the exact
  token bytes for every issuance, 11 token-validation cases, and a hash-chained
  issuance log.
- **[`gen-vectors.mjs`](./gen-vectors.mjs)**: the deterministic generator for
  the vectors. It does not import the gate: expected outcomes are written by
  hand from the specification, and expected tokens and log rows are built by
  separate code.
- **[`gate.test.mjs`](./gate.test.mjs)**: 68 tests. They run the vectors,
  check that no token outlives its window across thousands of instants, build
  forgeries that each isolate one verification check, and drive the demo
  server end to end.

## Run it

Tested with Node.js 25. It uses only the standard library.

```sh
node gate.mjs --vectors test-vectors.json           # every vector, PASS/FAIL per line
node gate.mjs --vectors test-vectors.json --tamper  # forged grants must be rejected
node --test                                         # the full test suite
node gen-vectors.mjs --check                        # vectors are byte-identical to the generator's output
```

### Demo server

```sh
node gate.mjs serve --vectors test-vectors.json --now 2026-06-01T00:00:00Z

curl -s -X POST http://127.0.0.1:8787/v1/token \
  -H 'authorization: Bearer rge-demo-secret-not-for-production' \
  -d '{"scopes":["retrieve:news"]}'
curl -s http://127.0.0.1:8787/v1/log
```

The demo server binds to 127.0.0.1, uses the test keys from the vectors, and
identifies its one client by a static secret that is printed in the vectors. It
exists to show the request and response shapes. Do not expose it.

## What the reference gate covers

| RGE-1 section | Covered | How |
|---|---|---|
| §4 grant profile | Yes | Grants are read from verified ADP-1 declarations. A `permitted` reservation without a usable `valid_until` is refused. |
| §5 state resolution | Yes | `resolveState`: supersession, most-specific actor, LIVE / EXPIRED / REVOKED / RESERVED. |
| §6.1 client binding | No | The caller supplies the actor. The demo server stands in a static secret for mTLS or OAuth2. |
| §6.2 issuance | Yes | `Gate.issue`: the TTL rule, scope check, Ed25519 tokens naming the authorizing declaration. |
| §6.4 dunning notices | No | |
| §6.5 offline and fail-closed | Partly | Expiry is enforced from local bytes, and issuance fails closed past `max_staleness`. The gate does not fetch from a registry or from Arweave; the caller loads bundles and reports a successful re-check with `markFresh`. |
| §8.1 issuance log | Partly | Hash-chained rows for every decision, and `verifyLog`. The log is held in memory and its head is not anchored anywhere. |

## What it does not do

- It does not check DNS for the `dns-01` authority proof. That check is
  structural unless the caller passes an `authorityCheck`.
- It does not authenticate clients, rate-limit, or enforce `max_qps`.
- It does not persist anything. Two consequences if you build on it:
  - Every decision is logged, denials included, and the log is in memory.
    Unauthenticated requests can grow it without limit. Rate-limit before
    logging and persist the log.
  - A restarted gate has no memory of revocations. Feed it an old set of
    bundles without the revocation and the grant is live again. Keep a durable
    record of superseded declarations (SPEC §6.5).
- `markFresh` records the caller's word that a re-check succeeded. Call it only
  when every bundle the re-check returned was accepted.
- A token's `jti` is derived from the log position, so it repeats across
  restarts and across replicas. Do not use it for replay detection as it is.
- It has one author. An automated security review was run against it and its
  findings were fixed or written into this list and into SPEC §9; no person
  outside the project has reviewed it. Treat a passing test run as evidence
  that the gate agrees with the vectors, and nothing more.

## Licence

No licence has been set for this draft or its code. All rights reserved until
one is.
