@fabrika/auth-core 0.3.3 → 0.4.0
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/package.json +1 -1
- package/src/app-environment.ts +44 -0
- package/src/canonical-path.ts +66 -0
- package/src/contract-canonical.ts +189 -0
- package/src/contract.ts +504 -0
- package/src/endpoint-digest.ts +141 -0
- package/src/endpoints.ts +292 -0
- package/src/framework-variables.ts +114 -0
- package/src/index.ts +14 -1
- package/src/installation-endpoint.ts +61 -0
- package/src/locale.ts +35 -0
- package/src/mail.ts +198 -0
- package/src/membership.ts +384 -0
- package/src/oauth.ts +50 -0
- package/src/path-segment.ts +9 -0
- package/src/requirement.ts +58 -0
- package/src/rpc.ts +22 -8
- package/src/sha256.ts +125 -0
- package/src/token.ts +255 -9
- package/src/types.ts +123 -43
- package/src/wire.ts +48 -0
- package/src/gates.ts +0 -29
package/src/contract.ts
ADDED
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
// The compiled form of an application contract (ADR-0012). Types only: each reader parses the artifact with its
|
|
2
|
+
// own schema, as gate rules are parsed today.
|
|
3
|
+
|
|
4
|
+
import type { Locale } from './locale'
|
|
5
|
+
import type { MailTemplates } from './mail'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An application's whole access surface as plain JSON. `compileContract` produces it from the evaluated contract and
|
|
9
|
+
* the static reader from source; both must produce the same value for the same contract. Every optional authored
|
|
10
|
+
* field is normalized to `null`, `[]` or `{}`, so a field is always present. Key order and the order of `endpoints`
|
|
11
|
+
* carry no meaning.
|
|
12
|
+
*/
|
|
13
|
+
export interface ContractArtifact extends EndpointTable {
|
|
14
|
+
readonly app: string
|
|
15
|
+
readonly dimensions: Readonly<Record<string, DimensionArtifact>>
|
|
16
|
+
/** Action → description. Exact actions only; the artifact holds no wildcard. */
|
|
17
|
+
readonly actions: Readonly<Record<string, string>>
|
|
18
|
+
readonly roles: Readonly<Record<string, RoleArtifact>>
|
|
19
|
+
readonly mounts: Readonly<Record<string, MountArtifact>>
|
|
20
|
+
readonly files: Readonly<Record<string, FileCollectionArtifact>>
|
|
21
|
+
readonly jobs: Readonly<Record<string, JobArtifact>>
|
|
22
|
+
readonly exports: Readonly<Record<string, ExportArtifact>>
|
|
23
|
+
readonly imports: Readonly<Record<string, ImportArtifact>>
|
|
24
|
+
readonly csp: CspExtensions
|
|
25
|
+
readonly tables: Readonly<Record<string, TablePolicyArtifact>>
|
|
26
|
+
/**
|
|
27
|
+
* The reach each site declares off the paved road, by site: an endpoint's id (a procedure or a webhook), or
|
|
28
|
+
* `job:<name>`; a site that declares nothing has no entry. Present for a contract of the staged kit, whose sites
|
|
29
|
+
* reach only what they declare; absent for a `defineContract` one, so its digest stays what it was.
|
|
30
|
+
*/
|
|
31
|
+
readonly declared?: Readonly<Record<string, DeclaredReach>>
|
|
32
|
+
/**
|
|
33
|
+
* The member directory each endpoint reads (`ctx.members` without management), by endpoint id: derived from the
|
|
34
|
+
* dimensions by `directoryReach` and recorded so the approval sees and the digest covers it. Absent when no endpoint
|
|
35
|
+
* reads one, so the digest of such a contract stays.
|
|
36
|
+
*/
|
|
37
|
+
readonly directories?: Readonly<Record<string, { readonly dimension: string; readonly lists: boolean }>>
|
|
38
|
+
/** The language IAM speaks to this application's people. Absent unless declared, so the digest stays. */
|
|
39
|
+
readonly locale?: Locale
|
|
40
|
+
/** The name IAM shows on its pages and e-mails for this application. Absent unless declared, so the digest stays. */
|
|
41
|
+
readonly displayName?: string
|
|
42
|
+
/** The application's wording of IAM's e-mails. Absent unless declared, so the digest stays. */
|
|
43
|
+
readonly mail?: MailTemplates
|
|
44
|
+
/**
|
|
45
|
+
* The application's own people, every holder of one of its roles: `manage` held globally administers its global roles in
|
|
46
|
+
* IAM's console, `list` held globally reads its member directory (`ctx.members` of an `app` endpoint of that action).
|
|
47
|
+
* Absent unless declared, so the digest stays.
|
|
48
|
+
*/
|
|
49
|
+
readonly members?: { readonly manage: string; readonly list: string }
|
|
50
|
+
/** `labels`: the application's directory shows labels to `members.list` holders. Absent unless declared, so digests stay. */
|
|
51
|
+
readonly directory?: 'labels'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ── The endpoint table ──────────────────────────────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
/** What `planEndpoint` selects from: the endpoint rows and the prefixes reserved for procedures. */
|
|
57
|
+
export interface EndpointTable {
|
|
58
|
+
readonly endpoints: readonly EndpointRow[]
|
|
59
|
+
/**
|
|
60
|
+
* Literal path prefixes of procedure mounts (`/api`). A path under one (`/api/…`) is served only by an endpoint
|
|
61
|
+
* declared under it, so an unknown procedure never reaches a broader endpoint such as the SPA.
|
|
62
|
+
*/
|
|
63
|
+
readonly reservedPrefixes: readonly string[]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Who may call a credentialed endpoint. There is no public caller: anonymous traffic is a fixed endpoint kind. */
|
|
67
|
+
export type Caller = 'human' | 'machine' | 'link' | `app:${string}`
|
|
68
|
+
|
|
69
|
+
/** `app:<id>`: the connector of the consuming application `<id>`. */
|
|
70
|
+
export const APP_CALLER_PREFIX = 'app:'
|
|
71
|
+
|
|
72
|
+
/** Every export procedure and description is served under this prefix; an `app:<id>` caller reaches nothing else. */
|
|
73
|
+
export const CONNECT_PREFIX = '/__fabrika/connect'
|
|
74
|
+
|
|
75
|
+
/** An RPC mount's callers: a link holder reaches only its own link mount, and an `app:<id>` only an export. */
|
|
76
|
+
export type RpcCaller = 'human' | 'machine'
|
|
77
|
+
|
|
78
|
+
export type EndpointMethod = 'GET' | 'POST'
|
|
79
|
+
|
|
80
|
+
/** Kinds that verify a credential, check the caller kind and the requirement. */
|
|
81
|
+
export type CredentialedEndpointKind = 'query' | 'mutation' | 'spa' | 'upload' | 'download'
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Fixed anonymous kinds: no caller, no requirement, no token. `link-spa` is the shell of a link mount's SPA;
|
|
85
|
+
* `public-spa` is an SPA anyone opens (`access: { public: true }`).
|
|
86
|
+
*/
|
|
87
|
+
export type AnonymousEndpointKind = 'health' | 'form' | 'webhook' | 'link-spa' | 'public-spa'
|
|
88
|
+
|
|
89
|
+
export type EndpointKind = CredentialedEndpointKind | AnonymousEndpointKind
|
|
90
|
+
|
|
91
|
+
/** The contract entry that declared an endpoint. */
|
|
92
|
+
export interface EndpointSource {
|
|
93
|
+
readonly kind: 'mount' | 'export' | 'files'
|
|
94
|
+
readonly name: string
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface EndpointRowBase {
|
|
98
|
+
/** Unique in the table: the framework dispatches by it and the proxy names it in `X-Fabrika-Endpoint`. */
|
|
99
|
+
readonly id: string
|
|
100
|
+
readonly source: EndpointSource
|
|
101
|
+
readonly method: EndpointMethod
|
|
102
|
+
/**
|
|
103
|
+
* Matched against the raw path, case-sensitively. Segments are literals, `:name` (one non-empty segment) or a
|
|
104
|
+
* terminal `*`. A non-root `*` needs the slash before it, and a trailing `/` is a literal empty segment.
|
|
105
|
+
*/
|
|
106
|
+
readonly path: string
|
|
107
|
+
/** The request body the endpoint admits; `null` admits none. */
|
|
108
|
+
readonly admission: BodyAdmission | null
|
|
109
|
+
readonly input: SchemaShape | null
|
|
110
|
+
readonly output: OutputShape | null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface CredentialedEndpoint extends EndpointRowBase {
|
|
114
|
+
readonly kind: CredentialedEndpointKind
|
|
115
|
+
readonly callers: readonly [Caller, ...Caller[]]
|
|
116
|
+
readonly requirement: EndpointRequirement
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface AnonymousEndpoint extends EndpointRowBase {
|
|
120
|
+
readonly kind: AnonymousEndpointKind
|
|
121
|
+
readonly callers: readonly []
|
|
122
|
+
readonly requirement: null
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type EndpointRow = CredentialedEndpoint | AnonymousEndpoint
|
|
126
|
+
|
|
127
|
+
export interface BodyAdmission {
|
|
128
|
+
readonly contentTypes: readonly [string, ...string[]]
|
|
129
|
+
readonly maxBytes: number | null
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* What the token must carry before the request reaches a handler. `member` is any permission of this application;
|
|
134
|
+
* `action` is that action in some scope, global included, and only globally under the `app` form. The access form is
|
|
135
|
+
* what the framework then resolves.
|
|
136
|
+
*/
|
|
137
|
+
export type EndpointRequirement = MemberRequirement | ActionRequirement
|
|
138
|
+
|
|
139
|
+
export interface MemberRequirement {
|
|
140
|
+
readonly kind: 'member'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface ActionRequirement {
|
|
144
|
+
readonly kind: 'action'
|
|
145
|
+
readonly action: string
|
|
146
|
+
readonly access: AccessForm
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** `from` names an input field or a path parameter of the endpoint. */
|
|
150
|
+
export type AccessForm =
|
|
151
|
+
| { readonly form: 'over'; readonly dimension: string }
|
|
152
|
+
| { readonly form: 'in'; readonly dimension: string; readonly from: string }
|
|
153
|
+
/** `from: null` is the object a link key is bound to. */
|
|
154
|
+
| { readonly form: 'on'; readonly dimension: string; readonly from: string | null }
|
|
155
|
+
| { readonly form: 'creates'; readonly dimension: string }
|
|
156
|
+
/** The action held as a global grant: data of the whole application, in no dimension. */
|
|
157
|
+
| { readonly form: 'app' }
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The structure and constraints of an input or output schema. Objects are always strict: a contract admits no other.
|
|
161
|
+
* The union is closed; a schema form it cannot represent is refused where the contract is compiled or read. A
|
|
162
|
+
* default makes a value optional to the caller, so it is `optional`; a discriminated union is a `union`.
|
|
163
|
+
*/
|
|
164
|
+
export type SchemaShape =
|
|
165
|
+
| StringShape
|
|
166
|
+
| NumberShape
|
|
167
|
+
| { readonly type: 'boolean' | 'null' }
|
|
168
|
+
| { readonly type: 'literal'; readonly value: string | number | boolean | null }
|
|
169
|
+
| { readonly type: 'enum'; readonly values: readonly string[] }
|
|
170
|
+
| ArrayShape
|
|
171
|
+
| TupleShape
|
|
172
|
+
| RecordShape
|
|
173
|
+
| { readonly type: 'object'; readonly fields: Readonly<Record<string, SchemaShape>> }
|
|
174
|
+
| { readonly type: 'optional'; readonly inner: SchemaShape }
|
|
175
|
+
| { readonly type: 'nullable'; readonly inner: SchemaShape }
|
|
176
|
+
| { readonly type: 'union'; readonly options: readonly SchemaShape[] }
|
|
177
|
+
|
|
178
|
+
/** What a procedure answers: a JSON value of a schema, or a file of rows (`attachment()` of `@fabrika/app/contract`). */
|
|
179
|
+
export type OutputShape = SchemaShape | AttachmentShape
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* A procedure whose handler returns rows and whose answer is a file the framework encodes from them. The columns are
|
|
183
|
+
* the row schema's fields in their declared order, which is the file's column order; each is a scalar cell.
|
|
184
|
+
*/
|
|
185
|
+
export interface AttachmentShape {
|
|
186
|
+
readonly type: 'attachment'
|
|
187
|
+
/** `csv`: RFC 4180 text; `xlsx`: one sheet of typed cells. */
|
|
188
|
+
readonly format: AttachmentFormat
|
|
189
|
+
/** The `Content-Disposition` file name: a fixed name the contract declares, never one a request sends. */
|
|
190
|
+
readonly filename: string
|
|
191
|
+
/** The encoded file's limit, in bytes: an encoding over it fails the invocation and sends no body. */
|
|
192
|
+
readonly maxBytes: number
|
|
193
|
+
readonly maxRows: number
|
|
194
|
+
readonly columns: readonly AttachmentColumn[]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export type AttachmentFormat = 'csv' | 'xlsx'
|
|
198
|
+
|
|
199
|
+
export interface AttachmentColumn {
|
|
200
|
+
readonly name: string
|
|
201
|
+
readonly shape: SchemaShape
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function isAttachmentShape(shape: OutputShape): shape is AttachmentShape {
|
|
205
|
+
return shape.type === 'attachment'
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface StringShape {
|
|
209
|
+
readonly type: 'string'
|
|
210
|
+
/** `datetime`, `date`, `time` and `duration` are the ISO 8601 forms of `z.iso`. */
|
|
211
|
+
readonly format: 'uuid' | 'email' | 'url' | 'datetime' | 'date' | 'time' | 'duration' | null
|
|
212
|
+
readonly minLength: number | null
|
|
213
|
+
readonly maxLength: number | null
|
|
214
|
+
/** The regular expression's source, without flags. */
|
|
215
|
+
readonly pattern: string | null
|
|
216
|
+
readonly startsWith: string | null
|
|
217
|
+
readonly endsWith: string | null
|
|
218
|
+
readonly includes: string | null
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** `exclusiveMin` and `exclusiveMax` are `false` when their bound is `null`. */
|
|
222
|
+
export interface NumberShape {
|
|
223
|
+
readonly type: 'number'
|
|
224
|
+
readonly int: boolean
|
|
225
|
+
readonly min: number | null
|
|
226
|
+
readonly exclusiveMin: boolean
|
|
227
|
+
readonly max: number | null
|
|
228
|
+
readonly exclusiveMax: boolean
|
|
229
|
+
readonly multipleOf: number | null
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Fixed positions, then any number of `rest` values when it is not `null`. */
|
|
233
|
+
export interface TupleShape {
|
|
234
|
+
readonly type: 'tuple'
|
|
235
|
+
readonly items: readonly SchemaShape[]
|
|
236
|
+
readonly rest: SchemaShape | null
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export interface RecordShape {
|
|
240
|
+
readonly type: 'record'
|
|
241
|
+
readonly key: SchemaShape
|
|
242
|
+
readonly value: SchemaShape
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export interface ArrayShape {
|
|
246
|
+
readonly type: 'array'
|
|
247
|
+
readonly items: SchemaShape
|
|
248
|
+
readonly minItems: number | null
|
|
249
|
+
readonly maxItems: number | null
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ── The vocabulary ──────────────────────────────────────────────────────────────────────────────
|
|
253
|
+
|
|
254
|
+
export interface DimensionArtifact {
|
|
255
|
+
/** The dimension that contains every value of this one (`note` is in `workspace`). */
|
|
256
|
+
readonly in: string | null
|
|
257
|
+
/** The role `creates` grants the caller in a new value of this dimension. */
|
|
258
|
+
readonly creator: string | null
|
|
259
|
+
readonly members: { readonly manage: string; readonly list: string } | null
|
|
260
|
+
/** `labels`: the member directory shows labels to `members.list` holders. Absent unless declared, so digests stay. */
|
|
261
|
+
readonly directory?: 'labels'
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface RoleArtifact {
|
|
265
|
+
readonly grants: readonly string[]
|
|
266
|
+
/**
|
|
267
|
+
* A membership role is granted only in a value of `scope`, by a holder of one of the `assignableBy` roles: there,
|
|
268
|
+
* or globally for an application-wide role (one with no `member`).
|
|
269
|
+
*/
|
|
270
|
+
readonly member: { readonly scope: string; readonly assignableBy: readonly string[] } | null
|
|
271
|
+
/** An external principal may hold this membership role (ADR-0051). Absent unless declared, so digests stay. */
|
|
272
|
+
readonly external?: true
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ── Mounts and companions ───────────────────────────────────────────────────────────────────────
|
|
276
|
+
|
|
277
|
+
export type MountArtifact =
|
|
278
|
+
| RpcMountArtifact
|
|
279
|
+
| LinkMountArtifact
|
|
280
|
+
| SpaMountArtifact
|
|
281
|
+
| HealthMountArtifact
|
|
282
|
+
| FormMountArtifact
|
|
283
|
+
| WebhookMountArtifact
|
|
284
|
+
|
|
285
|
+
export interface RpcMountArtifact {
|
|
286
|
+
readonly kind: 'rpc'
|
|
287
|
+
readonly prefix: string
|
|
288
|
+
readonly callers: readonly [RpcCaller, ...RpcCaller[]]
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export interface LinkMountArtifact {
|
|
292
|
+
readonly kind: 'link'
|
|
293
|
+
readonly prefix: string
|
|
294
|
+
/** The directory of the share SPA. */
|
|
295
|
+
readonly spa: string
|
|
296
|
+
readonly dimension: string
|
|
297
|
+
/** Frozen into every key the mount issues. */
|
|
298
|
+
readonly grants: readonly string[]
|
|
299
|
+
/** The action a `Ref` must carry to issue a link. */
|
|
300
|
+
readonly issuedBy: string
|
|
301
|
+
readonly maxDays: number
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export interface SpaMountArtifact {
|
|
305
|
+
readonly kind: 'spa'
|
|
306
|
+
readonly prefix: string
|
|
307
|
+
readonly dir: string
|
|
308
|
+
/** The application variables whose values the mount's pages read from its browser config; present only when declared. */
|
|
309
|
+
readonly public?: readonly string[]
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export interface HealthMountArtifact {
|
|
313
|
+
readonly kind: 'health'
|
|
314
|
+
readonly path: string
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface FormMountArtifact {
|
|
318
|
+
readonly kind: 'form'
|
|
319
|
+
readonly path: string
|
|
320
|
+
/** The inbox table the form appends to. */
|
|
321
|
+
readonly appendTo: string
|
|
322
|
+
/** Submissions per minute it admits from one client and accepts in all; each is present only when declared. */
|
|
323
|
+
readonly rate?: FormRate
|
|
324
|
+
/** The input field that must stay empty; a filled one is answered as accepted and writes nothing. */
|
|
325
|
+
readonly honeypot?: string
|
|
326
|
+
/** The challenge the framework verifies on the server before it writes the row. */
|
|
327
|
+
readonly challenge?: FormChallenge
|
|
328
|
+
/** The cron job each accepted submission kicks: one run in the background, coalesced on the job's lease. */
|
|
329
|
+
readonly then?: string
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export interface FormRate {
|
|
333
|
+
readonly perClient?: number
|
|
334
|
+
readonly perForm?: number
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** The closed list of challenge providers; each one's verification endpoint and browser origins are fixed. */
|
|
338
|
+
export type ChallengeProvider = 'turnstile'
|
|
339
|
+
|
|
340
|
+
export interface FormChallenge {
|
|
341
|
+
readonly provider: ChallengeProvider
|
|
342
|
+
/** The application variable holding the provider's secret: a name, never the value. */
|
|
343
|
+
readonly secret: string
|
|
344
|
+
/** The application variable holding the widget's public site key, which every SPA mount's browser config serves. */
|
|
345
|
+
readonly siteKey?: string
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export interface WebhookMountArtifact {
|
|
349
|
+
readonly kind: 'webhook'
|
|
350
|
+
readonly path: string
|
|
351
|
+
readonly scheme: 'github-sha256'
|
|
352
|
+
readonly boundTo: ScopeBinding
|
|
353
|
+
readonly actions: readonly string[]
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Where a workload's authority applies: the whole application, or one value of a dimension per binding. */
|
|
357
|
+
export type ScopeBinding = { readonly kind: 'app' } | { readonly kind: 'dimension'; readonly dimension: string }
|
|
358
|
+
|
|
359
|
+
export interface FileCollectionArtifact {
|
|
360
|
+
/** The table whose rows own the files; a file inherits its owner's access. */
|
|
361
|
+
readonly owner: string
|
|
362
|
+
readonly read: string
|
|
363
|
+
readonly write: string
|
|
364
|
+
readonly delete: string
|
|
365
|
+
readonly maxBytes: number
|
|
366
|
+
readonly types: readonly string[]
|
|
367
|
+
readonly maxPerOwner: number | null
|
|
368
|
+
/**
|
|
369
|
+
* Whether a share link to the owner reaches the files: `true` through every link mount bound to the owner's
|
|
370
|
+
* dimension, a list through the named link mounts only.
|
|
371
|
+
*/
|
|
372
|
+
readonly link: boolean | readonly string[]
|
|
373
|
+
/**
|
|
374
|
+
* Present when every upload waits `quarantined` for a clean malware verdict before it is served. Absent otherwise,
|
|
375
|
+
* so the digest of a contract that does not scan is what it was before the field existed.
|
|
376
|
+
*/
|
|
377
|
+
readonly scan?: true
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export interface JobArtifact {
|
|
381
|
+
readonly schedule: string
|
|
382
|
+
readonly actions: readonly string[]
|
|
383
|
+
readonly scope: ScopeBinding
|
|
384
|
+
readonly timeout: string | null
|
|
385
|
+
readonly retries: number
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** An export's procedures are endpoint rows whose source is this export. */
|
|
389
|
+
export interface ExportArtifact {
|
|
390
|
+
readonly version: number
|
|
391
|
+
readonly consumers: readonly string[]
|
|
392
|
+
readonly audience: string
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* What an export's consumer vendors (`fabrika connect add`): the export's identity and each procedure's schemas, by
|
|
397
|
+
* the procedure's dotted path in the export. `digest` is `exportDigest` of the rest, and the consumer's import names it.
|
|
398
|
+
*/
|
|
399
|
+
export interface ExportDescription {
|
|
400
|
+
readonly app: string
|
|
401
|
+
readonly export: string
|
|
402
|
+
readonly version: number
|
|
403
|
+
readonly audience: string
|
|
404
|
+
readonly procedures: Readonly<Record<string, ExportProcedureDescription>>
|
|
405
|
+
readonly digest: string
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export interface ExportProcedureDescription {
|
|
409
|
+
readonly input: SchemaShape | null
|
|
410
|
+
readonly output: SchemaShape
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export interface ImportArtifact {
|
|
414
|
+
readonly app: string
|
|
415
|
+
readonly export: string
|
|
416
|
+
readonly version: number
|
|
417
|
+
/** The digest of the vendored export snapshot. */
|
|
418
|
+
readonly digest: string
|
|
419
|
+
readonly procedures: readonly string[]
|
|
420
|
+
/** The endpoint ids whose handlers may call the import. */
|
|
421
|
+
readonly usedBy: readonly string[]
|
|
422
|
+
readonly timeoutMs: number | null
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** `job:<name>`: a job's key in `ContractArtifact.declared`; an endpoint is keyed by its id. */
|
|
426
|
+
export const JOB_SITE_PREFIX = 'job:'
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* What one site declares beyond its capability: `ctx.sql` in a mode, tables it reaches unscoped and in which mode, the
|
|
430
|
+
* imports it calls, the link mount it issues. A review signal for the approver, never an enforcement decision.
|
|
431
|
+
*/
|
|
432
|
+
export interface DeclaredReach {
|
|
433
|
+
readonly sql: 'read' | 'write' | null
|
|
434
|
+
readonly unscoped: Readonly<Record<string, 'read' | 'write'>>
|
|
435
|
+
readonly connect: readonly string[]
|
|
436
|
+
readonly issues: string | null
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export interface CspExtensions {
|
|
440
|
+
readonly connectSrc: readonly CspSource[]
|
|
441
|
+
readonly imgSrc: readonly CspSource[]
|
|
442
|
+
readonly frameAncestors: readonly CspSource[]
|
|
443
|
+
/** Present only when the contract names a script origin (or a challenge provider needs one). */
|
|
444
|
+
readonly scriptSrc?: readonly CspSource[]
|
|
445
|
+
/** Present only when the contract names a frame origin; `frame-src` is emitted only then. */
|
|
446
|
+
readonly frameSrc?: readonly CspSource[]
|
|
447
|
+
/** Present only when the contract names a stylesheet origin, such as a hosted font's stylesheet. */
|
|
448
|
+
readonly styleSrc?: readonly CspSource[]
|
|
449
|
+
/** Present only when the contract names a font origin. */
|
|
450
|
+
readonly fontSrc?: readonly CspSource[]
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export interface CspSource {
|
|
454
|
+
readonly origin: string
|
|
455
|
+
readonly purpose: string
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// ── Table policies ──────────────────────────────────────────────────────────────────────────────
|
|
459
|
+
|
|
460
|
+
export interface TablePolicyArtifact {
|
|
461
|
+
readonly scope: TableScope
|
|
462
|
+
/** An inbox's read action is `read`; it has no write action, because only its form appends. */
|
|
463
|
+
readonly actions: {
|
|
464
|
+
readonly read: string
|
|
465
|
+
readonly insert: string | null
|
|
466
|
+
readonly update: string | null
|
|
467
|
+
readonly delete: string | null
|
|
468
|
+
/**
|
|
469
|
+
* A dimension or child table's actions besides `read` with which an `app` endpoint reads every row, read-only;
|
|
470
|
+
* present only when the policy names one.
|
|
471
|
+
*/
|
|
472
|
+
readonly appRead?: readonly string[]
|
|
473
|
+
/** The actions with which an `app` endpoint counts and aggregates every row and reads none; present only when named. */
|
|
474
|
+
readonly appCount?: readonly string[]
|
|
475
|
+
}
|
|
476
|
+
readonly insert: readonly string[]
|
|
477
|
+
readonly update: readonly string[]
|
|
478
|
+
readonly version: string | null
|
|
479
|
+
readonly createdBy: string | null
|
|
480
|
+
/**
|
|
481
|
+
* Column → its own actions. A column without a read action falls back to the table's; a column with a write action
|
|
482
|
+
* is updated by that action only, never by the table's update action.
|
|
483
|
+
*/
|
|
484
|
+
readonly columns: Readonly<Record<string, ColumnPolicyArtifact>>
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** A column's own read action, its own write action, or both; an absent one is left out. */
|
|
488
|
+
export interface ColumnPolicyArtifact {
|
|
489
|
+
readonly read?: string
|
|
490
|
+
readonly write?: string
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export type TableScope =
|
|
494
|
+
| {
|
|
495
|
+
readonly mode: 'dimension'
|
|
496
|
+
readonly dimension: string
|
|
497
|
+
readonly key: string
|
|
498
|
+
readonly container: { readonly dimension: string; readonly column: string } | null
|
|
499
|
+
}
|
|
500
|
+
| { readonly mode: 'child'; readonly parent: string; readonly key: string; readonly column: string; readonly inherit: readonly string[] }
|
|
501
|
+
| { readonly mode: 'inbox'; readonly form: string }
|
|
502
|
+
| { readonly mode: 'reference'; readonly reason: string }
|
|
503
|
+
/** Rows of the whole application, in no dimension: an `app` endpoint reads and writes them. */
|
|
504
|
+
| { readonly mode: 'app'; readonly reason: string; readonly key: string }
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// The digests that bind the proxy and the application to the same rules (ADR-0022 rule 5): one per endpoint, sent
|
|
2
|
+
// with every allowed request, and one for the whole contract, checked once at startup.
|
|
3
|
+
|
|
4
|
+
import type { ContractArtifact, EndpointRow, ExportDescription } from './contract'
|
|
5
|
+
import { canonicalAdmission, canonicalArtifactJson, canonicalJson, canonicalShape } from './contract-canonical'
|
|
6
|
+
import type { InstallationEndpointRow, InstallationEndpointTable } from './installation-endpoint'
|
|
7
|
+
import { sha256Hex } from './sha256'
|
|
8
|
+
|
|
9
|
+
/** The variable that carries the digest of the contract a deploy was approved with; the application compares its own. */
|
|
10
|
+
export const FABRIKA_CONTRACT_DIGEST = 'FABRIKA_CONTRACT_DIGEST'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The variable that carries `installationTableDigest` of the table an installation service's proxy publishes; the
|
|
14
|
+
* service picks its own table by it and refuses to start when it names none.
|
|
15
|
+
*/
|
|
16
|
+
export const FABRIKA_SERVICE_TABLE_DIGEST = 'FABRIKA_SERVICE_TABLE_DIGEST'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a membership or scope-creating endpoint spends at IAM through a single-use delegation grant (ADR-0022 rule 6):
|
|
20
|
+
* `creates` for an endpoint that creates a value, `members` for one whose action is the `members.manage` action of the
|
|
21
|
+
* dimension its access form names.
|
|
22
|
+
*/
|
|
23
|
+
export type EndpointDelegation = 'members' | 'creates'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The fields of an endpoint row the proxy applies, with its delegation. Its digest covers exactly these. An
|
|
27
|
+
* installation row adds its OAuth scopes; an application row has no `oauth` field.
|
|
28
|
+
*/
|
|
29
|
+
export type EndpointRules =
|
|
30
|
+
| (Pick<EndpointRow, 'kind' | 'method' | 'path' | 'callers' | 'requirement' | 'admission'> & {
|
|
31
|
+
readonly delegation: EndpointDelegation | null
|
|
32
|
+
})
|
|
33
|
+
| (Pick<InstallationEndpointRow, 'kind' | 'method' | 'path' | 'callers' | 'requirement' | 'admission' | 'oauth'> & {
|
|
34
|
+
readonly delegation: null
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const DIGEST_PATTERN = /^sha256:[0-9a-f]{64}$/
|
|
38
|
+
|
|
39
|
+
/** Whether `value` has the form every digest here has, `sha256:<64 hex digits>`. */
|
|
40
|
+
export function isDigest(value: string): boolean {
|
|
41
|
+
return DIGEST_PATTERN.test(value)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The one derivation of an endpoint's delegation: the proxy table, the IAM schema and the digest all read it. */
|
|
45
|
+
export function endpointDelegation(artifact: Pick<ContractArtifact, 'dimensions'>, row: EndpointRow): EndpointDelegation | null {
|
|
46
|
+
if (row.requirement?.kind !== 'action') {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
const { action, access } = row.requirement
|
|
50
|
+
if (access.form === 'creates') {
|
|
51
|
+
return 'creates'
|
|
52
|
+
}
|
|
53
|
+
if (access.form === 'app') {
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
const managed = Object.hasOwn(artifact.dimensions, access.dimension) ? artifact.dimensions[access.dimension]?.members : null
|
|
57
|
+
return managed?.manage === action ? 'members' : null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The digest of an artifact's endpoint row, its delegation derived from the artifact. */
|
|
61
|
+
export function endpointDigest(artifact: Pick<ContractArtifact, 'dimensions'>, row: EndpointRow): string {
|
|
62
|
+
return requirementDigest({ ...row, delegation: endpointDelegation(artifact, row) })
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The digest of what the proxy applies to an endpoint: its kind, method, path, callers, requirement, admission and
|
|
67
|
+
* delegation, and an installation row's OAuth scopes.
|
|
68
|
+
* Caller, content-type and scope order carry no meaning, so they do not change it; input and output schemas are not
|
|
69
|
+
* part of it, because the proxy does not apply them.
|
|
70
|
+
*/
|
|
71
|
+
export function requirementDigest(rules: EndpointRules): string {
|
|
72
|
+
const callers = sortedCodeUnits(rules.callers)
|
|
73
|
+
const applied = {
|
|
74
|
+
kind: rules.kind,
|
|
75
|
+
method: rules.method,
|
|
76
|
+
path: rules.path,
|
|
77
|
+
callers,
|
|
78
|
+
requirement: rules.requirement,
|
|
79
|
+
admission: rules.admission === null ? null : canonicalAdmission(rules.admission),
|
|
80
|
+
delegation: rules.delegation,
|
|
81
|
+
}
|
|
82
|
+
// Only a row that admits `oauth` adds the key, so every application row keeps its digest byte for byte.
|
|
83
|
+
const oauth = 'oauth' in rules ? rules.oauth : null
|
|
84
|
+
return digestOf(canonicalJson(oauth === null ? applied : { ...applied, oauth: { scopes: sortedCodeUnits(oauth.scopes) } }))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function sortedCodeUnits(values: readonly string[]): string[] {
|
|
88
|
+
return [...values].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The digest of the whole artifact, over its one canonical serialization. */
|
|
92
|
+
export function contractDigest(artifact: ContractArtifact): string {
|
|
93
|
+
return digestOf(canonicalArtifactJson(artifact))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The digest of an installation service's table: each row's id and `requirementDigest`, and the reserved prefixes. A
|
|
98
|
+
* composition writes it beside the service from the table its proxy publishes, so the service refuses to start under
|
|
99
|
+
* another table (ADR-0022 "Installation entries"). Row and prefix order carry no meaning.
|
|
100
|
+
*/
|
|
101
|
+
export function installationTableDigest(table: InstallationEndpointTable): string {
|
|
102
|
+
const endpoints = table.endpoints.map((row) => `${row.id}@${requirementDigest({ ...row, delegation: null })}`)
|
|
103
|
+
return digestOf(canonicalJson({ endpoints: sortedCodeUnits(endpoints), reservedPrefixes: sortedCodeUnits(table.reservedPrefixes) }))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The digest of an export's description: what a consumer's vendored snapshot and its import name, so a schema that
|
|
108
|
+
* changed under the same version is told apart.
|
|
109
|
+
*/
|
|
110
|
+
export function exportDigest(description: Omit<ExportDescription, 'digest'>): string {
|
|
111
|
+
const procedures = Object.fromEntries(
|
|
112
|
+
Object.entries(description.procedures).map(([path, procedure]) => [path, {
|
|
113
|
+
input: procedure.input === null ? null : canonicalShape(procedure.input),
|
|
114
|
+
output: canonicalShape(procedure.output),
|
|
115
|
+
}]),
|
|
116
|
+
)
|
|
117
|
+
return digestOf(canonicalJson({
|
|
118
|
+
app: description.app,
|
|
119
|
+
export: description.export,
|
|
120
|
+
version: description.version,
|
|
121
|
+
audience: description.audience,
|
|
122
|
+
procedures,
|
|
123
|
+
}))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** `X-Fabrika-Endpoint`'s value: the endpoint id the proxy selected and the digest of the rules it applied. */
|
|
127
|
+
export function formatEndpointHeader(id: string, digest: string): string {
|
|
128
|
+
return `${id}@${digest}`
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Read `X-Fabrika-Endpoint`; `null` for anything that is not `<id>@sha256:<hex>`. */
|
|
132
|
+
export function parseEndpointHeader(value: string): { readonly id: string; readonly digest: string } | null {
|
|
133
|
+
const separator = value.lastIndexOf('@')
|
|
134
|
+
const id = value.slice(0, separator)
|
|
135
|
+
const digest = value.slice(separator + 1)
|
|
136
|
+
return separator <= 0 || !isDigest(digest) ? null : { id, digest }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function digestOf(json: string): string {
|
|
140
|
+
return `sha256:${sha256Hex(json)}`
|
|
141
|
+
}
|