@forgezero/access 0.1.1 → 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 +301 -51
- package/dist/conditions.js +7 -4
- package/dist/effects.js +7 -4
- package/dist/elysia.js +7 -4
- package/dist/fetch.js +7 -4
- package/dist/header.d.ts +41 -0
- package/dist/header.js +432 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -4
- package/dist/pipeline.js +7 -4
- package/dist/principal-session.d.ts +47 -0
- package/dist/principal-session.js +548 -0
- package/dist/principal.d.ts +56 -0
- package/dist/principal.js +366 -0
- package/dist/testing.js +7 -4
- package/package.json +14 -2
package/README.md
CHANGED
|
@@ -1,75 +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.
|
|
4
15
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
16
|
+
```text
|
|
17
|
+
import * as root from '@forgezero/access';
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Commands
|
|
8
21
|
|
|
9
|
-
|
|
10
|
-
anywhere `fetch` and Web Crypto exist.
|
|
22
|
+
bun add @forgezero/access — Install the framework-neutral access contracts and adapters.
|
|
11
23
|
|
|
12
|
-
```
|
|
24
|
+
```text
|
|
13
25
|
bun add @forgezero/access
|
|
14
26
|
```
|
|
15
27
|
|
|
16
|
-
##
|
|
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
|
|
17
77
|
|
|
18
|
-
|
|
19
|
-
import { defineRoutes, authorise } from '@forgezero/access';
|
|
78
|
+
The framework-neutral route, principal, factor, policy, rate-limit and effects pipeline.
|
|
20
79
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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' })
|
|
24
158
|
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## 1. Install
|
|
162
|
+
|
|
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.
|
|
164
|
+
|
|
165
|
+
```text
|
|
166
|
+
bun add @forgezero/access
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## 2. Declare routes and who may reach them
|
|
170
|
+
|
|
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.
|
|
172
|
+
|
|
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' })
|
|
180
|
+
});
|
|
181
|
+
|
|
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
|
+
}
|
|
189
|
+
});
|
|
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.
|
|
25
209
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
|
29
216
|
|
|
30
|
-
|
|
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
|
+
}
|
|
227
|
+
}
|
|
31
228
|
```
|
|
32
229
|
|
|
33
|
-
##
|
|
230
|
+
## A session factor NEVER satisfies an action factor
|
|
231
|
+
|
|
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
|
+
```
|
|
34
242
|
|
|
35
|
-
|
|
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.
|
|
243
|
+
## Test it without a server
|
|
41
244
|
|
|
42
|
-
|
|
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.
|
|
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.
|
|
47
246
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
writes by hand, each already carrying the right status code.
|
|
247
|
+
```text
|
|
248
|
+
import { simulate, reachableRoutes } from '@forgezero/access/testing';
|
|
51
249
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
+
```
|
|
55
272
|
|
|
56
|
-
##
|
|
273
|
+
## A refusal keeps its own status
|
|
57
274
|
|
|
58
|
-
|
|
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 |
|
|
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.
|
|
69
276
|
|
|
70
|
-
|
|
277
|
+
```text
|
|
278
|
+
throw new Refusal(409, 'STATE_INVALID', 'Already refunded.', { status: 'refunded' });
|
|
71
279
|
|
|
72
|
-
|
|
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
|
+
```
|
|
73
324
|
|
|
74
|
-
|
|
75
|
-
deploys — and usable entirely on its own, with no ForgeZero account.
|
|
325
|
+
Full rendered documentation: https://www.forgezero.net/docs/access
|
package/dist/conditions.js
CHANGED
|
@@ -60,13 +60,16 @@ function defineFactors(factors) {
|
|
|
60
60
|
return factors;
|
|
61
61
|
}
|
|
62
62
|
function page(label, options = {}) {
|
|
63
|
-
return { kind: "page", label, ...options };
|
|
63
|
+
return { kind: "page", label, accessGroup: "default", ...options };
|
|
64
64
|
}
|
|
65
65
|
function action(label, method, options = {}) {
|
|
66
|
-
return { kind: "action", label, method, ...options };
|
|
66
|
+
return { kind: "action", label, method, accessGroup: "default", ...options };
|
|
67
67
|
}
|
|
68
68
|
function defineRoutes(routes) {
|
|
69
69
|
for (const [key, route] of Object.entries(routes)) {
|
|
70
|
+
if (!/^[A-Za-z][A-Za-z0-9_.:-]{0,63}$/.test(route.accessGroup)) {
|
|
71
|
+
throw new AccessError("ROUTE_ACCESS_INVALID", `"${key}": accessGroup must be a bounded name.`);
|
|
72
|
+
}
|
|
70
73
|
const isApi = key.startsWith("api/");
|
|
71
74
|
if (isApi !== (route.kind === "action")) {
|
|
72
75
|
throw new AccessError("ROUTE_KIND_MISMATCH", `"${key}": routes under api/ must be actions, and actions must be under api/.`);
|
|
@@ -242,11 +245,11 @@ function authorise(args) {
|
|
|
242
245
|
return { allow: false, status: 404, code: "NOT_FOUND" };
|
|
243
246
|
}
|
|
244
247
|
const policy = access.sessionPolicyFor(routeKey);
|
|
245
|
-
if (
|
|
248
|
+
if ((policy?.factors.length ?? 0) === 0)
|
|
246
249
|
return { allow: true };
|
|
247
250
|
if (!session)
|
|
248
251
|
return { allow: false, status: 401, code: "AUTH_REQUIRED" };
|
|
249
|
-
const missing = policy
|
|
252
|
+
const missing = (policy?.factors ?? []).filter((factor) => !session.factors.includes(factor));
|
|
250
253
|
if (missing.length > 0) {
|
|
251
254
|
return { allow: false, status: 428, code: "SECURITY_REQUIRED", missing };
|
|
252
255
|
}
|
package/dist/effects.js
CHANGED
|
@@ -60,13 +60,16 @@ function defineFactors(factors) {
|
|
|
60
60
|
return factors;
|
|
61
61
|
}
|
|
62
62
|
function page(label, options = {}) {
|
|
63
|
-
return { kind: "page", label, ...options };
|
|
63
|
+
return { kind: "page", label, accessGroup: "default", ...options };
|
|
64
64
|
}
|
|
65
65
|
function action(label, method, options = {}) {
|
|
66
|
-
return { kind: "action", label, method, ...options };
|
|
66
|
+
return { kind: "action", label, method, accessGroup: "default", ...options };
|
|
67
67
|
}
|
|
68
68
|
function defineRoutes(routes) {
|
|
69
69
|
for (const [key, route] of Object.entries(routes)) {
|
|
70
|
+
if (!/^[A-Za-z][A-Za-z0-9_.:-]{0,63}$/.test(route.accessGroup)) {
|
|
71
|
+
throw new AccessError("ROUTE_ACCESS_INVALID", `"${key}": accessGroup must be a bounded name.`);
|
|
72
|
+
}
|
|
70
73
|
const isApi = key.startsWith("api/");
|
|
71
74
|
if (isApi !== (route.kind === "action")) {
|
|
72
75
|
throw new AccessError("ROUTE_KIND_MISMATCH", `"${key}": routes under api/ must be actions, and actions must be under api/.`);
|
|
@@ -242,11 +245,11 @@ function authorise(args) {
|
|
|
242
245
|
return { allow: false, status: 404, code: "NOT_FOUND" };
|
|
243
246
|
}
|
|
244
247
|
const policy = access.sessionPolicyFor(routeKey);
|
|
245
|
-
if (
|
|
248
|
+
if ((policy?.factors.length ?? 0) === 0)
|
|
246
249
|
return { allow: true };
|
|
247
250
|
if (!session)
|
|
248
251
|
return { allow: false, status: 401, code: "AUTH_REQUIRED" };
|
|
249
|
-
const missing = policy
|
|
252
|
+
const missing = (policy?.factors ?? []).filter((factor) => !session.factors.includes(factor));
|
|
250
253
|
if (missing.length > 0) {
|
|
251
254
|
return { allow: false, status: 428, code: "SECURITY_REQUIRED", missing };
|
|
252
255
|
}
|
package/dist/elysia.js
CHANGED
|
@@ -60,13 +60,16 @@ function defineFactors(factors) {
|
|
|
60
60
|
return factors;
|
|
61
61
|
}
|
|
62
62
|
function page(label, options = {}) {
|
|
63
|
-
return { kind: "page", label, ...options };
|
|
63
|
+
return { kind: "page", label, accessGroup: "default", ...options };
|
|
64
64
|
}
|
|
65
65
|
function action(label, method, options = {}) {
|
|
66
|
-
return { kind: "action", label, method, ...options };
|
|
66
|
+
return { kind: "action", label, method, accessGroup: "default", ...options };
|
|
67
67
|
}
|
|
68
68
|
function defineRoutes(routes) {
|
|
69
69
|
for (const [key, route] of Object.entries(routes)) {
|
|
70
|
+
if (!/^[A-Za-z][A-Za-z0-9_.:-]{0,63}$/.test(route.accessGroup)) {
|
|
71
|
+
throw new AccessError("ROUTE_ACCESS_INVALID", `"${key}": accessGroup must be a bounded name.`);
|
|
72
|
+
}
|
|
70
73
|
const isApi = key.startsWith("api/");
|
|
71
74
|
if (isApi !== (route.kind === "action")) {
|
|
72
75
|
throw new AccessError("ROUTE_KIND_MISMATCH", `"${key}": routes under api/ must be actions, and actions must be under api/.`);
|
|
@@ -242,11 +245,11 @@ function authorise(args) {
|
|
|
242
245
|
return { allow: false, status: 404, code: "NOT_FOUND" };
|
|
243
246
|
}
|
|
244
247
|
const policy = access.sessionPolicyFor(routeKey);
|
|
245
|
-
if (
|
|
248
|
+
if ((policy?.factors.length ?? 0) === 0)
|
|
246
249
|
return { allow: true };
|
|
247
250
|
if (!session)
|
|
248
251
|
return { allow: false, status: 401, code: "AUTH_REQUIRED" };
|
|
249
|
-
const missing = policy
|
|
252
|
+
const missing = (policy?.factors ?? []).filter((factor) => !session.factors.includes(factor));
|
|
250
253
|
if (missing.length > 0) {
|
|
251
254
|
return { allow: false, status: 428, code: "SECURITY_REQUIRED", missing };
|
|
252
255
|
}
|
package/dist/fetch.js
CHANGED
|
@@ -60,13 +60,16 @@ function defineFactors(factors) {
|
|
|
60
60
|
return factors;
|
|
61
61
|
}
|
|
62
62
|
function page(label, options = {}) {
|
|
63
|
-
return { kind: "page", label, ...options };
|
|
63
|
+
return { kind: "page", label, accessGroup: "default", ...options };
|
|
64
64
|
}
|
|
65
65
|
function action(label, method, options = {}) {
|
|
66
|
-
return { kind: "action", label, method, ...options };
|
|
66
|
+
return { kind: "action", label, method, accessGroup: "default", ...options };
|
|
67
67
|
}
|
|
68
68
|
function defineRoutes(routes) {
|
|
69
69
|
for (const [key, route] of Object.entries(routes)) {
|
|
70
|
+
if (!/^[A-Za-z][A-Za-z0-9_.:-]{0,63}$/.test(route.accessGroup)) {
|
|
71
|
+
throw new AccessError("ROUTE_ACCESS_INVALID", `"${key}": accessGroup must be a bounded name.`);
|
|
72
|
+
}
|
|
70
73
|
const isApi = key.startsWith("api/");
|
|
71
74
|
if (isApi !== (route.kind === "action")) {
|
|
72
75
|
throw new AccessError("ROUTE_KIND_MISMATCH", `"${key}": routes under api/ must be actions, and actions must be under api/.`);
|
|
@@ -242,11 +245,11 @@ function authorise(args) {
|
|
|
242
245
|
return { allow: false, status: 404, code: "NOT_FOUND" };
|
|
243
246
|
}
|
|
244
247
|
const policy = access.sessionPolicyFor(routeKey);
|
|
245
|
-
if (
|
|
248
|
+
if ((policy?.factors.length ?? 0) === 0)
|
|
246
249
|
return { allow: true };
|
|
247
250
|
if (!session)
|
|
248
251
|
return { allow: false, status: 401, code: "AUTH_REQUIRED" };
|
|
249
|
-
const missing = policy
|
|
252
|
+
const missing = (policy?.factors ?? []).filter((factor) => !session.factors.includes(factor));
|
|
250
253
|
if (missing.length > 0) {
|
|
251
254
|
return { allow: false, status: 428, code: "SECURITY_REQUIRED", missing };
|
|
252
255
|
}
|
package/dist/header.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type PrincipalAssignment, type RbacPrincipal } from './principal';
|
|
2
|
+
export interface HeaderIdentity {
|
|
3
|
+
subject: string;
|
|
4
|
+
/** Authentication facts established by the source, never RBAC assignments. */
|
|
5
|
+
claims?: Readonly<Record<string, string>>;
|
|
6
|
+
}
|
|
7
|
+
/** A header is transport; this verifier is the authentication boundary. */
|
|
8
|
+
export interface HeaderIdentitySource {
|
|
9
|
+
key: string;
|
|
10
|
+
header: string;
|
|
11
|
+
verify(input: {
|
|
12
|
+
value: string;
|
|
13
|
+
request: Request;
|
|
14
|
+
source: string;
|
|
15
|
+
}): Promise<HeaderIdentity | undefined>;
|
|
16
|
+
}
|
|
17
|
+
export interface HeaderPrincipal extends RbacPrincipal {
|
|
18
|
+
kind: 'header-identity';
|
|
19
|
+
/** Stable RBAC principal key. Safe to persist in a membership row. */
|
|
20
|
+
principalKey: string;
|
|
21
|
+
source: string;
|
|
22
|
+
subject: string;
|
|
23
|
+
claims: Readonly<Record<string, string>>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The host owns assignments. It may read a database, configuration service or
|
|
27
|
+
* code policy; the credential itself never supplies roles. This is called for
|
|
28
|
+
* every request so assignment, disablement and revocation take effect at once.
|
|
29
|
+
*/
|
|
30
|
+
export interface HeaderAssignmentResolver {
|
|
31
|
+
(input: {
|
|
32
|
+
principalKey: string;
|
|
33
|
+
source: string;
|
|
34
|
+
subject: string;
|
|
35
|
+
claims: Readonly<Record<string, string>>;
|
|
36
|
+
request: Request;
|
|
37
|
+
}): Promise<readonly PrincipalAssignment[]>;
|
|
38
|
+
}
|
|
39
|
+
export declare function defineHeaderIdentitySources<const T extends readonly HeaderIdentitySource[]>(sources: T): T;
|
|
40
|
+
/** Resolve exactly one authenticated header source and its fresh RBAC assignment. */
|
|
41
|
+
export declare function headerIdentityResolver(sourcesInput: readonly HeaderIdentitySource[], resolveAssignments: HeaderAssignmentResolver): (request: Request) => Promise<HeaderPrincipal | undefined>;
|