@forgezero/access 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +307 -97
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,115 +1,325 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
GENERATED FILE — do not edit.
|
|
3
|
+
|
|
4
|
+
Change scripts/generate-guides.ts or its typed sources, run `bun run guides`,
|
|
5
|
+
and commit the generator and rendered files together.
|
|
6
|
+
-->
|
|
7
|
+
|
|
1
8
|
# @forgezero/access
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
Declare your security posture as seven lists. Handlers hold business logic only.
|
|
11
|
+
|
|
12
|
+
## Global package root and supported runtimes
|
|
13
|
+
|
|
14
|
+
Anyone building a service on our request shape: one declared matrix of routes and factors, enforced by one pipeline whatever the framework. Imports no sibling at all, so it can be adopted on its own. Supported runtimes: bun, node, workers, deno. The global base/root import is @forgezero/access. Every public import or command is listed below; the documentation inventory is checked in both directions against package.json exports.
|
|
15
|
+
|
|
16
|
+
```text
|
|
17
|
+
import * as root from '@forgezero/access';
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Commands
|
|
21
|
+
|
|
22
|
+
bun add @forgezero/access — Install the framework-neutral access contracts and adapters.
|
|
23
|
+
|
|
24
|
+
```text
|
|
25
|
+
bun add @forgezero/access
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## @forgezero/access
|
|
29
|
+
|
|
30
|
+
Declare routes, factors and policies as orthogonal lists; authorise against them.
|
|
31
|
+
|
|
32
|
+
```text
|
|
33
|
+
import * as api from '@forgezero/access';
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## @forgezero/access/conditions
|
|
37
|
+
|
|
38
|
+
The twelve guards every project writes into `before`, each with the status its refusal deserves.
|
|
39
|
+
|
|
40
|
+
```text
|
|
41
|
+
import * as api from '@forgezero/access/conditions';
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## @forgezero/access/effects
|
|
45
|
+
|
|
46
|
+
Audit, emit, meter, invalidate and notify — what happens after a request is decided, never failing it.
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
import * as api from '@forgezero/access/effects';
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## @forgezero/access/security
|
|
53
|
+
|
|
54
|
+
Constant-time comparison, CSPRNG tokens, HMAC, HKDF, AES-GCM sealing and redaction. Web Crypto only.
|
|
55
|
+
|
|
56
|
+
```text
|
|
57
|
+
import * as api from '@forgezero/access/security';
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## @forgezero/access/rate-limit
|
|
61
|
+
|
|
62
|
+
Request counters over a window — in memory, in Redis, or in a Durable Object.
|
|
63
|
+
|
|
64
|
+
```text
|
|
65
|
+
import * as api from '@forgezero/access/rate-limit';
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## @forgezero/access/fetch
|
|
69
|
+
|
|
70
|
+
A Fetch-native adapter over the declared access pipeline.
|
|
71
|
+
|
|
72
|
+
```text
|
|
73
|
+
import * as api from '@forgezero/access/fetch';
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## @forgezero/access/pipeline
|
|
77
|
+
|
|
78
|
+
The framework-neutral route, principal, factor, policy, rate-limit and effects pipeline.
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
import * as api from '@forgezero/access/pipeline';
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## @forgezero/access/elysia
|
|
85
|
+
|
|
86
|
+
Elysia integration over the same access pipeline and route declarations.
|
|
87
|
+
|
|
88
|
+
```text
|
|
89
|
+
import * as api from '@forgezero/access/elysia';
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## @forgezero/access/client
|
|
93
|
+
|
|
94
|
+
Typed client helpers that fulfil factors without duplicating the security matrix.
|
|
95
|
+
|
|
96
|
+
```text
|
|
97
|
+
import * as api from '@forgezero/access/client';
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## @forgezero/access/testing
|
|
101
|
+
|
|
102
|
+
Deterministic access-pipeline fixtures and assertions for consumer tests.
|
|
103
|
+
|
|
104
|
+
```text
|
|
105
|
+
import * as api from '@forgezero/access/testing';
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## @forgezero/access/header
|
|
109
|
+
|
|
110
|
+
Strict configurable header-identity extraction with canonical names and bounded values.
|
|
111
|
+
|
|
112
|
+
```text
|
|
113
|
+
import * as api from '@forgezero/access/header';
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## @forgezero/access/principal
|
|
117
|
+
|
|
118
|
+
Generic principal-source resolution for browser, API-key, attestation or future identity adapters.
|
|
119
|
+
|
|
120
|
+
```text
|
|
121
|
+
import * as api from '@forgezero/access/principal';
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## @forgezero/access/principal-session
|
|
125
|
+
|
|
126
|
+
Short-lived scope-bound delegated-principal sessions with sliding and absolute expiry.
|
|
127
|
+
|
|
128
|
+
```text
|
|
129
|
+
import * as api from '@forgezero/access/principal-session';
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## @forgezero/access/authenticator
|
|
133
|
+
|
|
134
|
+
Authentication-source contracts for adding identity mechanisms without changing route policy.
|
|
135
|
+
|
|
136
|
+
```text
|
|
137
|
+
import * as api from '@forgezero/access/authenticator';
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## @forgezero/access/ceremony-modes
|
|
141
|
+
|
|
142
|
+
Named security-ceremony modes and their session/action fulfilment semantics.
|
|
143
|
+
|
|
144
|
+
```text
|
|
145
|
+
import * as api from '@forgezero/access/ceremony-modes';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Declare routes before handlers
|
|
149
|
+
|
|
150
|
+
Routes and their access policy are data; Fetch and Elysia adapters enforce the same declaration.
|
|
151
|
+
|
|
152
|
+
```text
|
|
153
|
+
import { defineRoutes, action, page } from '@forgezero/access';
|
|
154
|
+
|
|
155
|
+
export const routes = defineRoutes({
|
|
156
|
+
orders: page('Orders'),
|
|
157
|
+
'api/orders': action('List orders', 'GET', { page: 'orders' })
|
|
158
|
+
});
|
|
159
|
+
```
|
|
4
160
|
|
|
5
|
-
|
|
6
|
-
factors it demands — and the same declaration authorises the request, renders the
|
|
7
|
-
navigation and fails the build when the two disagree.
|
|
161
|
+
## 1. Install
|
|
8
162
|
|
|
9
|
-
Zero runtime dependencies. Bun, Node 18+,
|
|
10
|
-
anywhere `fetch` and Web Crypto exist.
|
|
163
|
+
Zero runtime dependencies. The core is fetch types plus plain JSON Schema, so it runs on Bun, Node 18+, Cloudflare Workers, Deno and every edge runtime.
|
|
11
164
|
|
|
12
|
-
```
|
|
165
|
+
```text
|
|
13
166
|
bun add @forgezero/access
|
|
14
167
|
```
|
|
15
168
|
|
|
16
|
-
##
|
|
169
|
+
## 2. Declare routes and who may reach them
|
|
17
170
|
|
|
18
|
-
|
|
19
|
-
import { defineRoutes, authorise } from '@forgezero/access';
|
|
171
|
+
Routes carry their contract — label, method, and the shapes they accept and return. WHO may reach them is a separate list, because the two change for different reasons and on different schedules.
|
|
20
172
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
173
|
+
```text
|
|
174
|
+
import { defineRoutes, defineAccessControl, page, action } from '@forgezero/access';
|
|
175
|
+
|
|
176
|
+
export const ROUTES = defineRoutes({
|
|
177
|
+
orders: page('Orders'),
|
|
178
|
+
'api/orders': action('List', 'GET', { page: 'orders' }),
|
|
179
|
+
'api/orders/[id]/refund': action('Refund', 'POST', { page: 'orders' })
|
|
24
180
|
});
|
|
25
181
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
## What it gives you that a middleware does not
|
|
34
|
-
|
|
35
|
-
**Two layers, and the second never trusts the first.** A session factor is proved
|
|
36
|
-
once and persists — it answers *who is this*. An action factor is proved per call
|
|
37
|
-
and never persists — it answers *is this a human, now, for this record*. A
|
|
38
|
-
five-minute "recently verified" timestamp looks equivalent and is not: a
|
|
39
|
-
timestamp **is** a persisted factor, so a left-open laptop replays the privileged
|
|
40
|
-
action. `fulfilledActionFactors()` returns `[]` — always, by design.
|
|
41
|
-
|
|
42
|
-
**A refusal that says what happened.** `401` no session · `403` role lacks the
|
|
43
|
-
route · `404` route exists but not in this stage · `409` a condition refused ·
|
|
44
|
-
`423` locked, a human must act · `428` a factor is missing, and the headers name
|
|
45
|
-
which. Every one is distinct because "403" for all of them is how a support
|
|
46
|
-
queue fills up.
|
|
47
|
-
|
|
48
|
-
**Twelve guards already written.** `requireBalance`, `requireApproval`,
|
|
49
|
-
`requireQuota`, `requireFreshness` and the rest — the conditions every project
|
|
50
|
-
writes by hand, each already carrying the right status code.
|
|
51
|
-
|
|
52
|
-
**Testable with no server.** `@forgezero/access/testing` builds sessions and
|
|
53
|
-
asserts the decision directly, so the truth table is a unit test rather than an
|
|
54
|
-
integration suite.
|
|
55
|
-
|
|
56
|
-
## Subpaths
|
|
57
|
-
|
|
58
|
-
| import | what it is |
|
|
59
|
-
|---|---|
|
|
60
|
-
| `@forgezero/access` | the matrix, roles, `authorise`, `Refusal` |
|
|
61
|
-
| `/conditions` | the twelve guards, each with its own status |
|
|
62
|
-
| `/effects` | audit, emit, meter, invalidate, notify — after the decision |
|
|
63
|
-
| `/security` | constant-time compare, CSPRNG tokens, HMAC, HKDF, AES-GCM sealing |
|
|
64
|
-
| `/rate-limit` | counters over a window — memory, Redis, or a Durable Object |
|
|
65
|
-
| `/elysia` · `/fetch` | adapters |
|
|
66
|
-
| `/client` | the browser half, including the 428 replay |
|
|
67
|
-
| `/testing` | decide without a server |
|
|
68
|
-
| `/pipeline` · `/authenticator` | the resolver, and WebAuthn |
|
|
69
|
-
| `/header` | verified header identities with fresh, host-owned RBAC assignments |
|
|
70
|
-
| `/principal` | generic multi-assignment principals, route groups, methods and expiry |
|
|
71
|
-
| `/principal-session` | short-lived server sessions bound to an opaque client identity |
|
|
72
|
-
|
|
73
|
-
Header identities are a separate principal, not a browser session. A source
|
|
74
|
-
verifier authenticates one configured header and returns only a stable subject;
|
|
75
|
-
the host resolves that subject's role assignments on every request. Roles are
|
|
76
|
-
never accepted from the header value, and a header principal cannot satisfy a
|
|
77
|
-
session or fresh-action factor. Multiple source adapters can coexist, but a
|
|
78
|
-
request presenting more than one source is refused as ambiguous.
|
|
79
|
-
|
|
80
|
-
`accessGroup` is the single open, host-defined route classification used by
|
|
81
|
-
generic principal assignments—`public`, `user`, `admin`, `custody`,
|
|
82
|
-
`orchestration`, or any future product vocabulary. Session policies remain the
|
|
83
|
-
generic package's authentication boundary; ForgeZero additionally requires its
|
|
84
|
-
`public` group to use anonymous authentication and rejects contradictions at
|
|
85
|
-
boot. The package does not hard-code a purpose or identity type.
|
|
86
|
-
|
|
87
|
-
```ts
|
|
88
|
-
import { headerIdentityResolver } from '@forgezero/access/header';
|
|
89
|
-
import { decidePrincipalAccess } from '@forgezero/access/principal';
|
|
90
|
-
|
|
91
|
-
const resolveHeader = headerIdentityResolver(
|
|
92
|
-
[{
|
|
93
|
-
key: 'partner-sso',
|
|
94
|
-
header: 'x-partner-identity',
|
|
95
|
-
verify: ({ value, request }) => verifyPartnerAssertion(value, request)
|
|
96
|
-
}],
|
|
97
|
-
// Read the current admin/custodian assignment. Do not cache it in the token.
|
|
98
|
-
({ principalKey }) => memberships.assignments(principalKey)
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
const principal = await resolveHeader(request);
|
|
102
|
-
const decision = decidePrincipalAccess({
|
|
103
|
-
access, principal, roles: await policy.roles(), routeKey: 'api/orders/write', method: 'POST'
|
|
182
|
+
export const ACCESS = defineAccessControl({
|
|
183
|
+
factors: FACTORS,
|
|
184
|
+
routes: ROUTES,
|
|
185
|
+
sessionPolicies: {
|
|
186
|
+
member: { factors: ['passkey'], routes: ['orders', 'api/orders'] },
|
|
187
|
+
admin: { factors: ['passkey'], routes: ['api/orders/[id]/refund'] }
|
|
188
|
+
}
|
|
104
189
|
});
|
|
105
|
-
|
|
106
|
-
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## 3. Mount it
|
|
193
|
+
|
|
194
|
+
One line per runtime. The pipeline is written once and shared, so an adapter never re-decides anything.
|
|
195
|
+
|
|
196
|
+
```text
|
|
197
|
+
// anywhere fetch exists — Workers, Deno, Node, Bun
|
|
198
|
+
import { toFetch } from '@forgezero/access/fetch';
|
|
199
|
+
export default { fetch: toFetch({ access: ACCESS, handlers: HANDLERS }) };
|
|
200
|
+
|
|
201
|
+
// Elysia
|
|
202
|
+
import { elysia } from '@forgezero/access/elysia';
|
|
203
|
+
app.use(elysia({ access: ACCESS, handlers: HANDLERS, Elysia }));
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Every route needs a session policy, or the build fails
|
|
207
|
+
|
|
208
|
+
Exactly one policy must cover each route. A route nobody classified is a route whose security nobody decided — and adding a route is precisely when that happens. This is the rule that catches omission rather than typos, and it is why forgetting is not possible rather than merely discouraged.
|
|
209
|
+
|
|
210
|
+
```text
|
|
211
|
+
SESSION_INCOMPLETE: No session policy covers: api/orders/export.
|
|
212
|
+
Every route needs exactly one.
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Fresh proof for dangerous actions
|
|
216
|
+
|
|
217
|
+
actionFactorsRequired is a COUNT: 1 is any-one, the list length is all-of, and anything between is M-of-N with no special case. The proof binds to the record your before-handler loaded, so a key minted to refund order A cannot refund order B.
|
|
218
|
+
|
|
219
|
+
```text
|
|
220
|
+
actionPolicies: {
|
|
221
|
+
sensitive: {
|
|
222
|
+
factors: ['passkey', 'telegram', 'totp'],
|
|
223
|
+
required: 2, // any 2 of the 3
|
|
224
|
+
target: 'orders',
|
|
225
|
+
routes: ['api/orders/[id]/refund']
|
|
226
|
+
}
|
|
107
227
|
}
|
|
108
228
|
```
|
|
109
229
|
|
|
110
|
-
|
|
230
|
+
## A session factor NEVER satisfies an action factor
|
|
111
231
|
|
|
112
|
-
|
|
232
|
+
fulfilledActionFactors() returns an empty array, always. A five-minute verified-at timestamp looks equivalent and is not: a timestamp IS a persisted factor, so a left-open laptop or a stolen cookie replays the privileged action. Returning nothing makes that impossible by construction rather than by policy.
|
|
233
|
+
|
|
234
|
+
## Disabling a factor tells you what it breaks, first
|
|
235
|
+
|
|
236
|
+
The effective pool is the route factors intersected with the factors you have enabled. If that falls below the required count the route becomes UNAVAILABLE — never quietly weakened to whatever remains. Ask before saving rather than discovering it during an incident.
|
|
237
|
+
|
|
238
|
+
```text
|
|
239
|
+
impactOfDisabling(ACCESS, ['passkey'])
|
|
240
|
+
// [{ route: 'api/orders/[id]/refund', available: 1, required: 2 }]
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Test it without a server
|
|
244
|
+
|
|
245
|
+
simulate() reports which policy decided, which a browser 403 never tells you. reachableRoutes() answers what a role can actually do — worth running, because stem inheritance grants more than a grant list reads like.
|
|
246
|
+
|
|
247
|
+
```text
|
|
248
|
+
import { simulate, reachableRoutes } from '@forgezero/access/testing';
|
|
249
|
+
|
|
250
|
+
simulate(ACCESS, { route: 'api/settings', roles, as: 'member' });
|
|
251
|
+
// { allowed: false, status: 403, reason: 'ACCESS_DENIED', sessionPolicy: 'admin' }
|
|
252
|
+
|
|
253
|
+
reachableRoutes(ACCESS, roles, ['member']);
|
|
254
|
+
// holding 'orders' also grants api/orders/[id]/refund — stems inherit
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Conditions: the guards, already written
|
|
258
|
+
|
|
259
|
+
Every project writes the same dozen checks into its before-handlers. These are those checks, each declaring the status it refuses with. requireOwner answers 404 rather than 403 on purpose — a 403 confirms the record exists, which turns sequential identifiers into an enumeration oracle.
|
|
260
|
+
|
|
261
|
+
```text
|
|
262
|
+
import { loadTarget, requireOwner, requireState, requireBalance }
|
|
263
|
+
from '@forgezero/access/conditions';
|
|
264
|
+
|
|
265
|
+
beforeHandlers: {
|
|
266
|
+
load: loadTarget({ routes: ['api/orders/[id]/refund'], load: findOrder }),
|
|
267
|
+
mine: requireOwner({ routes: ['api/orders/[id]/refund'], owner: o => o.userKey }),
|
|
268
|
+
payable: requireState({ routes: ['api/orders/[id]/refund'],
|
|
269
|
+
status: o => o.status, allowed: ['paid'] })
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## A refusal keeps its own status
|
|
274
|
+
|
|
275
|
+
A condition throws a Refusal carrying the status the client deserves, so "already refunded" is a 409 the UI can explain rather than a 500 that tells the client to retry. An idempotent replay throws Settled instead and returns the original answer — because a retrying client needs to learn the transfer succeeded, which no error code can tell it.
|
|
276
|
+
|
|
277
|
+
```text
|
|
278
|
+
throw new Refusal(409, 'STATE_INVALID', 'Already refunded.', { status: 'refunded' });
|
|
279
|
+
|
|
280
|
+
// idempotent replay — the ORIGINAL result, not an error
|
|
281
|
+
throw new Settled({ ok: true, id: 'ref_1' }, 201);
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Undeclared refusals fail the build
|
|
285
|
+
|
|
286
|
+
Each condition knows which statuses it can produce, so the set a route can answer is derivable. Add a quota check to a route whose contract never mentions 409 and the build stops — rather than a client rendering "an unexpected error" for the one refusal a user could have acted on.
|
|
287
|
+
|
|
288
|
+
```text
|
|
289
|
+
assertDeclaredRefusals({ routes: ROUTES, conditions: CONDITIONS });
|
|
290
|
+
// Undeclared refusals:
|
|
291
|
+
// api/wallet/withdraw can 409 (requireQuota) but does not declare it
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## Effects: what happens once it is decided
|
|
295
|
+
|
|
296
|
+
Audit, emit, meter, invalidate and notify. None can fail the request — a dead audit sink must not turn a completed transfer into a 500. Audit records denials as well as successes, because a run of 403s from one session is the signal an incident is reconstructed from. Every payload is redacted first.
|
|
297
|
+
|
|
298
|
+
```text
|
|
299
|
+
import { audit, emit, meter } from '@forgezero/access/effects';
|
|
300
|
+
|
|
301
|
+
afterHandlers: {
|
|
302
|
+
trail: audit({ routes: [...], sink }),
|
|
303
|
+
event: emit({ routes: [...], outbox, type: 'order.refunded',
|
|
304
|
+
payload: (ctx, result) => ({ id: result.id }) }),
|
|
305
|
+
usage: meter({ routes: [...], meter, unit: 'request' }) // successes only
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Response codes, and what each means
|
|
310
|
+
|
|
311
|
+
A 428 is not a rejection: it names the missing proof and the same request succeeds on replay. A 404 for a route outside its stage is deliberate — a 403 would confirm the route exists.
|
|
312
|
+
|
|
313
|
+
```text
|
|
314
|
+
401 no session
|
|
315
|
+
403 session is fine, the role lacks this route
|
|
316
|
+
404 route exists but not in this stage, realm or feature
|
|
317
|
+
409 a condition refused — state, balance, quota or approval
|
|
318
|
+
412 the record changed since you read it
|
|
319
|
+
422 the request did not match what the route accepts
|
|
320
|
+
423 locked — a human must act, unlike 503
|
|
321
|
+
428 a factor is missing — headers name which
|
|
322
|
+
429 rate limited
|
|
323
|
+
```
|
|
113
324
|
|
|
114
|
-
|
|
115
|
-
deploys — and usable entirely on its own, with no ForgeZero account.
|
|
325
|
+
Full rendered documentation: https://www.forgezero.net/docs/access
|