@proofoftech/flowsafe 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/README.md +14 -4
- package/dist/agent-host/approval-resumer.d.ts +18 -0
- package/dist/agent-host/approval-resumer.d.ts.map +1 -0
- package/dist/agent-host/approval-resumer.js +53 -0
- package/dist/agent-host/approval-resumer.js.map +1 -0
- package/dist/agent-host/catalog.d.ts +6 -0
- package/dist/agent-host/catalog.d.ts.map +1 -0
- package/dist/agent-host/catalog.js +194 -0
- package/dist/agent-host/catalog.js.map +1 -0
- package/dist/agent-host/index.d.ts +9 -0
- package/dist/agent-host/index.d.ts.map +1 -0
- package/dist/agent-host/index.js +11 -0
- package/dist/agent-host/index.js.map +1 -0
- package/dist/agent-host/router.d.ts +11 -0
- package/dist/agent-host/router.d.ts.map +1 -0
- package/dist/agent-host/router.js +240 -0
- package/dist/agent-host/router.js.map +1 -0
- package/dist/agent-host/thread-host.d.ts +74 -0
- package/dist/agent-host/thread-host.d.ts.map +1 -0
- package/dist/agent-host/thread-host.js +688 -0
- package/dist/agent-host/thread-host.js.map +1 -0
- package/dist/agent-host/thread-topology.d.ts +39 -0
- package/dist/agent-host/thread-topology.d.ts.map +1 -0
- package/dist/agent-host/thread-topology.js +127 -0
- package/dist/agent-host/thread-topology.js.map +1 -0
- package/dist/agent-host/trusted-context.d.ts +8 -0
- package/dist/agent-host/trusted-context.d.ts.map +1 -0
- package/dist/agent-host/trusted-context.js +38 -0
- package/dist/agent-host/trusted-context.js.map +1 -0
- package/dist/agent-host/types.d.ts +85 -0
- package/dist/agent-host/types.d.ts.map +1 -0
- package/dist/agent-host/types.js +3 -0
- package/dist/agent-host/types.js.map +1 -0
- package/dist/agent-runner/agent-run-state.d.ts +38 -0
- package/dist/agent-runner/agent-run-state.d.ts.map +1 -0
- package/dist/agent-runner/agent-run-state.js +118 -0
- package/dist/agent-runner/agent-run-state.js.map +1 -0
- package/dist/agent-runner/durable-agent-runner.d.ts +12 -0
- package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
- package/dist/agent-runner/durable-agent-runner.js +130 -13
- package/dist/agent-runner/durable-agent-runner.js.map +1 -1
- package/dist/agent-runner/index.d.ts +2 -0
- package/dist/agent-runner/index.d.ts.map +1 -1
- package/dist/agent-runner/index.js +1 -0
- package/dist/agent-runner/index.js.map +1 -1
- package/dist/approval-api/contract.d.ts.map +1 -1
- package/dist/approval-api/contract.js.map +1 -1
- package/dist/approval-api/d1-store.d.ts.map +1 -1
- package/dist/approval-api/d1-store.js +24 -9
- package/dist/approval-api/d1-store.js.map +1 -1
- package/dist/approval-api/index.d.ts +2 -0
- package/dist/approval-api/index.d.ts.map +1 -1
- package/dist/approval-api/index.js +7 -0
- package/dist/approval-api/index.js.map +1 -1
- package/dist/approval-api/principal.d.ts +214 -0
- package/dist/approval-api/principal.d.ts.map +1 -0
- package/dist/approval-api/principal.js +391 -0
- package/dist/approval-api/principal.js.map +1 -0
- package/dist/approval-api/service.d.ts +42 -1
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +142 -13
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/tenant-context.d.ts +7 -0
- package/dist/approval-api/tenant-context.d.ts.map +1 -1
- package/dist/approval-api/tenant-context.js +31 -3
- package/dist/approval-api/tenant-context.js.map +1 -1
- package/dist/approval-api/types.d.ts +19 -3
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js.map +1 -1
- package/dist/do-runner/execution-context.d.ts +19 -0
- package/dist/do-runner/execution-context.d.ts.map +1 -0
- package/dist/do-runner/execution-context.js +70 -0
- package/dist/do-runner/execution-context.js.map +1 -0
- package/dist/do-runner/index.d.ts +2 -1
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +2 -1
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/runtime.d.ts +8 -0
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +157 -19
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/thread-do.d.ts +15 -1
- package/dist/do-runner/thread-do.d.ts.map +1 -1
- package/dist/do-runner/thread-do.js +31 -5
- package/dist/do-runner/thread-do.js.map +1 -1
- package/dist/do-runner/thread-header.d.ts +17 -2
- package/dist/do-runner/thread-header.d.ts.map +1 -1
- package/dist/do-runner/thread-header.js +17 -2
- package/dist/do-runner/thread-header.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts +12 -13
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +63 -25
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +13 -0
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +11 -3
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/host-approval-service.d.ts +10 -5
- package/dist/host-kit/host-approval-service.d.ts.map +1 -1
- package/dist/host-kit/host-approval-service.js +10 -10
- package/dist/host-kit/host-approval-service.js.map +1 -1
- package/dist/host-kit/index.d.ts +2 -2
- package/dist/host-kit/index.d.ts.map +1 -1
- package/dist/host-kit/index.js +1 -1
- package/dist/host-kit/index.js.map +1 -1
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +1 -6
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/host-kit/thread-topology.d.ts +1 -1
- package/dist/host-kit/thread-topology.d.ts.map +1 -1
- package/dist/host-kit/thread-topology.js +29 -3
- package/dist/host-kit/thread-topology.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/schedules/tick.d.ts +3 -7
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +7 -41
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signal-providers/delivery.d.ts +1 -1
- package/dist/signal-providers/delivery.d.ts.map +1 -1
- package/dist/signal-providers/delivery.js +9 -3
- package/dist/signal-providers/delivery.js.map +1 -1
- package/dist/signals/thread-do-routes.d.ts +12 -1
- package/dist/signals/thread-do-routes.d.ts.map +1 -1
- package/dist/signals/thread-do-routes.js +48 -12
- package/dist/signals/thread-do-routes.js.map +1 -1
- package/package.json +6 -3
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { type ApprovalActor, type ApprovalRole } from './contract.js';
|
|
2
|
+
/**
|
|
3
|
+
* Mirrors breakwater's `PrincipalKind` by value, for the same reason
|
|
4
|
+
* contract.ts mirrors the request-context keys: flowsafe does not import
|
|
5
|
+
* breakwater at runtime. The cross-package contract test pins the equality.
|
|
6
|
+
*/
|
|
7
|
+
export type ExecutionPrincipalKind = 'human' | 'service' | 'agent' | 'system';
|
|
8
|
+
export declare const EXECUTION_PRINCIPAL_KINDS: readonly ExecutionPrincipalKind[];
|
|
9
|
+
/** Automated kinds — everything that is not a logged-in person. */
|
|
10
|
+
export declare const AUTOMATED_PRINCIPAL_KINDS: readonly ExecutionPrincipalKind[];
|
|
11
|
+
export type AutomatedPrincipalKind = Exclude<ExecutionPrincipalKind, 'human'>;
|
|
12
|
+
/**
|
|
13
|
+
* `purpose` is REQUIRED on every automated kind, not optional as the roadmap
|
|
14
|
+
* sketch had it. The failure being fixed is that fabricated operators "lose
|
|
15
|
+
* provenance"; an optional field would let each new automated path skip the one
|
|
16
|
+
* thing that restores it. A human needs no purpose — the person is the reason.
|
|
17
|
+
*
|
|
18
|
+
* Fields are `readonly` because a principal is an authorization snapshot, not a
|
|
19
|
+
* mutable record: every consumer re-reads `kind` to decide what the holder may
|
|
20
|
+
* do, so a principal that can change between two reads has no meaning. The
|
|
21
|
+
* modifier is the compile-time half; `trustAutomationPrincipal` freezes the
|
|
22
|
+
* runtime half. `readonly` is not checked in assignability, so producers may
|
|
23
|
+
* still build one from an ordinary object literal.
|
|
24
|
+
*/
|
|
25
|
+
export type ExecutionPrincipal = {
|
|
26
|
+
readonly kind: 'human';
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly tenantId: string;
|
|
29
|
+
readonly role: ApprovalRole;
|
|
30
|
+
} | {
|
|
31
|
+
readonly kind: 'service';
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly tenantId: string;
|
|
34
|
+
readonly purpose: string;
|
|
35
|
+
/** Only an agent delegates; `never` makes a wrong shape a type error. */
|
|
36
|
+
readonly delegatedBy?: never;
|
|
37
|
+
} | {
|
|
38
|
+
readonly kind: 'agent';
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly tenantId: string;
|
|
41
|
+
readonly purpose: string;
|
|
42
|
+
/** The principal that delegated this run, for agent-to-agent work. */
|
|
43
|
+
readonly delegatedBy?: string;
|
|
44
|
+
} | {
|
|
45
|
+
readonly kind: 'system';
|
|
46
|
+
readonly id: string;
|
|
47
|
+
readonly tenantId: string;
|
|
48
|
+
readonly purpose: string;
|
|
49
|
+
readonly delegatedBy?: never;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The trusted-automation brand, following the TENANT_BOUND idiom for the same
|
|
53
|
+
* reason: TypeScript is structural, so a parameter typed plain
|
|
54
|
+
* `ExecutionPrincipal` is satisfied by any object literal. Without this, the
|
|
55
|
+
* service's automated entries authorize on the CALLER'S OWN ASSERTION that it
|
|
56
|
+
* is a robot — which would give automation strictly more create authority than
|
|
57
|
+
* a human `viewer`.
|
|
58
|
+
*
|
|
59
|
+
* The symbol is exported because declaration emit cannot reference a
|
|
60
|
+
* module-private name from an exported type. It is deliberately absent from the
|
|
61
|
+
* package barrel, but that is API-surface hygiene, NOT a capability boundary:
|
|
62
|
+
* the brand is recoverable by reflection from any vouched principal
|
|
63
|
+
* (`Object.getOwnPropertySymbols(maintenancePrincipal('x'))`), so in-process
|
|
64
|
+
* code that means to forge one still can. That residual is the same one
|
|
65
|
+
* TENANT_BOUND accepts, and it is deliberate — a TCB bypass on par with an `as`
|
|
66
|
+
* cast. What the brand eliminates is accidental satisfaction and the rushed fix
|
|
67
|
+
* that hands a request-derived principal to a trusted entry.
|
|
68
|
+
*/
|
|
69
|
+
export declare const TRUSTED_AUTOMATION: unique symbol;
|
|
70
|
+
/**
|
|
71
|
+
* Any non-human principal — the shape a duty needs when it wants provenance but
|
|
72
|
+
* derives no authority from the principal (the cron SLA sweep is the case).
|
|
73
|
+
* Separate from `TrustedAutomationPrincipal` so the brand is demanded only
|
|
74
|
+
* where it is actually read, which is `ApprovalService`'s two trusted entries.
|
|
75
|
+
*/
|
|
76
|
+
export type AutomatedExecutionPrincipal = Extract<ExecutionPrincipal, {
|
|
77
|
+
kind: AutomatedPrincipalKind;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* An automated principal that trusted platform code vouched for. The only kind
|
|
81
|
+
* accepted by `ApprovalService.createAsPrincipal` and
|
|
82
|
+
* `supersedeStaleAsPrincipal`.
|
|
83
|
+
*/
|
|
84
|
+
export type TrustedAutomationPrincipal = AutomatedExecutionPrincipal & {
|
|
85
|
+
readonly [TRUSTED_AUTOMATION]: true;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Vouch for an automated principal. Calling this IS the trust assertion, which
|
|
89
|
+
* is why it is a named function rather than a cast: it should be greppable, and
|
|
90
|
+
* it should never appear on a path that took the principal from a request.
|
|
91
|
+
*
|
|
92
|
+
* Throws on a human or a malformed principal — those must use the
|
|
93
|
+
* role-authorized entries.
|
|
94
|
+
*
|
|
95
|
+
* Returns a CANONICAL CLONE, branded and frozen. Validating the caller's object
|
|
96
|
+
* and handing the same reference back made the vouch time-of-check/time-of-use:
|
|
97
|
+
* the caller kept a mutable alias, so a validated `system` principal could be
|
|
98
|
+
* rewritten into `{kind:'human', role:'admin'}` after the check and before the
|
|
99
|
+
* service read it — and `#authorizeAutomated` re-reads `kind`. The clone comes
|
|
100
|
+
* from `canonicalPrincipal`, so it holds the exact values that were validated
|
|
101
|
+
* and no extra property rides into the trusted computing base; the brand lets
|
|
102
|
+
* consumers prove the value came from here, and the freeze keeps both true for
|
|
103
|
+
* the object's whole life.
|
|
104
|
+
*/
|
|
105
|
+
export declare function trustAutomationPrincipal(principal: ExecutionPrincipal): TrustedAutomationPrincipal;
|
|
106
|
+
/**
|
|
107
|
+
* Does this value carry the brand AND still hold a valid automated shape?
|
|
108
|
+
*
|
|
109
|
+
* The parameter type alone proves nothing at runtime — TypeScript is erased,
|
|
110
|
+
* and the ways in that remain (an `as` cast, a value rebuilt from storage or a
|
|
111
|
+
* structured clone across a boundary) all produce something the type accepts.
|
|
112
|
+
* This is the check that makes the brand load-bearing rather than decorative,
|
|
113
|
+
* so the trusted service entries call it instead of trusting their signature.
|
|
114
|
+
*
|
|
115
|
+
* Deliberately NOT on the package barrel: it is the enforcement half of an
|
|
116
|
+
* internal invariant, and exporting it would put a runtime shape check into the
|
|
117
|
+
* public API that consumers would have to keep working across minor versions.
|
|
118
|
+
*/
|
|
119
|
+
export declare function isTrustedAutomationPrincipal(value: unknown): value is TrustedAutomationPrincipal;
|
|
120
|
+
/**
|
|
121
|
+
* Validate and return a canonical principal that is not a person.
|
|
122
|
+
*
|
|
123
|
+
* The returned snapshot contains the exact values that passed validation, so
|
|
124
|
+
* callers never need to re-read a mutable or adversarial input. Kept off the
|
|
125
|
+
* package barrel: this is an internal enforcement helper, not public API.
|
|
126
|
+
*/
|
|
127
|
+
export declare function canonicalAutomatedPrincipal(value: unknown): AutomatedExecutionPrincipal | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* Structural validation only. The TENANT binding is checked separately by
|
|
130
|
+
* `assertExecutionPrincipal`, because "is this shaped like a principal" and "is
|
|
131
|
+
* this principal allowed here" are different questions and conflating them
|
|
132
|
+
* produces call sites that answer neither.
|
|
133
|
+
*
|
|
134
|
+
* This answers only "was a valid principal readable from this value". It does
|
|
135
|
+
* NOT promise a later plain read returns what was validated — nothing can, for
|
|
136
|
+
* an object built to lie. Anything that goes on to USE the fields must take
|
|
137
|
+
* them from `canonicalPrincipal`'s snapshot (as `trustAutomationPrincipal` and
|
|
138
|
+
* `assertExecutionPrincipal` do) rather than from the argument.
|
|
139
|
+
*/
|
|
140
|
+
export declare function isExecutionPrincipal(value: unknown): value is ExecutionPrincipal;
|
|
141
|
+
/**
|
|
142
|
+
* Validate a principal AND bind it to the tenant that is about to act on it.
|
|
143
|
+
*
|
|
144
|
+
* `tenantId` on a decoded principal crosses an authentication boundary exactly
|
|
145
|
+
* as `ApprovalActor.tenantId` does — the type says `string`, and the type system
|
|
146
|
+
* has no authority over a value read back out of D1 or a DO's storage.
|
|
147
|
+
*
|
|
148
|
+
* Returns the canonical snapshot, not the argument: the tenant that was
|
|
149
|
+
* compared and the tenant the caller goes on to use are then the same string.
|
|
150
|
+
*/
|
|
151
|
+
export declare function assertExecutionPrincipal(value: unknown, expectedTenantId: string, label: string): ExecutionPrincipal;
|
|
152
|
+
/**
|
|
153
|
+
* Structural equality across every kind-specific field.
|
|
154
|
+
*
|
|
155
|
+
* Cast-free: the union narrows on `kind`, so a future variant with a new
|
|
156
|
+
* discriminating field becomes a compile error here rather than silently
|
|
157
|
+
* comparing equal. Three rebinding guards depend on that.
|
|
158
|
+
*/
|
|
159
|
+
export declare function samePrincipal(left: ExecutionPrincipal, right: ExecutionPrincipal): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* The role an automated principal projects into breakwater's `Actor`.
|
|
162
|
+
*
|
|
163
|
+
* `Actor.role` is required, so an automated principal must carry SOME label.
|
|
164
|
+
* breakwater's gate does not consult the role allowlist for a non-human kind,
|
|
165
|
+
* making this value inert there — it is the least-privileged role precisely so
|
|
166
|
+
* that any consumer which reads `actor.role` WITHOUT understanding `kind` gets
|
|
167
|
+
* the minimum rather than the `operator` these paths used to fabricate.
|
|
168
|
+
*
|
|
169
|
+
* `viewer` also holds no decider role, so an automated principal projected onto
|
|
170
|
+
* `ApprovalService` can never satisfy DECIDER_ROLES and approve anything.
|
|
171
|
+
*/
|
|
172
|
+
export declare const AUTOMATED_PROJECTED_ROLE: ApprovalRole;
|
|
173
|
+
/**
|
|
174
|
+
* Project a principal onto the approval-service identity.
|
|
175
|
+
*
|
|
176
|
+
* Automated principals keep their own id — attribution stays truthful — while
|
|
177
|
+
* borrowing the least-privileged role so that the approval service's own role
|
|
178
|
+
* gates (CAN_CREATE, DECIDER_ROLES) treat them as read-only.
|
|
179
|
+
*/
|
|
180
|
+
export declare function principalActor(principal: ExecutionPrincipal): ApprovalActor;
|
|
181
|
+
/**
|
|
182
|
+
* The principal as breakwater's `Actor`. The ONE place the projection rule is
|
|
183
|
+
* written; `principalActor` and the trusted request context both go through it
|
|
184
|
+
* so the breakwater-facing and approval-facing identities cannot disagree.
|
|
185
|
+
*/
|
|
186
|
+
export declare function breakwaterActorFor(principal: ExecutionPrincipal): {
|
|
187
|
+
id: string;
|
|
188
|
+
role: ApprovalRole;
|
|
189
|
+
kind: ExecutionPrincipalKind;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Serialize a principal for the trusted thread header. Every field is already
|
|
193
|
+
* bounded and header-control-free (`boundedText`), so plain JSON is safe and
|
|
194
|
+
* stays readable in a trace.
|
|
195
|
+
*/
|
|
196
|
+
export declare function encodeExecutionPrincipal(principal: ExecutionPrincipal): string;
|
|
197
|
+
/**
|
|
198
|
+
* Rebuild a principal from the trusted header, binding it to the tenant the DO
|
|
199
|
+
* already authenticated. Fields are picked EXPLICITLY rather than spread, so an
|
|
200
|
+
* attacker-supplied extra property cannot ride into DO storage or D1.
|
|
201
|
+
*
|
|
202
|
+
* Returns undefined on anything malformed; the caller fails closed.
|
|
203
|
+
*/
|
|
204
|
+
export declare function decodeExecutionPrincipal(header: string, tenantId: string): ExecutionPrincipal | undefined;
|
|
205
|
+
/** An authenticated human at the HTTP boundary, as an execution principal. */
|
|
206
|
+
export declare function humanPrincipal(actor: ApprovalActor): ExecutionPrincipal;
|
|
207
|
+
/** Correlation fields carried into every audit event for this principal. */
|
|
208
|
+
export declare function principalAuditFields(principal: ExecutionPrincipal): {
|
|
209
|
+
principalKind: ExecutionPrincipalKind;
|
|
210
|
+
principalId: string;
|
|
211
|
+
purpose?: string;
|
|
212
|
+
delegatedBy?: string;
|
|
213
|
+
};
|
|
214
|
+
//# sourceMappingURL=principal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principal.d.ts","sourceRoot":"","sources":["../../src/approval-api/principal.ts"],"names":[],"mappings":"AAoBA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;AAEvB;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE9E,eAAO,MAAM,yBAAyB,EAAE,SAAS,sBAAsB,EAKtE,CAAC;AAEF,mEAAmE;AACnE,eAAO,MAAM,yBAAyB,EAAE,SAAS,sBAAsB,EAItE,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,EAAE,OAAO,CAAC,CAAC;AAM9E;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,kBAAkB,GAC1B;IACE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;CAC7B,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,yEAAyE;IACzE,QAAQ,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC;CAC9B,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;I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B,CACtC,KAAK,EAAE,OAAO,EACd,gBAAgB,EAAE,MAAM,EACxB,KAAK,EAAE,MAAM,GACZ,kBAAkB,CAWpB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,kBAAkB,EACxB,KAAK,EAAE,kBAAkB,GACxB,OAAO,CAcT;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,wBAAwB,EAAE,YAAuB,CAAC;AAE/D;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,kBAAkB,GAAG,aAAa,CAG3E;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,kBAAkB,GAAG;IACjE,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,YAAY,CAAC;IACnB,IAAI,EAAE,sBAAsB,CAAC;CAC9B,CAOA;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,kBAAkB,GAC5B,MAAM,CAaR;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACf,kBAAkB,GAAG,SAAS,CAsBhC;AAED,8EAA8E;AAC9E,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,GAAG,kBAAkB,CAOvE;AAED,4EAA4E;AAC5E,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,kBAAkB,GAAG;IACnE,aAAa,EAAE,sBAAsB,CAAC;IACtC,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAYA"}
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// ExecutionPrincipal — WHO is executing, as opposed to who approved.
|
|
3
|
+
//
|
|
4
|
+
// Before this existed the platform had one identity, ApprovalActor, whose only
|
|
5
|
+
// authority vocabulary is a human ApprovalRole. Every automated path therefore
|
|
6
|
+
// fabricated a human: the schedule tick, cron maintenance, signal-provider
|
|
7
|
+
// delivery, and the suspension-reconcile bridge all minted `role: 'operator'`.
|
|
8
|
+
// That loses provenance (nothing records WHY the call exists) and, worse, hands
|
|
9
|
+
// autonomous execution whatever an operator may do.
|
|
10
|
+
//
|
|
11
|
+
// ApprovalActor is deliberately NOT replaced. It stays exactly what it always
|
|
12
|
+
// was: the identity of an authenticated human at the HTTP boundary and of a
|
|
13
|
+
// reviewer deciding an approval. A human approving an agent's request is still
|
|
14
|
+
// attributed to that human — `decidedBy` is unaffected by anything here.
|
|
15
|
+
//
|
|
16
|
+
// This lives in approval-api rather than contract.ts because contract.ts is the
|
|
17
|
+
// breakwater wire contract, mirrored by value and pinned by the cross-package
|
|
18
|
+
// tests. A principal is a host concept: breakwater learns only the KIND (its
|
|
19
|
+
// `PrincipalKind`), never the tenant or the purpose.
|
|
20
|
+
import { APPROVAL_ROLES, } from './contract.js';
|
|
21
|
+
export const EXECUTION_PRINCIPAL_KINDS = [
|
|
22
|
+
'human',
|
|
23
|
+
'service',
|
|
24
|
+
'agent',
|
|
25
|
+
'system',
|
|
26
|
+
];
|
|
27
|
+
/** Automated kinds — everything that is not a logged-in person. */
|
|
28
|
+
export const AUTOMATED_PRINCIPAL_KINDS = [
|
|
29
|
+
'service',
|
|
30
|
+
'agent',
|
|
31
|
+
'system',
|
|
32
|
+
];
|
|
33
|
+
/** Upper bound on the free-text provenance fields, so audit rows stay bounded. */
|
|
34
|
+
const MAX_PURPOSE_LENGTH = 200;
|
|
35
|
+
const MAX_PRINCIPAL_ID_LENGTH = 200;
|
|
36
|
+
/**
|
|
37
|
+
* The trusted-automation brand, following the TENANT_BOUND idiom for the same
|
|
38
|
+
* reason: TypeScript is structural, so a parameter typed plain
|
|
39
|
+
* `ExecutionPrincipal` is satisfied by any object literal. Without this, the
|
|
40
|
+
* service's automated entries authorize on the CALLER'S OWN ASSERTION that it
|
|
41
|
+
* is a robot — which would give automation strictly more create authority than
|
|
42
|
+
* a human `viewer`.
|
|
43
|
+
*
|
|
44
|
+
* The symbol is exported because declaration emit cannot reference a
|
|
45
|
+
* module-private name from an exported type. It is deliberately absent from the
|
|
46
|
+
* package barrel, but that is API-surface hygiene, NOT a capability boundary:
|
|
47
|
+
* the brand is recoverable by reflection from any vouched principal
|
|
48
|
+
* (`Object.getOwnPropertySymbols(maintenancePrincipal('x'))`), so in-process
|
|
49
|
+
* code that means to forge one still can. That residual is the same one
|
|
50
|
+
* TENANT_BOUND accepts, and it is deliberate — a TCB bypass on par with an `as`
|
|
51
|
+
* cast. What the brand eliminates is accidental satisfaction and the rushed fix
|
|
52
|
+
* that hands a request-derived principal to a trusted entry.
|
|
53
|
+
*/
|
|
54
|
+
export const TRUSTED_AUTOMATION = Symbol('flowsafe.trustedAutomation');
|
|
55
|
+
/**
|
|
56
|
+
* Vouch for an automated principal. Calling this IS the trust assertion, which
|
|
57
|
+
* is why it is a named function rather than a cast: it should be greppable, and
|
|
58
|
+
* it should never appear on a path that took the principal from a request.
|
|
59
|
+
*
|
|
60
|
+
* Throws on a human or a malformed principal — those must use the
|
|
61
|
+
* role-authorized entries.
|
|
62
|
+
*
|
|
63
|
+
* Returns a CANONICAL CLONE, branded and frozen. Validating the caller's object
|
|
64
|
+
* and handing the same reference back made the vouch time-of-check/time-of-use:
|
|
65
|
+
* the caller kept a mutable alias, so a validated `system` principal could be
|
|
66
|
+
* rewritten into `{kind:'human', role:'admin'}` after the check and before the
|
|
67
|
+
* service read it — and `#authorizeAutomated` re-reads `kind`. The clone comes
|
|
68
|
+
* from `canonicalPrincipal`, so it holds the exact values that were validated
|
|
69
|
+
* and no extra property rides into the trusted computing base; the brand lets
|
|
70
|
+
* consumers prove the value came from here, and the freeze keeps both true for
|
|
71
|
+
* the object's whole life.
|
|
72
|
+
*/
|
|
73
|
+
export function trustAutomationPrincipal(principal) {
|
|
74
|
+
// The brand is stamped onto the CANONICAL SNAPSHOT, never onto the argument.
|
|
75
|
+
// Validating one object and minting from another is how a value that passed
|
|
76
|
+
// the automated check came back out as a human admin; taking both from the
|
|
77
|
+
// same read makes that impossible rather than merely difficult.
|
|
78
|
+
const canonical = canonicalPrincipal(principal);
|
|
79
|
+
if (canonical === undefined || canonical.kind === 'human') {
|
|
80
|
+
throw new Error('execution principal: only a valid automated principal can be trusted for platform work');
|
|
81
|
+
}
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
...canonical,
|
|
84
|
+
[TRUSTED_AUTOMATION]: true,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Read one field as an OWN DATA property, or `undefined`.
|
|
89
|
+
*
|
|
90
|
+
* Every principal field is read through this rather than by `value.kind`,
|
|
91
|
+
* because a plain read is not a stable observation of a value someone else
|
|
92
|
+
* built: it walks the prototype chain, so an inherited getter answers it, and
|
|
93
|
+
* an own accessor's getter can answer differently each time. `Object.freeze`
|
|
94
|
+
* is no substitute — it constrains a value's own data properties and says
|
|
95
|
+
* nothing about accessors, inherited fields, or virtual ones.
|
|
96
|
+
*
|
|
97
|
+
* This does NOT by itself prove the field cannot lie: over a Proxy with an
|
|
98
|
+
* extensible target, `Object.getOwnPropertyDescriptor` is a trap as
|
|
99
|
+
* unconstrained as `get`. What closes the class is that the single read is
|
|
100
|
+
* captured — see `canonicalPrincipal`, which returns a snapshot built from
|
|
101
|
+
* these reads, so nothing downstream re-reads the caller's object at all.
|
|
102
|
+
*
|
|
103
|
+
* The minter emits plain own data properties, so this refuses nothing real.
|
|
104
|
+
*/
|
|
105
|
+
function ownField(value, key) {
|
|
106
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
107
|
+
return descriptor !== undefined && 'value' in descriptor
|
|
108
|
+
? descriptor.value
|
|
109
|
+
: undefined;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Does this value carry the brand AND still hold a valid automated shape?
|
|
113
|
+
*
|
|
114
|
+
* The parameter type alone proves nothing at runtime — TypeScript is erased,
|
|
115
|
+
* and the ways in that remain (an `as` cast, a value rebuilt from storage or a
|
|
116
|
+
* structured clone across a boundary) all produce something the type accepts.
|
|
117
|
+
* This is the check that makes the brand load-bearing rather than decorative,
|
|
118
|
+
* so the trusted service entries call it instead of trusting their signature.
|
|
119
|
+
*
|
|
120
|
+
* Deliberately NOT on the package barrel: it is the enforcement half of an
|
|
121
|
+
* internal invariant, and exporting it would put a runtime shape check into the
|
|
122
|
+
* public API that consumers would have to keep working across minor versions.
|
|
123
|
+
*/
|
|
124
|
+
export function isTrustedAutomationPrincipal(value) {
|
|
125
|
+
if (value === null || typeof value !== 'object')
|
|
126
|
+
return false;
|
|
127
|
+
return (canonicalAutomatedPrincipal(value) !== undefined &&
|
|
128
|
+
// The brand is read as an OWN DATA property for the same reason every other
|
|
129
|
+
// field is: a plain read would accept one inherited from a prototype, which
|
|
130
|
+
// nobody stamped.
|
|
131
|
+
ownField(value, TRUSTED_AUTOMATION) === true &&
|
|
132
|
+
// A branded-but-UNFROZEN principal is by definition one somebody stamped
|
|
133
|
+
// onto a live mutable object rather than minting. It is also what forces a
|
|
134
|
+
// Proxy's descriptor and `get` channels to agree, since the spec invariants
|
|
135
|
+
// only bind over a non-extensible target.
|
|
136
|
+
Object.isFrozen(value));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Validate and return a canonical principal that is not a person.
|
|
140
|
+
*
|
|
141
|
+
* The returned snapshot contains the exact values that passed validation, so
|
|
142
|
+
* callers never need to re-read a mutable or adversarial input. Kept off the
|
|
143
|
+
* package barrel: this is an internal enforcement helper, not public API.
|
|
144
|
+
*/
|
|
145
|
+
export function canonicalAutomatedPrincipal(value) {
|
|
146
|
+
const principal = canonicalPrincipal(value);
|
|
147
|
+
return principal !== undefined && principal.kind !== 'human'
|
|
148
|
+
? principal
|
|
149
|
+
: undefined;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Bounded, non-empty, and free of control characters.
|
|
153
|
+
*
|
|
154
|
+
* Not an injection barrier for the wire — the principal travels through
|
|
155
|
+
* `JSON.stringify`, which escapes U+0000–U+001F. It matters because these
|
|
156
|
+
* fields do not stop at the wire: `id` becomes `requestedBy`/`decidedBy` in D1
|
|
157
|
+
* and the actor on every audit row, and `purpose` rides into the SIEM export.
|
|
158
|
+
* The bounds keep an audit row bounded; the control-character refusal keeps
|
|
159
|
+
* those strings clean at the boundary rather than downstream. Mirrors the
|
|
160
|
+
* `containsHeaderControl` check `createTenantResolver` applies to an actor id.
|
|
161
|
+
*/
|
|
162
|
+
function boundedText(value, max) {
|
|
163
|
+
if (typeof value !== 'string' || value.trim() === '' || value.length > max) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
for (const character of value) {
|
|
167
|
+
const code = character.charCodeAt(0);
|
|
168
|
+
if (code <= 0x1f || code === 0x7f)
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Validate AND canonicalize in one pass: every field is read exactly once, and
|
|
175
|
+
* the result is a fresh plain object built from THOSE reads.
|
|
176
|
+
*
|
|
177
|
+
* Validating a caller's object and then reading it again is the mistake this
|
|
178
|
+
* module keeps re-learning. The first version handed back the caller's own
|
|
179
|
+
* mutable reference; the second cloned but re-read plainly, which an accessor
|
|
180
|
+
* or a prototype getter could answer differently; reading through descriptors
|
|
181
|
+
* narrows the channel but does not close it, because
|
|
182
|
+
* `Object.getOwnPropertyDescriptor` is itself a Proxy trap and, over an
|
|
183
|
+
* extensible target, is as unconstrained as `get`.
|
|
184
|
+
*
|
|
185
|
+
* Returning the snapshot ends the class rather than narrowing it: "validated"
|
|
186
|
+
* and "used" become the same values, so no consumer can be handed something
|
|
187
|
+
* other than what was checked, whatever the input was built from.
|
|
188
|
+
*
|
|
189
|
+
* Fields are picked EXPLICITLY. tsc catches a new kind (the returns stop being
|
|
190
|
+
* exhaustive) and a new REQUIRED field (the literal stops matching), but NOT a
|
|
191
|
+
* new optional one — that compiles clean and is silently dropped, so add it to
|
|
192
|
+
* the pick by hand (`delegatedBy` is the existing example).
|
|
193
|
+
*/
|
|
194
|
+
function canonicalPrincipal(value) {
|
|
195
|
+
if (value === null || typeof value !== 'object')
|
|
196
|
+
return undefined;
|
|
197
|
+
const id = ownField(value, 'id');
|
|
198
|
+
const tenantId = ownField(value, 'tenantId');
|
|
199
|
+
const kind = ownField(value, 'kind');
|
|
200
|
+
if (!boundedText(id, MAX_PRINCIPAL_ID_LENGTH) ||
|
|
201
|
+
typeof tenantId !== 'string' ||
|
|
202
|
+
tenantId === '') {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
if (kind === 'human') {
|
|
206
|
+
const role = ownField(value, 'role');
|
|
207
|
+
return typeof role === 'string' &&
|
|
208
|
+
APPROVAL_ROLES.includes(role)
|
|
209
|
+
? { kind: 'human', id, tenantId, role: role }
|
|
210
|
+
: undefined;
|
|
211
|
+
}
|
|
212
|
+
if (kind !== 'service' && kind !== 'agent' && kind !== 'system') {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
const purpose = ownField(value, 'purpose');
|
|
216
|
+
if (!boundedText(purpose, MAX_PURPOSE_LENGTH))
|
|
217
|
+
return undefined;
|
|
218
|
+
const delegatedBy = ownField(value, 'delegatedBy');
|
|
219
|
+
if (delegatedBy !== undefined) {
|
|
220
|
+
return kind === 'agent' && boundedText(delegatedBy, MAX_PRINCIPAL_ID_LENGTH)
|
|
221
|
+
? { kind: 'agent', id, tenantId, purpose, delegatedBy }
|
|
222
|
+
: undefined;
|
|
223
|
+
}
|
|
224
|
+
if (kind === 'agent')
|
|
225
|
+
return { kind: 'agent', id, tenantId, purpose };
|
|
226
|
+
if (kind === 'service')
|
|
227
|
+
return { kind: 'service', id, tenantId, purpose };
|
|
228
|
+
return { kind: 'system', id, tenantId, purpose };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Structural validation only. The TENANT binding is checked separately by
|
|
232
|
+
* `assertExecutionPrincipal`, because "is this shaped like a principal" and "is
|
|
233
|
+
* this principal allowed here" are different questions and conflating them
|
|
234
|
+
* produces call sites that answer neither.
|
|
235
|
+
*
|
|
236
|
+
* This answers only "was a valid principal readable from this value". It does
|
|
237
|
+
* NOT promise a later plain read returns what was validated — nothing can, for
|
|
238
|
+
* an object built to lie. Anything that goes on to USE the fields must take
|
|
239
|
+
* them from `canonicalPrincipal`'s snapshot (as `trustAutomationPrincipal` and
|
|
240
|
+
* `assertExecutionPrincipal` do) rather than from the argument.
|
|
241
|
+
*/
|
|
242
|
+
export function isExecutionPrincipal(value) {
|
|
243
|
+
return canonicalPrincipal(value) !== undefined;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Validate a principal AND bind it to the tenant that is about to act on it.
|
|
247
|
+
*
|
|
248
|
+
* `tenantId` on a decoded principal crosses an authentication boundary exactly
|
|
249
|
+
* as `ApprovalActor.tenantId` does — the type says `string`, and the type system
|
|
250
|
+
* has no authority over a value read back out of D1 or a DO's storage.
|
|
251
|
+
*
|
|
252
|
+
* Returns the canonical snapshot, not the argument: the tenant that was
|
|
253
|
+
* compared and the tenant the caller goes on to use are then the same string.
|
|
254
|
+
*/
|
|
255
|
+
export function assertExecutionPrincipal(value, expectedTenantId, label) {
|
|
256
|
+
const principal = canonicalPrincipal(value);
|
|
257
|
+
if (principal === undefined) {
|
|
258
|
+
throw new Error(`execution principal: ${label} is malformed`);
|
|
259
|
+
}
|
|
260
|
+
if (principal.tenantId !== expectedTenantId) {
|
|
261
|
+
throw new Error(`execution principal: ${label} belongs to tenant '${principal.tenantId}', not '${expectedTenantId}'`);
|
|
262
|
+
}
|
|
263
|
+
return principal;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Structural equality across every kind-specific field.
|
|
267
|
+
*
|
|
268
|
+
* Cast-free: the union narrows on `kind`, so a future variant with a new
|
|
269
|
+
* discriminating field becomes a compile error here rather than silently
|
|
270
|
+
* comparing equal. Three rebinding guards depend on that.
|
|
271
|
+
*/
|
|
272
|
+
export function samePrincipal(left, right) {
|
|
273
|
+
if (left.id !== right.id || left.tenantId !== right.tenantId)
|
|
274
|
+
return false;
|
|
275
|
+
if (left.kind === 'human' || right.kind === 'human') {
|
|
276
|
+
return (left.kind === 'human' &&
|
|
277
|
+
right.kind === 'human' &&
|
|
278
|
+
left.role === right.role);
|
|
279
|
+
}
|
|
280
|
+
return (left.kind === right.kind &&
|
|
281
|
+
left.purpose === right.purpose &&
|
|
282
|
+
left.delegatedBy === right.delegatedBy);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* The role an automated principal projects into breakwater's `Actor`.
|
|
286
|
+
*
|
|
287
|
+
* `Actor.role` is required, so an automated principal must carry SOME label.
|
|
288
|
+
* breakwater's gate does not consult the role allowlist for a non-human kind,
|
|
289
|
+
* making this value inert there — it is the least-privileged role precisely so
|
|
290
|
+
* that any consumer which reads `actor.role` WITHOUT understanding `kind` gets
|
|
291
|
+
* the minimum rather than the `operator` these paths used to fabricate.
|
|
292
|
+
*
|
|
293
|
+
* `viewer` also holds no decider role, so an automated principal projected onto
|
|
294
|
+
* `ApprovalService` can never satisfy DECIDER_ROLES and approve anything.
|
|
295
|
+
*/
|
|
296
|
+
export const AUTOMATED_PROJECTED_ROLE = 'viewer';
|
|
297
|
+
/**
|
|
298
|
+
* Project a principal onto the approval-service identity.
|
|
299
|
+
*
|
|
300
|
+
* Automated principals keep their own id — attribution stays truthful — while
|
|
301
|
+
* borrowing the least-privileged role so that the approval service's own role
|
|
302
|
+
* gates (CAN_CREATE, DECIDER_ROLES) treat them as read-only.
|
|
303
|
+
*/
|
|
304
|
+
export function principalActor(principal) {
|
|
305
|
+
const { id, role } = breakwaterActorFor(principal);
|
|
306
|
+
return { id, role, tenantId: principal.tenantId };
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The principal as breakwater's `Actor`. The ONE place the projection rule is
|
|
310
|
+
* written; `principalActor` and the trusted request context both go through it
|
|
311
|
+
* so the breakwater-facing and approval-facing identities cannot disagree.
|
|
312
|
+
*/
|
|
313
|
+
export function breakwaterActorFor(principal) {
|
|
314
|
+
return {
|
|
315
|
+
id: principal.id,
|
|
316
|
+
role: principal.kind === 'human' ? principal.role : AUTOMATED_PROJECTED_ROLE,
|
|
317
|
+
kind: principal.kind,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Serialize a principal for the trusted thread header. Every field is already
|
|
322
|
+
* bounded and header-control-free (`boundedText`), so plain JSON is safe and
|
|
323
|
+
* stays readable in a trace.
|
|
324
|
+
*/
|
|
325
|
+
export function encodeExecutionPrincipal(principal) {
|
|
326
|
+
return JSON.stringify(principal.kind === 'human'
|
|
327
|
+
? { kind: 'human', id: principal.id, role: principal.role }
|
|
328
|
+
: {
|
|
329
|
+
kind: principal.kind,
|
|
330
|
+
id: principal.id,
|
|
331
|
+
purpose: principal.purpose,
|
|
332
|
+
...(principal.delegatedBy !== undefined
|
|
333
|
+
? { delegatedBy: principal.delegatedBy }
|
|
334
|
+
: {}),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Rebuild a principal from the trusted header, binding it to the tenant the DO
|
|
339
|
+
* already authenticated. Fields are picked EXPLICITLY rather than spread, so an
|
|
340
|
+
* attacker-supplied extra property cannot ride into DO storage or D1.
|
|
341
|
+
*
|
|
342
|
+
* Returns undefined on anything malformed; the caller fails closed.
|
|
343
|
+
*/
|
|
344
|
+
export function decodeExecutionPrincipal(header, tenantId) {
|
|
345
|
+
let parsed;
|
|
346
|
+
try {
|
|
347
|
+
parsed = JSON.parse(header);
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
if (parsed === null || typeof parsed !== 'object')
|
|
353
|
+
return undefined;
|
|
354
|
+
const fields = parsed;
|
|
355
|
+
const candidate = fields.kind === 'human'
|
|
356
|
+
? { kind: 'human', id: fields.id, tenantId, role: fields.role }
|
|
357
|
+
: {
|
|
358
|
+
kind: fields.kind,
|
|
359
|
+
id: fields.id,
|
|
360
|
+
tenantId,
|
|
361
|
+
purpose: fields.purpose,
|
|
362
|
+
...(fields.delegatedBy !== undefined
|
|
363
|
+
? { delegatedBy: fields.delegatedBy }
|
|
364
|
+
: {}),
|
|
365
|
+
};
|
|
366
|
+
return isExecutionPrincipal(candidate) ? candidate : undefined;
|
|
367
|
+
}
|
|
368
|
+
/** An authenticated human at the HTTP boundary, as an execution principal. */
|
|
369
|
+
export function humanPrincipal(actor) {
|
|
370
|
+
return {
|
|
371
|
+
kind: 'human',
|
|
372
|
+
id: actor.id,
|
|
373
|
+
tenantId: actor.tenantId,
|
|
374
|
+
role: actor.role,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
/** Correlation fields carried into every audit event for this principal. */
|
|
378
|
+
export function principalAuditFields(principal) {
|
|
379
|
+
if (principal.kind === 'human') {
|
|
380
|
+
return { principalKind: 'human', principalId: principal.id };
|
|
381
|
+
}
|
|
382
|
+
return {
|
|
383
|
+
principalKind: principal.kind,
|
|
384
|
+
principalId: principal.id,
|
|
385
|
+
purpose: principal.purpose,
|
|
386
|
+
...(principal.kind === 'agent' && principal.delegatedBy !== undefined
|
|
387
|
+
? { delegatedBy: principal.delegatedBy }
|
|
388
|
+
: {}),
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
//# sourceMappingURL=principal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principal.js","sourceRoot":"","sources":["../../src/approval-api/principal.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,qEAAqE;AACrE,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,2EAA2E;AAC3E,+EAA+E;AAC/E,gFAAgF;AAChF,oDAAoD;AACpD,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,+EAA+E;AAC/E,yEAAyE;AACz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