Skip to content

Commit 5cb6ed3

Browse files
rustyconoverclaude
andcommitted
Orchard hosted mode (VGI_ORCHARD_HOSTED=1)
A hosted Field Worker accepts only Orchard Passes (RFC 9068 at+jwt, ES256) for one Desk Address, verified locally against the Front Desk JWKS, and fails closed everywhere: - Startup refuses unless VGI_JWT_ISSUER, VGI_JWT_AUDIENCE and VGI_JWT_JWKS_URI are each set to exactly one non-empty value (no comma splitting), and refuses VGI_BEARER_TOKENS, grant keys (env or --grant-key), resolve_token/mint_grant overrides, VGI_OAUTH_RESOURCE, proxy proof, Iroh/peer identity, a custom authenticate callback, --tcp/--unix/ --iroh-raw-upstream, and unrecognised VGI_ORCHARD_HOSTED values. - Verification: ES256 (VGI_JWT_ALGORITHMS overrides; none/HS* refused), typ at+jwt, exact iss/aud, exp required, 60 s skew, JWKS max age 600 s (fails closed, 503), unknown-kid refetch at most every 60 s. - Every request except HEAD /health and CORS preflights needs a Pass; otherwise 401 with WWW-Authenticate: Bearer error="invalid_token", the VGI capability headers and no resource_metadata. Auth runs before the body is size-checked or decompressed. CORS is *. - Claims reach function code via AuthContext with their JSON types (domain "orchard", principal = sub). Also: VGI_JWT_ALGORITHMS is honoured by ordinary JWT auth when set. Tests run a real vgi-serve hosted worker against a local ES256 JWKS: valid Pass and claim types, every rejection case, no Authorization in --debug logs, and each startup refusal. Documented in docs/authentication.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fe93619 commit 5cb6ed3

6 files changed

Lines changed: 1078 additions & 2 deletions

File tree

‎docs/authentication.md‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ the principal in the `AuthContext` available to functions.
2727
| `VGI_JWT_ISSUER` | JWT issuer URL (requires `vgi-python[oauth]` extra) |
2828
| `VGI_JWT_AUDIENCE` | JWT audience string, comma-separated for multiple audiences (required when `VGI_JWT_ISSUER` is set) |
2929
| `VGI_JWT_JWKS_URI` | JWKS endpoint URL (auto-discovered from issuer if omitted) |
30+
| `VGI_JWT_ALGORITHMS` | Comma-separated allow-list of JWS algorithms (e.g. `ES256`). `none` and `HS*` are refused. Unset keeps joserfc's recommended set (`ES256` only in hosted mode) |
31+
| `VGI_ORCHARD_HOSTED` | `1` turns on [Orchard hosted mode](#orchard-hosted-mode) |
3032
| `VGI_OAUTH_RESOURCE` | OAuth resource URL for RFC 9728 metadata |
3133
| `VGI_OAUTH_AUTH_SERVERS` | Comma-separated authorization server URLs |
3234
| `VGI_OAUTH_SCOPES` | Comma-separated supported scopes (optional) |
@@ -81,6 +83,94 @@ minting answers only for the caller. Grants and `resolve_token` bearers are
8183
HTTP-only, like the rest of Identity. When `VGI_PROXY_PROOF_MODE` is on, both
8284
are accepted only *inside* the proxy-proof gate, never as an alternative to it.
8385

86+
## Orchard hosted mode
87+
88+
`VGI_ORCHARD_HOSTED=1` runs the worker as an Orchard *Field Worker*. The
89+
hosted base image sets it; you do not set it per deployment. Callers present a
90+
*Pass*: a short-lived [RFC 9068](https://www.rfc-editor.org/rfc/rfc9068) JWT
91+
access token issued by the Orchard Front Desk for one canonical Desk Address.
92+
The worker verifies every Pass locally and accepts no other credential. The
93+
full design is in the extension repository's `docs/location-delegation.md`.
94+
95+
```bash
96+
VGI_ORCHARD_HOSTED=1 \
97+
VGI_JWT_ISSUER=https://auth.queryfarm.cloud \
98+
VGI_JWT_AUDIENCE=https://desk.queryfarm.cloud/d/dep_123 \
99+
VGI_JWT_JWKS_URI=https://auth.queryfarm.cloud/.well-known/jwks.json \
100+
vgi-serve my_app:MyWorker --http
101+
```
102+
103+
**Startup fails closed.** The worker exits with an error and a non-zero status
104+
unless all three of `VGI_JWT_ISSUER`, `VGI_JWT_AUDIENCE` and
105+
`VGI_JWT_JWKS_URI` are set. Unset and empty are both errors. Each takes
106+
exactly one value: there is no comma splitting, and a value containing a comma
107+
is refused. It also refuses to start when any other way in is configured:
108+
109+
- `VGI_BEARER_TOKENS`;
110+
- sealed-grant keys (`VGI_RPC_GRANT_KEYS` or `--grant-key`);
111+
- a worker that overrides `resolve_token` or `mint_grant`;
112+
- `VGI_OAUTH_RESOURCE` (a Field Worker never advertises `resource_metadata`);
113+
- `VGI_PROXY_PROOF_MODE` other than `off`, or Iroh / peer identity;
114+
- a custom `authenticate` callback passed to `create_app`;
115+
- `--tcp`, `--unix` or `--iroh-raw-upstream`, which carry no Pass;
116+
- a `VGI_ORCHARD_HOSTED` value other than `1`/`0`/`true`/`false`/`yes`/`no`/`on`/`off`.
117+
118+
**Verification:**
119+
120+
- algorithm `ES256` only (`VGI_JWT_ALGORITHMS` overrides; `none` and `HS*`
121+
are always refused);
122+
- JOSE header `typ` must be `at+jwt` (case-insensitive, `application/` prefix
123+
optional);
124+
- `iss` and `aud` must match exactly;
125+
- `exp` is required, and `exp` / `nbf` are checked with 60 s of clock skew;
126+
- the JWKS is cached for at most 10 minutes, or less if its
127+
`Cache-Control: max-age` is shorter. When that time is up the keys are
128+
discarded. If the re-fetch fails, requests get a 503 until it succeeds;
129+
stale keys are never used;
130+
- a Pass with an unknown `kid` triggers a JWKS re-fetch at most once every 60
131+
seconds. Between re-fetches it is simply rejected.
132+
133+
**Every request needs a Pass**, except `HEAD /health` (the Front Desk's
134+
liveness probe) and CORS preflights, which browsers send without credentials.
135+
That includes `GET /health`, `OPTIONS /health`, the landing and describe pages
136+
and unknown paths. A request without a valid Pass, including one with no
137+
`Authorization` header at all, gets:
138+
139+
```text
140+
HTTP/1.1 401 Unauthorized
141+
WWW-Authenticate: Bearer error="invalid_token"
142+
VGI-Auth-Reason: missing_credential | invalid_credential | expired_credential
143+
VGI-Supported-Encodings: ... (and the other VGI capability headers)
144+
```
145+
146+
There is no `resource_metadata` in the challenge. If the JWKS cannot be
147+
fetched, the answer is 503 with `Retry-After` instead: the worker cannot tell
148+
whether the Pass is valid, so it does not claim that it isn't. Authentication
149+
runs before the request body is read, size-checked or decompressed. CORS is
150+
open (`Access-Control-Allow-Origin: *`), because the only credential is the
151+
bearer Pass. The `Authorization` header is never logged, including under
152+
`--debug`.
153+
154+
**Function code sees the Pass's claims** through the usual
155+
[`Auth()`](#scalar-functions-auth-annotation) / `ProcessParams.auth` path, with
156+
their JSON types (`email_verified` is a `bool`, `exp` an `int`).
157+
`AuthContext.principal` is `sub` and `AuthContext.domain` is `"orchard"`.
158+
159+
| Claim | Meaning |
160+
|-------|---------|
161+
| `sub` | Users: the WorkOS user id. Workloads: an opaque Registry id |
162+
| `kind` | `user` or `workload` |
163+
| `email`, `email_verified` | Users only |
164+
| `name` | The user's name, or the name an admin gave the Workload |
165+
| `org_id`, `org_name` | The workspace that owns the deployment |
166+
| `federated_iss`, `federated_sub` | Workloads signed in through a Trust Rule only |
167+
| `client_id`, `iat`, `nbf`, `exp`, `jti` | Standard token claims |
168+
169+
A valid Pass means "a member, or a Workload, of the workspace that owns this
170+
deployment". Anything finer is the worker's own decision. Code that authorizes
171+
by `sub` or `name` must also check `kind`, because a Workload's id and name
172+
could look like a user's.
173+
84174
## Programmatic API
85175

86176
```python test="skip"

0 commit comments

Comments
 (0)