@fabrika/auth-core 0.9.0-dev.45 → 0.9.0-dev.47
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/agent.ts +89 -0
- package/src/contract.ts +6 -3
- package/src/index.ts +1 -0
- package/src/installation-endpoint.ts +3 -2
- package/src/oauth.ts +10 -2
- package/src/requirement.ts +7 -2
- package/src/rpc.ts +29 -0
- package/src/token.ts +92 -2
- package/src/types.ts +8 -0
- package/types/src/agent.d.ts +37 -0
- package/types/src/contract.d.ts +6 -3
- package/types/src/index.d.ts +1 -0
- package/types/src/installation-endpoint.d.ts +3 -2
- package/types/src/oauth.d.ts +5 -1
- package/types/src/requirement.d.ts +3 -2
- package/types/src/rpc.d.ts +26 -0
- package/types/src/token.d.ts +31 -1
- package/types/src/types.d.ts +7 -0
package/package.json
CHANGED
package/src/agent.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// An agent's device grant at an application environment (ADR-0090): the client, the caller its exchanged token
|
|
2
|
+
// presents, the scope it approved and the narrowing of the person's permissions to one scope value.
|
|
3
|
+
|
|
4
|
+
import type { CredentialedEndpointKind } from './contract'
|
|
5
|
+
import { isScopeParameterText } from './oauth'
|
|
6
|
+
import { narrowPermissions, uniquePermissions } from './permissions'
|
|
7
|
+
import { DIMENSION_NAME, parseRelationInside, type PermissionEntry, type Scope } from './types'
|
|
8
|
+
|
|
9
|
+
export { AGENT_CLIENT_ID } from './oauth'
|
|
10
|
+
|
|
11
|
+
/** The caller a person's application token presents when the proxy exchanged an agent's grant for it. */
|
|
12
|
+
export const AGENT_CALLER = 'agent'
|
|
13
|
+
|
|
14
|
+
export const AGENT_SCOPE_MODES = ['read', 'write'] as const
|
|
15
|
+
export type AgentScopeMode = (typeof AGENT_SCOPE_MODES)[number]
|
|
16
|
+
|
|
17
|
+
/** The mode a grant gets when the client asks for no scope. */
|
|
18
|
+
export const DEFAULT_AGENT_SCOPE_MODE: AgentScopeMode = 'read'
|
|
19
|
+
|
|
20
|
+
/** The endpoint kinds each mode admits. Neither admits an SPA: an agent calls procedures. */
|
|
21
|
+
export const AGENT_SCOPE_KINDS: Readonly<Record<AgentScopeMode, readonly CredentialedEndpointKind[]>> = Object.freeze({
|
|
22
|
+
read: Object.freeze(['query', 'download'] as const),
|
|
23
|
+
write: Object.freeze(['query', 'mutation', 'upload', 'download'] as const),
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
/** The scope one grant holds: its mode, and the one scope value it is narrowed to, or null for none. */
|
|
27
|
+
export interface AgentScope {
|
|
28
|
+
readonly mode: AgentScopeMode
|
|
29
|
+
readonly within: Scope | null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Read one scope token: `read`, `write`, or either narrowed to one value of a dimension, `read:<dimension>=<value>`.
|
|
34
|
+
* The dimension is a contract's dimension name; the value is opaque text of a parameterized scope
|
|
35
|
+
* (`isScopeParameterText`). Anything else is null.
|
|
36
|
+
*/
|
|
37
|
+
export function parseAgentScope(token: string): AgentScope | null {
|
|
38
|
+
const colon = token.indexOf(':')
|
|
39
|
+
const mode = colon === -1 ? token : token.slice(0, colon)
|
|
40
|
+
if (!isAgentScopeMode(mode)) return null
|
|
41
|
+
if (colon === -1) return { mode, within: null }
|
|
42
|
+
const rest = token.slice(colon + 1)
|
|
43
|
+
const equals = rest.indexOf('=')
|
|
44
|
+
if (equals === -1) return null
|
|
45
|
+
const type = rest.slice(0, equals)
|
|
46
|
+
const value = rest.slice(equals + 1)
|
|
47
|
+
if (!DIMENSION_NAME.test(type) || !isScopeParameterText(type) || !isScopeParameterText(value)) return null
|
|
48
|
+
return { mode, within: { type, value } }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The scope token of `scope`; `parseAgentScope` reads it back to the same scope. */
|
|
52
|
+
export function agentScopeToken(scope: AgentScope): string {
|
|
53
|
+
return scope.within === null ? scope.mode : `${scope.mode}:${scope.within.type}=${scope.within.value}`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isAgentScopeMode(value: string): value is AgentScopeMode {
|
|
57
|
+
return value === 'read' || value === 'write'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Whether a grant of `mode` reaches an endpoint of `kind`. */
|
|
61
|
+
export function agentScopeAdmits(mode: AgentScopeMode, kind: string): boolean {
|
|
62
|
+
return AGENT_SCOPE_KINDS[mode].some((admitted) => admitted === kind)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The person's entries narrowed to the one scope value `within` (ADR-0090 §3): `narrowPermissions` under `*` at `within`
|
|
67
|
+
* (a global entry becomes the same action there, an entry at `within` stays, every other scope is dropped), plus the
|
|
68
|
+
* relations the person holds inside `within`. A relation held anywhere (`~rel`) is dropped: an access view bound to
|
|
69
|
+
* its member reaches rows for a node that a hold anywhere does not reach (ADR-0076), so moving it inside `within` would
|
|
70
|
+
* widen it. Never adds an action or a reach the person does not hold.
|
|
71
|
+
*/
|
|
72
|
+
export function narrowToValue(entries: readonly PermissionEntry[], within: Scope): PermissionEntry[] {
|
|
73
|
+
if (!DIMENSION_NAME.test(within.type)) throw new Error('a grant is narrowed to a value of a dimension')
|
|
74
|
+
const scope = { type: within.type, value: within.value }
|
|
75
|
+
const inside = entries.filter((entry) => entry.scope !== null && relationInside(entry.scope, within))
|
|
76
|
+
return uniquePermissions([...narrowPermissions(entries, [{ action: '*', scope }]), ...inside])
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Whether every entry lies within `within`, as `narrowToValue` leaves them: a check a token's reader repeats. */
|
|
80
|
+
export function liesWithinValue(entries: readonly PermissionEntry[], within: Scope): boolean {
|
|
81
|
+
return entries.every((entry) =>
|
|
82
|
+
entry.scope !== null && ((entry.scope.type === within.type && entry.scope.value === within.value) || relationInside(entry.scope, within))
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function relationInside(scope: Scope, within: Scope): boolean {
|
|
87
|
+
const inside = parseRelationInside(scope)
|
|
88
|
+
return inside !== null && inside.dimension === within.type && inside.node === within.value
|
|
89
|
+
}
|
package/src/contract.ts
CHANGED
|
@@ -143,8 +143,11 @@ export interface EndpointTable {
|
|
|
143
143
|
readonly reservedPrefixes: readonly string[]
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
/**
|
|
147
|
-
|
|
146
|
+
/**
|
|
147
|
+
* Who may call a credentialed endpoint. There is no public caller: anonymous traffic is a fixed endpoint kind. `agent` is a
|
|
148
|
+
* person's agent holding a device grant for the environment (ADR-0090).
|
|
149
|
+
*/
|
|
150
|
+
export type Caller = 'human' | 'machine' | 'agent' | 'link' | `app:${string}`
|
|
148
151
|
|
|
149
152
|
/** `app:<id>`: the connector of the consuming application `<id>`. */
|
|
150
153
|
export const APP_CALLER_PREFIX = 'app:'
|
|
@@ -153,7 +156,7 @@ export const APP_CALLER_PREFIX = 'app:'
|
|
|
153
156
|
export const CONNECT_PREFIX = '/__fabrika/connect'
|
|
154
157
|
|
|
155
158
|
/** An RPC mount's callers: a link holder reaches only its own link mount, and an `app:<id>` only an export. */
|
|
156
|
-
export type RpcCaller = 'human' | 'machine'
|
|
159
|
+
export type RpcCaller = 'human' | 'machine' | 'agent'
|
|
157
160
|
|
|
158
161
|
export type EndpointMethod = 'GET' | 'POST'
|
|
159
162
|
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The endpoint forms only an installation service's table may use (ADR-0022 "Installation entries"). Types only,
|
|
2
2
|
// like the contract artifact: each reader parses them, and no application contract can declare them.
|
|
3
3
|
|
|
4
|
+
import type { AGENT_CALLER } from './agent'
|
|
4
5
|
import type { BodyAdmission, Caller, EndpointMethod } from './contract'
|
|
5
6
|
|
|
6
7
|
/** Any method. The ambiguity check treats it as overlapping every method of the same path shape. */
|
|
@@ -11,8 +12,8 @@ export type InstallationEndpointMethod = EndpointMethod | typeof ANY_METHOD
|
|
|
11
12
|
/** The caller an OAuth resource token presents. Only an installation row may admit it. */
|
|
12
13
|
export const OAUTH_CALLER = 'oauth'
|
|
13
14
|
|
|
14
|
-
/** Every caller a verified token can present: an application caller,
|
|
15
|
-
export type TokenCaller = Caller | typeof OAUTH_CALLER
|
|
15
|
+
/** Every caller a verified token can present: an application caller, `oauth`, or an agent's exchanged grant. */
|
|
16
|
+
export type TokenCaller = Caller | typeof OAUTH_CALLER | typeof AGENT_CALLER
|
|
16
17
|
|
|
17
18
|
export type InstallationCaller = 'human' | 'machine' | typeof OAUTH_CALLER
|
|
18
19
|
|
package/src/oauth.ts
CHANGED
|
@@ -33,9 +33,12 @@ export function isCimdClientId(value: unknown): value is string {
|
|
|
33
33
|
*/
|
|
34
34
|
export const OAUTH_CLI_CLIENT_ID = 'fabrika-cli'
|
|
35
35
|
|
|
36
|
-
/**
|
|
36
|
+
/** The public device client an agent obtains an application grant with (ADR-0090). It is never served Control's resources. */
|
|
37
|
+
export const AGENT_CLIENT_ID = 'agent-cli'
|
|
38
|
+
|
|
39
|
+
/** A client id a token or revocation request may name: a CIMD document's URL, or one of the two built-in clients. */
|
|
37
40
|
export function isOAuthClientId(value: unknown): value is string {
|
|
38
|
-
return value === OAUTH_CLI_CLIENT_ID || isCimdClientId(value)
|
|
41
|
+
return value === OAUTH_CLI_CLIENT_ID || value === AGENT_CLIENT_ID || isCimdClientId(value)
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
/** RFC 6749 scope-token syntax; duplicates and empty sets are refused. */
|
|
@@ -51,6 +54,11 @@ export function parseOAuthScopes(value: unknown): string[] | null {
|
|
|
51
54
|
return scopes
|
|
52
55
|
}
|
|
53
56
|
|
|
57
|
+
/** A scope dimension type, or a value of one, as a parameterized scope token carries it (`deploy:alpha`, `read:project=a`). */
|
|
58
|
+
export function isScopeParameterText(value: unknown): value is string {
|
|
59
|
+
return typeof value === 'string' && value.length <= 100 && /^[A-Za-z0-9][A-Za-z0-9._@-]*$/.test(value)
|
|
60
|
+
}
|
|
61
|
+
|
|
54
62
|
function isUriText(value: unknown): value is string {
|
|
55
63
|
return typeof value === 'string' && value.length > 0 && value.length <= 2048 && !/[\s\\<>"{}|^`]/.test(value)
|
|
56
64
|
&& !/%(?![A-Fa-f0-9]{2})/.test(value) && /^https?:\/\/[^/?#]+(?:[/?#]|$)/i.test(value)
|
package/src/requirement.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AGENT_CALLER } from './agent'
|
|
1
2
|
import { type ActionRequirement, APP_CALLER_PREFIX, type CapabilityRequirement, CONNECT_PREFIX, type EndpointRequirement } from './contract'
|
|
2
3
|
import { type AuthenticatedRequirement, OAUTH_CALLER, type TokenCaller } from './installation-endpoint'
|
|
3
4
|
import { permits } from './permissions'
|
|
@@ -37,13 +38,17 @@ function holdsAction(requirement: ActionRequirement | CapabilityRequirement, per
|
|
|
37
38
|
/**
|
|
38
39
|
* The caller kind a verified token presents: a `user` principal is `human`, a `service` principal is `machine`, a
|
|
39
40
|
* connector key of application `<id>` is `app:<id>`, and a token bound to no principal is a share link. An OAuth
|
|
40
|
-
* resource token is `oauth`, which only an installation row admits. A
|
|
41
|
-
*
|
|
41
|
+
* resource token is `oauth`, which only an installation row admits. A person's token that acts for an agent (`act`,
|
|
42
|
+
* ADR-0090) is `agent`, admitted only where a row lists it. A job, webhook or public procedure token is no caller:
|
|
43
|
+
* `null` is refused by every endpoint.
|
|
42
44
|
*/
|
|
43
45
|
export function callerOf(claims: AccessTokenClaims): TokenCaller | null {
|
|
44
46
|
if (claims.oauth !== undefined) {
|
|
45
47
|
return OAUTH_CALLER
|
|
46
48
|
}
|
|
49
|
+
if (claims.act !== undefined) {
|
|
50
|
+
return AGENT_CALLER
|
|
51
|
+
}
|
|
47
52
|
switch (claims.ptype) {
|
|
48
53
|
case 'user':
|
|
49
54
|
return 'human'
|
package/src/rpc.ts
CHANGED
|
@@ -99,6 +99,35 @@ export interface IamHandoffRpc {
|
|
|
99
99
|
exchangeAuthCode(input: ExchangeAuthCodeInput): Promise<ExchangeAuthCodeResult>
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
// ── An agent's device grant at an application (ADR-0090) ──────────────────────
|
|
103
|
+
//
|
|
104
|
+
// The proxy verifies an agent's resource token for its own host first, then asks IAM for the approving person's
|
|
105
|
+
// application token: the person's live permissions, narrowed when the grant names one scope value, with `act`. Like the
|
|
106
|
+
// handoff it is on the proxy's surface only, never on `IamRpc`, which every application holds.
|
|
107
|
+
|
|
108
|
+
export interface MintFromGrantInput {
|
|
109
|
+
/** The application environment `<app>@<env>` the proxy fronts. */
|
|
110
|
+
app: string
|
|
111
|
+
/** The resource the proxy serves the request on: the host's origin. */
|
|
112
|
+
resource: string
|
|
113
|
+
/** The agent's resource access token, as it arrived as the bearer. */
|
|
114
|
+
token: string
|
|
115
|
+
requestId: string
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type MintFromGrantResult =
|
|
119
|
+
| { ok: true; token: string; expiresAt: number }
|
|
120
|
+
/**
|
|
121
|
+
* `invalid_grant`: the token, its grant, the approving login or the person no longer holds. `write_refused`: the
|
|
122
|
+
* environment serves no `write` grant. `token_too_large`: a decided refusal, as for a key.
|
|
123
|
+
*/
|
|
124
|
+
| { ok: false; reason: 'invalid_grant' | 'write_refused' | 'token_too_large' }
|
|
125
|
+
|
|
126
|
+
/** The exchange of an agent's grant, as a contract of its own: only the proxy calls it. */
|
|
127
|
+
export interface IamGrantRpc {
|
|
128
|
+
mintFromGrant(input: MintFromGrantInput): Promise<MintFromGrantResult>
|
|
129
|
+
}
|
|
130
|
+
|
|
102
131
|
/**
|
|
103
132
|
* Where the proxy answers the handoff, on every app host it fronts. Reserved: an app route at this
|
|
104
133
|
* path is shadowed by the proxy and never reaches the upstream, so it is deliberately namespaced.
|
package/src/token.ts
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
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
28
|
import { isOAuthClientId, isOAuthResourceUri, parseOAuthScopes } from './oauth'
|
|
@@ -137,12 +138,39 @@ export interface AccessTokenClaims {
|
|
|
137
138
|
amr?: TerminalAuthenticationMethod
|
|
138
139
|
/** The terminal the person is signed in on; present exactly when `amr` is. */
|
|
139
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
|
|
140
146
|
/** Present only on an OAuth resource token (`aud` is then a resource URI, not an app id). */
|
|
141
147
|
oauth?: OAuthTokenClaims
|
|
142
148
|
/** Present only on a share-link token, which has no `ptype`: `sub` is then the link key's id. */
|
|
143
149
|
link?: LinkTokenClaims
|
|
144
150
|
}
|
|
145
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
|
+
|
|
146
174
|
/** The `amr` value of a session an application asserted at a terminal. */
|
|
147
175
|
export const TERMINAL_AUTHENTICATION_METHOD = 'terminal_assertion'
|
|
148
176
|
export type TerminalAuthenticationMethod = typeof TERMINAL_AUTHENTICATION_METHOD
|
|
@@ -237,6 +265,8 @@ export interface AccessTokenParams {
|
|
|
237
265
|
session?: true
|
|
238
266
|
/** The terminal of a person's session asserted on one; sets `amr` too, and requires a user `type` and `session`. */
|
|
239
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
|
|
240
270
|
permissions: PermissionEntry[]
|
|
241
271
|
issuedAt: number
|
|
242
272
|
expiresAt: number
|
|
@@ -304,15 +334,23 @@ export function matchesTokenAudience(claims: AccessTokenClaims, profile: TokenAu
|
|
|
304
334
|
export type AccessTokenLayout = 'entries' | 'compact'
|
|
305
335
|
|
|
306
336
|
/** The signed payload: the claims, with the global grants of the compact layout in `gperms`. */
|
|
307
|
-
export interface AccessTokenPayload extends AccessTokenClaims {
|
|
337
|
+
export interface AccessTokenPayload extends Omit<AccessTokenClaims, 'act'> {
|
|
338
|
+
act?: AgentActPayload
|
|
308
339
|
/** The compact layout's global grants, by action name; absent in the entries layout and when there are none. */
|
|
309
340
|
gperms?: string[]
|
|
310
341
|
}
|
|
311
342
|
|
|
312
343
|
/** The payload that carries `claims` in `layout`. */
|
|
313
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) }
|
|
314
347
|
const gperms = layout === 'compact' ? claims.perms.filter(isGlobalGrant).map((entry) => entry.action) : []
|
|
315
|
-
return gperms.length === 0 ?
|
|
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 }
|
|
316
354
|
}
|
|
317
355
|
|
|
318
356
|
/**
|
|
@@ -375,6 +413,13 @@ export function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims
|
|
|
375
413
|
claims.amr = TERMINAL_AUTHENTICATION_METHOD
|
|
376
414
|
claims.terminal = { id: params.terminal.id, label: params.terminal.label }
|
|
377
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
|
+
}
|
|
378
423
|
if (params.link !== undefined) {
|
|
379
424
|
if (params.type !== undefined) throw new Error('A share-link token has no principal type')
|
|
380
425
|
claims.link = { mount: params.link.mount, chain: params.link.chain.map((scope) => ({ type: scope.type, value: scope.value })) }
|
|
@@ -430,6 +475,10 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
430
475
|
if (terminal === undefined || (terminal !== null && (ptype !== 'user' || session !== true || oauth !== null))) {
|
|
431
476
|
return null
|
|
432
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
|
+
}
|
|
433
482
|
const claims: AccessTokenClaims = { iss, aud, sub, iat, exp, perms, label }
|
|
434
483
|
if (ptype !== null) {
|
|
435
484
|
claims.ptype = ptype
|
|
@@ -447,6 +496,9 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
447
496
|
claims.amr = TERMINAL_AUTHENTICATION_METHOD
|
|
448
497
|
claims.terminal = terminal
|
|
449
498
|
}
|
|
499
|
+
if (act !== null) {
|
|
500
|
+
claims.act = act
|
|
501
|
+
}
|
|
450
502
|
if (oauth !== null) {
|
|
451
503
|
claims.oauth = oauth
|
|
452
504
|
}
|
|
@@ -456,6 +508,44 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
|
|
|
456
508
|
return claims
|
|
457
509
|
}
|
|
458
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
|
+
|
|
459
549
|
/** A job, webhook or mount name, or an application id. */
|
|
460
550
|
const WORKLOAD_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/
|
|
461
551
|
|
package/src/types.ts
CHANGED
|
@@ -46,6 +46,9 @@ export interface Scope {
|
|
|
46
46
|
value: string
|
|
47
47
|
}
|
|
48
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
|
+
|
|
49
52
|
/**
|
|
50
53
|
* The pseudo-scope of a relation entry: `{ type: '~rel', value: <relation> }` means the action is held where a row of
|
|
51
54
|
* the application names the principal in that relation. No dimension may start with `~`, so no reader that compares
|
|
@@ -243,6 +246,11 @@ export interface AppSchema {
|
|
|
243
246
|
directory?: 'labels'
|
|
244
247
|
/** The app's identification page for a person at an enrolled terminal (ADR-0067); absent, no terminal lists it. */
|
|
245
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
|
|
246
254
|
}
|
|
247
255
|
|
|
248
256
|
/**
|
|
@@ -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;
|
package/types/src/contract.d.ts
CHANGED
|
@@ -137,14 +137,17 @@ export interface EndpointTable {
|
|
|
137
137
|
*/
|
|
138
138
|
readonly reservedPrefixes: readonly string[];
|
|
139
139
|
}
|
|
140
|
-
/**
|
|
141
|
-
|
|
140
|
+
/**
|
|
141
|
+
* Who may call a credentialed endpoint. There is no public caller: anonymous traffic is a fixed endpoint kind. `agent` is a
|
|
142
|
+
* person's agent holding a device grant for the environment (ADR-0090).
|
|
143
|
+
*/
|
|
144
|
+
export type Caller = 'human' | 'machine' | 'agent' | 'link' | `app:${string}`;
|
|
142
145
|
/** `app:<id>`: the connector of the consuming application `<id>`. */
|
|
143
146
|
export declare const APP_CALLER_PREFIX = "app:";
|
|
144
147
|
/** Every export procedure and description is served under this prefix; an `app:<id>` caller reaches nothing else. */
|
|
145
148
|
export declare const CONNECT_PREFIX = "/__fabrika/connect";
|
|
146
149
|
/** An RPC mount's callers: a link holder reaches only its own link mount, and an `app:<id>` only an export. */
|
|
147
|
-
export type RpcCaller = 'human' | 'machine';
|
|
150
|
+
export type RpcCaller = 'human' | 'machine' | 'agent';
|
|
148
151
|
export type EndpointMethod = 'GET' | 'POST';
|
|
149
152
|
/** Kinds that verify a credential, check the caller kind and the requirement. */
|
|
150
153
|
export type CredentialedEndpointKind = 'query' | 'mutation' | 'spa' | 'upload' | 'download';
|
package/types/src/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import type { AGENT_CALLER } from './agent';
|
|
1
2
|
import type { BodyAdmission, Caller, EndpointMethod } from './contract';
|
|
2
3
|
/** Any method. The ambiguity check treats it as overlapping every method of the same path shape. */
|
|
3
4
|
export declare const ANY_METHOD = "*";
|
|
4
5
|
export type InstallationEndpointMethod = EndpointMethod | typeof ANY_METHOD;
|
|
5
6
|
/** The caller an OAuth resource token presents. Only an installation row may admit it. */
|
|
6
7
|
export declare const OAUTH_CALLER = "oauth";
|
|
7
|
-
/** Every caller a verified token can present: an application caller,
|
|
8
|
-
export type TokenCaller = Caller | typeof OAUTH_CALLER;
|
|
8
|
+
/** Every caller a verified token can present: an application caller, `oauth`, or an agent's exchanged grant. */
|
|
9
|
+
export type TokenCaller = Caller | typeof OAUTH_CALLER | typeof AGENT_CALLER;
|
|
9
10
|
export type InstallationCaller = 'human' | 'machine' | typeof OAUTH_CALLER;
|
|
10
11
|
/** `service` verifies a credential; `protocol` and `health` are anonymous, and `protocol` keeps `Authorization`. */
|
|
11
12
|
export type InstallationEndpointKind = 'service' | 'protocol' | 'health';
|
package/types/src/oauth.d.ts
CHANGED
|
@@ -8,7 +8,11 @@ export declare function isCimdClientId(value: unknown): value is string;
|
|
|
8
8
|
* It is the one client id that is not a CIMD URL.
|
|
9
9
|
*/
|
|
10
10
|
export declare const OAUTH_CLI_CLIENT_ID = "fabrika-cli";
|
|
11
|
-
/**
|
|
11
|
+
/** The public device client an agent obtains an application grant with (ADR-0090). It is never served Control's resources. */
|
|
12
|
+
export declare const AGENT_CLIENT_ID = "agent-cli";
|
|
13
|
+
/** A client id a token or revocation request may name: a CIMD document's URL, or one of the two built-in clients. */
|
|
12
14
|
export declare function isOAuthClientId(value: unknown): value is string;
|
|
13
15
|
/** RFC 6749 scope-token syntax; duplicates and empty sets are refused. */
|
|
14
16
|
export declare function parseOAuthScopes(value: unknown): string[] | null;
|
|
17
|
+
/** A scope dimension type, or a value of one, as a parameterized scope token carries it (`deploy:alpha`, `read:project=a`). */
|
|
18
|
+
export declare function isScopeParameterText(value: unknown): value is string;
|
|
@@ -13,8 +13,9 @@ export declare function satisfies(requirement: EndpointRequirement | Authenticat
|
|
|
13
13
|
/**
|
|
14
14
|
* The caller kind a verified token presents: a `user` principal is `human`, a `service` principal is `machine`, a
|
|
15
15
|
* connector key of application `<id>` is `app:<id>`, and a token bound to no principal is a share link. An OAuth
|
|
16
|
-
* resource token is `oauth`, which only an installation row admits. A
|
|
17
|
-
*
|
|
16
|
+
* resource token is `oauth`, which only an installation row admits. A person's token that acts for an agent (`act`,
|
|
17
|
+
* ADR-0090) is `agent`, admitted only where a row lists it. A job, webhook or public procedure token is no caller:
|
|
18
|
+
* `null` is refused by every endpoint.
|
|
18
19
|
*/
|
|
19
20
|
export declare function callerOf(claims: AccessTokenClaims): TokenCaller | null;
|
|
20
21
|
/**
|
package/types/src/rpc.d.ts
CHANGED
|
@@ -69,6 +69,32 @@ export type ExchangeAuthCodeResult =
|
|
|
69
69
|
export interface IamHandoffRpc {
|
|
70
70
|
exchangeAuthCode(input: ExchangeAuthCodeInput): Promise<ExchangeAuthCodeResult>;
|
|
71
71
|
}
|
|
72
|
+
export interface MintFromGrantInput {
|
|
73
|
+
/** The application environment `<app>@<env>` the proxy fronts. */
|
|
74
|
+
app: string;
|
|
75
|
+
/** The resource the proxy serves the request on: the host's origin. */
|
|
76
|
+
resource: string;
|
|
77
|
+
/** The agent's resource access token, as it arrived as the bearer. */
|
|
78
|
+
token: string;
|
|
79
|
+
requestId: string;
|
|
80
|
+
}
|
|
81
|
+
export type MintFromGrantResult = {
|
|
82
|
+
ok: true;
|
|
83
|
+
token: string;
|
|
84
|
+
expiresAt: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* `invalid_grant`: the token, its grant, the approving login or the person no longer holds. `write_refused`: the
|
|
88
|
+
* environment serves no `write` grant. `token_too_large`: a decided refusal, as for a key.
|
|
89
|
+
*/
|
|
90
|
+
| {
|
|
91
|
+
ok: false;
|
|
92
|
+
reason: 'invalid_grant' | 'write_refused' | 'token_too_large';
|
|
93
|
+
};
|
|
94
|
+
/** The exchange of an agent's grant, as a contract of its own: only the proxy calls it. */
|
|
95
|
+
export interface IamGrantRpc {
|
|
96
|
+
mintFromGrant(input: MintFromGrantInput): Promise<MintFromGrantResult>;
|
|
97
|
+
}
|
|
72
98
|
/**
|
|
73
99
|
* Where the proxy answers the handoff, on every app host it fronts. Reserved: an app route at this
|
|
74
100
|
* path is shadowed by the proxy and never reaches the upstream, so it is deliberately namespaced.
|
package/types/src/token.d.ts
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
*
|
|
22
22
|
* Pure: no jose, no I/O. Signing/verifying (key material) stays in the packages that own it.
|
|
23
23
|
*/
|
|
24
|
+
import { AGENT_CLIENT_ID, type AgentScope } from './agent';
|
|
24
25
|
import type { ResolvedPrincipal } from './rpc';
|
|
25
26
|
import { type PermissionEntry, type Scope, type TokenPrincipalType } from './types';
|
|
26
27
|
/** Namespace reserved for browser credentials owned by IAM and the Fabrika proxy. */
|
|
@@ -119,11 +120,35 @@ export interface AccessTokenClaims {
|
|
|
119
120
|
amr?: TerminalAuthenticationMethod;
|
|
120
121
|
/** The terminal the person is signed in on; present exactly when `amr` is. */
|
|
121
122
|
terminal?: TerminalTokenClaims;
|
|
123
|
+
/**
|
|
124
|
+
* Present only on a person's application token the proxy exchanged an agent's device grant for (ADR-0090), never on a
|
|
125
|
+
* session's: the agent acts as the person, under the grant's scope.
|
|
126
|
+
*/
|
|
127
|
+
act?: AgentActClaims;
|
|
122
128
|
/** Present only on an OAuth resource token (`aud` is then a resource URI, not an app id). */
|
|
123
129
|
oauth?: OAuthTokenClaims;
|
|
124
130
|
/** Present only on a share-link token, which has no `ptype`: `sub` is then the link key's id. */
|
|
125
131
|
link?: LinkTokenClaims;
|
|
126
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* The agent a person's application token acts for: the device client, the grant the person approved, its parsed scope
|
|
135
|
+
* (ADR-0090 §2) and the label the agent gave itself, when it gave one.
|
|
136
|
+
*/
|
|
137
|
+
export interface AgentActClaims {
|
|
138
|
+
sub: typeof AGENT_CLIENT_ID;
|
|
139
|
+
grant: string;
|
|
140
|
+
scope: AgentScope;
|
|
141
|
+
label?: string;
|
|
142
|
+
}
|
|
143
|
+
/** `AgentActClaims` as the token carries them: the scope as its scope token (`agentScopeToken`). */
|
|
144
|
+
export interface AgentActPayload {
|
|
145
|
+
sub: typeof AGENT_CLIENT_ID;
|
|
146
|
+
grant: string;
|
|
147
|
+
scope: string;
|
|
148
|
+
label?: string;
|
|
149
|
+
}
|
|
150
|
+
/** The longest grant id and agent label an `act` claim carries. */
|
|
151
|
+
export declare const MAX_AGENT_ACT_FIELD_LENGTH = 64;
|
|
127
152
|
/** The `amr` value of a session an application asserted at a terminal. */
|
|
128
153
|
export declare const TERMINAL_AUTHENTICATION_METHOD = "terminal_assertion";
|
|
129
154
|
export type TerminalAuthenticationMethod = typeof TERMINAL_AUTHENTICATION_METHOD;
|
|
@@ -204,6 +229,8 @@ export interface AccessTokenParams {
|
|
|
204
229
|
session?: true;
|
|
205
230
|
/** The terminal of a person's session asserted on one; sets `amr` too, and requires a user `type` and `session`. */
|
|
206
231
|
terminal?: TerminalTokenClaims;
|
|
232
|
+
/** The agent a person's exchanged token acts for; requires a user `type`, no `session` and no `terminal`. */
|
|
233
|
+
act?: AgentActClaims;
|
|
207
234
|
permissions: PermissionEntry[];
|
|
208
235
|
issuedAt: number;
|
|
209
236
|
expiresAt: number;
|
|
@@ -243,7 +270,8 @@ export declare function matchesTokenAudience(claims: AccessTokenClaims, profile:
|
|
|
243
270
|
*/
|
|
244
271
|
export type AccessTokenLayout = 'entries' | 'compact';
|
|
245
272
|
/** The signed payload: the claims, with the global grants of the compact layout in `gperms`. */
|
|
246
|
-
export interface AccessTokenPayload extends AccessTokenClaims {
|
|
273
|
+
export interface AccessTokenPayload extends Omit<AccessTokenClaims, 'act'> {
|
|
274
|
+
act?: AgentActPayload;
|
|
247
275
|
/** The compact layout's global grants, by action name; absent in the entries layout and when there are none. */
|
|
248
276
|
gperms?: string[];
|
|
249
277
|
}
|
|
@@ -264,6 +292,8 @@ export declare function buildAccessClaims(params: AccessTokenParams): AccessToke
|
|
|
264
292
|
* the SDK reads them without an `as` cast. Returns null on any malformed claim shape.
|
|
265
293
|
*/
|
|
266
294
|
export declare function parseAccessClaims(payload: unknown): AccessTokenClaims | null;
|
|
295
|
+
/** The name an agent gives itself (`agent_label`): not blank, at most 64 characters, no control character. */
|
|
296
|
+
export declare function isAgentLabel(value: string): boolean;
|
|
267
297
|
/**
|
|
268
298
|
* Map a verified PRINCIPAL-bound access token into the `ResolvedPrincipal` the existing
|
|
269
299
|
* `AuthContext` consumes. Returns null for an anonymous token (no `ptype`) and for a workload token,
|
package/types/src/types.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export interface Scope {
|
|
|
35
35
|
type: string;
|
|
36
36
|
value: string;
|
|
37
37
|
}
|
|
38
|
+
/** A dimension's name as a contract declares it; also the dimension of a scope an agent's grant is narrowed to. */
|
|
39
|
+
export declare const DIMENSION_NAME: RegExp;
|
|
38
40
|
/**
|
|
39
41
|
* The pseudo-scope of a relation entry: `{ type: '~rel', value: <relation> }` means the action is held where a row of
|
|
40
42
|
* the application names the principal in that relation. No dimension may start with `~`, so no reader that compares
|
|
@@ -210,6 +212,11 @@ export interface AppSchema {
|
|
|
210
212
|
terminal?: {
|
|
211
213
|
identify: string;
|
|
212
214
|
};
|
|
215
|
+
/**
|
|
216
|
+
* A group of the contract admits the caller `agent` (ADR-0090): the environment's origin is then an OAuth resource of
|
|
217
|
+
* the agent client. Absent or false, IAM serves the environment to no agent.
|
|
218
|
+
*/
|
|
219
|
+
agentCallers?: boolean;
|
|
213
220
|
}
|
|
214
221
|
/**
|
|
215
222
|
* A job, a webhook or a public procedure (`public`, named by its endpoint id) of a contract application, or a consuming
|