@fabrika/auth-core 0.9.0-dev.5 → 0.9.0-dev.50
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/agent.ts +89 -0
- 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 +220 -17
- package/src/egress.ts +97 -0
- package/src/endpoint-digest.ts +22 -2
- package/src/framework-release.ts +70 -0
- package/src/framework-variables.ts +14 -0
- package/src/iam-mail.ts +354 -0
- package/src/index.ts +7 -0
- package/src/installation-endpoint.ts +3 -2
- package/src/locale.ts +13 -1
- package/src/managed-environment.ts +11 -0
- package/src/membership.ts +12 -4
- package/src/oauth.ts +19 -0
- package/src/ops.ts +487 -16
- package/src/permissions.ts +42 -0
- package/src/public-egress.ts +168 -0
- package/src/requirement.ts +12 -4
- package/src/rpc.ts +137 -9
- package/src/scope-values.ts +123 -0
- package/src/sha256.ts +52 -14
- package/src/token.ts +195 -14
- package/src/types.ts +72 -1
- package/src/wire.ts +2 -1
- package/types/src/agent.d.ts +37 -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 +205 -16
- package/types/src/egress.d.ts +27 -0
- package/types/src/endpoint-digest.d.ts +5 -0
- package/types/src/framework-release.d.ts +23 -0
- package/types/src/framework-variables.d.ts +6 -1
- package/types/src/iam-mail.d.ts +92 -0
- package/types/src/index.d.ts +7 -0
- package/types/src/installation-endpoint.d.ts +3 -2
- package/types/src/locale.d.ts +11 -1
- package/types/src/managed-environment.d.ts +1 -1
- package/types/src/membership.d.ts +8 -3
- package/types/src/oauth.d.ts +11 -0
- package/types/src/ops.d.ts +133 -8
- package/types/src/permissions.d.ts +12 -0
- package/types/src/public-egress.d.ts +31 -0
- package/types/src/requirement.d.ts +5 -4
- package/types/src/rpc.d.ts +113 -6
- package/types/src/scope-values.d.ts +61 -0
- package/types/src/sha256.d.ts +10 -0
- package/types/src/token.d.ts +70 -8
- package/types/src/types.d.ts +48 -1
- package/types/src/wire.d.ts +2 -1
package/src/sha256.ts
CHANGED
|
@@ -76,21 +76,60 @@ function rotateRight(value: number, bits: number): number {
|
|
|
76
76
|
|
|
77
77
|
/** The SHA-256 of `text`'s UTF-8 bytes, as 64 lower-case hex digits. */
|
|
78
78
|
export function sha256Hex(text: string): string {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
79
|
+
return new Sha256().update(text).hex()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* SHA-256 over text fed in pieces, so a digest of a large serialization never needs the whole text in memory. Each
|
|
84
|
+
* piece is whole text: a surrogate pair split across two pieces encodes as two replacement characters.
|
|
85
|
+
*/
|
|
86
|
+
export class Sha256 {
|
|
87
|
+
readonly #hash = new Uint32Array(INITIAL_HASH)
|
|
88
|
+
readonly #schedule = new Uint32Array(64)
|
|
89
|
+
readonly #block = new Uint8Array(64)
|
|
90
|
+
readonly #blockView = new DataView(this.#block.buffer)
|
|
91
|
+
readonly #encoder = new TextEncoder()
|
|
92
|
+
#filled = 0
|
|
93
|
+
#length = 0
|
|
94
|
+
|
|
95
|
+
update(text: string): this {
|
|
96
|
+
const bytes = this.#encoder.encode(text)
|
|
97
|
+
this.#length += bytes.length
|
|
98
|
+
let offset = 0
|
|
99
|
+
while (offset < bytes.length) {
|
|
100
|
+
const taken = Math.min(64 - this.#filled, bytes.length - offset)
|
|
101
|
+
this.#block.set(bytes.subarray(offset, offset + taken), this.#filled)
|
|
102
|
+
this.#filled += taken
|
|
103
|
+
offset += taken
|
|
104
|
+
if (this.#filled === 64) {
|
|
105
|
+
this.#compress()
|
|
106
|
+
this.#filled = 0
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return this
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The digest of everything fed so far; the hash takes no piece after it. */
|
|
113
|
+
hex(): string {
|
|
114
|
+
const bitLength = this.#length * 8
|
|
115
|
+
// The message, a 0x80 byte, zero padding and the 64-bit bit length fill whole 64-byte blocks.
|
|
116
|
+
this.#block[this.#filled] = 0x80
|
|
117
|
+
this.#block.fill(0, this.#filled + 1)
|
|
118
|
+
if (this.#filled + 1 > 56) {
|
|
119
|
+
this.#compress()
|
|
120
|
+
this.#block.fill(0)
|
|
121
|
+
}
|
|
122
|
+
this.#blockView.setUint32(56, Math.floor(bitLength / 0x1_0000_0000))
|
|
123
|
+
this.#blockView.setUint32(60, bitLength >>> 0)
|
|
124
|
+
this.#compress()
|
|
125
|
+
return Array.from(this.#hash, (word) => word.toString(16).padStart(8, '0')).join('')
|
|
126
|
+
}
|
|
88
127
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
128
|
+
#compress(): void {
|
|
129
|
+
const hash = this.#hash
|
|
130
|
+
const schedule = this.#schedule
|
|
92
131
|
for (let index = 0; index < 16; index++) {
|
|
93
|
-
schedule[index] =
|
|
132
|
+
schedule[index] = this.#blockView.getUint32(index * 4)
|
|
94
133
|
}
|
|
95
134
|
for (let index = 16; index < 64; index++) {
|
|
96
135
|
const early = schedule[index - 15] ?? 0
|
|
@@ -121,5 +160,4 @@ export function sha256Hex(text: string): string {
|
|
|
121
160
|
hash[index] = (hash[index] ?? 0) + value
|
|
122
161
|
}
|
|
123
162
|
}
|
|
124
|
-
return Array.from(hash, (word) => word.toString(16).padStart(8, '0')).join('')
|
|
125
163
|
}
|
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`);
|
|
@@ -22,9 +22,10 @@
|
|
|
22
22
|
* Pure: no jose, no I/O. Signing/verifying (key material) stays in the packages that own it.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
+
import { AGENT_CLIENT_ID, type AgentScope, agentScopeToken, liesWithinValue, parseAgentScope } from './agent'
|
|
25
26
|
import { numberField, prop, stringField } from './json'
|
|
26
27
|
import { isNormalizedMailbox } from './membership'
|
|
27
|
-
import {
|
|
28
|
+
import { isOAuthClientId, isOAuthResourceUri, parseOAuthScopes } from './oauth'
|
|
28
29
|
import type { ResolvedPrincipal } from './rpc'
|
|
29
30
|
import { isWorkloadKind, type PermissionEntry, type PermissionSource, type Scope, type TokenPrincipalType, type WorkloadKind } from './types'
|
|
30
31
|
import { isOwnWorkloadName } from './workloads'
|
|
@@ -47,7 +48,7 @@ export const FABRIKA_COOKIE_PREFIX = '__Host-px_'
|
|
|
47
48
|
* because those origins are potentially trustworthy.
|
|
48
49
|
*/
|
|
49
50
|
export const SESSION_COOKIE = `${FABRIKA_COOKIE_PREFIX}session`
|
|
50
|
-
/** Prefix marking
|
|
51
|
+
/** Prefix marking an IAM opaque credential (API key / share link), e.g. `Bearer px_<random>`. */
|
|
51
52
|
export const API_KEY_PREFIX = 'px_'
|
|
52
53
|
|
|
53
54
|
/**
|
|
@@ -56,7 +57,7 @@ export const API_KEY_PREFIX = 'px_'
|
|
|
56
57
|
*/
|
|
57
58
|
export const WORKLOAD_TOKEN_PATH = '/auth/workload/token'
|
|
58
59
|
|
|
59
|
-
/** Signing algorithm
|
|
60
|
+
/** Signing algorithm IAM issues with and the SDK verifies (ECDSA P-256 — compact, ubiquitous). */
|
|
60
61
|
export const TOKEN_ALG = 'ES256'
|
|
61
62
|
/** JWT `typ` of an OAuth resource access token (RFC 9068). Native app tokens carry no `typ`. */
|
|
62
63
|
export const RESOURCE_ACCESS_TOKEN_TYP = 'at+jwt'
|
|
@@ -90,12 +91,12 @@ export const APPLICATION_SERVER_MAX_HEADER_BYTES = 65536
|
|
|
90
91
|
// ── Claim shape (one, no discriminator) ────────────────────────────────────────
|
|
91
92
|
|
|
92
93
|
/**
|
|
93
|
-
* The
|
|
94
|
+
* The IAM access token. `perms` is always present (matched via `permits`); `ptype` is present
|
|
94
95
|
* only when `sub` is a principal. `label` is the audit actor — the principal's label when bound, the
|
|
95
96
|
* credential label otherwise (or null).
|
|
96
97
|
*/
|
|
97
98
|
export interface AccessTokenClaims {
|
|
98
|
-
/** Issuer —
|
|
99
|
+
/** Issuer — IAM's own origin (e.g. `https://iam.example.com`). */
|
|
99
100
|
iss: string
|
|
100
101
|
/** The app id this token is scoped to. The SDK REJECTS a token whose `aud` is not its app. */
|
|
101
102
|
aud: string
|
|
@@ -105,7 +106,10 @@ export interface AccessTokenClaims {
|
|
|
105
106
|
iat: number
|
|
106
107
|
/** Expiry (unix seconds). */
|
|
107
108
|
exp: number
|
|
108
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* Resolved/effective permissions; `can(action, scope?)` = `permits(perms, action, scope)`. On the wire a compact token
|
|
111
|
+
* carries its global grants in `gperms` (`AccessTokenPayload`); the parser reads them back into this list.
|
|
112
|
+
*/
|
|
109
113
|
perms: PermissionEntry[]
|
|
110
114
|
/** Present only when the token is bound to a principal (`sub` is that principal). */
|
|
111
115
|
ptype?: TokenPrincipalType
|
|
@@ -134,12 +138,39 @@ export interface AccessTokenClaims {
|
|
|
134
138
|
amr?: TerminalAuthenticationMethod
|
|
135
139
|
/** The terminal the person is signed in on; present exactly when `amr` is. */
|
|
136
140
|
terminal?: TerminalTokenClaims
|
|
141
|
+
/**
|
|
142
|
+
* Present only on a person's application token the proxy exchanged an agent's device grant for (ADR-0090), never on a
|
|
143
|
+
* session's: the agent acts as the person, under the grant's scope.
|
|
144
|
+
*/
|
|
145
|
+
act?: AgentActClaims
|
|
137
146
|
/** Present only on an OAuth resource token (`aud` is then a resource URI, not an app id). */
|
|
138
147
|
oauth?: OAuthTokenClaims
|
|
139
148
|
/** Present only on a share-link token, which has no `ptype`: `sub` is then the link key's id. */
|
|
140
149
|
link?: LinkTokenClaims
|
|
141
150
|
}
|
|
142
151
|
|
|
152
|
+
/**
|
|
153
|
+
* The agent a person's application token acts for: the device client, the grant the person approved, its parsed scope
|
|
154
|
+
* (ADR-0090 §2) and the label the agent gave itself, when it gave one.
|
|
155
|
+
*/
|
|
156
|
+
export interface AgentActClaims {
|
|
157
|
+
sub: typeof AGENT_CLIENT_ID
|
|
158
|
+
grant: string
|
|
159
|
+
scope: AgentScope
|
|
160
|
+
label?: string
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** `AgentActClaims` as the token carries them: the scope as its scope token (`agentScopeToken`). */
|
|
164
|
+
export interface AgentActPayload {
|
|
165
|
+
sub: typeof AGENT_CLIENT_ID
|
|
166
|
+
grant: string
|
|
167
|
+
scope: string
|
|
168
|
+
label?: string
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The longest grant id and agent label an `act` claim carries. */
|
|
172
|
+
export const MAX_AGENT_ACT_FIELD_LENGTH = 64
|
|
173
|
+
|
|
143
174
|
/** The `amr` value of a session an application asserted at a terminal. */
|
|
144
175
|
export const TERMINAL_AUTHENTICATION_METHOD = 'terminal_assertion'
|
|
145
176
|
export type TerminalAuthenticationMethod = typeof TERMINAL_AUTHENTICATION_METHOD
|
|
@@ -180,6 +211,11 @@ export interface OAuthTokenClaims {
|
|
|
180
211
|
grantId: string
|
|
181
212
|
/** Unique token id, fresh on every mint. */
|
|
182
213
|
tokenId: string
|
|
214
|
+
/**
|
|
215
|
+
* The keys of the roles the person holds at an external application that declares roles; absent on every other
|
|
216
|
+
* token. The application decides what each role allows; IAM grants it and names it here.
|
|
217
|
+
*/
|
|
218
|
+
roles?: string[]
|
|
183
219
|
}
|
|
184
220
|
|
|
185
221
|
/** Which tokens a verifier accepts: native app tokens, or OAuth tokens minted for one resource of one app. */
|
|
@@ -189,7 +225,7 @@ export type TokenAudienceProfile = { kind: 'app'; app: string } | { kind: 'resou
|
|
|
189
225
|
//
|
|
190
226
|
// Transport is the IAM service binding (`getJwks()` RPC), NOT a public HTTPS URL: app↔IAM is
|
|
191
227
|
// RPC over the binding, which never traverses the Access edge — so the SDK reaches the key set
|
|
192
|
-
// even while
|
|
228
|
+
// even while IAM's own hostname is still gated by Cloudflare Access. The Worker also
|
|
193
229
|
// serves the standard `/.well-known/jwks.json`, for when Access is gone. Both emit this shape.
|
|
194
230
|
|
|
195
231
|
/** A published public signing key (EC P-256 / ES256). A jose-compatible JWK subset. */
|
|
@@ -211,7 +247,7 @@ export interface Jwks {
|
|
|
211
247
|
// ── Build (Worker side, pre-sign) ──────────────────────────────────────────────
|
|
212
248
|
|
|
213
249
|
export interface AccessTokenParams {
|
|
214
|
-
/** Issuer (
|
|
250
|
+
/** Issuer (IAM origin). */
|
|
215
251
|
iss: string
|
|
216
252
|
/** App id this token is for (becomes `aud`). */
|
|
217
253
|
app: string
|
|
@@ -229,6 +265,8 @@ export interface AccessTokenParams {
|
|
|
229
265
|
session?: true
|
|
230
266
|
/** The terminal of a person's session asserted on one; sets `amr` too, and requires a user `type` and `session`. */
|
|
231
267
|
terminal?: TerminalTokenClaims
|
|
268
|
+
/** The agent a person's exchanged token acts for; requires a user `type`, no `session` and no `terminal`. */
|
|
269
|
+
act?: AgentActClaims
|
|
232
270
|
permissions: PermissionEntry[]
|
|
233
271
|
issuedAt: number
|
|
234
272
|
expiresAt: number
|
|
@@ -249,6 +287,10 @@ export interface ResourceAccessTokenParams {
|
|
|
249
287
|
scopes: string[]
|
|
250
288
|
grantId: string
|
|
251
289
|
tokenId: string
|
|
290
|
+
/** The person's normalized mailbox; an identity resource's token carries it. */
|
|
291
|
+
email?: string
|
|
292
|
+
/** The roles the person holds at an external application that declares roles (`OAuthTokenClaims.roles`). */
|
|
293
|
+
roles?: string[]
|
|
252
294
|
}
|
|
253
295
|
|
|
254
296
|
/**
|
|
@@ -271,6 +313,8 @@ export function buildResourceAccessClaims(params: ResourceAccessTokenParams): Re
|
|
|
271
313
|
scope: params.scopes.join(' '),
|
|
272
314
|
grant: params.grantId,
|
|
273
315
|
jti: params.tokenId,
|
|
316
|
+
...(params.email === undefined ? {} : { email: params.email }),
|
|
317
|
+
...(params.roles === undefined ? {} : { roles: params.roles }),
|
|
274
318
|
}
|
|
275
319
|
if (parseAccessClaims(payload)?.oauth === undefined) throw new Error('Invalid resource access token claims')
|
|
276
320
|
return payload
|
|
@@ -282,6 +326,55 @@ export function matchesTokenAudience(claims: AccessTokenClaims, profile: TokenAu
|
|
|
282
326
|
return claims.oauth !== undefined && claims.aud === profile.resource && claims.oauth.app === profile.app
|
|
283
327
|
}
|
|
284
328
|
|
|
329
|
+
/**
|
|
330
|
+
* How an access token's claims travel. `entries` writes every permission into `perms`. `compact` writes each global
|
|
331
|
+
* grant (scope null, source `grant`) into `gperms` as its bare action name and the rest into `perms`; it is a third of
|
|
332
|
+
* the size for a person holding a few hundred actions. `parseAccessClaims` reads both into the same `perms`.
|
|
333
|
+
*/
|
|
334
|
+
export type AccessTokenLayout = 'entries' | 'compact'
|
|
335
|
+
|
|
336
|
+
/** The signed payload: the claims, with the global grants of the compact layout in `gperms`. */
|
|
337
|
+
export interface AccessTokenPayload extends Omit<AccessTokenClaims, 'act'> {
|
|
338
|
+
act?: AgentActPayload
|
|
339
|
+
/** The compact layout's global grants, by action name; absent in the entries layout and when there are none. */
|
|
340
|
+
gperms?: string[]
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The payload that carries `claims` in `layout`. */
|
|
344
|
+
export function accessTokenPayload(claims: AccessTokenClaims, layout: AccessTokenLayout): AccessTokenPayload {
|
|
345
|
+
const { act, ...rest } = claims
|
|
346
|
+
const payload: AccessTokenPayload = act === undefined ? rest : { ...rest, act: agentActPayload(act) }
|
|
347
|
+
const gperms = layout === 'compact' ? claims.perms.filter(isGlobalGrant).map((entry) => entry.action) : []
|
|
348
|
+
return gperms.length === 0 ? payload : { ...payload, perms: claims.perms.filter((entry) => !isGlobalGrant(entry)), gperms }
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function agentActPayload(act: AgentActClaims): AgentActPayload {
|
|
352
|
+
const scope = agentScopeToken(act.scope)
|
|
353
|
+
return act.label === undefined ? { sub: act.sub, grant: act.grant, scope } : { sub: act.sub, grant: act.grant, scope, label: act.label }
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Sign `claims` in the entries layout, or in the compact one when the entries layout is above `bound` bytes; the caller
|
|
358
|
+
* refuses a result still above it. A reader older than `gperms` reads a compact token without its global grants, which
|
|
359
|
+
* denies more and never allows more, and receives one only where the entries layout would have been refused anyway, so
|
|
360
|
+
* a platform release never changes a token an older framework release or a pinned proxy reads (ADR-0062).
|
|
361
|
+
*/
|
|
362
|
+
export async function signAccessClaims(
|
|
363
|
+
claims: AccessTokenClaims,
|
|
364
|
+
bound: number,
|
|
365
|
+
sign: (payload: AccessTokenPayload) => Promise<string>,
|
|
366
|
+
): Promise<string> {
|
|
367
|
+
const entries = await sign(accessTokenPayload(claims, 'entries'))
|
|
368
|
+
if (entries.length <= bound || !claims.perms.some(isGlobalGrant)) {
|
|
369
|
+
return entries
|
|
370
|
+
}
|
|
371
|
+
return sign(accessTokenPayload(claims, 'compact'))
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function isGlobalGrant(entry: PermissionEntry): boolean {
|
|
375
|
+
return entry.scope === null && entry.source === 'grant'
|
|
376
|
+
}
|
|
377
|
+
|
|
285
378
|
/** Assemble the claim object for an access token. Pure — the caller signs the result. */
|
|
286
379
|
export function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims {
|
|
287
380
|
const claims: AccessTokenClaims = {
|
|
@@ -320,6 +413,13 @@ export function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims
|
|
|
320
413
|
claims.amr = TERMINAL_AUTHENTICATION_METHOD
|
|
321
414
|
claims.terminal = { id: params.terminal.id, label: params.terminal.label }
|
|
322
415
|
}
|
|
416
|
+
if (params.act !== undefined) {
|
|
417
|
+
const act = parseActClaims(agentActPayload(params.act), params.permissions)
|
|
418
|
+
if (params.type !== 'user' || params.session !== undefined || params.terminal !== undefined || act === null || act === undefined) {
|
|
419
|
+
throw new Error("Only a person's token outside a session acts for an agent, under a scope its permissions lie within")
|
|
420
|
+
}
|
|
421
|
+
claims.act = act
|
|
422
|
+
}
|
|
323
423
|
if (params.link !== undefined) {
|
|
324
424
|
if (params.type !== undefined) throw new Error('A share-link token has no principal type')
|
|
325
425
|
claims.link = { mount: params.link.mount, chain: params.link.chain.map((scope) => ({ type: scope.type, value: scope.value })) }
|
|
@@ -340,15 +440,17 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
340
440
|
const sub = stringField(payload, 'sub')
|
|
341
441
|
const iat = numberField(payload, 'iat')
|
|
342
442
|
const exp = numberField(payload, 'exp')
|
|
343
|
-
const
|
|
443
|
+
const entries = parsePermissionEntries(prop(payload, 'perms'))
|
|
444
|
+
const globalGrants = parseGlobalGrants(prop(payload, 'gperms'))
|
|
344
445
|
const label = parseNullableString(prop(payload, 'label'))
|
|
345
446
|
const ptype = parseOptionalPrincipalType(prop(payload, 'ptype'))
|
|
346
447
|
if (iss === undefined || aud === undefined || sub === undefined || iat === undefined || exp === undefined) {
|
|
347
448
|
return null
|
|
348
449
|
}
|
|
349
|
-
if (
|
|
450
|
+
if (entries === null || globalGrants === null || label === undefined || ptype === undefined) {
|
|
350
451
|
return null
|
|
351
452
|
}
|
|
453
|
+
const perms = [...entries, ...globalGrants]
|
|
352
454
|
const oauth = parseOAuthClaims(payload)
|
|
353
455
|
if (oauth === undefined || (oauth !== null && (ptype !== 'user' || !isOAuthResourceUri(aud)))) {
|
|
354
456
|
return null
|
|
@@ -373,6 +475,10 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
373
475
|
if (terminal === undefined || (terminal !== null && (ptype !== 'user' || session !== true || oauth !== null))) {
|
|
374
476
|
return null
|
|
375
477
|
}
|
|
478
|
+
const act = parseActClaims(prop(payload, 'act'), perms)
|
|
479
|
+
if (act === undefined || (act !== null && (ptype !== 'user' || session !== undefined || terminal !== null || oauth !== null))) {
|
|
480
|
+
return null
|
|
481
|
+
}
|
|
376
482
|
const claims: AccessTokenClaims = { iss, aud, sub, iat, exp, perms, label }
|
|
377
483
|
if (ptype !== null) {
|
|
378
484
|
claims.ptype = ptype
|
|
@@ -390,6 +496,9 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
390
496
|
claims.amr = TERMINAL_AUTHENTICATION_METHOD
|
|
391
497
|
claims.terminal = terminal
|
|
392
498
|
}
|
|
499
|
+
if (act !== null) {
|
|
500
|
+
claims.act = act
|
|
501
|
+
}
|
|
393
502
|
if (oauth !== null) {
|
|
394
503
|
claims.oauth = oauth
|
|
395
504
|
}
|
|
@@ -399,6 +508,44 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
399
508
|
return claims
|
|
400
509
|
}
|
|
401
510
|
|
|
511
|
+
/**
|
|
512
|
+
* Absent → null; the agent client, a grant id, a scope token and an optional label, with every entry of a narrowed
|
|
513
|
+
* scope inside its value → the claims; anything else → undefined. The narrowing is IAM's at the exchange; reading it
|
|
514
|
+
* again here keeps a token that carries an entry outside its value from reaching any reader.
|
|
515
|
+
*/
|
|
516
|
+
function parseActClaims(value: unknown, perms: readonly PermissionEntry[]): AgentActClaims | null | undefined {
|
|
517
|
+
if (value === undefined) {
|
|
518
|
+
return null
|
|
519
|
+
}
|
|
520
|
+
const client = stringField(value, 'sub')
|
|
521
|
+
const grant = stringField(value, 'grant')
|
|
522
|
+
const token = stringField(value, 'scope')
|
|
523
|
+
const label = prop(value, 'label')
|
|
524
|
+
if (client !== AGENT_CLIENT_ID || grant === undefined || token === undefined || !isActField(grant)) {
|
|
525
|
+
return undefined
|
|
526
|
+
}
|
|
527
|
+
if (label !== undefined && (typeof label !== 'string' || !isAgentLabel(label))) {
|
|
528
|
+
return undefined
|
|
529
|
+
}
|
|
530
|
+
const scope = parseAgentScope(token)
|
|
531
|
+
if (scope === null || (scope.within !== null && !liesWithinValue(perms, scope.within))) {
|
|
532
|
+
return undefined
|
|
533
|
+
}
|
|
534
|
+
return label === undefined ? { sub: AGENT_CLIENT_ID, grant, scope } : { sub: AGENT_CLIENT_ID, grant, scope, label }
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function isActField(value: string): boolean {
|
|
538
|
+
return value !== '' && value.trim() === value && value.length <= MAX_AGENT_ACT_FIELD_LENGTH
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** The name an agent gives itself (`agent_label`): not blank, at most 64 characters, no control character. */
|
|
542
|
+
export function isAgentLabel(value: string): boolean {
|
|
543
|
+
return isActField(value) && [...value].every((char) => {
|
|
544
|
+
const code = char.codePointAt(0) ?? 0
|
|
545
|
+
return code > 0x1f && (code < 0x7f || code > 0x9f)
|
|
546
|
+
})
|
|
547
|
+
}
|
|
548
|
+
|
|
402
549
|
/** A job, webhook or mount name, or an application id. */
|
|
403
550
|
const WORKLOAD_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/
|
|
404
551
|
|
|
@@ -472,12 +619,28 @@ function parseOAuthClaims(payload: unknown): OAuthTokenClaims | null | undefined
|
|
|
472
619
|
}
|
|
473
620
|
const scopes = typeof scope === 'string' ? parseOAuthScopes(scope.split(' ')) : null
|
|
474
621
|
if (
|
|
475
|
-
typeof app !== 'string' || app === '' || !
|
|
622
|
+
typeof app !== 'string' || app === '' || !isOAuthClientId(clientId) || scopes === null || typeof grantId !== 'string' || grantId === ''
|
|
476
623
|
|| typeof tokenId !== 'string' || tokenId === ''
|
|
477
624
|
) {
|
|
478
625
|
return undefined
|
|
479
626
|
}
|
|
480
|
-
|
|
627
|
+
const roles = parseTokenRoles(prop(payload, 'roles'))
|
|
628
|
+
if (roles === undefined) return undefined
|
|
629
|
+
return roles === null ? { app, clientId, scopes, grantId, tokenId } : { app, clientId, scopes, grantId, tokenId, roles }
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const TOKEN_ROLE = /^[a-z0-9][a-z0-9-]{0,62}$/
|
|
633
|
+
|
|
634
|
+
/** Absent → null; distinct role keys → them; anything else → undefined. */
|
|
635
|
+
function parseTokenRoles(value: unknown): string[] | null | undefined {
|
|
636
|
+
if (value === undefined) return null
|
|
637
|
+
if (!Array.isArray(value) || value.length > 32) return undefined
|
|
638
|
+
const roles: string[] = []
|
|
639
|
+
for (const role of value) {
|
|
640
|
+
if (typeof role !== 'string' || !TOKEN_ROLE.test(role) || roles.includes(role)) return undefined
|
|
641
|
+
roles.push(role)
|
|
642
|
+
}
|
|
643
|
+
return roles
|
|
481
644
|
}
|
|
482
645
|
|
|
483
646
|
/**
|
|
@@ -539,6 +702,24 @@ function parseSource(value: unknown): PermissionSource | null {
|
|
|
539
702
|
return null
|
|
540
703
|
}
|
|
541
704
|
|
|
705
|
+
/** The compact layout's `gperms`: absent → none; a non-empty array of non-empty action names → their global grants. */
|
|
706
|
+
function parseGlobalGrants(value: unknown): PermissionEntry[] | null {
|
|
707
|
+
if (value === undefined) {
|
|
708
|
+
return []
|
|
709
|
+
}
|
|
710
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
711
|
+
return null
|
|
712
|
+
}
|
|
713
|
+
const out: PermissionEntry[] = []
|
|
714
|
+
for (const action of value) {
|
|
715
|
+
if (typeof action !== 'string' || action === '') {
|
|
716
|
+
return null
|
|
717
|
+
}
|
|
718
|
+
out.push({ action, scope: null, source: 'grant' })
|
|
719
|
+
}
|
|
720
|
+
return out
|
|
721
|
+
}
|
|
722
|
+
|
|
542
723
|
function parsePermissionEntries(value: unknown): PermissionEntry[] | null {
|
|
543
724
|
if (!Array.isArray(value)) {
|
|
544
725
|
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
|
|
|
@@ -45,6 +46,60 @@ export interface Scope {
|
|
|
45
46
|
value: string
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
/** A dimension's name as a contract declares it; also the dimension of a scope an agent's grant is narrowed to. */
|
|
50
|
+
export const DIMENSION_NAME = /^[a-z][A-Za-z0-9_]*$/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The pseudo-scope of a relation entry: `{ type: '~rel', value: <relation> }` means the action is held where a row of
|
|
54
|
+
* the application names the principal in that relation. No dimension may start with `~`, so no reader that compares
|
|
55
|
+
* scopes by `(type, value)` mistakes one for a dimension value, and none resolves it but the framework.
|
|
56
|
+
*/
|
|
57
|
+
export const RELATION_SCOPE_TYPE = '~rel'
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The pseudo-scope of a relation entry held inside one node: `{ type: '~relin', value: '<relation>:<dimension>:<node>' }`
|
|
61
|
+
* means the action is held where a row names the principal in that relation, on rows that lie inside `node` of
|
|
62
|
+
* `dimension`. IAM gives it a grant at that node of a role with `via`. Its own type, never `~rel`: a release that reads
|
|
63
|
+
* `~rel` as "the relation anywhere" ignores this one and reaches less.
|
|
64
|
+
*/
|
|
65
|
+
export const RELATION_INSIDE_SCOPE_TYPE = '~relin'
|
|
66
|
+
|
|
67
|
+
/** A relation held inside one node of a dimension, as a `~relin` entry names it. */
|
|
68
|
+
export interface RelationInside {
|
|
69
|
+
readonly relation: string
|
|
70
|
+
readonly dimension: string
|
|
71
|
+
readonly node: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The `~relin` scope of `relation` inside `node` of `dimension`. A relation or dimension name never holds `:` (the
|
|
76
|
+
* contract's names do not), so the node, last, is read back verbatim, colons included; a name that holds one is refused.
|
|
77
|
+
*/
|
|
78
|
+
export function relationInsideScope(inside: RelationInside): Scope {
|
|
79
|
+
if (inside.relation === '' || inside.dimension === '' || inside.relation.includes(':') || inside.dimension.includes(':')) {
|
|
80
|
+
throw new Error('a relation held inside a node names a relation and a dimension without ":"')
|
|
81
|
+
}
|
|
82
|
+
return { type: RELATION_INSIDE_SCOPE_TYPE, value: `${inside.relation}:${inside.dimension}:${inside.node}` }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The relation, dimension and node of a `~relin` scope, or null for any other scope or a malformed value. */
|
|
86
|
+
export function parseRelationInside(scope: Scope): RelationInside | null {
|
|
87
|
+
if (scope.type !== RELATION_INSIDE_SCOPE_TYPE) {
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
const first = scope.value.indexOf(':')
|
|
91
|
+
const second = first === -1 ? -1 : scope.value.indexOf(':', first + 1)
|
|
92
|
+
if (first <= 0 || second <= first + 1 || second === scope.value.length - 1) {
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
return { relation: scope.value.slice(0, first), dimension: scope.value.slice(first + 1, second), node: scope.value.slice(second + 1) }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A scope type no dimension, grant or key may name: IAM derives a `~` entry itself and never stores one. */
|
|
99
|
+
export function isReservedScopeType(type: string): boolean {
|
|
100
|
+
return type.startsWith('~')
|
|
101
|
+
}
|
|
102
|
+
|
|
48
103
|
export interface PermissionEntry {
|
|
49
104
|
action: string
|
|
50
105
|
/** null = global / all scopes */
|
|
@@ -80,6 +135,13 @@ export interface RoleDef {
|
|
|
80
135
|
member?: { scope: string; assignableBy?: string[] }
|
|
81
136
|
/** An external principal may hold this membership role (ADR-0051). Only a membership role may declare it. */
|
|
82
137
|
external?: true
|
|
138
|
+
/**
|
|
139
|
+
* Relation → exact actions. A GLOBAL grant of this role also carries `{ action, scope: { type: '~rel', value:
|
|
140
|
+
* relation } }`: the application holds the action where one of its rows names the principal in that relation. A
|
|
141
|
+
* grant at a node carries `~relin` instead (`relationInsideScope`): the relation holds only inside that node. Only an
|
|
142
|
+
* internal role declares it.
|
|
143
|
+
*/
|
|
144
|
+
via?: Record<string, string[]>
|
|
83
145
|
}
|
|
84
146
|
|
|
85
147
|
/**
|
|
@@ -105,6 +167,10 @@ export interface AppScopeDef {
|
|
|
105
167
|
* answers a lookup only for one of them, held on the directory's value. Absent, no endpoint looks one up.
|
|
106
168
|
*/
|
|
107
169
|
lookups?: string[]
|
|
170
|
+
/** The values the console offers for a grant in this dimension (ADR-0085); absent, it takes free text. */
|
|
171
|
+
values?: DimensionValue[]
|
|
172
|
+
/** The procedure IAM asks for this dimension's values as the administrator (ADR-0085 §4); absent, only `values` count. */
|
|
173
|
+
valueSource?: DimensionValueSource
|
|
108
174
|
}
|
|
109
175
|
|
|
110
176
|
/**
|
|
@@ -147,7 +213,7 @@ export interface AppDelegationDef {
|
|
|
147
213
|
purpose: 'members' | 'creates'
|
|
148
214
|
}
|
|
149
215
|
|
|
150
|
-
/** An app's full authz vocabulary, declared in the app's code, reconciled into
|
|
216
|
+
/** An app's full authz vocabulary, declared in the app's code, reconciled into IAM. */
|
|
151
217
|
export interface AppSchema {
|
|
152
218
|
scopes: AppScopeDef[]
|
|
153
219
|
actions: AppActionDef[]
|
|
@@ -180,6 +246,11 @@ export interface AppSchema {
|
|
|
180
246
|
directory?: 'labels'
|
|
181
247
|
/** The app's identification page for a person at an enrolled terminal (ADR-0067); absent, no terminal lists it. */
|
|
182
248
|
terminal?: { identify: string }
|
|
249
|
+
/**
|
|
250
|
+
* A group of the contract admits the caller `agent` (ADR-0090): the environment's origin is then an OAuth resource of
|
|
251
|
+
* the agent client. Absent or false, IAM serves the environment to no agent.
|
|
252
|
+
*/
|
|
253
|
+
agentCallers?: boolean
|
|
183
254
|
}
|
|
184
255
|
|
|
185
256
|
/**
|
package/src/wire.ts
CHANGED
|
@@ -65,7 +65,8 @@ export function proxySecretVariable(id: string): string {
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
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
|
|
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.
|
|
69
70
|
*/
|
|
70
71
|
export const IAM_PROXY_SECRET_VARIABLE = 'FABRIKA_INSTALLATION_PROXY_SECRET_IAM'
|
|
71
72
|
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CredentialedEndpointKind } from './contract';
|
|
2
|
+
import { type PermissionEntry, type Scope } from './types';
|
|
3
|
+
export { AGENT_CLIENT_ID } from './oauth';
|
|
4
|
+
/** The caller a person's application token presents when the proxy exchanged an agent's grant for it. */
|
|
5
|
+
export declare const AGENT_CALLER = "agent";
|
|
6
|
+
export declare const AGENT_SCOPE_MODES: readonly ["read", "write"];
|
|
7
|
+
export type AgentScopeMode = (typeof AGENT_SCOPE_MODES)[number];
|
|
8
|
+
/** The mode a grant gets when the client asks for no scope. */
|
|
9
|
+
export declare const DEFAULT_AGENT_SCOPE_MODE: AgentScopeMode;
|
|
10
|
+
/** The endpoint kinds each mode admits. Neither admits an SPA: an agent calls procedures. */
|
|
11
|
+
export declare const AGENT_SCOPE_KINDS: Readonly<Record<AgentScopeMode, readonly CredentialedEndpointKind[]>>;
|
|
12
|
+
/** The scope one grant holds: its mode, and the one scope value it is narrowed to, or null for none. */
|
|
13
|
+
export interface AgentScope {
|
|
14
|
+
readonly mode: AgentScopeMode;
|
|
15
|
+
readonly within: Scope | null;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Read one scope token: `read`, `write`, or either narrowed to one value of a dimension, `read:<dimension>=<value>`.
|
|
19
|
+
* The dimension is a contract's dimension name; the value is opaque text of a parameterized scope
|
|
20
|
+
* (`isScopeParameterText`). Anything else is null.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseAgentScope(token: string): AgentScope | null;
|
|
23
|
+
/** The scope token of `scope`; `parseAgentScope` reads it back to the same scope. */
|
|
24
|
+
export declare function agentScopeToken(scope: AgentScope): string;
|
|
25
|
+
export declare function isAgentScopeMode(value: string): value is AgentScopeMode;
|
|
26
|
+
/** Whether a grant of `mode` reaches an endpoint of `kind`. */
|
|
27
|
+
export declare function agentScopeAdmits(mode: AgentScopeMode, kind: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* The person's entries narrowed to the one scope value `within` (ADR-0090 §3): `narrowPermissions` under `*` at `within`
|
|
30
|
+
* (a global entry becomes the same action there, an entry at `within` stays, every other scope is dropped), plus the
|
|
31
|
+
* relations the person holds inside `within`. A relation held anywhere (`~rel`) is dropped: an access view bound to
|
|
32
|
+
* its member reaches rows for a node that a hold anywhere does not reach (ADR-0076), so moving it inside `within` would
|
|
33
|
+
* widen it. Never adds an action or a reach the person does not hold.
|
|
34
|
+
*/
|
|
35
|
+
export declare function narrowToValue(entries: readonly PermissionEntry[], within: Scope): PermissionEntry[];
|
|
36
|
+
/** Whether every entry lies within `within`, as `narrowToValue` leaves them: a check a token's reader repeats. */
|
|
37
|
+
export declare function liesWithinValue(entries: readonly PermissionEntry[], within: Scope): boolean;
|
|
@@ -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;
|