@kybernesis/arp-pdp 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kybernesis AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # `@kybernesis/arp-pdp`
2
+
3
+ Policy Decision Point. Thin wrapper around `@cedar-policy/cedar-wasm` that
4
+ adds ARP's `@obligation` annotation semantics.
5
+
6
+ ## Use
7
+
8
+ ```ts
9
+ import { createPdp } from '@kybernesis/arp-pdp';
10
+ import schemaJson from '@kybernesis/arp-spec/cedar-schema.json' with { type: 'json' };
11
+
12
+ const pdp = createPdp(JSON.stringify(schemaJson));
13
+
14
+ const decision = pdp.evaluate({
15
+ cedarPolicies: [
16
+ 'permit (principal == Agent::"did:web:ghost.agent", action == Action::"read", resource in Project::"alpha");',
17
+ ],
18
+ obligationPolicies: [
19
+ `@obligation("rate_limit")
20
+ @obligation_params({ "max_requests_per_hour": 60 })
21
+ permit (principal, action, resource in Project::"alpha");`,
22
+ ],
23
+ principal: { type: 'Agent', id: 'did:web:ghost.agent' },
24
+ action: 'read',
25
+ resource: {
26
+ type: 'Document',
27
+ id: 'alpha/q2',
28
+ parents: [{ type: 'Project', id: 'alpha' }],
29
+ },
30
+ context: { /* time, spend, vcs, ... */ },
31
+ });
32
+ // → { decision: 'allow', obligations: [...], policies_fired: [...], reasons: [] }
33
+ ```
34
+
35
+ ## Decision semantics
36
+
37
+ Matches `ARP-policy-examples.md §10` exactly:
38
+
39
+ 1. Deny by default.
40
+ 2. Any matching `permit` flips to `allow`.
41
+ 3. Any matching `forbid` flips back to `deny` (forbid wins over permit).
42
+ 4. On `allow`, evaluate obligation policies. Each obligation policy that
43
+ fires contributes `{ type, params }` to the obligation list.
44
+
45
+ `obligationPolicies` are *not* evaluated when the decision is `deny`.
46
+
47
+ ## Obligation annotations
48
+
49
+ ARP extends Cedar with two annotations:
50
+
51
+ ```cedar
52
+ @obligation("redact_fields")
53
+ @obligation_params({ "fields": ["client.name", "client.email"] })
54
+ permit (principal == Agent::"did:web:ghost.agent", action == Action::"read", resource in Project::"alpha");
55
+ ```
56
+
57
+ `@obligation_params(...)` accepts:
58
+
59
+ - A JSON-object literal (with bare keys + single-quoted strings tolerated)
60
+ - A JSON string — `@obligation_params("{\"fields\":[...]}")`
61
+
62
+ Both forms are stripped before handing the policy to Cedar so the upstream
63
+ parser never sees the non-standard syntax.
64
+
65
+ ## Context value conventions
66
+
67
+ Cedar's runtime type system is `long | boolean | string | set | record` — there
68
+ are **no native floats and no native timestamps**. The ARP PDP adapts the
69
+ `context.*` vocabulary from `docs/ARP-policy-examples.md` accordingly, and
70
+ every upstream consumer (scope-catalog compiler, adapters, owner-app consent
71
+ renderer, reference agents) MUST use the same representations:
72
+
73
+ | Vocabulary from the doc | On the wire (PDP input + policy body) |
74
+ |---|---|
75
+ | `quoted_price_usd` | `quoted_price_cents` (integer) |
76
+ | `spend_last_24h_usd` | `spend_last_24h_cents` (integer) |
77
+ | `spend_last_30d_usd` | `spend_last_30d_cents` (integer) |
78
+ | `spend_all_time_usd` | `spend_all_time_cents` (integer) |
79
+ | `time.now` | `time.now_ms` (epoch milliseconds, integer) |
80
+ | `connection.expires_at` | `connection.expires_at_ms` (epoch milliseconds, integer) |
81
+ | `connection.created_at` | `connection.created_at_ms` (epoch milliseconds, integer) |
82
+
83
+ Money in cents prevents floating-point drift on cap comparisons; epoch
84
+ milliseconds give us ordered integer comparisons without parsing ISO strings
85
+ inside Cedar. The doc's narrative float/ISO syntax is illustrative, not
86
+ normative — Cedar policies compiled by the scope-catalog compiler already
87
+ follow this convention, and adapters that synthesise ad-hoc policies must
88
+ follow it too.
89
+
90
+ If Cedar later adds a decimal or timestamp type, we migrate by rewriting the
91
+ scope templates and updating this table; the rest of the stack is unchanged
92
+ because it only ever reads the integer fields.
93
+
94
+ ## Design notes
95
+
96
+ - Each input policy gets an auto-assigned `@id("p_<n>")` / `@id("o_<n>")` if
97
+ it doesn't already have one; `policies_fired` returns those IDs.
98
+ - `isAuthorized` is called without `enableRequestValidation` in v0. Phase 5
99
+ can toggle strict schema enforcement once reference agents' contexts are
100
+ stable.
101
+ - Cedar WASM must be imported per-call; the library memoises the engine
102
+ internally, so repeated calls are cheap.
package/dist/index.cjs ADDED
@@ -0,0 +1,278 @@
1
+ 'use strict';
2
+
3
+ var cedarWasm = require('@cedar-policy/cedar-wasm');
4
+
5
+ // src/cedar.ts
6
+ function assertSchemaParses(schemaJson) {
7
+ const trimmed = schemaJson.trim();
8
+ if (!trimmed) return;
9
+ const r = cedarWasm.checkParseSchema(trimmed);
10
+ if (r.type !== "success") {
11
+ throw new Error(
12
+ `Cedar schema failed to parse: ${JSON.stringify(r.errors, null, 2)}`
13
+ );
14
+ }
15
+ }
16
+ function toCedarValue(value) {
17
+ if (value === null || value === void 0) return null;
18
+ if (typeof value === "string" || typeof value === "boolean") return value;
19
+ if (typeof value === "number") {
20
+ if (!Number.isFinite(value)) {
21
+ throw new Error(`non-finite number cannot be encoded for Cedar: ${value}`);
22
+ }
23
+ return value;
24
+ }
25
+ if (value instanceof Date) {
26
+ return value.toISOString();
27
+ }
28
+ if (Array.isArray(value)) {
29
+ return value.map(toCedarValue);
30
+ }
31
+ if (typeof value === "object") {
32
+ const out = {};
33
+ for (const [k, v] of Object.entries(value)) {
34
+ out[k] = toCedarValue(v);
35
+ }
36
+ return out;
37
+ }
38
+ throw new Error(`unsupported Cedar value type: ${typeof value}`);
39
+ }
40
+ function toContext(ctx) {
41
+ if (!ctx) return {};
42
+ const out = {};
43
+ for (const [k, v] of Object.entries(ctx)) {
44
+ out[k] = toCedarValue(v);
45
+ }
46
+ return out;
47
+ }
48
+ function entityToJson(entity) {
49
+ return {
50
+ uid: { type: entity.type, id: entity.id },
51
+ attrs: entity.attrs ? Object.fromEntries(
52
+ Object.entries(entity.attrs).map(([k, v]) => [k, toCedarValue(v)])
53
+ ) : {},
54
+ parents: (entity.parents ?? []).map((p) => ({ type: p.type, id: p.id }))
55
+ };
56
+ }
57
+ function buildCedarCall(opts) {
58
+ const actionType = opts.actionType ?? "Action";
59
+ const all = [opts.principal, opts.resource, ...opts.entities ?? []];
60
+ const seen = /* @__PURE__ */ new Set();
61
+ const entities = [];
62
+ for (const e of all) {
63
+ const key = `${e.type}::${e.id}`;
64
+ if (seen.has(key)) continue;
65
+ seen.add(key);
66
+ entities.push(entityToJson(e));
67
+ }
68
+ return {
69
+ principal: { type: opts.principal.type, id: opts.principal.id },
70
+ action: { type: actionType, id: opts.action },
71
+ resource: { type: opts.resource.type, id: opts.resource.id },
72
+ context: toContext(opts.context),
73
+ slice: {
74
+ policies: opts.policies,
75
+ entities,
76
+ templates: null,
77
+ templateInstantiations: null
78
+ }
79
+ };
80
+ }
81
+ function cedarIsAuthorized(call) {
82
+ return cedarWasm.isAuthorized(call);
83
+ }
84
+
85
+ // src/obligations.ts
86
+ function parseObligationPolicy(text, fallbackId) {
87
+ const { type, rest: afterType } = extractObligationType(text);
88
+ const { params, rest: afterParams } = extractObligationParams(afterType);
89
+ const existingId = extractExistingId(afterParams);
90
+ const cleaned = afterParams.trim();
91
+ const id = existingId ?? fallbackId;
92
+ const withId = existingId ? cleaned : `@id("${id}")
93
+ ${cleaned}`;
94
+ return {
95
+ id,
96
+ obligationType: type,
97
+ params,
98
+ cleanedText: withId
99
+ };
100
+ }
101
+ function extractObligationType(text) {
102
+ const match = /@obligation\s*\(\s*"([^"\\]*(?:\\.[^"\\]*)*)"\s*\)/m.exec(text);
103
+ if (!match) {
104
+ throw new Error(
105
+ 'obligation policy missing @obligation("<type>") annotation'
106
+ );
107
+ }
108
+ const rest = text.slice(0, match.index) + text.slice(match.index + match[0].length);
109
+ return { type: match[1] ?? "", rest };
110
+ }
111
+ function extractObligationParams(text) {
112
+ const headIdx = text.indexOf("@obligation_params");
113
+ if (headIdx < 0) return { params: {}, rest: text };
114
+ const parenIdx = text.indexOf("(", headIdx);
115
+ if (parenIdx < 0) return { params: {}, rest: text };
116
+ let depth = 1;
117
+ let inString = null;
118
+ let i = parenIdx + 1;
119
+ while (i < text.length && depth > 0) {
120
+ const ch = text[i];
121
+ if (inString) {
122
+ if (ch === "\\") {
123
+ i += 2;
124
+ continue;
125
+ }
126
+ if (ch === inString) inString = null;
127
+ i++;
128
+ continue;
129
+ }
130
+ if (ch === '"' || ch === "'") {
131
+ inString = ch;
132
+ i++;
133
+ continue;
134
+ }
135
+ if (ch === "(" || ch === "{" || ch === "[") depth++;
136
+ else if (ch === ")" || ch === "}" || ch === "]") depth--;
137
+ if (depth === 0 && ch === ")") break;
138
+ i++;
139
+ }
140
+ if (depth !== 0) {
141
+ throw new Error("unbalanced @obligation_params annotation");
142
+ }
143
+ const innerRaw = text.slice(parenIdx + 1, i).trim();
144
+ const rest = text.slice(0, headIdx) + text.slice(i + 1);
145
+ const params = parseParamValue(innerRaw);
146
+ return { params, rest };
147
+ }
148
+ function parseParamValue(raw) {
149
+ if (!raw) return {};
150
+ if (raw.startsWith('"') && raw.endsWith('"') || raw.startsWith("'") && raw.endsWith("'")) {
151
+ const unquoted = raw.slice(1, -1).replace(/\\(.)/g, "$1");
152
+ try {
153
+ const parsed = JSON.parse(unquoted);
154
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
155
+ return parsed;
156
+ }
157
+ throw new Error("expected object");
158
+ } catch (err) {
159
+ throw new Error(
160
+ `@obligation_params string payload isn't valid JSON object: ${err.message}`
161
+ );
162
+ }
163
+ }
164
+ const normalised = coerceToJson(raw);
165
+ try {
166
+ const parsed = JSON.parse(normalised);
167
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
168
+ return parsed;
169
+ }
170
+ throw new Error("expected object");
171
+ } catch (err) {
172
+ throw new Error(
173
+ `@obligation_params payload isn't a valid JSON object (after coercion): ${raw}: ${err.message}`
174
+ );
175
+ }
176
+ }
177
+ function coerceToJson(raw) {
178
+ let out = raw.replace(/'([^'\\]*(?:\\.[^'\\]*)*)'/g, (_m, inner) => {
179
+ return JSON.stringify(inner);
180
+ });
181
+ out = out.replace(
182
+ /([{,]\s*)([A-Za-z_][A-Za-z0-9_]*)(\s*:)/g,
183
+ (_m, lead, key, tail) => `${lead}"${key}"${tail}`
184
+ );
185
+ return out;
186
+ }
187
+ function extractExistingId(text) {
188
+ const match = /@id\s*\(\s*"([^"\\]*(?:\\.[^"\\]*)*)"\s*\)/m.exec(text);
189
+ return match ? match[1] ?? null : null;
190
+ }
191
+ function obligationRecord(parsed) {
192
+ return { type: parsed.obligationType, params: parsed.params };
193
+ }
194
+
195
+ // src/pdp.ts
196
+ function createPdp(schemaJson) {
197
+ assertSchemaParses(schemaJson);
198
+ return {
199
+ evaluate(input) {
200
+ const joined = input.cedarPolicies.map((text) => text.trim()).filter((t) => t.length > 0).join("\n");
201
+ const decisionCall = buildCedarCall({
202
+ policies: joined,
203
+ principal: input.principal,
204
+ action: input.action,
205
+ resource: input.resource,
206
+ context: input.context ?? {},
207
+ entities: input.entities ?? []
208
+ });
209
+ const decisionAnswer = cedarIsAuthorized(decisionCall);
210
+ if (decisionAnswer.type !== "success") {
211
+ throw new Error(
212
+ `Cedar authorization failed: ${JSON.stringify(decisionAnswer.errors)}`
213
+ );
214
+ }
215
+ const rawDecision = decisionAnswer.response.decision;
216
+ const decision = rawDecision === "Allow" ? "allow" : "deny";
217
+ const firedDecisionPolicies = toStringArray(decisionAnswer.response.diagnostics.reason);
218
+ const reasons = decisionAnswer.response.diagnostics.errors.map(
219
+ (e) => e.error.message
220
+ );
221
+ const obligations = [];
222
+ const obligationFired = [];
223
+ if (decision === "allow" && input.obligationPolicies?.length) {
224
+ const parsed = input.obligationPolicies.map(
225
+ (text, idx) => parseObligationPolicy(text, `o_${idx}`)
226
+ );
227
+ const obligationMap = {};
228
+ for (const p of parsed) obligationMap[p.id] = p.cleanedText;
229
+ const obligationCall = buildCedarCall({
230
+ policies: obligationMap,
231
+ principal: input.principal,
232
+ action: input.action,
233
+ resource: input.resource,
234
+ context: input.context ?? {},
235
+ entities: input.entities ?? []
236
+ });
237
+ const obligationAnswer = cedarIsAuthorized(obligationCall);
238
+ if (obligationAnswer.type !== "success") {
239
+ throw new Error(
240
+ `Cedar obligation evaluation failed: ${JSON.stringify(obligationAnswer.errors)}`
241
+ );
242
+ }
243
+ const fired = new Set(
244
+ toStringArray(obligationAnswer.response.diagnostics.reason)
245
+ );
246
+ for (const p of parsed) {
247
+ if (fired.has(p.id)) {
248
+ obligations.push(obligationRecord(p));
249
+ obligationFired.push(p.id);
250
+ }
251
+ }
252
+ }
253
+ return {
254
+ decision,
255
+ obligations,
256
+ policies_fired: [...firedDecisionPolicies, ...obligationFired],
257
+ reasons
258
+ };
259
+ }
260
+ };
261
+ }
262
+ function toStringArray(value) {
263
+ if (Array.isArray(value)) {
264
+ return value.map((v) => String(v));
265
+ }
266
+ if (value && typeof value === "object" && Symbol.iterator in value) {
267
+ return Array.from(value).map((v) => String(v));
268
+ }
269
+ return [];
270
+ }
271
+
272
+ exports.createPdp = createPdp;
273
+ exports.entityToJson = entityToJson;
274
+ exports.obligationRecord = obligationRecord;
275
+ exports.parseObligationPolicy = parseObligationPolicy;
276
+ exports.toCedarValue = toCedarValue;
277
+ //# sourceMappingURL=index.cjs.map
278
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cedar.ts","../src/obligations.ts","../src/pdp.ts"],"names":["checkParseSchema","isAuthorized"],"mappings":";;;;;AASO,SAAS,mBAAmB,UAAA,EAA0B;AAC3D,EAAA,MAAM,OAAA,GAAU,WAAW,IAAA,EAAK;AAChC,EAAA,IAAI,CAAC,OAAA,EAAS;AACd,EAAA,MAAM,CAAA,GAAIA,2BAAiB,OAAO,CAAA;AAClC,EAAA,IAAI,CAAA,CAAE,SAAS,SAAA,EAAW;AACxB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,iCAAiC,IAAA,CAAK,SAAA,CAAU,EAAE,MAAA,EAAQ,IAAA,EAAM,CAAC,CAAC,CAAA;AAAA,KACpE;AAAA,EACF;AACF;AAOO,SAAS,aAAa,KAAA,EAAgC;AAC3D,EAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,KAAA,KAAU,MAAA,EAAW,OAAO,IAAA;AAClD,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,OAAO,KAAA,KAAU,WAAW,OAAO,KAAA;AACpE,EAAA,IAAI,OAAO,UAAU,QAAA,EAAU;AAC7B,IAAA,IAAI,CAAC,MAAA,CAAO,QAAA,CAAS,KAAK,CAAA,EAAG;AAC3B,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,+CAAA,EAAkD,KAAK,CAAA,CAAE,CAAA;AAAA,IAC3E;AACA,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,IAAI,iBAAiB,IAAA,EAAM;AACzB,IAAA,OA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AC,CAAC,CAAA;AACpC,YAAA,eAAA,CAAgB,IAAA,CAAK,EAAE,EAAE,CAAA;AAAA,UAC3B;AAAA,QACF;AAAA,MACF;AAEA,MAAA,OAAO;AAAA,QACL,QAAA;AAAA,QACA,WAAA;AAAA,QACA,cAAA,EAAgB,CAAC,GAAG,qBAAA,EAAuB,GAAG,eAAe,CAAA;AAAA,QAC7D;AAAA,OACF;AAAA,IACF;AAAA,GACF;AACF;AAEA,SAAS,cAAc,KAAA,EAA0B;AAG/C,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,EAAG;AACxB,IAAA,OAAO,MAAM,GAAA,CAAI,CAAC,CAAA,KAAM,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA,EACnC;AACA,EAAA,IAAI,SAAS,OAAO,KAAA,KAAU,QAAA,IAAY,MAAA,CAAO,YAAY,KAAA,EAAO;AAClE,IAAA,OAAO,KAAA,CAAM,KAAK,KAA0B,CAAA,CAAE,IAAI,CAAC,CAAA,KAAM,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA,EACpE;AACA,EAAA,OAAO,EAAC;AACV","file":"index.cjs","sourcesContent":["import {\n checkParseSchema,\n isAuthorized,\n type AuthorizationCall,\n type CedarValueJson,\n type EntityJson,\n} from '@cedar-policy/cedar-wasm';\nimport type { Entity } from './types.js';\n\nexport function assertSchemaParses(schemaJson: string): void {\n const trimmed = schemaJson.trim();\n if (!trimmed) return;\n const r = checkParseSchema(trimmed);\n if (r.type !== 'success') {\n throw new Error(\n `Cedar schema failed to parse: ${JSON.stringify(r.errors, null, 2)}`,\n );\n }\n}\n\n/**\n * Convert a plain JS value into the Cedar JSON form accepted by\n * `@cedar-policy/cedar-wasm`. Arrays stay arrays, objects stay objects,\n * primitives stay primitives. Dates serialise to their ISO string.\n */\nexport function toCedarValue(value: unknown): CedarValueJson {\n if (value === null || value === undefined) return null;\n if (typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') {\n if (!Number.isFinite(value)) {\n throw new Error(`non-finite number cannot be encoded for Cedar: ${value}`);\n }\n return value;\n }\n if (value instanceof Date) {\n return value.toISOString();\n }\n if (Array.isArray(value)) {\n return value.map(toCedarValue);\n }\n if (typeof value === 'object') {\n const out: Record<string, CedarValueJson> = {};\n for (const [k, v] of Object.entries(value as Record<string, unknown>)) {\n out[k] = toCedarValue(v);\n }\n return out;\n }\n throw new Error(`unsupported Cedar value type: ${typeof value}`);\n}\n\nexport function toContext(\n ctx: Record<string, unknown> | undefined,\n): Record<string, CedarValueJson> {\n if (!ctx) return {};\n const out: Record<string, CedarValueJson> = {};\n for (const [k, v] of Object.entries(ctx)) {\n out[k] = toCedarValue(v);\n }\n return out;\n}\n\nexport function entityToJson(entity: Entity): EntityJson {\n return {\n uid: { type: entity.type, id: entity.id },\n attrs: entity.attrs\n ? Object.fromEntries(\n Object.entries(entity.attrs).map(([k, v]) => [k, toCedarValue(v)]),\n )\n : {},\n parents: (entity.parents ?? []).map((p) => ({ type: p.type, id: p.id })),\n };\n}\n\nexport interface CedarCallParts {\n call: AuthorizationCall;\n}\n\nexport function buildCedarCall(opts: {\n policies: Record<string, string> | string;\n principal: Entity;\n action: string;\n resource: Entity;\n context?: Record<string, unknown>;\n entities?: Entity[];\n actionType?: string;\n}): AuthorizationCall {\n const actionType = opts.actionType ?? 'Action';\n const all: Entity[] = [opts.principal, opts.resource, ...(opts.entities ?? [])];\n const seen = new Set<string>();\n const entities: EntityJson[] = [];\n for (const e of all) {\n const key = `${e.type}::${e.id}`;\n if (seen.has(key)) continue;\n seen.add(key);\n entities.push(entityToJson(e));\n }\n return {\n principal: { type: opts.principal.type, id: opts.principal.id },\n action: { type: actionType, id: opts.action },\n resource: { type: opts.resource.type, id: opts.resource.id },\n context: toContext(opts.context),\n slice: {\n policies: opts.policies,\n entities,\n templates: null,\n templateInstantiations: null,\n },\n };\n}\n\nexport function cedarIsAuthorized(call: AuthorizationCall) {\n return isAuthorized(call);\n}\n","import type { Obligation } from '@kybernesis/arp-spec';\n\nexport interface ParsedObligationPolicy {\n /** Auto-generated stable id assigned to the cleaned-up policy text. */\n id: string;\n /** Obligation `type` from `@obligation(\"...\")`. */\n obligationType: string;\n /** Parsed params from `@obligation_params(...)` (object, JSON string, or empty). */\n params: Record<string, unknown>;\n /** Policy text with ARP-specific annotations removed, safe for Cedar. */\n cleanedText: string;\n}\n\n/**\n * Parse ARP's non-standard obligation annotations out of a Cedar policy.\n *\n * Accepted forms:\n *\n * @obligation(\"redact_fields\")\n * @obligation_params({ \"fields\": [\"client.name\"] }) // object literal\n * @obligation_params(\"{\\\"fields\\\":[...]}\") // JSON string\n *\n * The returned `cleanedText` has both annotations removed and is safe to pass\n * straight to Cedar. Auto-prefixes an `@id(...)` so callers can track the\n * policy's `policies_fired` id.\n */\nexport function parseObligationPolicy(\n text: string,\n fallbackId: string,\n): ParsedObligationPolicy {\n const { type, rest: afterType } = extractObligationType(text);\n const { params, rest: afterParams } = extractObligationParams(afterType);\n const existingId = extractExistingId(afterParams);\n const cleaned = afterParams.trim();\n const id = existingId ?? fallbackId;\n const withId = existingId ? cleaned : `@id(\"${id}\")\\n${cleaned}`;\n return {\n id,\n obligationType: type,\n params,\n cleanedText: withId,\n };\n}\n\nfunction extractObligationType(text: string): { type: string; rest: string } {\n const match = /@obligation\\s*\\(\\s*\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"\\s*\\)/m.exec(text);\n if (!match) {\n throw new Error(\n 'obligation policy missing @obligation(\"<type>\") annotation',\n );\n }\n const rest = text.slice(0, match.index) + text.slice(match.index + match[0].length);\n return { type: match[1] ?? '', rest };\n}\n\nfunction extractObligationParams(\n text: string,\n): { params: Record<string, unknown>; rest: string } {\n const headIdx = text.indexOf('@obligation_params');\n if (headIdx < 0) return { params: {}, rest: text };\n\n // Find the opening paren after the keyword.\n const parenIdx = text.indexOf('(', headIdx);\n if (parenIdx < 0) return { params: {}, rest: text };\n\n // Walk forward balancing braces/quotes to find the matching close paren.\n let depth = 1;\n let inString: '\"' | \"'\" | null = null;\n let i = parenIdx + 1;\n while (i < text.length && depth > 0) {\n const ch = text[i];\n if (inString) {\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === inString) inString = null;\n i++;\n continue;\n }\n if (ch === '\"' || ch === \"'\") {\n inString = ch;\n i++;\n continue;\n }\n if (ch === '(' || ch === '{' || ch === '[') depth++;\n else if (ch === ')' || ch === '}' || ch === ']') depth--;\n if (depth === 0 && ch === ')') break;\n i++;\n }\n if (depth !== 0) {\n throw new Error('unbalanced @obligation_params annotation');\n }\n\n const innerRaw = text.slice(parenIdx + 1, i).trim();\n const rest = text.slice(0, headIdx) + text.slice(i + 1);\n const params = parseParamValue(innerRaw);\n return { params, rest };\n}\n\nfunction parseParamValue(raw: string): Record<string, unknown> {\n if (!raw) return {};\n // If the whole value is a quoted string, it's a JSON-stringified payload.\n if ((raw.startsWith('\"') && raw.endsWith('\"')) || (raw.startsWith(\"'\") && raw.endsWith(\"'\"))) {\n const unquoted = raw.slice(1, -1).replace(/\\\\(.)/g, '$1');\n try {\n const parsed = JSON.parse(unquoted);\n if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n throw new Error('expected object');\n } catch (err) {\n throw new Error(\n `@obligation_params string payload isn't valid JSON object: ${(err as Error).message}`,\n );\n }\n }\n // Otherwise treat as a direct JSON5-ish object literal — standardise quotes.\n const normalised = coerceToJson(raw);\n try {\n const parsed = JSON.parse(normalised);\n if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n throw new Error('expected object');\n } catch (err) {\n throw new Error(\n `@obligation_params payload isn't a valid JSON object (after coercion): ${raw}: ${(err as Error).message}`,\n );\n }\n}\n\n/**\n * Light JSON coercion for the Cedar annotation param form. Converts\n * single-quoted strings to double-quoted, and unquoted object keys to quoted.\n * Good enough for the param shapes in ARP-policy-examples.md §5 — not a full\n * JSON5 parser.\n */\nfunction coerceToJson(raw: string): string {\n let out = raw.replace(/'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/g, (_m, inner: string) => {\n return JSON.stringify(inner);\n });\n // Quote bare keys: {{ foo: ... }} → {{ \"foo\": ... }}\n out = out.replace(\n /([{,]\\s*)([A-Za-z_][A-Za-z0-9_]*)(\\s*:)/g,\n (_m, lead: string, key: string, tail: string) => `${lead}\"${key}\"${tail}`,\n );\n return out;\n}\n\nfunction extractExistingId(text: string): string | null {\n const match = /@id\\s*\\(\\s*\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"\\s*\\)/m.exec(text);\n return match ? (match[1] ?? null) : null;\n}\n\nexport function obligationRecord(parsed: ParsedObligationPolicy): Obligation {\n return { type: parsed.obligationType, params: parsed.params };\n}\n","import type { Obligation } from '@kybernesis/arp-spec';\nimport {\n assertSchemaParses,\n buildCedarCall,\n cedarIsAuthorized,\n} from './cedar.js';\nimport { parseObligationPolicy, obligationRecord } from './obligations.js';\nimport type { Pdp, PdpDecision } from './types.js';\n\n/**\n * Build a PDP bound to a Cedar schema. The schema is parsed eagerly so\n * malformed schemas fail at construction rather than on first evaluation.\n *\n * v0 does NOT enforce the schema at request time — `isAuthorized` is called\n * without `enableRequestValidation`, mirroring the posture in Phase 2. Phase\n * 5 can turn validation on once all reference agents' contexts conform.\n */\nexport function createPdp(schemaJson: string): Pdp {\n assertSchemaParses(schemaJson);\n\n return {\n evaluate(input): PdpDecision {\n // Concatenate all permit/forbid entries into a single policy-set string.\n // Each entry may contain multiple Cedar policies (e.g. a permit + a\n // sibling forbid). Cedar auto-generates IDs (`policy0`, `policy1`, ...)\n // which surface in diagnostics.reason; we return those as\n // `policies_fired`.\n const joined = input.cedarPolicies\n .map((text) => text.trim())\n .filter((t) => t.length > 0)\n .join('\\n');\n\n const decisionCall = buildCedarCall({\n policies: joined,\n principal: input.principal,\n action: input.action,\n resource: input.resource,\n context: input.context ?? {},\n entities: input.entities ?? [],\n });\n const decisionAnswer = cedarIsAuthorized(decisionCall);\n if (decisionAnswer.type !== 'success') {\n throw new Error(\n `Cedar authorization failed: ${JSON.stringify(decisionAnswer.errors)}`,\n );\n }\n const rawDecision = decisionAnswer.response.decision;\n const decision: 'allow' | 'deny' = rawDecision === 'Allow' ? 'allow' : 'deny';\n\n const firedDecisionPolicies = toStringArray(decisionAnswer.response.diagnostics.reason);\n const reasons = decisionAnswer.response.diagnostics.errors.map(\n (e) => e.error.message,\n );\n\n const obligations: Obligation[] = [];\n const obligationFired: string[] = [];\n if (decision === 'allow' && input.obligationPolicies?.length) {\n const parsed = input.obligationPolicies.map((text, idx) =>\n parseObligationPolicy(text, `o_${idx}`),\n );\n const obligationMap: Record<string, string> = {};\n for (const p of parsed) obligationMap[p.id] = p.cleanedText;\n\n const obligationCall = buildCedarCall({\n policies: obligationMap,\n principal: input.principal,\n action: input.action,\n resource: input.resource,\n context: input.context ?? {},\n entities: input.entities ?? [],\n });\n const obligationAnswer = cedarIsAuthorized(obligationCall);\n if (obligationAnswer.type !== 'success') {\n throw new Error(\n `Cedar obligation evaluation failed: ${JSON.stringify(obligationAnswer.errors)}`,\n );\n }\n const fired = new Set(\n toStringArray(obligationAnswer.response.diagnostics.reason),\n );\n for (const p of parsed) {\n if (fired.has(p.id)) {\n obligations.push(obligationRecord(p));\n obligationFired.push(p.id);\n }\n }\n }\n\n return {\n decision,\n obligations,\n policies_fired: [...firedDecisionPolicies, ...obligationFired],\n reasons,\n };\n },\n };\n}\n\nfunction toStringArray(value: unknown): string[] {\n // cedar-wasm types `reason` as `Set<String>` at the TS layer but returns a\n // plain array at runtime. Defensive normalise — treat either as iterable.\n if (Array.isArray(value)) {\n return value.map((v) => String(v));\n }\n if (value && typeof value === 'object' && Symbol.iterator in value) {\n return Array.from(value as Iterable<unknown>).map((v) => String(v));\n }\n return [];\n}\n\n"]}
@@ -0,0 +1,77 @@
1
+ import { Obligation } from '@kybernesis/arp-spec';
2
+ import { EntityJson, CedarValueJson } from '@cedar-policy/cedar-wasm';
3
+
4
+ type PdpDecision = {
5
+ decision: 'allow' | 'deny';
6
+ obligations: Obligation[];
7
+ policies_fired: string[];
8
+ reasons: string[];
9
+ };
10
+ interface Entity {
11
+ type: string;
12
+ id: string;
13
+ attrs?: Record<string, unknown>;
14
+ parents?: Array<{
15
+ type: string;
16
+ id: string;
17
+ }>;
18
+ }
19
+ interface EvaluateInput {
20
+ cedarPolicies: string[];
21
+ obligationPolicies?: string[];
22
+ principal: Entity;
23
+ action: string;
24
+ resource: Entity;
25
+ context?: Record<string, unknown>;
26
+ /** Extra entity records to hand to Cedar (parents, attribute lookups). */
27
+ entities?: Entity[];
28
+ }
29
+ interface Pdp {
30
+ evaluate(input: EvaluateInput): PdpDecision;
31
+ }
32
+
33
+ /**
34
+ * Build a PDP bound to a Cedar schema. The schema is parsed eagerly so
35
+ * malformed schemas fail at construction rather than on first evaluation.
36
+ *
37
+ * v0 does NOT enforce the schema at request time — `isAuthorized` is called
38
+ * without `enableRequestValidation`, mirroring the posture in Phase 2. Phase
39
+ * 5 can turn validation on once all reference agents' contexts conform.
40
+ */
41
+ declare function createPdp(schemaJson: string): Pdp;
42
+
43
+ interface ParsedObligationPolicy {
44
+ /** Auto-generated stable id assigned to the cleaned-up policy text. */
45
+ id: string;
46
+ /** Obligation `type` from `@obligation("...")`. */
47
+ obligationType: string;
48
+ /** Parsed params from `@obligation_params(...)` (object, JSON string, or empty). */
49
+ params: Record<string, unknown>;
50
+ /** Policy text with ARP-specific annotations removed, safe for Cedar. */
51
+ cleanedText: string;
52
+ }
53
+ /**
54
+ * Parse ARP's non-standard obligation annotations out of a Cedar policy.
55
+ *
56
+ * Accepted forms:
57
+ *
58
+ * @obligation("redact_fields")
59
+ * @obligation_params({ "fields": ["client.name"] }) // object literal
60
+ * @obligation_params("{\"fields\":[...]}") // JSON string
61
+ *
62
+ * The returned `cleanedText` has both annotations removed and is safe to pass
63
+ * straight to Cedar. Auto-prefixes an `@id(...)` so callers can track the
64
+ * policy's `policies_fired` id.
65
+ */
66
+ declare function parseObligationPolicy(text: string, fallbackId: string): ParsedObligationPolicy;
67
+ declare function obligationRecord(parsed: ParsedObligationPolicy): Obligation;
68
+
69
+ /**
70
+ * Convert a plain JS value into the Cedar JSON form accepted by
71
+ * `@cedar-policy/cedar-wasm`. Arrays stay arrays, objects stay objects,
72
+ * primitives stay primitives. Dates serialise to their ISO string.
73
+ */
74
+ declare function toCedarValue(value: unknown): CedarValueJson;
75
+ declare function entityToJson(entity: Entity): EntityJson;
76
+
77
+ export { type Entity, type EvaluateInput, type ParsedObligationPolicy, type Pdp, type PdpDecision, createPdp, entityToJson, obligationRecord, parseObligationPolicy, toCedarValue };
@@ -0,0 +1,77 @@
1
+ import { Obligation } from '@kybernesis/arp-spec';
2
+ import { EntityJson, CedarValueJson } from '@cedar-policy/cedar-wasm';
3
+
4
+ type PdpDecision = {
5
+ decision: 'allow' | 'deny';
6
+ obligations: Obligation[];
7
+ policies_fired: string[];
8
+ reasons: string[];
9
+ };
10
+ interface Entity {
11
+ type: string;
12
+ id: string;
13
+ attrs?: Record<string, unknown>;
14
+ parents?: Array<{
15
+ type: string;
16
+ id: string;
17
+ }>;
18
+ }
19
+ interface EvaluateInput {
20
+ cedarPolicies: string[];
21
+ obligationPolicies?: string[];
22
+ principal: Entity;
23
+ action: string;
24
+ resource: Entity;
25
+ context?: Record<string, unknown>;
26
+ /** Extra entity records to hand to Cedar (parents, attribute lookups). */
27
+ entities?: Entity[];
28
+ }
29
+ interface Pdp {
30
+ evaluate(input: EvaluateInput): PdpDecision;
31
+ }
32
+
33
+ /**
34
+ * Build a PDP bound to a Cedar schema. The schema is parsed eagerly so
35
+ * malformed schemas fail at construction rather than on first evaluation.
36
+ *
37
+ * v0 does NOT enforce the schema at request time — `isAuthorized` is called
38
+ * without `enableRequestValidation`, mirroring the posture in Phase 2. Phase
39
+ * 5 can turn validation on once all reference agents' contexts conform.
40
+ */
41
+ declare function createPdp(schemaJson: string): Pdp;
42
+
43
+ interface ParsedObligationPolicy {
44
+ /** Auto-generated stable id assigned to the cleaned-up policy text. */
45
+ id: string;
46
+ /** Obligation `type` from `@obligation("...")`. */
47
+ obligationType: string;
48
+ /** Parsed params from `@obligation_params(...)` (object, JSON string, or empty). */
49
+ params: Record<string, unknown>;
50
+ /** Policy text with ARP-specific annotations removed, safe for Cedar. */
51
+ cleanedText: string;
52
+ }
53
+ /**
54
+ * Parse ARP's non-standard obligation annotations out of a Cedar policy.
55
+ *
56
+ * Accepted forms:
57
+ *
58
+ * @obligation("redact_fields")
59
+ * @obligation_params({ "fields": ["client.name"] }) // object literal
60
+ * @obligation_params("{\"fields\":[...]}") // JSON string
61
+ *
62
+ * The returned `cleanedText` has both annotations removed and is safe to pass
63
+ * straight to Cedar. Auto-prefixes an `@id(...)` so callers can track the
64
+ * policy's `policies_fired` id.
65
+ */
66
+ declare function parseObligationPolicy(text: string, fallbackId: string): ParsedObligationPolicy;
67
+ declare function obligationRecord(parsed: ParsedObligationPolicy): Obligation;
68
+
69
+ /**
70
+ * Convert a plain JS value into the Cedar JSON form accepted by
71
+ * `@cedar-policy/cedar-wasm`. Arrays stay arrays, objects stay objects,
72
+ * primitives stay primitives. Dates serialise to their ISO string.
73
+ */
74
+ declare function toCedarValue(value: unknown): CedarValueJson;
75
+ declare function entityToJson(entity: Entity): EntityJson;
76
+
77
+ export { type Entity, type EvaluateInput, type ParsedObligationPolicy, type Pdp, type PdpDecision, createPdp, entityToJson, obligationRecord, parseObligationPolicy, toCedarValue };
package/dist/index.js ADDED
@@ -0,0 +1,272 @@
1
+ import { checkParseSchema, isAuthorized } from '@cedar-policy/cedar-wasm';
2
+
3
+ // src/cedar.ts
4
+ function assertSchemaParses(schemaJson) {
5
+ const trimmed = schemaJson.trim();
6
+ if (!trimmed) return;
7
+ const r = checkParseSchema(trimmed);
8
+ if (r.type !== "success") {
9
+ throw new Error(
10
+ `Cedar schema failed to parse: ${JSON.stringify(r.errors, null, 2)}`
11
+ );
12
+ }
13
+ }
14
+ function toCedarValue(value) {
15
+ if (value === null || value === void 0) return null;
16
+ if (typeof value === "string" || typeof value === "boolean") return value;
17
+ if (typeof value === "number") {
18
+ if (!Number.isFinite(value)) {
19
+ throw new Error(`non-finite number cannot be encoded for Cedar: ${value}`);
20
+ }
21
+ return value;
22
+ }
23
+ if (value instanceof Date) {
24
+ return value.toISOString();
25
+ }
26
+ if (Array.isArray(value)) {
27
+ return value.map(toCedarValue);
28
+ }
29
+ if (typeof value === "object") {
30
+ const out = {};
31
+ for (const [k, v] of Object.entries(value)) {
32
+ out[k] = toCedarValue(v);
33
+ }
34
+ return out;
35
+ }
36
+ throw new Error(`unsupported Cedar value type: ${typeof value}`);
37
+ }
38
+ function toContext(ctx) {
39
+ if (!ctx) return {};
40
+ const out = {};
41
+ for (const [k, v] of Object.entries(ctx)) {
42
+ out[k] = toCedarValue(v);
43
+ }
44
+ return out;
45
+ }
46
+ function entityToJson(entity) {
47
+ return {
48
+ uid: { type: entity.type, id: entity.id },
49
+ attrs: entity.attrs ? Object.fromEntries(
50
+ Object.entries(entity.attrs).map(([k, v]) => [k, toCedarValue(v)])
51
+ ) : {},
52
+ parents: (entity.parents ?? []).map((p) => ({ type: p.type, id: p.id }))
53
+ };
54
+ }
55
+ function buildCedarCall(opts) {
56
+ const actionType = opts.actionType ?? "Action";
57
+ const all = [opts.principal, opts.resource, ...opts.entities ?? []];
58
+ const seen = /* @__PURE__ */ new Set();
59
+ const entities = [];
60
+ for (const e of all) {
61
+ const key = `${e.type}::${e.id}`;
62
+ if (seen.has(key)) continue;
63
+ seen.add(key);
64
+ entities.push(entityToJson(e));
65
+ }
66
+ return {
67
+ principal: { type: opts.principal.type, id: opts.principal.id },
68
+ action: { type: actionType, id: opts.action },
69
+ resource: { type: opts.resource.type, id: opts.resource.id },
70
+ context: toContext(opts.context),
71
+ slice: {
72
+ policies: opts.policies,
73
+ entities,
74
+ templates: null,
75
+ templateInstantiations: null
76
+ }
77
+ };
78
+ }
79
+ function cedarIsAuthorized(call) {
80
+ return isAuthorized(call);
81
+ }
82
+
83
+ // src/obligations.ts
84
+ function parseObligationPolicy(text, fallbackId) {
85
+ const { type, rest: afterType } = extractObligationType(text);
86
+ const { params, rest: afterParams } = extractObligationParams(afterType);
87
+ const existingId = extractExistingId(afterParams);
88
+ const cleaned = afterParams.trim();
89
+ const id = existingId ?? fallbackId;
90
+ const withId = existingId ? cleaned : `@id("${id}")
91
+ ${cleaned}`;
92
+ return {
93
+ id,
94
+ obligationType: type,
95
+ params,
96
+ cleanedText: withId
97
+ };
98
+ }
99
+ function extractObligationType(text) {
100
+ const match = /@obligation\s*\(\s*"([^"\\]*(?:\\.[^"\\]*)*)"\s*\)/m.exec(text);
101
+ if (!match) {
102
+ throw new Error(
103
+ 'obligation policy missing @obligation("<type>") annotation'
104
+ );
105
+ }
106
+ const rest = text.slice(0, match.index) + text.slice(match.index + match[0].length);
107
+ return { type: match[1] ?? "", rest };
108
+ }
109
+ function extractObligationParams(text) {
110
+ const headIdx = text.indexOf("@obligation_params");
111
+ if (headIdx < 0) return { params: {}, rest: text };
112
+ const parenIdx = text.indexOf("(", headIdx);
113
+ if (parenIdx < 0) return { params: {}, rest: text };
114
+ let depth = 1;
115
+ let inString = null;
116
+ let i = parenIdx + 1;
117
+ while (i < text.length && depth > 0) {
118
+ const ch = text[i];
119
+ if (inString) {
120
+ if (ch === "\\") {
121
+ i += 2;
122
+ continue;
123
+ }
124
+ if (ch === inString) inString = null;
125
+ i++;
126
+ continue;
127
+ }
128
+ if (ch === '"' || ch === "'") {
129
+ inString = ch;
130
+ i++;
131
+ continue;
132
+ }
133
+ if (ch === "(" || ch === "{" || ch === "[") depth++;
134
+ else if (ch === ")" || ch === "}" || ch === "]") depth--;
135
+ if (depth === 0 && ch === ")") break;
136
+ i++;
137
+ }
138
+ if (depth !== 0) {
139
+ throw new Error("unbalanced @obligation_params annotation");
140
+ }
141
+ const innerRaw = text.slice(parenIdx + 1, i).trim();
142
+ const rest = text.slice(0, headIdx) + text.slice(i + 1);
143
+ const params = parseParamValue(innerRaw);
144
+ return { params, rest };
145
+ }
146
+ function parseParamValue(raw) {
147
+ if (!raw) return {};
148
+ if (raw.startsWith('"') && raw.endsWith('"') || raw.startsWith("'") && raw.endsWith("'")) {
149
+ const unquoted = raw.slice(1, -1).replace(/\\(.)/g, "$1");
150
+ try {
151
+ const parsed = JSON.parse(unquoted);
152
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
153
+ return parsed;
154
+ }
155
+ throw new Error("expected object");
156
+ } catch (err) {
157
+ throw new Error(
158
+ `@obligation_params string payload isn't valid JSON object: ${err.message}`
159
+ );
160
+ }
161
+ }
162
+ const normalised = coerceToJson(raw);
163
+ try {
164
+ const parsed = JSON.parse(normalised);
165
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
166
+ return parsed;
167
+ }
168
+ throw new Error("expected object");
169
+ } catch (err) {
170
+ throw new Error(
171
+ `@obligation_params payload isn't a valid JSON object (after coercion): ${raw}: ${err.message}`
172
+ );
173
+ }
174
+ }
175
+ function coerceToJson(raw) {
176
+ let out = raw.replace(/'([^'\\]*(?:\\.[^'\\]*)*)'/g, (_m, inner) => {
177
+ return JSON.stringify(inner);
178
+ });
179
+ out = out.replace(
180
+ /([{,]\s*)([A-Za-z_][A-Za-z0-9_]*)(\s*:)/g,
181
+ (_m, lead, key, tail) => `${lead}"${key}"${tail}`
182
+ );
183
+ return out;
184
+ }
185
+ function extractExistingId(text) {
186
+ const match = /@id\s*\(\s*"([^"\\]*(?:\\.[^"\\]*)*)"\s*\)/m.exec(text);
187
+ return match ? match[1] ?? null : null;
188
+ }
189
+ function obligationRecord(parsed) {
190
+ return { type: parsed.obligationType, params: parsed.params };
191
+ }
192
+
193
+ // src/pdp.ts
194
+ function createPdp(schemaJson) {
195
+ assertSchemaParses(schemaJson);
196
+ return {
197
+ evaluate(input) {
198
+ const joined = input.cedarPolicies.map((text) => text.trim()).filter((t) => t.length > 0).join("\n");
199
+ const decisionCall = buildCedarCall({
200
+ policies: joined,
201
+ principal: input.principal,
202
+ action: input.action,
203
+ resource: input.resource,
204
+ context: input.context ?? {},
205
+ entities: input.entities ?? []
206
+ });
207
+ const decisionAnswer = cedarIsAuthorized(decisionCall);
208
+ if (decisionAnswer.type !== "success") {
209
+ throw new Error(
210
+ `Cedar authorization failed: ${JSON.stringify(decisionAnswer.errors)}`
211
+ );
212
+ }
213
+ const rawDecision = decisionAnswer.response.decision;
214
+ const decision = rawDecision === "Allow" ? "allow" : "deny";
215
+ const firedDecisionPolicies = toStringArray(decisionAnswer.response.diagnostics.reason);
216
+ const reasons = decisionAnswer.response.diagnostics.errors.map(
217
+ (e) => e.error.message
218
+ );
219
+ const obligations = [];
220
+ const obligationFired = [];
221
+ if (decision === "allow" && input.obligationPolicies?.length) {
222
+ const parsed = input.obligationPolicies.map(
223
+ (text, idx) => parseObligationPolicy(text, `o_${idx}`)
224
+ );
225
+ const obligationMap = {};
226
+ for (const p of parsed) obligationMap[p.id] = p.cleanedText;
227
+ const obligationCall = buildCedarCall({
228
+ policies: obligationMap,
229
+ principal: input.principal,
230
+ action: input.action,
231
+ resource: input.resource,
232
+ context: input.context ?? {},
233
+ entities: input.entities ?? []
234
+ });
235
+ const obligationAnswer = cedarIsAuthorized(obligationCall);
236
+ if (obligationAnswer.type !== "success") {
237
+ throw new Error(
238
+ `Cedar obligation evaluation failed: ${JSON.stringify(obligationAnswer.errors)}`
239
+ );
240
+ }
241
+ const fired = new Set(
242
+ toStringArray(obligationAnswer.response.diagnostics.reason)
243
+ );
244
+ for (const p of parsed) {
245
+ if (fired.has(p.id)) {
246
+ obligations.push(obligationRecord(p));
247
+ obligationFired.push(p.id);
248
+ }
249
+ }
250
+ }
251
+ return {
252
+ decision,
253
+ obligations,
254
+ policies_fired: [...firedDecisionPolicies, ...obligationFired],
255
+ reasons
256
+ };
257
+ }
258
+ };
259
+ }
260
+ function toStringArray(value) {
261
+ if (Array.isArray(value)) {
262
+ return value.map((v) => String(v));
263
+ }
264
+ if (value && typeof value === "object" && Symbol.iterator in value) {
265
+ return Array.from(value).map((v) => String(v));
266
+ }
267
+ return [];
268
+ }
269
+
270
+ export { createPdp, entityToJson, obligationRecord, parseObligationPolicy, toCedarValue };
271
+ //# sourceMappingURL=index.js.map
272
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cedar.ts","../src/obligations.ts","../src/pdp.ts"],"names":[],"mappings":";;;AASO,SAAS,mBAAmB,UAAA,EAA0B;AAC3D,EAAA,MAAM,OAAA,GAAU,WAAW,IAAA,EAAK;AAChC,EAAA,IAAI,CAAC,OAAA,EAAS;AACd,EAAA,MAAM,CAAA,GAAI,iBAAiB,OAAO,CAAA;AAClC,EAAA,IAAI,CAAA,CAAE,SAAS,SAAA,EAAW;AACxB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,iCAAiC,IAAA,CAAK,SAAA,CAAU,EAAE,MAAA,EAAQ,IAAA,EAAM,CAAC,CAAC,CAAA;AAAA,KACpE;AAAA,EACF;AACF;AAOO,SAAS,aAAa,KAAA,EAAgC;AAC3D,EAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,KAAA,KAAU,MAAA,EAAW,OAAO,IAAA;AAClD,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,OAAO,KAAA,KAAU,WAAW,OAAO,KAAA;AACpE,EAAA,IAAI,OAAO,UAAU,QAAA,EAAU;AAC7B,IAAA,IAAI,CAAC,MAAA,CAAO,QAAA,CAAS,KAAK,CAAA,EAAG;AAC3B,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,+CAAA,EAAkD,KAAK,CAAA,CAAE,CAAA;AAAA,IAC3E;AACA,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,IAAI,iBAAiB,IAAA,EAAM;AACzB,IAAA,OAAO,MAAM,WAAA,EAAY;AAAA,EAC3B;AACA,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,EAAG;AACxB,IAAA,O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,MAAA,OAAO;AAAA,QACL,QAAA;AAAA,QACA,WAAA;AAAA,QACA,cAAA,EAAgB,CAAC,GAAG,qBAAA,EAAuB,GAAG,eAAe,CAAA;AAAA,QAC7D;AAAA,OACF;AAAA,IACF;AAAA,GACF;AACF;AAEA,SAAS,cAAc,KAAA,EAA0B;AAG/C,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,EAAG;AACxB,IAAA,OAAO,MAAM,GAAA,CAAI,CAAC,CAAA,KAAM,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA,EACnC;AACA,EAAA,IAAI,SAAS,OAAO,KAAA,KAAU,QAAA,IAAY,MAAA,CAAO,YAAY,KAAA,EAAO;AAClE,IAAA,OAAO,KAAA,CAAM,KAAK,KAA0B,CAAA,CAAE,IAAI,CAAC,CAAA,KAAM,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA,EACpE;AACA,EAAA,OAAO,EAAC;AACV","file":"index.js","sourcesContent":["import {\n checkParseSchema,\n isAuthorized,\n type AuthorizationCall,\n type CedarValueJson,\n type EntityJson,\n} from '@cedar-policy/cedar-wasm';\nimport type { Entity } from './types.js';\n\nexport function assertSchemaParses(schemaJson: string): void {\n const trimmed = schemaJson.trim();\n if (!trimmed) return;\n const r = checkParseSchema(trimmed);\n if (r.type !== 'success') {\n throw new Error(\n `Cedar schema failed to parse: ${JSON.stringify(r.errors, null, 2)}`,\n );\n }\n}\n\n/**\n * Convert a plain JS value into the Cedar JSON form accepted by\n * `@cedar-policy/cedar-wasm`. Arrays stay arrays, objects stay objects,\n * primitives stay primitives. Dates serialise to their ISO string.\n */\nexport function toCedarValue(value: unknown): CedarValueJson {\n if (value === null || value === undefined) return null;\n if (typeof value === 'string' || typeof value === 'boolean') return value;\n if (typeof value === 'number') {\n if (!Number.isFinite(value)) {\n throw new Error(`non-finite number cannot be encoded for Cedar: ${value}`);\n }\n return value;\n }\n if (value instanceof Date) {\n return value.toISOString();\n }\n if (Array.isArray(value)) {\n return value.map(toCedarValue);\n }\n if (typeof value === 'object') {\n const out: Record<string, CedarValueJson> = {};\n for (const [k, v] of Object.entries(value as Record<string, unknown>)) {\n out[k] = toCedarValue(v);\n }\n return out;\n }\n throw new Error(`unsupported Cedar value type: ${typeof value}`);\n}\n\nexport function toContext(\n ctx: Record<string, unknown> | undefined,\n): Record<string, CedarValueJson> {\n if (!ctx) return {};\n const out: Record<string, CedarValueJson> = {};\n for (const [k, v] of Object.entries(ctx)) {\n out[k] = toCedarValue(v);\n }\n return out;\n}\n\nexport function entityToJson(entity: Entity): EntityJson {\n return {\n uid: { type: entity.type, id: entity.id },\n attrs: entity.attrs\n ? Object.fromEntries(\n Object.entries(entity.attrs).map(([k, v]) => [k, toCedarValue(v)]),\n )\n : {},\n parents: (entity.parents ?? []).map((p) => ({ type: p.type, id: p.id })),\n };\n}\n\nexport interface CedarCallParts {\n call: AuthorizationCall;\n}\n\nexport function buildCedarCall(opts: {\n policies: Record<string, string> | string;\n principal: Entity;\n action: string;\n resource: Entity;\n context?: Record<string, unknown>;\n entities?: Entity[];\n actionType?: string;\n}): AuthorizationCall {\n const actionType = opts.actionType ?? 'Action';\n const all: Entity[] = [opts.principal, opts.resource, ...(opts.entities ?? [])];\n const seen = new Set<string>();\n const entities: EntityJson[] = [];\n for (const e of all) {\n const key = `${e.type}::${e.id}`;\n if (seen.has(key)) continue;\n seen.add(key);\n entities.push(entityToJson(e));\n }\n return {\n principal: { type: opts.principal.type, id: opts.principal.id },\n action: { type: actionType, id: opts.action },\n resource: { type: opts.resource.type, id: opts.resource.id },\n context: toContext(opts.context),\n slice: {\n policies: opts.policies,\n entities,\n templates: null,\n templateInstantiations: null,\n },\n };\n}\n\nexport function cedarIsAuthorized(call: AuthorizationCall) {\n return isAuthorized(call);\n}\n","import type { Obligation } from '@kybernesis/arp-spec';\n\nexport interface ParsedObligationPolicy {\n /** Auto-generated stable id assigned to the cleaned-up policy text. */\n id: string;\n /** Obligation `type` from `@obligation(\"...\")`. */\n obligationType: string;\n /** Parsed params from `@obligation_params(...)` (object, JSON string, or empty). */\n params: Record<string, unknown>;\n /** Policy text with ARP-specific annotations removed, safe for Cedar. */\n cleanedText: string;\n}\n\n/**\n * Parse ARP's non-standard obligation annotations out of a Cedar policy.\n *\n * Accepted forms:\n *\n * @obligation(\"redact_fields\")\n * @obligation_params({ \"fields\": [\"client.name\"] }) // object literal\n * @obligation_params(\"{\\\"fields\\\":[...]}\") // JSON string\n *\n * The returned `cleanedText` has both annotations removed and is safe to pass\n * straight to Cedar. Auto-prefixes an `@id(...)` so callers can track the\n * policy's `policies_fired` id.\n */\nexport function parseObligationPolicy(\n text: string,\n fallbackId: string,\n): ParsedObligationPolicy {\n const { type, rest: afterType } = extractObligationType(text);\n const { params, rest: afterParams } = extractObligationParams(afterType);\n const existingId = extractExistingId(afterParams);\n const cleaned = afterParams.trim();\n const id = existingId ?? fallbackId;\n const withId = existingId ? cleaned : `@id(\"${id}\")\\n${cleaned}`;\n return {\n id,\n obligationType: type,\n params,\n cleanedText: withId,\n };\n}\n\nfunction extractObligationType(text: string): { type: string; rest: string } {\n const match = /@obligation\\s*\\(\\s*\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"\\s*\\)/m.exec(text);\n if (!match) {\n throw new Error(\n 'obligation policy missing @obligation(\"<type>\") annotation',\n );\n }\n const rest = text.slice(0, match.index) + text.slice(match.index + match[0].length);\n return { type: match[1] ?? '', rest };\n}\n\nfunction extractObligationParams(\n text: string,\n): { params: Record<string, unknown>; rest: string } {\n const headIdx = text.indexOf('@obligation_params');\n if (headIdx < 0) return { params: {}, rest: text };\n\n // Find the opening paren after the keyword.\n const parenIdx = text.indexOf('(', headIdx);\n if (parenIdx < 0) return { params: {}, rest: text };\n\n // Walk forward balancing braces/quotes to find the matching close paren.\n let depth = 1;\n let inString: '\"' | \"'\" | null = null;\n let i = parenIdx + 1;\n while (i < text.length && depth > 0) {\n const ch = text[i];\n if (inString) {\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === inString) inString = null;\n i++;\n continue;\n }\n if (ch === '\"' || ch === \"'\") {\n inString = ch;\n i++;\n continue;\n }\n if (ch === '(' || ch === '{' || ch === '[') depth++;\n else if (ch === ')' || ch === '}' || ch === ']') depth--;\n if (depth === 0 && ch === ')') break;\n i++;\n }\n if (depth !== 0) {\n throw new Error('unbalanced @obligation_params annotation');\n }\n\n const innerRaw = text.slice(parenIdx + 1, i).trim();\n const rest = text.slice(0, headIdx) + text.slice(i + 1);\n const params = parseParamValue(innerRaw);\n return { params, rest };\n}\n\nfunction parseParamValue(raw: string): Record<string, unknown> {\n if (!raw) return {};\n // If the whole value is a quoted string, it's a JSON-stringified payload.\n if ((raw.startsWith('\"') && raw.endsWith('\"')) || (raw.startsWith(\"'\") && raw.endsWith(\"'\"))) {\n const unquoted = raw.slice(1, -1).replace(/\\\\(.)/g, '$1');\n try {\n const parsed = JSON.parse(unquoted);\n if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n throw new Error('expected object');\n } catch (err) {\n throw new Error(\n `@obligation_params string payload isn't valid JSON object: ${(err as Error).message}`,\n );\n }\n }\n // Otherwise treat as a direct JSON5-ish object literal — standardise quotes.\n const normalised = coerceToJson(raw);\n try {\n const parsed = JSON.parse(normalised);\n if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n throw new Error('expected object');\n } catch (err) {\n throw new Error(\n `@obligation_params payload isn't a valid JSON object (after coercion): ${raw}: ${(err as Error).message}`,\n );\n }\n}\n\n/**\n * Light JSON coercion for the Cedar annotation param form. Converts\n * single-quoted strings to double-quoted, and unquoted object keys to quoted.\n * Good enough for the param shapes in ARP-policy-examples.md §5 — not a full\n * JSON5 parser.\n */\nfunction coerceToJson(raw: string): string {\n let out = raw.replace(/'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/g, (_m, inner: string) => {\n return JSON.stringify(inner);\n });\n // Quote bare keys: {{ foo: ... }} → {{ \"foo\": ... }}\n out = out.replace(\n /([{,]\\s*)([A-Za-z_][A-Za-z0-9_]*)(\\s*:)/g,\n (_m, lead: string, key: string, tail: string) => `${lead}\"${key}\"${tail}`,\n );\n return out;\n}\n\nfunction extractExistingId(text: string): string | null {\n const match = /@id\\s*\\(\\s*\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"\\s*\\)/m.exec(text);\n return match ? (match[1] ?? null) : null;\n}\n\nexport function obligationRecord(parsed: ParsedObligationPolicy): Obligation {\n return { type: parsed.obligationType, params: parsed.params };\n}\n","import type { Obligation } from '@kybernesis/arp-spec';\nimport {\n assertSchemaParses,\n buildCedarCall,\n cedarIsAuthorized,\n} from './cedar.js';\nimport { parseObligationPolicy, obligationRecord } from './obligations.js';\nimport type { Pdp, PdpDecision } from './types.js';\n\n/**\n * Build a PDP bound to a Cedar schema. The schema is parsed eagerly so\n * malformed schemas fail at construction rather than on first evaluation.\n *\n * v0 does NOT enforce the schema at request time — `isAuthorized` is called\n * without `enableRequestValidation`, mirroring the posture in Phase 2. Phase\n * 5 can turn validation on once all reference agents' contexts conform.\n */\nexport function createPdp(schemaJson: string): Pdp {\n assertSchemaParses(schemaJson);\n\n return {\n evaluate(input): PdpDecision {\n // Concatenate all permit/forbid entries into a single policy-set string.\n // Each entry may contain multiple Cedar policies (e.g. a permit + a\n // sibling forbid). Cedar auto-generates IDs (`policy0`, `policy1`, ...)\n // which surface in diagnostics.reason; we return those as\n // `policies_fired`.\n const joined = input.cedarPolicies\n .map((text) => text.trim())\n .filter((t) => t.length > 0)\n .join('\\n');\n\n const decisionCall = buildCedarCall({\n policies: joined,\n principal: input.principal,\n action: input.action,\n resource: input.resource,\n context: input.context ?? {},\n entities: input.entities ?? [],\n });\n const decisionAnswer = cedarIsAuthorized(decisionCall);\n if (decisionAnswer.type !== 'success') {\n throw new Error(\n `Cedar authorization failed: ${JSON.stringify(decisionAnswer.errors)}`,\n );\n }\n const rawDecision = decisionAnswer.response.decision;\n const decision: 'allow' | 'deny' = rawDecision === 'Allow' ? 'allow' : 'deny';\n\n const firedDecisionPolicies = toStringArray(decisionAnswer.response.diagnostics.reason);\n const reasons = decisionAnswer.response.diagnostics.errors.map(\n (e) => e.error.message,\n );\n\n const obligations: Obligation[] = [];\n const obligationFired: string[] = [];\n if (decision === 'allow' && input.obligationPolicies?.length) {\n const parsed = input.obligationPolicies.map((text, idx) =>\n parseObligationPolicy(text, `o_${idx}`),\n );\n const obligationMap: Record<string, string> = {};\n for (const p of parsed) obligationMap[p.id] = p.cleanedText;\n\n const obligationCall = buildCedarCall({\n policies: obligationMap,\n principal: input.principal,\n action: input.action,\n resource: input.resource,\n context: input.context ?? {},\n entities: input.entities ?? [],\n });\n const obligationAnswer = cedarIsAuthorized(obligationCall);\n if (obligationAnswer.type !== 'success') {\n throw new Error(\n `Cedar obligation evaluation failed: ${JSON.stringify(obligationAnswer.errors)}`,\n );\n }\n const fired = new Set(\n toStringArray(obligationAnswer.response.diagnostics.reason),\n );\n for (const p of parsed) {\n if (fired.has(p.id)) {\n obligations.push(obligationRecord(p));\n obligationFired.push(p.id);\n }\n }\n }\n\n return {\n decision,\n obligations,\n policies_fired: [...firedDecisionPolicies, ...obligationFired],\n reasons,\n };\n },\n };\n}\n\nfunction toStringArray(value: unknown): string[] {\n // cedar-wasm types `reason` as `Set<String>` at the TS layer but returns a\n // plain array at runtime. Defensive normalise — treat either as iterable.\n if (Array.isArray(value)) {\n return value.map((v) => String(v));\n }\n if (value && typeof value === 'object' && Symbol.iterator in value) {\n return Array.from(value as Iterable<unknown>).map((v) => String(v));\n }\n return [];\n}\n\n"]}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@kybernesis/arp-pdp",
3
+ "version": "0.3.0",
4
+ "description": "ARP Policy Decision Point — wraps @cedar-policy/cedar-wasm, adds ARP's @obligation annotation semantics.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/KybernesisAI/arp.git",
9
+ "directory": "packages/pdp"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js",
22
+ "require": "./dist/index.cjs"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
29
+ "dependencies": {
30
+ "@cedar-policy/cedar-wasm": "^3.2.4",
31
+ "@kybernesis/arp-spec": "0.2.0"
32
+ },
33
+ "devDependencies": {},
34
+ "scripts": {
35
+ "build": "tsup",
36
+ "test": "vitest run",
37
+ "typecheck": "tsc --noEmit",
38
+ "lint": "eslint src tests"
39
+ }
40
+ }