@fabrika/auth-core 0.9.0-dev.4 → 0.9.0-dev.40
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 +3 -2
- package/src/canonical-path.ts +18 -0
- package/src/client-address.ts +74 -0
- package/src/contract-canonical.ts +168 -2
- package/src/contract-encoding.ts +181 -0
- package/src/contract.ts +212 -12
- package/src/dev-environment.ts +6 -0
- package/src/egress.ts +97 -0
- package/src/endpoint-digest.ts +22 -2
- package/src/framework-release.ts +72 -0
- package/src/framework-variables.ts +23 -0
- package/src/iam-mail.ts +354 -0
- package/src/index.ts +10 -0
- package/src/locale.ts +13 -1
- package/src/managed-environment.ts +147 -0
- package/src/membership.ts +12 -4
- package/src/oauth.ts +11 -0
- package/src/ops.ts +1933 -0
- package/src/permissions.ts +42 -0
- package/src/public-egress.ts +168 -0
- package/src/requirement.ts +5 -2
- package/src/rpc.ts +8 -8
- package/src/scope-values.ts +123 -0
- package/src/sha256.ts +52 -14
- package/src/terminal.ts +105 -0
- package/src/token.ts +170 -19
- package/src/types.ts +68 -2
- package/src/wire.ts +7 -0
- package/types/src/canonical-path.d.ts +6 -0
- package/types/src/client-address.d.ts +7 -0
- package/types/src/contract-canonical.d.ts +11 -1
- package/types/src/contract-encoding.d.ts +18 -0
- package/types/src/contract.d.ts +197 -11
- package/types/src/dev-environment.d.ts +5 -0
- package/types/src/egress.d.ts +27 -0
- package/types/src/endpoint-digest.d.ts +5 -0
- package/types/src/framework-release.d.ts +25 -0
- package/types/src/framework-variables.d.ts +12 -1
- package/types/src/iam-mail.d.ts +92 -0
- package/types/src/index.d.ts +10 -0
- package/types/src/locale.d.ts +11 -1
- package/types/src/managed-environment.d.ts +32 -0
- package/types/src/membership.d.ts +8 -3
- package/types/src/oauth.d.ts +7 -0
- package/types/src/ops.d.ts +657 -0
- package/types/src/permissions.d.ts +12 -0
- package/types/src/public-egress.d.ts +31 -0
- package/types/src/requirement.d.ts +2 -2
- package/types/src/rpc.d.ts +5 -5
- package/types/src/scope-values.d.ts +61 -0
- package/types/src/sha256.d.ts +10 -0
- package/types/src/terminal.d.ts +72 -0
- package/types/src/token.d.ts +69 -12
- package/types/src/types.d.ts +47 -2
- package/types/src/wire.d.ts +6 -0
package/src/token.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
2
|
+
* The IAM-issued token contract — the SECOND thing (after `IamRpc`) that must not drift
|
|
3
3
|
* between the Worker and the SDK.
|
|
4
4
|
*
|
|
5
5
|
* Background: today a request is authorized by the Worker resolving a Cloudflare Access JWT on
|
|
6
6
|
* every call over RPC (the CF token carries only identity, so authz needs a round-trip). When
|
|
7
|
-
*
|
|
7
|
+
* IAM issues its OWN token it can embed the already-resolved permission set, and the SDK
|
|
8
8
|
* authorizes LOCALLY — no per-request round-trip. This module is the wire shape of that token:
|
|
9
9
|
*
|
|
10
10
|
* - the Worker BUILDS claims here and signs them (`buildAccessClaims`);
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
import { numberField, prop, stringField } from './json'
|
|
26
26
|
import { isNormalizedMailbox } from './membership'
|
|
27
|
-
import {
|
|
27
|
+
import { isOAuthClientId, isOAuthResourceUri, parseOAuthScopes } from './oauth'
|
|
28
28
|
import type { ResolvedPrincipal } from './rpc'
|
|
29
29
|
import { isWorkloadKind, type PermissionEntry, type PermissionSource, type Scope, type TokenPrincipalType, type WorkloadKind } from './types'
|
|
30
30
|
import { isOwnWorkloadName } from './workloads'
|
|
@@ -47,7 +47,7 @@ export const FABRIKA_COOKIE_PREFIX = '__Host-px_'
|
|
|
47
47
|
* because those origins are potentially trustworthy.
|
|
48
48
|
*/
|
|
49
49
|
export const SESSION_COOKIE = `${FABRIKA_COOKIE_PREFIX}session`
|
|
50
|
-
/** Prefix marking
|
|
50
|
+
/** Prefix marking an IAM opaque credential (API key / share link), e.g. `Bearer px_<random>`. */
|
|
51
51
|
export const API_KEY_PREFIX = 'px_'
|
|
52
52
|
|
|
53
53
|
/**
|
|
@@ -56,7 +56,7 @@ export const API_KEY_PREFIX = 'px_'
|
|
|
56
56
|
*/
|
|
57
57
|
export const WORKLOAD_TOKEN_PATH = '/auth/workload/token'
|
|
58
58
|
|
|
59
|
-
/** Signing algorithm
|
|
59
|
+
/** Signing algorithm IAM issues with and the SDK verifies (ECDSA P-256 — compact, ubiquitous). */
|
|
60
60
|
export const TOKEN_ALG = 'ES256'
|
|
61
61
|
/** JWT `typ` of an OAuth resource access token (RFC 9068). Native app tokens carry no `typ`. */
|
|
62
62
|
export const RESOURCE_ACCESS_TOKEN_TYP = 'at+jwt'
|
|
@@ -70,22 +70,32 @@ export const DEFAULT_TOKEN_TTL_SECONDS = 300
|
|
|
70
70
|
export const TOKEN_REFRESH_SKEW_SECONDS = 30
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
* The largest access token IAM signs, in bytes (a JWT is ASCII)
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
73
|
+
* The largest access token IAM signs, in bytes (a JWT is ASCII), except an application token below. A client sends
|
|
74
|
+
* such a token itself (an OAuth access token, a console bearer), so it crosses Caddy's `forward_auth` into the proxy's
|
|
75
|
+
* auth service and reaches installation services, which run on Bun with its default: a request's headers at most
|
|
76
|
+
* 16 KiB in total. 8 KiB leaves the rest for the request's own headers and cookies.
|
|
77
77
|
*/
|
|
78
78
|
export const MAX_ACCESS_TOKEN_BYTES = 8192
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* The largest token IAM mints for the proxy to inject toward a contract application, in bytes. No client sends it; on
|
|
82
|
+
* that hop it travels twice (`X-Fabrika-Token`, and `Authorization` on a bearer request), so two copies and 16 KiB of
|
|
83
|
+
* the request's own headers fit the header limit the platform starts the application's Bun with.
|
|
84
|
+
*/
|
|
85
|
+
export const MAX_APPLICATION_TOKEN_BYTES = 24576
|
|
86
|
+
|
|
87
|
+
/** The request-header limit the platform starts an application's Bun server with (`--max-http-header-size`). */
|
|
88
|
+
export const APPLICATION_SERVER_MAX_HEADER_BYTES = 65536
|
|
89
|
+
|
|
80
90
|
// ── Claim shape (one, no discriminator) ────────────────────────────────────────
|
|
81
91
|
|
|
82
92
|
/**
|
|
83
|
-
* The
|
|
93
|
+
* The IAM access token. `perms` is always present (matched via `permits`); `ptype` is present
|
|
84
94
|
* only when `sub` is a principal. `label` is the audit actor — the principal's label when bound, the
|
|
85
95
|
* credential label otherwise (or null).
|
|
86
96
|
*/
|
|
87
97
|
export interface AccessTokenClaims {
|
|
88
|
-
/** Issuer —
|
|
98
|
+
/** Issuer — IAM's own origin (e.g. `https://iam.example.com`). */
|
|
89
99
|
iss: string
|
|
90
100
|
/** The app id this token is scoped to. The SDK REJECTS a token whose `aud` is not its app. */
|
|
91
101
|
aud: string
|
|
@@ -95,7 +105,10 @@ export interface AccessTokenClaims {
|
|
|
95
105
|
iat: number
|
|
96
106
|
/** Expiry (unix seconds). */
|
|
97
107
|
exp: number
|
|
98
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Resolved/effective permissions; `can(action, scope?)` = `permits(perms, action, scope)`. On the wire a compact token
|
|
110
|
+
* carries its global grants in `gperms` (`AccessTokenPayload`); the parser reads them back into this list.
|
|
111
|
+
*/
|
|
99
112
|
perms: PermissionEntry[]
|
|
100
113
|
/** Present only when the token is bound to a principal (`sub` is that principal). */
|
|
101
114
|
ptype?: TokenPrincipalType
|
|
@@ -117,12 +130,29 @@ export interface AccessTokenClaims {
|
|
|
117
130
|
* key carries what the key's own grants narrow, so a check that resolves the person's permissions requires this.
|
|
118
131
|
*/
|
|
119
132
|
session?: true
|
|
133
|
+
/**
|
|
134
|
+
* Present only on a session token of a person an application asserted at an enrolled terminal (ADR-0067), and
|
|
135
|
+
* always together with `terminal`.
|
|
136
|
+
*/
|
|
137
|
+
amr?: TerminalAuthenticationMethod
|
|
138
|
+
/** The terminal the person is signed in on; present exactly when `amr` is. */
|
|
139
|
+
terminal?: TerminalTokenClaims
|
|
120
140
|
/** Present only on an OAuth resource token (`aud` is then a resource URI, not an app id). */
|
|
121
141
|
oauth?: OAuthTokenClaims
|
|
122
142
|
/** Present only on a share-link token, which has no `ptype`: `sub` is then the link key's id. */
|
|
123
143
|
link?: LinkTokenClaims
|
|
124
144
|
}
|
|
125
145
|
|
|
146
|
+
/** The `amr` value of a session an application asserted at a terminal. */
|
|
147
|
+
export const TERMINAL_AUTHENTICATION_METHOD = 'terminal_assertion'
|
|
148
|
+
export type TerminalAuthenticationMethod = typeof TERMINAL_AUTHENTICATION_METHOD
|
|
149
|
+
|
|
150
|
+
/** The terminal a person's session was asserted on: its principal id and the label an administrator gave it. */
|
|
151
|
+
export interface TerminalTokenClaims {
|
|
152
|
+
id: string
|
|
153
|
+
label: string
|
|
154
|
+
}
|
|
155
|
+
|
|
126
156
|
/** What a share-link token adds: the link mount that issued the key and the chain the key is bound to. */
|
|
127
157
|
export interface LinkTokenClaims {
|
|
128
158
|
mount: string
|
|
@@ -153,6 +183,11 @@ export interface OAuthTokenClaims {
|
|
|
153
183
|
grantId: string
|
|
154
184
|
/** Unique token id, fresh on every mint. */
|
|
155
185
|
tokenId: string
|
|
186
|
+
/**
|
|
187
|
+
* The keys of the roles the person holds at an external application that declares roles; absent on every other
|
|
188
|
+
* token. The application decides what each role allows; IAM grants it and names it here.
|
|
189
|
+
*/
|
|
190
|
+
roles?: string[]
|
|
156
191
|
}
|
|
157
192
|
|
|
158
193
|
/** Which tokens a verifier accepts: native app tokens, or OAuth tokens minted for one resource of one app. */
|
|
@@ -162,7 +197,7 @@ export type TokenAudienceProfile = { kind: 'app'; app: string } | { kind: 'resou
|
|
|
162
197
|
//
|
|
163
198
|
// Transport is the IAM service binding (`getJwks()` RPC), NOT a public HTTPS URL: app↔IAM is
|
|
164
199
|
// RPC over the binding, which never traverses the Access edge — so the SDK reaches the key set
|
|
165
|
-
// even while
|
|
200
|
+
// even while IAM's own hostname is still gated by Cloudflare Access. The Worker also
|
|
166
201
|
// serves the standard `/.well-known/jwks.json`, for when Access is gone. Both emit this shape.
|
|
167
202
|
|
|
168
203
|
/** A published public signing key (EC P-256 / ES256). A jose-compatible JWK subset. */
|
|
@@ -184,7 +219,7 @@ export interface Jwks {
|
|
|
184
219
|
// ── Build (Worker side, pre-sign) ──────────────────────────────────────────────
|
|
185
220
|
|
|
186
221
|
export interface AccessTokenParams {
|
|
187
|
-
/** Issuer (
|
|
222
|
+
/** Issuer (IAM origin). */
|
|
188
223
|
iss: string
|
|
189
224
|
/** App id this token is for (becomes `aud`). */
|
|
190
225
|
app: string
|
|
@@ -200,6 +235,8 @@ export interface AccessTokenParams {
|
|
|
200
235
|
email?: string
|
|
201
236
|
/** Minted from a signed-in session; refused on a token without `type` or of a workload. */
|
|
202
237
|
session?: true
|
|
238
|
+
/** The terminal of a person's session asserted on one; sets `amr` too, and requires a user `type` and `session`. */
|
|
239
|
+
terminal?: TerminalTokenClaims
|
|
203
240
|
permissions: PermissionEntry[]
|
|
204
241
|
issuedAt: number
|
|
205
242
|
expiresAt: number
|
|
@@ -220,6 +257,10 @@ export interface ResourceAccessTokenParams {
|
|
|
220
257
|
scopes: string[]
|
|
221
258
|
grantId: string
|
|
222
259
|
tokenId: string
|
|
260
|
+
/** The person's normalized mailbox; an identity resource's token carries it. */
|
|
261
|
+
email?: string
|
|
262
|
+
/** The roles the person holds at an external application that declares roles (`OAuthTokenClaims.roles`). */
|
|
263
|
+
roles?: string[]
|
|
223
264
|
}
|
|
224
265
|
|
|
225
266
|
/**
|
|
@@ -242,6 +283,8 @@ export function buildResourceAccessClaims(params: ResourceAccessTokenParams): Re
|
|
|
242
283
|
scope: params.scopes.join(' '),
|
|
243
284
|
grant: params.grantId,
|
|
244
285
|
jti: params.tokenId,
|
|
286
|
+
...(params.email === undefined ? {} : { email: params.email }),
|
|
287
|
+
...(params.roles === undefined ? {} : { roles: params.roles }),
|
|
245
288
|
}
|
|
246
289
|
if (parseAccessClaims(payload)?.oauth === undefined) throw new Error('Invalid resource access token claims')
|
|
247
290
|
return payload
|
|
@@ -253,6 +296,47 @@ export function matchesTokenAudience(claims: AccessTokenClaims, profile: TokenAu
|
|
|
253
296
|
return claims.oauth !== undefined && claims.aud === profile.resource && claims.oauth.app === profile.app
|
|
254
297
|
}
|
|
255
298
|
|
|
299
|
+
/**
|
|
300
|
+
* How an access token's claims travel. `entries` writes every permission into `perms`. `compact` writes each global
|
|
301
|
+
* grant (scope null, source `grant`) into `gperms` as its bare action name and the rest into `perms`; it is a third of
|
|
302
|
+
* the size for a person holding a few hundred actions. `parseAccessClaims` reads both into the same `perms`.
|
|
303
|
+
*/
|
|
304
|
+
export type AccessTokenLayout = 'entries' | 'compact'
|
|
305
|
+
|
|
306
|
+
/** The signed payload: the claims, with the global grants of the compact layout in `gperms`. */
|
|
307
|
+
export interface AccessTokenPayload extends AccessTokenClaims {
|
|
308
|
+
/** The compact layout's global grants, by action name; absent in the entries layout and when there are none. */
|
|
309
|
+
gperms?: string[]
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The payload that carries `claims` in `layout`. */
|
|
313
|
+
export function accessTokenPayload(claims: AccessTokenClaims, layout: AccessTokenLayout): AccessTokenPayload {
|
|
314
|
+
const gperms = layout === 'compact' ? claims.perms.filter(isGlobalGrant).map((entry) => entry.action) : []
|
|
315
|
+
return gperms.length === 0 ? { ...claims } : { ...claims, perms: claims.perms.filter((entry) => !isGlobalGrant(entry)), gperms }
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Sign `claims` in the entries layout, or in the compact one when the entries layout is above `bound` bytes; the caller
|
|
320
|
+
* refuses a result still above it. A reader older than `gperms` reads a compact token without its global grants, which
|
|
321
|
+
* denies more and never allows more, and receives one only where the entries layout would have been refused anyway, so
|
|
322
|
+
* a platform release never changes a token an older framework release or a pinned proxy reads (ADR-0062).
|
|
323
|
+
*/
|
|
324
|
+
export async function signAccessClaims(
|
|
325
|
+
claims: AccessTokenClaims,
|
|
326
|
+
bound: number,
|
|
327
|
+
sign: (payload: AccessTokenPayload) => Promise<string>,
|
|
328
|
+
): Promise<string> {
|
|
329
|
+
const entries = await sign(accessTokenPayload(claims, 'entries'))
|
|
330
|
+
if (entries.length <= bound || !claims.perms.some(isGlobalGrant)) {
|
|
331
|
+
return entries
|
|
332
|
+
}
|
|
333
|
+
return sign(accessTokenPayload(claims, 'compact'))
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function isGlobalGrant(entry: PermissionEntry): boolean {
|
|
337
|
+
return entry.scope === null && entry.source === 'grant'
|
|
338
|
+
}
|
|
339
|
+
|
|
256
340
|
/** Assemble the claim object for an access token. Pure — the caller signs the result. */
|
|
257
341
|
export function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims {
|
|
258
342
|
const claims: AccessTokenClaims = {
|
|
@@ -284,6 +368,13 @@ export function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims
|
|
|
284
368
|
if (params.type === undefined || isWorkloadKind(params.type)) throw new Error("Only a person's or a service's token is minted from a session")
|
|
285
369
|
claims.session = params.session
|
|
286
370
|
}
|
|
371
|
+
if (params.terminal !== undefined) {
|
|
372
|
+
if (params.type !== 'user' || params.session !== true || !isTerminalClaims(params.terminal)) {
|
|
373
|
+
throw new Error("Only a person's session token names a terminal, and only by a non-empty id and label")
|
|
374
|
+
}
|
|
375
|
+
claims.amr = TERMINAL_AUTHENTICATION_METHOD
|
|
376
|
+
claims.terminal = { id: params.terminal.id, label: params.terminal.label }
|
|
377
|
+
}
|
|
287
378
|
if (params.link !== undefined) {
|
|
288
379
|
if (params.type !== undefined) throw new Error('A share-link token has no principal type')
|
|
289
380
|
claims.link = { mount: params.link.mount, chain: params.link.chain.map((scope) => ({ type: scope.type, value: scope.value })) }
|
|
@@ -304,15 +395,17 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
304
395
|
const sub = stringField(payload, 'sub')
|
|
305
396
|
const iat = numberField(payload, 'iat')
|
|
306
397
|
const exp = numberField(payload, 'exp')
|
|
307
|
-
const
|
|
398
|
+
const entries = parsePermissionEntries(prop(payload, 'perms'))
|
|
399
|
+
const globalGrants = parseGlobalGrants(prop(payload, 'gperms'))
|
|
308
400
|
const label = parseNullableString(prop(payload, 'label'))
|
|
309
401
|
const ptype = parseOptionalPrincipalType(prop(payload, 'ptype'))
|
|
310
402
|
if (iss === undefined || aud === undefined || sub === undefined || iat === undefined || exp === undefined) {
|
|
311
403
|
return null
|
|
312
404
|
}
|
|
313
|
-
if (
|
|
405
|
+
if (entries === null || globalGrants === null || label === undefined || ptype === undefined) {
|
|
314
406
|
return null
|
|
315
407
|
}
|
|
408
|
+
const perms = [...entries, ...globalGrants]
|
|
316
409
|
const oauth = parseOAuthClaims(payload)
|
|
317
410
|
if (oauth === undefined || (oauth !== null && (ptype !== 'user' || !isOAuthResourceUri(aud)))) {
|
|
318
411
|
return null
|
|
@@ -333,6 +426,10 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
333
426
|
if (session !== undefined && (session !== true || ptype === null || isWorkloadKind(ptype) || oauth !== null)) {
|
|
334
427
|
return null
|
|
335
428
|
}
|
|
429
|
+
const terminal = parseTerminalClaims(prop(payload, 'amr'), prop(payload, 'terminal'))
|
|
430
|
+
if (terminal === undefined || (terminal !== null && (ptype !== 'user' || session !== true || oauth !== null))) {
|
|
431
|
+
return null
|
|
432
|
+
}
|
|
336
433
|
const claims: AccessTokenClaims = { iss, aud, sub, iat, exp, perms, label }
|
|
337
434
|
if (ptype !== null) {
|
|
338
435
|
claims.ptype = ptype
|
|
@@ -346,6 +443,10 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
346
443
|
if (session === true) {
|
|
347
444
|
claims.session = true
|
|
348
445
|
}
|
|
446
|
+
if (terminal !== null) {
|
|
447
|
+
claims.amr = TERMINAL_AUTHENTICATION_METHOD
|
|
448
|
+
claims.terminal = terminal
|
|
449
|
+
}
|
|
349
450
|
if (oauth !== null) {
|
|
350
451
|
claims.oauth = oauth
|
|
351
452
|
}
|
|
@@ -379,6 +480,22 @@ function parseEmail(value: unknown, ptype: TokenPrincipalType | null): string |
|
|
|
379
480
|
return ptype === 'user' && typeof value === 'string' && isNormalizedMailbox(value) ? value : undefined
|
|
380
481
|
}
|
|
381
482
|
|
|
483
|
+
/** Both absent → null; `amr` naming the terminal method beside a well-formed `terminal` → it; anything else → undefined. */
|
|
484
|
+
function parseTerminalClaims(amr: unknown, value: unknown): TerminalTokenClaims | null | undefined {
|
|
485
|
+
if (amr === undefined && value === undefined) {
|
|
486
|
+
return null
|
|
487
|
+
}
|
|
488
|
+
if (amr !== TERMINAL_AUTHENTICATION_METHOD) {
|
|
489
|
+
return undefined
|
|
490
|
+
}
|
|
491
|
+
const terminal = { id: stringField(value, 'id'), label: stringField(value, 'label') }
|
|
492
|
+
return isTerminalClaims(terminal) ? { id: terminal.id, label: terminal.label } : undefined
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function isTerminalClaims(value: { id: string | undefined; label: string | undefined }): value is TerminalTokenClaims {
|
|
496
|
+
return value.id !== undefined && value.id !== '' && value.label !== undefined && value.label !== ''
|
|
497
|
+
}
|
|
498
|
+
|
|
382
499
|
/** Absent → null; a mount and a non-empty chain → the claims; anything else → undefined. */
|
|
383
500
|
function parseLinkClaims(value: unknown): LinkTokenClaims | null | undefined {
|
|
384
501
|
if (value === undefined) {
|
|
@@ -412,12 +529,28 @@ function parseOAuthClaims(payload: unknown): OAuthTokenClaims | null | undefined
|
|
|
412
529
|
}
|
|
413
530
|
const scopes = typeof scope === 'string' ? parseOAuthScopes(scope.split(' ')) : null
|
|
414
531
|
if (
|
|
415
|
-
typeof app !== 'string' || app === '' || !
|
|
532
|
+
typeof app !== 'string' || app === '' || !isOAuthClientId(clientId) || scopes === null || typeof grantId !== 'string' || grantId === ''
|
|
416
533
|
|| typeof tokenId !== 'string' || tokenId === ''
|
|
417
534
|
) {
|
|
418
535
|
return undefined
|
|
419
536
|
}
|
|
420
|
-
|
|
537
|
+
const roles = parseTokenRoles(prop(payload, 'roles'))
|
|
538
|
+
if (roles === undefined) return undefined
|
|
539
|
+
return roles === null ? { app, clientId, scopes, grantId, tokenId } : { app, clientId, scopes, grantId, tokenId, roles }
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const TOKEN_ROLE = /^[a-z0-9][a-z0-9-]{0,62}$/
|
|
543
|
+
|
|
544
|
+
/** Absent → null; distinct role keys → them; anything else → undefined. */
|
|
545
|
+
function parseTokenRoles(value: unknown): string[] | null | undefined {
|
|
546
|
+
if (value === undefined) return null
|
|
547
|
+
if (!Array.isArray(value) || value.length > 32) return undefined
|
|
548
|
+
const roles: string[] = []
|
|
549
|
+
for (const role of value) {
|
|
550
|
+
if (typeof role !== 'string' || !TOKEN_ROLE.test(role) || roles.includes(role)) return undefined
|
|
551
|
+
roles.push(role)
|
|
552
|
+
}
|
|
553
|
+
return roles
|
|
421
554
|
}
|
|
422
555
|
|
|
423
556
|
/**
|
|
@@ -470,7 +603,7 @@ function parseScope(value: unknown): Scope | null | undefined {
|
|
|
470
603
|
|
|
471
604
|
/** Validate `source` against the union — our own token, but read without `as`. */
|
|
472
605
|
function parseSource(value: unknown): PermissionSource | null {
|
|
473
|
-
if (value === 'grant' || value === 'bootstrap') {
|
|
606
|
+
if (value === 'grant' || value === 'bootstrap' || value === 'terminal') {
|
|
474
607
|
return value
|
|
475
608
|
}
|
|
476
609
|
if (typeof value === 'string' && value.startsWith('group:')) {
|
|
@@ -479,6 +612,24 @@ function parseSource(value: unknown): PermissionSource | null {
|
|
|
479
612
|
return null
|
|
480
613
|
}
|
|
481
614
|
|
|
615
|
+
/** The compact layout's `gperms`: absent → none; a non-empty array of non-empty action names → their global grants. */
|
|
616
|
+
function parseGlobalGrants(value: unknown): PermissionEntry[] | null {
|
|
617
|
+
if (value === undefined) {
|
|
618
|
+
return []
|
|
619
|
+
}
|
|
620
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
621
|
+
return null
|
|
622
|
+
}
|
|
623
|
+
const out: PermissionEntry[] = []
|
|
624
|
+
for (const action of value) {
|
|
625
|
+
if (typeof action !== 'string' || action === '') {
|
|
626
|
+
return null
|
|
627
|
+
}
|
|
628
|
+
out.push({ action, scope: null, source: 'grant' })
|
|
629
|
+
}
|
|
630
|
+
return out
|
|
631
|
+
}
|
|
632
|
+
|
|
482
633
|
function parsePermissionEntries(value: unknown): PermissionEntry[] | null {
|
|
483
634
|
if (!Array.isArray(value)) {
|
|
484
635
|
return null
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Locale } from './locale'
|
|
2
2
|
import type { MailTemplates } from './mail'
|
|
3
|
+
import type { DimensionValue, DimensionValueSource } from './scope-values'
|
|
3
4
|
|
|
4
5
|
export type PrincipalType = 'user' | 'service'
|
|
5
6
|
|
|
@@ -31,8 +32,9 @@ export function isWorkloadKind(value: unknown): value is WorkloadKind {
|
|
|
31
32
|
* - 'grant' — explicit `grants` row
|
|
32
33
|
* - 'bootstrap' — IAM_BOOTSTRAP_ADMINS env match (resolution-time only)
|
|
33
34
|
* - `group:${groupRef}` — IdP group → role mapping (the `<org>/<team>` ref)
|
|
35
|
+
* - 'terminal' — a grant of the terminal a person is signed in on (ADR-0067)
|
|
34
36
|
*/
|
|
35
|
-
export type PermissionSource = 'grant' | 'bootstrap' | `group:${string}`
|
|
37
|
+
export type PermissionSource = 'grant' | 'bootstrap' | 'terminal' | `group:${string}`
|
|
36
38
|
|
|
37
39
|
/**
|
|
38
40
|
* A flat, app-owned scope coordinate — one dimension (`type`) and an opaque value.
|
|
@@ -44,6 +46,57 @@ export interface Scope {
|
|
|
44
46
|
value: string
|
|
45
47
|
}
|
|
46
48
|
|
|
49
|
+
/**
|
|
50
|
+
* The pseudo-scope of a relation entry: `{ type: '~rel', value: <relation> }` means the action is held where a row of
|
|
51
|
+
* the application names the principal in that relation. No dimension may start with `~`, so no reader that compares
|
|
52
|
+
* scopes by `(type, value)` mistakes one for a dimension value, and none resolves it but the framework.
|
|
53
|
+
*/
|
|
54
|
+
export const RELATION_SCOPE_TYPE = '~rel'
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The pseudo-scope of a relation entry held inside one node: `{ type: '~relin', value: '<relation>:<dimension>:<node>' }`
|
|
58
|
+
* means the action is held where a row names the principal in that relation, on rows that lie inside `node` of
|
|
59
|
+
* `dimension`. IAM gives it a grant at that node of a role with `via`. Its own type, never `~rel`: a release that reads
|
|
60
|
+
* `~rel` as "the relation anywhere" ignores this one and reaches less.
|
|
61
|
+
*/
|
|
62
|
+
export const RELATION_INSIDE_SCOPE_TYPE = '~relin'
|
|
63
|
+
|
|
64
|
+
/** A relation held inside one node of a dimension, as a `~relin` entry names it. */
|
|
65
|
+
export interface RelationInside {
|
|
66
|
+
readonly relation: string
|
|
67
|
+
readonly dimension: string
|
|
68
|
+
readonly node: string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The `~relin` scope of `relation` inside `node` of `dimension`. A relation or dimension name never holds `:` (the
|
|
73
|
+
* contract's names do not), so the node, last, is read back verbatim, colons included; a name that holds one is refused.
|
|
74
|
+
*/
|
|
75
|
+
export function relationInsideScope(inside: RelationInside): Scope {
|
|
76
|
+
if (inside.relation === '' || inside.dimension === '' || inside.relation.includes(':') || inside.dimension.includes(':')) {
|
|
77
|
+
throw new Error('a relation held inside a node names a relation and a dimension without ":"')
|
|
78
|
+
}
|
|
79
|
+
return { type: RELATION_INSIDE_SCOPE_TYPE, value: `${inside.relation}:${inside.dimension}:${inside.node}` }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The relation, dimension and node of a `~relin` scope, or null for any other scope or a malformed value. */
|
|
83
|
+
export function parseRelationInside(scope: Scope): RelationInside | null {
|
|
84
|
+
if (scope.type !== RELATION_INSIDE_SCOPE_TYPE) {
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
const first = scope.value.indexOf(':')
|
|
88
|
+
const second = first === -1 ? -1 : scope.value.indexOf(':', first + 1)
|
|
89
|
+
if (first <= 0 || second <= first + 1 || second === scope.value.length - 1) {
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
return { relation: scope.value.slice(0, first), dimension: scope.value.slice(first + 1, second), node: scope.value.slice(second + 1) }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A scope type no dimension, grant or key may name: IAM derives a `~` entry itself and never stores one. */
|
|
96
|
+
export function isReservedScopeType(type: string): boolean {
|
|
97
|
+
return type.startsWith('~')
|
|
98
|
+
}
|
|
99
|
+
|
|
47
100
|
export interface PermissionEntry {
|
|
48
101
|
action: string
|
|
49
102
|
/** null = global / all scopes */
|
|
@@ -79,6 +132,13 @@ export interface RoleDef {
|
|
|
79
132
|
member?: { scope: string; assignableBy?: string[] }
|
|
80
133
|
/** An external principal may hold this membership role (ADR-0051). Only a membership role may declare it. */
|
|
81
134
|
external?: true
|
|
135
|
+
/**
|
|
136
|
+
* Relation → exact actions. A GLOBAL grant of this role also carries `{ action, scope: { type: '~rel', value:
|
|
137
|
+
* relation } }`: the application holds the action where one of its rows names the principal in that relation. A
|
|
138
|
+
* grant at a node carries `~relin` instead (`relationInsideScope`): the relation holds only inside that node. Only an
|
|
139
|
+
* internal role declares it.
|
|
140
|
+
*/
|
|
141
|
+
via?: Record<string, string[]>
|
|
82
142
|
}
|
|
83
143
|
|
|
84
144
|
/**
|
|
@@ -104,6 +164,10 @@ export interface AppScopeDef {
|
|
|
104
164
|
* answers a lookup only for one of them, held on the directory's value. Absent, no endpoint looks one up.
|
|
105
165
|
*/
|
|
106
166
|
lookups?: string[]
|
|
167
|
+
/** The values the console offers for a grant in this dimension (ADR-0085); absent, it takes free text. */
|
|
168
|
+
values?: DimensionValue[]
|
|
169
|
+
/** The procedure IAM asks for this dimension's values as the administrator (ADR-0085 §4); absent, only `values` count. */
|
|
170
|
+
valueSource?: DimensionValueSource
|
|
107
171
|
}
|
|
108
172
|
|
|
109
173
|
/**
|
|
@@ -146,7 +210,7 @@ export interface AppDelegationDef {
|
|
|
146
210
|
purpose: 'members' | 'creates'
|
|
147
211
|
}
|
|
148
212
|
|
|
149
|
-
/** An app's full authz vocabulary, declared in the app's code, reconciled into
|
|
213
|
+
/** An app's full authz vocabulary, declared in the app's code, reconciled into IAM. */
|
|
150
214
|
export interface AppSchema {
|
|
151
215
|
scopes: AppScopeDef[]
|
|
152
216
|
actions: AppActionDef[]
|
|
@@ -177,6 +241,8 @@ export interface AppSchema {
|
|
|
177
241
|
members?: { manage: string; list: string }
|
|
178
242
|
/** `labels`: a holder of the app's `members.list` also sees each member's label. Absent, labels stay with `members.manage`. */
|
|
179
243
|
directory?: 'labels'
|
|
244
|
+
/** The app's identification page for a person at an enrolled terminal (ADR-0067); absent, no terminal lists it. */
|
|
245
|
+
terminal?: { identify: string }
|
|
180
246
|
}
|
|
181
247
|
|
|
182
248
|
/**
|
package/src/wire.ts
CHANGED
|
@@ -63,6 +63,13 @@ export function proxySecretVariable(id: string): string {
|
|
|
63
63
|
return `${FABRIKA_PROXY_SECRET}_${sha256Hex(id).slice(0, 32).toUpperCase()}`
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
/**
|
|
67
|
+
* The variable holding the secret the platform proxy sends IAM's installation entry in `PROXY_SECRET_HEADER`. The proxy
|
|
68
|
+
* and IAM hold it under this one name. It proves only who wrote IAM's request-id header; IAM keys every request without
|
|
69
|
+
* it on one shared client bucket.
|
|
70
|
+
*/
|
|
71
|
+
export const IAM_PROXY_SECRET_VARIABLE = 'FABRIKA_INSTALLATION_PROXY_SECRET_IAM'
|
|
72
|
+
|
|
66
73
|
/**
|
|
67
74
|
* Every `X-Fabrika-*` name a proxy hop carries to an application. Caddy's first step and the Cloudflare proxy Worker
|
|
68
75
|
* delete each of them from the client's request before anything else, then set only what the decision asserts. A new
|
|
@@ -18,3 +18,9 @@ export declare function splitRequestTarget(target: string): RequestTarget;
|
|
|
18
18
|
* the string because `new URL()` resolves dot segments and so hides the very paths that must be refused.
|
|
19
19
|
*/
|
|
20
20
|
export declare function rawRequestTarget(url: string): RequestTarget;
|
|
21
|
+
/**
|
|
22
|
+
* The absolute URL of `path` on `origin`, or `null` when `path` is not a path of that origin: the one rule for a
|
|
23
|
+
* redirect or return target that must stay on its own origin. A leading `//` or any `\` would make a browser read a
|
|
24
|
+
* host from it, and a browser strips a tab or newline before parsing, so no control character is admitted.
|
|
25
|
+
*/
|
|
26
|
+
export declare function sameOriginTarget(origin: string, path: string): string | null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client a client-address header value counts as: an IPv4 address, or an IPv6 address's /64 (one subscriber's
|
|
3
|
+
* prefix, of which it may hold every address); an IPv4-mapped IPv6 address counts as its IPv4 address. Only the first
|
|
4
|
+
* value counts, so a caller that appends a second copy cannot change the key. `null` when the value is absent or no
|
|
5
|
+
* IP address.
|
|
6
|
+
*/
|
|
7
|
+
export declare function clientAddressKey(header: string | null): string | null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BodyAdmission, ContractArtifact, OutputShape, SchemaShape, TableActionArtifact } from './contract';
|
|
1
|
+
import type { BodyAdmission, ConditionArtifact, ContractArtifact, OutputShape, SchemaShape, TableActionArtifact } from './contract';
|
|
2
2
|
/**
|
|
3
3
|
* The artifact with every array whose order carries no meaning sorted: endpoints by id, and every list of names.
|
|
4
4
|
* Tuple items and union options keep their order, because it changes what a schema admits. Object keys are left
|
|
@@ -11,9 +11,19 @@ export declare function canonicalArtifactJson(artifact: ContractArtifact): strin
|
|
|
11
11
|
export declare function canonicalJson(value: unknown): string;
|
|
12
12
|
/** Each action of one table operation, `[]` for none: how a reader of the artifact reads `actions.read` and the others. */
|
|
13
13
|
export declare function tableActions(actions: TableActionArtifact | null): readonly string[];
|
|
14
|
+
/**
|
|
15
|
+
* A condition with its connectives' operands and its `in` / `notIn` lists sorted by their canonical JSON: AND and OR
|
|
16
|
+
* commute and a list is a set, so neither order changes which rows it admits.
|
|
17
|
+
*/
|
|
18
|
+
export declare function canonicalCondition(condition: ConditionArtifact): ConditionArtifact;
|
|
14
19
|
/** A body admission with its content types sorted. */
|
|
15
20
|
export declare function canonicalAdmission(admission: BodyAdmission): BodyAdmission;
|
|
16
21
|
/** An output shape: an attachment keeps its column order, which is the file's; each column's shape is canonical. */
|
|
17
22
|
export declare function canonicalOutput(shape: OutputShape): OutputShape;
|
|
18
23
|
/** A schema shape with its enum values sorted; tuple items and union options keep their order. */
|
|
19
24
|
export declare function canonicalShape(shape: SchemaShape): SchemaShape;
|
|
25
|
+
/**
|
|
26
|
+
* `canonicalJson(value)` written in pieces to `write`, so a digest of a large value never holds its whole text. Each
|
|
27
|
+
* object is rebuilt as `sortKeys` rebuilds it, so integer-like keys come first exactly as `JSON.stringify` orders them.
|
|
28
|
+
*/
|
|
29
|
+
export declare function writeCanonicalJson(value: unknown, write: (piece: string) => void): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ContractArtifact } from './contract';
|
|
2
|
+
/** The `encoding` of an encoded artifact; a plain artifact has no such key. */
|
|
3
|
+
export declare const SHARED_SHAPES_ENCODING = "shared-shapes";
|
|
4
|
+
/** In place of a shape: `shapes[index]` of the encoded artifact, which is always written before it is referenced. */
|
|
5
|
+
export interface SharedShapeReference {
|
|
6
|
+
readonly type: 'shared';
|
|
7
|
+
readonly index: number;
|
|
8
|
+
}
|
|
9
|
+
/** An artifact in the shared encoding, as JSON. */
|
|
10
|
+
export interface EncodedArtifact {
|
|
11
|
+
readonly encoding: typeof SHARED_SHAPES_ENCODING;
|
|
12
|
+
readonly shapes: readonly object[];
|
|
13
|
+
readonly artifact: object;
|
|
14
|
+
}
|
|
15
|
+
/** The canonical artifact in the shared encoding. */
|
|
16
|
+
export declare function encodeArtifact(artifact: ContractArtifact): EncodedArtifact;
|
|
17
|
+
/** The encoded artifact as text, keys sorted: what a build bundles. */
|
|
18
|
+
export declare function encodedArtifactJson(artifact: ContractArtifact): string;
|