@dads-framework/postgres-access 0.0.0-stage → 0.1.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.
Files changed (105) hide show
  1. package/BENCHMARK.md +275 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +212 -2
  4. package/dist/compile-access-policy.d.ts +9 -0
  5. package/dist/compile-access-policy.d.ts.map +1 -0
  6. package/dist/compile-access-policy.js +77 -0
  7. package/dist/compile-access-policy.js.map +1 -0
  8. package/dist/drizzle-executor.d.ts +5 -0
  9. package/dist/drizzle-executor.d.ts.map +1 -0
  10. package/dist/drizzle-executor.js +25 -0
  11. package/dist/drizzle-executor.js.map +1 -0
  12. package/dist/index.d.ts +8 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +8 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/lookup-primary-pool.d.ts +5 -0
  17. package/dist/lookup-primary-pool.d.ts.map +1 -0
  18. package/dist/lookup-primary-pool.js +24 -0
  19. package/dist/lookup-primary-pool.js.map +1 -0
  20. package/dist/lookup-revision-query.d.ts +7 -0
  21. package/dist/lookup-revision-query.d.ts.map +1 -0
  22. package/dist/lookup-revision-query.js +73 -0
  23. package/dist/lookup-revision-query.js.map +1 -0
  24. package/dist/lookup-revision-schema.d.ts +10 -0
  25. package/dist/lookup-revision-schema.d.ts.map +1 -0
  26. package/dist/lookup-revision-schema.js +75 -0
  27. package/dist/lookup-revision-schema.js.map +1 -0
  28. package/dist/lower-permissions.d.ts +8 -0
  29. package/dist/lower-permissions.d.ts.map +1 -0
  30. package/dist/lower-permissions.js +76 -0
  31. package/dist/lower-permissions.js.map +1 -0
  32. package/dist/policy/compile-linear-batch.d.ts +15 -0
  33. package/dist/policy/compile-linear-batch.d.ts.map +1 -0
  34. package/dist/policy/compile-linear-batch.js +34 -0
  35. package/dist/policy/compile-linear-batch.js.map +1 -0
  36. package/dist/policy/compile-linear-check.d.ts +5 -0
  37. package/dist/policy/compile-linear-check.d.ts.map +1 -0
  38. package/dist/policy/compile-linear-check.js +71 -0
  39. package/dist/policy/compile-linear-check.js.map +1 -0
  40. package/dist/policy/compile-postgres-policy.d.ts +28 -0
  41. package/dist/policy/compile-postgres-policy.d.ts.map +1 -0
  42. package/dist/policy/compile-postgres-policy.js +189 -0
  43. package/dist/policy/compile-postgres-policy.js.map +1 -0
  44. package/dist/policy/compile-reverse-policy.d.ts +5 -0
  45. package/dist/policy/compile-reverse-policy.d.ts.map +1 -0
  46. package/dist/policy/compile-reverse-policy.js +60 -0
  47. package/dist/policy/compile-reverse-policy.js.map +1 -0
  48. package/dist/policy/compile-shared-point-check.d.ts +18 -0
  49. package/dist/policy/compile-shared-point-check.d.ts.map +1 -0
  50. package/dist/policy/compile-shared-point-check.js +126 -0
  51. package/dist/policy/compile-shared-point-check.js.map +1 -0
  52. package/dist/policy/policy-model.d.ts +86 -0
  53. package/dist/policy/policy-model.d.ts.map +1 -0
  54. package/dist/policy/policy-model.js +2 -0
  55. package/dist/policy/policy-model.js.map +1 -0
  56. package/dist/policy/policy-sql.d.ts +20 -0
  57. package/dist/policy/policy-sql.d.ts.map +1 -0
  58. package/dist/policy/policy-sql.js +59 -0
  59. package/dist/policy/policy-sql.js.map +1 -0
  60. package/dist/postgres-access.tsbuildinfo +1 -0
  61. package/dist/postgres-authorization.d.ts +35 -0
  62. package/dist/postgres-authorization.d.ts.map +1 -0
  63. package/dist/postgres-authorization.js +170 -0
  64. package/dist/postgres-authorization.js.map +1 -0
  65. package/dist/postgres-response.d.ts +4 -0
  66. package/dist/postgres-response.d.ts.map +1 -0
  67. package/dist/postgres-response.js +13 -0
  68. package/dist/postgres-response.js.map +1 -0
  69. package/dist/postgres-serialization-conflict.d.ts +11 -0
  70. package/dist/postgres-serialization-conflict.d.ts.map +1 -0
  71. package/dist/postgres-serialization-conflict.js +29 -0
  72. package/dist/postgres-serialization-conflict.js.map +1 -0
  73. package/dist/postgres-tuples.d.ts +19 -0
  74. package/dist/postgres-tuples.d.ts.map +1 -0
  75. package/dist/postgres-tuples.js +135 -0
  76. package/dist/postgres-tuples.js.map +1 -0
  77. package/dist/postgres-types.d.ts +17 -0
  78. package/dist/postgres-types.d.ts.map +1 -0
  79. package/dist/postgres-types.js +10 -0
  80. package/dist/postgres-types.js.map +1 -0
  81. package/dist/prepared-check-executor.d.ts +12 -0
  82. package/dist/prepared-check-executor.d.ts.map +1 -0
  83. package/dist/prepared-check-executor.js +39 -0
  84. package/dist/prepared-check-executor.js.map +1 -0
  85. package/dist/protected-authorization-scope.d.ts +10 -0
  86. package/dist/protected-authorization-scope.d.ts.map +1 -0
  87. package/dist/protected-authorization-scope.js +41 -0
  88. package/dist/protected-authorization-scope.js.map +1 -0
  89. package/dist/relationship-schema.d.ts +11 -0
  90. package/dist/relationship-schema.d.ts.map +1 -0
  91. package/dist/relationship-schema.js +36 -0
  92. package/dist/relationship-schema.js.map +1 -0
  93. package/dist/revisioned-lookup.d.ts +13 -0
  94. package/dist/revisioned-lookup.d.ts.map +1 -0
  95. package/dist/revisioned-lookup.js +129 -0
  96. package/dist/revisioned-lookup.js.map +1 -0
  97. package/dist/transactional-access-changes.d.ts +19 -0
  98. package/dist/transactional-access-changes.d.ts.map +1 -0
  99. package/dist/transactional-access-changes.js +35 -0
  100. package/dist/transactional-access-changes.js.map +1 -0
  101. package/dist/with-protected-postgres-transaction.d.ts +22 -0
  102. package/dist/with-protected-postgres-transaction.d.ts.map +1 -0
  103. package/dist/with-protected-postgres-transaction.js +47 -0
  104. package/dist/with-protected-postgres-transaction.js.map +1 -0
  105. package/package.json +41 -4
@@ -0,0 +1,8 @@
1
+ export { PostgresAuthorization, type PostgresAccessOptions, type PostgresAccessExecutor, } from "./postgres-authorization.js";
2
+ export { preparePostgresChecks } from "./prepared-check-executor.js";
3
+ export { relationshipSchemaMigration } from "./relationship-schema.js";
4
+ export { AlreadyAppliedAccessChanges, TransactionalAccessChanges, } from "./transactional-access-changes.js";
5
+ export { lookupRevisionSchemaMigration } from "./lookup-revision-schema.js";
6
+ export { POSTGRES_CONFLICT, PostgresSerializationConflict, readPostgresSerializationConflict, } from "./postgres-serialization-conflict.js";
7
+ export { withProtectedPostgresTransaction, type ProtectedPostgresTransaction, type ProtectedPostgresTransactionOptions, } from "./with-protected-postgres-transaction.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,GAC5B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EACL,2BAA2B,EAC3B,0BAA0B,GAC3B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,iCAAiC,GAClC,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EACL,gCAAgC,EAChC,KAAK,4BAA4B,EACjC,KAAK,mCAAmC,GACzC,MAAM,0CAA0C,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { PostgresAuthorization, } from "./postgres-authorization.js";
2
+ export { preparePostgresChecks } from "./prepared-check-executor.js";
3
+ export { relationshipSchemaMigration } from "./relationship-schema.js";
4
+ export { AlreadyAppliedAccessChanges, TransactionalAccessChanges, } from "./transactional-access-changes.js";
5
+ export { lookupRevisionSchemaMigration } from "./lookup-revision-schema.js";
6
+ export { POSTGRES_CONFLICT, PostgresSerializationConflict, readPostgresSerializationConflict, } from "./postgres-serialization-conflict.js";
7
+ export { withProtectedPostgresTransaction, } from "./with-protected-postgres-transaction.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAGtB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EACL,2BAA2B,EAC3B,0BAA0B,GAC3B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,iCAAiC,GAClC,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EACL,gCAAgC,GAGjC,MAAM,0CAA0C,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { PostgresConnection } from "@dads-framework/postgres";
2
+ import type { PostgresQuery, PostgresRow } from "./postgres-types.js";
3
+ /** An idle direct pg connection runs exactly one autocommit statement. */
4
+ export declare function readFromLookupPrimary(pool: PostgresConnection["pool"], query: PostgresQuery, timeoutMs: number): Promise<readonly PostgresRow[]>;
5
+ //# sourceMappingURL=lookup-primary-pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup-primary-pool.d.ts","sourceRoot":"","sources":["../src/lookup-primary-pool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEtE,0EAA0E;AAC1E,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,EAChC,KAAK,EAAE,aAAa,EACpB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,SAAS,WAAW,EAAE,CAAC,CAoBjC"}
@@ -0,0 +1,24 @@
1
+ /** An idle direct pg connection runs exactly one autocommit statement. */
2
+ export async function readFromLookupPrimary(pool, query, timeoutMs) {
3
+ const client = await pool.connect();
4
+ let destroy = true;
5
+ try {
6
+ // A client returned to the pool in a transaction is unsafe, including an
7
+ // otherwise read-only transaction with a retained snapshot.
8
+ if (client.getTransactionStatus() !== "I")
9
+ throw new Error("Postgres lookup reuse requires an idle pool connection");
10
+ const config = {
11
+ ...query,
12
+ query_timeout: timeoutMs,
13
+ };
14
+ const result = await client.query(config);
15
+ if (client.getTransactionStatus() !== "I")
16
+ throw new Error("Postgres lookup reuse did not complete in autocommit");
17
+ destroy = false;
18
+ return result.rows;
19
+ }
20
+ finally {
21
+ client.release(destroy);
22
+ }
23
+ }
24
+ //# sourceMappingURL=lookup-primary-pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup-primary-pool.js","sourceRoot":"","sources":["../src/lookup-primary-pool.ts"],"names":[],"mappings":"AAGA,0EAA0E;AAC1E,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,IAAgC,EAChC,KAAoB,EACpB,SAAiB;IAEjB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;IACpC,IAAI,OAAO,GAAG,IAAI,CAAC;IACnB,IAAI,CAAC;QACH,yEAAyE;QACzE,4DAA4D;QAC5D,IAAI,MAAM,CAAC,oBAAoB,EAAE,KAAK,GAAG;YACvC,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;QAC5E,MAAM,MAAM,GAAkB;YAC5B,GAAG,KAAK;YACR,aAAa,EAAE,SAAS;SACzB,CAAC;QACF,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,KAAK,CAAc,MAAM,CAAC,CAAC;QACvD,IAAI,MAAM,CAAC,oBAAoB,EAAE,KAAK,GAAG;YACvC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,OAAO,GAAG,KAAK,CAAC;QAChB,OAAO,MAAM,CAAC,IAAI,CAAC;IACrB,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { PostgresQuery, PostgresRow } from "./postgres-types.js";
2
+ export declare function lookupRevisionQuery(namespace: string): PostgresQuery;
3
+ /** The revision and ordered result must use the exact same database snapshot. */
4
+ export declare function lookupWithRevision(query: PostgresQuery, namespace: string): PostgresQuery;
5
+ export declare function readLookupRevision(row: PostgresRow | undefined): string;
6
+ export declare function readRevisionedIds(row: PostgresRow | undefined): readonly string[];
7
+ //# sourceMappingURL=lookup-revision-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup-revision-query.d.ts","sourceRoot":"","sources":["../src/lookup-revision-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AA2CtE,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAEpE;AAED,iFAAiF;AACjF,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,EACpB,SAAS,EAAE,MAAM,GAChB,aAAa,CAWf;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,CAOvE;AAED,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,WAAW,GAAG,SAAS,GAC3B,SAAS,MAAM,EAAE,CAWnB"}
@@ -0,0 +1,73 @@
1
+ // Include installation, server incarnation, database, role and physical table
2
+ // identity so an executor cannot accidentally reuse a stamp across authorities.
3
+ function revisionQuery(namespace) {
4
+ return `SELECT pg_catalog.json_build_array(
5
+ epoch.installation, epoch.revision, COALESCE(revision.revision, 0),
6
+ pg_catalog.pg_postmaster_start_time(), current_database(),
7
+ pg_catalog.inet_server_addr(), pg_catalog.inet_server_port(), current_user,
8
+ relation.oid, pg_catalog.pg_relation_filenode(relation.oid)
9
+ )::text AS stamp
10
+ FROM dad_lookup_revision.epoch epoch
11
+ LEFT JOIN dad_lookup_revision.namespace revision ON revision.namespace = ${namespace}
12
+ JOIN pg_catalog.pg_class relation ON relation.oid = 'public.dad_authorization_relationship'::regclass
13
+ WHERE epoch.singleton
14
+ AND NOT pg_catalog.pg_is_in_recovery()
15
+ AND pg_catalog.pg_current_xact_id_if_assigned() IS NULL
16
+ AND pg_catalog.to_regclass('dad_authorization_relationship') = relation.oid
17
+ AND relation.relkind = 'r' AND relation.relpersistence = 'p'
18
+ AND NOT relation.relhassubclass AND NOT relation.relrowsecurity
19
+ AND NOT pg_catalog.pg_has_role(current_user, relation.relowner, 'MEMBER')
20
+ AND pg_catalog.has_table_privilege(current_user, relation.oid, 'SELECT')
21
+ AND NOT pg_catalog.has_table_privilege(current_user, relation.oid, 'TRIGGER')
22
+ AND NOT pg_catalog.has_schema_privilege(current_user, 'dad_lookup_revision', 'CREATE')
23
+ AND NOT pg_catalog.has_table_privilege(current_user, 'dad_lookup_revision.epoch', 'INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER')
24
+ AND NOT pg_catalog.has_table_privilege(current_user, 'dad_lookup_revision.namespace', 'INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER')
25
+ AND NOT pg_catalog.has_sequence_privilege(current_user, 'dad_lookup_revision.token', 'USAGE,UPDATE')
26
+ AND NOT EXISTS (
27
+ SELECT 1 FROM pg_catalog.pg_class metadata
28
+ WHERE metadata.oid IN (
29
+ 'dad_lookup_revision.epoch'::regclass, 'dad_lookup_revision.namespace'::regclass,
30
+ 'dad_lookup_revision.token'::regclass
31
+ ) AND pg_catalog.pg_has_role(current_user, metadata.relowner, 'MEMBER')
32
+ )
33
+ AND 4 = (
34
+ SELECT count(*) FROM pg_catalog.pg_trigger trigger
35
+ WHERE trigger.tgrelid = relation.oid AND trigger.tgenabled = 'A'
36
+ AND trigger.tgfoid = 'dad_lookup_revision.mark()'::regprocedure
37
+ AND trigger.tgname IN ('dad_lookup_revision_insert', 'dad_lookup_revision_delete',
38
+ 'dad_lookup_revision_update', 'dad_lookup_revision_truncate')
39
+ )`;
40
+ }
41
+ export function lookupRevisionQuery(namespace) {
42
+ return { text: revisionQuery("$1"), values: [namespace] };
43
+ }
44
+ /** The revision and ordered result must use the exact same database snapshot. */
45
+ export function lookupWithRevision(query, namespace) {
46
+ const values = query.values ?? [];
47
+ return {
48
+ text: `SELECT revision.stamp,
49
+ pg_catalog.array_to_json(ARRAY(
50
+ SELECT result.resource_id FROM (${query.text}) result
51
+ ORDER BY result.resource_id
52
+ ))::text AS resource_ids
53
+ FROM (${revisionQuery(`$${String(values.length + 1)}`)}) revision`,
54
+ values: [...values, namespace],
55
+ };
56
+ }
57
+ export function readLookupRevision(row) {
58
+ const stamp = row?.["stamp"];
59
+ if (typeof stamp !== "string")
60
+ throw new Error("Postgres lookup reuse requires the versioned primary and restricted runtime role");
61
+ return stamp;
62
+ }
63
+ export function readRevisionedIds(row) {
64
+ const text = row?.["resource_ids"];
65
+ if (typeof text !== "string")
66
+ throw new Error("Postgres authorization returned invalid resource IDs");
67
+ const ids = JSON.parse(text);
68
+ if (!Array.isArray(ids) ||
69
+ !ids.every((id) => typeof id === "string"))
70
+ throw new Error("Postgres authorization returned invalid resource IDs");
71
+ return Object.freeze(ids);
72
+ }
73
+ //# sourceMappingURL=lookup-revision-query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup-revision-query.js","sourceRoot":"","sources":["../src/lookup-revision-query.ts"],"names":[],"mappings":"AAEA,8EAA8E;AAC9E,gFAAgF;AAChF,SAAS,aAAa,CAAC,SAAiB;IACtC,OAAO;;;;;;;6EAOoE,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;MA4BhF,CAAC;AACP,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,SAAiB;IACnD,OAAO,EAAE,IAAI,EAAE,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC;AAC5D,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,kBAAkB,CAChC,KAAoB,EACpB,SAAiB;IAEjB,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,EAAE,CAAC;IAClC,OAAO;QACL,IAAI,EAAE;;0CAEgC,KAAK,CAAC,IAAI;;;cAGtC,aAAa,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,YAAY;QACpE,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,SAAS,CAAC;KAC/B,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,GAA4B;IAC7D,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC;IAC7B,IAAI,OAAO,KAAK,KAAK,QAAQ;QAC3B,MAAM,IAAI,KAAK,CACb,kFAAkF,CACnF,CAAC;IACJ,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,GAA4B;IAE5B,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC,cAAc,CAAC,CAAC;IACnC,IAAI,OAAO,IAAI,KAAK,QAAQ;QAC1B,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAC1E,MAAM,GAAG,GAAY,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACtC,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;QACnB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAW,EAAgB,EAAE,CAAC,OAAO,EAAE,KAAK,QAAQ,CAAC;QAEjE,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,10 @@
1
+ /** Optional migration for immutable lookup results revalidated on a primary. */
2
+ export declare const lookupRevisionSchemaMigration: Readonly<{
3
+ adapter: "postgres-access";
4
+ version: 2;
5
+ id: "dad-authorization-lookup-revision-v2";
6
+ checksum: string;
7
+ sql: string;
8
+ advisoryLockSql: "SELECT pg_advisory_xact_lock(hashtext($1))";
9
+ }>;
10
+ //# sourceMappingURL=lookup-revision-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup-revision-schema.d.ts","sourceRoot":"","sources":["../src/lookup-revision-schema.ts"],"names":[],"mappings":"AAmEA,gFAAgF;AAChF,eAAO,MAAM,6BAA6B;;;;;;;EAOxC,CAAC"}
@@ -0,0 +1,75 @@
1
+ import { createHash } from "node:crypto";
2
+ const lookupRevisionSql = `
3
+ CREATE SCHEMA IF NOT EXISTS dad_lookup_revision;
4
+ REVOKE ALL ON SCHEMA dad_lookup_revision FROM PUBLIC;
5
+
6
+ CREATE SEQUENCE IF NOT EXISTS dad_lookup_revision.token;
7
+ CREATE TABLE IF NOT EXISTS dad_lookup_revision.epoch (
8
+ singleton boolean PRIMARY KEY CHECK (singleton),
9
+ installation uuid NOT NULL DEFAULT pg_catalog.gen_random_uuid(),
10
+ revision bigint NOT NULL DEFAULT 0
11
+ );
12
+ INSERT INTO dad_lookup_revision.epoch (singleton) VALUES (true)
13
+ ON CONFLICT (singleton) DO UPDATE SET installation = excluded.installation;
14
+ CREATE TABLE IF NOT EXISTS dad_lookup_revision.namespace (
15
+ namespace text PRIMARY KEY,
16
+ revision bigint NOT NULL
17
+ );
18
+
19
+ CREATE OR REPLACE FUNCTION dad_lookup_revision.mark()
20
+ RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
21
+ BEGIN
22
+ IF TG_OP = 'TRUNCATE' THEN
23
+ UPDATE dad_lookup_revision.epoch
24
+ SET revision = nextval('dad_lookup_revision.token') WHERE singleton;
25
+ ELSIF TG_OP = 'INSERT' THEN
26
+ INSERT INTO dad_lookup_revision.namespace
27
+ SELECT touched.namespace, nextval('dad_lookup_revision.token')
28
+ FROM (SELECT DISTINCT namespace FROM new_rows ORDER BY namespace) AS touched
29
+ ON CONFLICT (namespace) DO UPDATE SET revision = excluded.revision;
30
+ ELSIF TG_OP = 'DELETE' THEN
31
+ INSERT INTO dad_lookup_revision.namespace
32
+ SELECT touched.namespace, nextval('dad_lookup_revision.token')
33
+ FROM (SELECT DISTINCT namespace FROM old_rows ORDER BY namespace) AS touched
34
+ ON CONFLICT (namespace) DO UPDATE SET revision = excluded.revision;
35
+ ELSE
36
+ INSERT INTO dad_lookup_revision.namespace
37
+ SELECT touched.namespace, nextval('dad_lookup_revision.token')
38
+ FROM (SELECT namespace FROM new_rows UNION SELECT namespace FROM old_rows ORDER BY namespace) AS touched
39
+ ON CONFLICT (namespace) DO UPDATE SET revision = excluded.revision;
40
+ END IF;
41
+ RETURN NULL;
42
+ END
43
+ $$;
44
+ REVOKE ALL ON FUNCTION dad_lookup_revision.mark() FROM PUBLIC;
45
+
46
+ CREATE OR REPLACE TRIGGER dad_lookup_revision_insert
47
+ AFTER INSERT ON public.dad_authorization_relationship
48
+ REFERENCING NEW TABLE AS new_rows FOR EACH STATEMENT
49
+ EXECUTE FUNCTION dad_lookup_revision.mark();
50
+ CREATE OR REPLACE TRIGGER dad_lookup_revision_delete
51
+ AFTER DELETE ON public.dad_authorization_relationship
52
+ REFERENCING OLD TABLE AS old_rows FOR EACH STATEMENT
53
+ EXECUTE FUNCTION dad_lookup_revision.mark();
54
+ CREATE OR REPLACE TRIGGER dad_lookup_revision_update
55
+ AFTER UPDATE ON public.dad_authorization_relationship
56
+ REFERENCING OLD TABLE AS old_rows NEW TABLE AS new_rows FOR EACH STATEMENT
57
+ EXECUTE FUNCTION dad_lookup_revision.mark();
58
+ CREATE OR REPLACE TRIGGER dad_lookup_revision_truncate
59
+ AFTER TRUNCATE ON public.dad_authorization_relationship
60
+ FOR EACH STATEMENT EXECUTE FUNCTION dad_lookup_revision.mark();
61
+ ALTER TABLE public.dad_authorization_relationship ENABLE ALWAYS TRIGGER dad_lookup_revision_insert;
62
+ ALTER TABLE public.dad_authorization_relationship ENABLE ALWAYS TRIGGER dad_lookup_revision_delete;
63
+ ALTER TABLE public.dad_authorization_relationship ENABLE ALWAYS TRIGGER dad_lookup_revision_update;
64
+ ALTER TABLE public.dad_authorization_relationship ENABLE ALWAYS TRIGGER dad_lookup_revision_truncate;
65
+ `.trim();
66
+ /** Optional migration for immutable lookup results revalidated on a primary. */
67
+ export const lookupRevisionSchemaMigration = Object.freeze({
68
+ adapter: "postgres-access",
69
+ version: 2,
70
+ id: "dad-authorization-lookup-revision-v2",
71
+ checksum: createHash("sha256").update(lookupRevisionSql).digest("hex"),
72
+ sql: lookupRevisionSql,
73
+ advisoryLockSql: "SELECT pg_advisory_xact_lock(hashtext($1))",
74
+ });
75
+ //# sourceMappingURL=lookup-revision-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup-revision-schema.js","sourceRoot":"","sources":["../src/lookup-revision-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,iBAAiB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+DzB,CAAC,IAAI,EAAE,CAAC;AAET,gFAAgF;AAChF,MAAM,CAAC,MAAM,6BAA6B,GAAG,MAAM,CAAC,MAAM,CAAC;IACzD,OAAO,EAAE,iBAAiB;IAC1B,OAAO,EAAE,CAAC;IACV,EAAE,EAAE,sCAAsC;IAC1C,QAAQ,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;IACtE,GAAG,EAAE,iBAAiB;IACtB,eAAe,EAAE,4CAA4C;CAC9D,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { AccessIrDefinition } from "@dads-framework/access";
2
+ import type { CompiledAuthorizationPermission } from "./policy/policy-model.js";
3
+ export type PermissionLowering = {
4
+ readonly byName: ReadonlyMap<string, AccessIrDefinition>;
5
+ readonly generated: ReadonlyMap<string, ReadonlyMap<string, string>>;
6
+ };
7
+ export declare function lowerPermissions(definition: AccessIrDefinition, context: PermissionLowering): readonly CompiledAuthorizationPermission[];
8
+ //# sourceMappingURL=lower-permissions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lower-permissions.d.ts","sourceRoot":"","sources":["../src/lower-permissions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAgB,MAAM,wBAAwB,CAAC;AAC/E,OAAO,KAAK,EACV,+BAA+B,EAEhC,MAAM,0BAA0B,CAAC;AAElC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACzD,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACtE,CAAC;AAIF,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,kBAAkB,EAC9B,OAAO,EAAE,kBAAkB,GAC1B,SAAS,+BAA+B,EAAE,CAoC5C"}
@@ -0,0 +1,76 @@
1
+ export function lowerPermissions(definition, context) {
2
+ const byName = new Map(definition.permissions.map((entry) => [entry.name, entry.terms]));
3
+ const cached = new Map();
4
+ const visiting = new Set();
5
+ function permission(name) {
6
+ const existing = cached.get(name);
7
+ if (existing !== undefined)
8
+ return existing;
9
+ if (visiting.has(name))
10
+ throw new Error(`Cyclic permission reference ${definition.wireName}#${name}`);
11
+ const terms = byName.get(name);
12
+ if (terms === undefined)
13
+ throw new Error(`Unknown permission ${definition.wireName}#${name}`);
14
+ visiting.add(name);
15
+ const compiled = group("any-of", terms.map(lower));
16
+ visiting.delete(name);
17
+ cached.set(name, compiled);
18
+ return compiled;
19
+ }
20
+ function lower(term) {
21
+ if (term.kind === "permission")
22
+ return permission(term.name);
23
+ if (term.kind === "through")
24
+ return lowerArrow(definition, context, term);
25
+ return lowerLocal(term, lower);
26
+ }
27
+ return definition.permissions.map(({ name }, index) => ({
28
+ entity: definition.wireName,
29
+ sourceName: `permission_${index}`,
30
+ wireName: name,
31
+ expression: permission(name),
32
+ }));
33
+ }
34
+ function lowerArrow(definition, context, term) {
35
+ const role = definition.roles.find(({ name }) => name === term.roleName);
36
+ const types = role?.subjects.filter((subject) => subject.kind === "type") ?? [];
37
+ const [target] = types;
38
+ if (target === undefined || types.length !== 1) {
39
+ throw new Error(`Postgres arrow ${definition.wireName}#${term.roleName} requires one direct entity type`);
40
+ }
41
+ const targetDefinition = context.byName.get(target.wireName);
42
+ const name = targetDefinition?.permissions.some(({ name }) => name === term.targetPermission)
43
+ ? term.targetPermission
44
+ : context.generated.get(target.wireName)?.get(term.targetPermission);
45
+ if (name === undefined) {
46
+ throw new Error(`Unknown arrow target ${target.wireName}#${term.targetPermission}`);
47
+ }
48
+ return { kind: "through", relation: term.roleName, permission: name };
49
+ }
50
+ function lowerLocal(term, lower) {
51
+ switch (term.kind) {
52
+ case "role":
53
+ return { kind: "relation", relation: term.name };
54
+ case "or":
55
+ return group("any-of", term.terms.map(lower));
56
+ case "both":
57
+ return group("all-of", [lower(term.left), lower(term.right)]);
58
+ case "gated":
59
+ return group("all-of", [lower(term.term), lower(term.condition)]);
60
+ case "excluding":
61
+ return {
62
+ kind: "exclude",
63
+ include: lower(term.term),
64
+ exclude: lower(term.banned),
65
+ };
66
+ }
67
+ }
68
+ function group(kind, expressions) {
69
+ if (expressions.length === 0)
70
+ throw new Error("Postgres authorization requires nonempty permission expressions");
71
+ const [only] = expressions;
72
+ return expressions.length === 1 && only !== undefined
73
+ ? only
74
+ : { kind, expressions };
75
+ }
76
+ //# sourceMappingURL=lower-permissions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lower-permissions.js","sourceRoot":"","sources":["../src/lower-permissions.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,gBAAgB,CAC9B,UAA8B,EAC9B,OAA2B;IAE3B,MAAM,MAAM,GAAG,IAAI,GAAG,CACpB,UA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SAAS,KAAK,CACZ,IAAyB,EACzB,WAAoD;IAEpD,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;QAC1B,MAAM,IAAI,KAAK,CACb,iEAAiE,CAClE,CAAC;IACJ,MAAM,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC;IAC3B,OAAO,WAAW,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,KAAK,SAAS;QACnD,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { AuthorizationModelDefinition } from "./policy-model.js";
2
+ import type { QueryBuilder } from "./policy-sql.js";
3
+ type LinearBatchBranch = {
4
+ readonly namespace: string;
5
+ readonly resourceType: string;
6
+ readonly permission: string;
7
+ readonly index: number;
8
+ };
9
+ /** Evaluate a deep linear policy once for each actor in this batch branch. */
10
+ export declare function compileLinearBatchBranch(model: AuthorizationModelDefinition, builder: QueryBuilder, { namespace, resourceType, permission, index }: LinearBatchBranch): {
11
+ readonly hits: string;
12
+ readonly decision: string;
13
+ };
14
+ export {};
15
+ //# sourceMappingURL=compile-linear-batch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-linear-batch.d.ts","sourceRoot":"","sources":["../../src/policy/compile-linear-batch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGpD,KAAK,iBAAiB,GAAG;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,8EAA8E;AAC9E,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,4BAA4B,EACnC,OAAO,EAAE,YAAY,EACrB,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE,iBAAiB,GAChE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CA8BtD"}
@@ -0,0 +1,34 @@
1
+ import { compileSharedPointRows } from "./compile-shared-point-check.js";
2
+ /** Evaluate a deep linear policy once for each actor in this batch branch. */
3
+ export function compileLinearBatchBranch(model, builder, { namespace, resourceType, permission, index }) {
4
+ const resourceKind = builder.value(resourceType);
5
+ const permissionName = builder.value(permission);
6
+ const branch = `i.resource_kind = ${resourceKind} AND i.permission = ${permissionName}`;
7
+ const hits = `linear_hits_${String(index)}`;
8
+ const permitted = compileSharedPointRows(model, builder, {
9
+ namespace,
10
+ subjectKind: "actor.subject_kind",
11
+ subjectId: "actor.subject_id",
12
+ resourceEntity: resourceType,
13
+ permission,
14
+ });
15
+ return {
16
+ hits: `${hits} AS MATERIALIZED (
17
+ SELECT actor.subject_kind, actor.subject_id, permitted.resource_id
18
+ FROM (
19
+ SELECT subject_kind, subject_id, array_agg(DISTINCT resource_id) AS resource_ids
20
+ FROM input i WHERE ${branch}
21
+ GROUP BY subject_kind, subject_id
22
+ ) AS actor
23
+ CROSS JOIN LATERAL (${permitted}) AS permitted
24
+ WHERE permitted.resource_kind = ${resourceKind}
25
+ AND permitted.resource_id = ANY(actor.resource_ids)
26
+ )`,
27
+ decision: `WHEN ${branch} THEN EXISTS (
28
+ SELECT 1 FROM ${hits}
29
+ WHERE subject_kind = i.subject_kind AND subject_id = i.subject_id
30
+ AND resource_id = i.resource_id
31
+ )`,
32
+ };
33
+ }
34
+ //# sourceMappingURL=compile-linear-batch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-linear-batch.js","sourceRoot":"","sources":["../../src/policy/compile-linear-batch.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AASzE,8EAA8E;AAC9E,MAAM,UAAU,wBAAwB,CACtC,KAAmC,EACnC,OAAqB,EACrB,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,EAAqB;IAEjE,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACjD,MAAM,cAAc,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,qBAAqB,YAAY,uBAAuB,cAAc,EAAE,CAAC;IACxF,MAAM,IAAI,GAAG,eAAe,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;IAC5C,MAAM,SAAS,GAAG,sBAAsB,CAAC,KAAK,EAAE,OAAO,EAAE;QACvD,SAAS;QACT,WAAW,EAAE,oBAAoB;QACjC,SAAS,EAAE,kBAAkB;QAC7B,cAAc,EAAE,YAAY;QAC5B,UAAU;KACX,CAAC,CAAC;IACH,OAAO;QACL,IAAI,EAAE,GAAG,IAAI;;;;6BAIY,MAAM;;;4BAGP,SAAS;wCACG,YAAY;;MAE9C;QACF,QAAQ,EAAE,QAAQ,MAAM;sBACN,IAAI;;;MAGpB;KACH,CAAC;AACJ,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { AuthorizationModelDefinition, NormalizedPermissionCheck } from "./policy-model.js";
2
+ import type { CompiledAuthorizationQuery } from "./compile-postgres-policy.js";
3
+ export declare function compileLinearCheck(model: AuthorizationModelDefinition, namespace: string, check: NormalizedPermissionCheck): CompiledAuthorizationQuery | undefined;
4
+ export declare function readLinearPath(model: AuthorizationModelDefinition, resourceEntity: string, permission: string): readonly string[] | undefined;
5
+ //# sourceMappingURL=compile-linear-check.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-linear-check.d.ts","sourceRoot":"","sources":["../../src/policy/compile-linear-check.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,4BAA4B,EAC5B,yBAAyB,EAC1B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAQ/E,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,4BAA4B,EACnC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,yBAAyB,GAC/B,0BAA0B,GAAG,SAAS,CAexC;AAkCD,wBAAgB,cAAc,CAC5B,KAAK,EAAE,4BAA4B,EACnC,cAAc,EAAE,MAAM,EACtB,UAAU,EAAE,MAAM,GACjB,SAAS,MAAM,EAAE,GAAG,SAAS,CAkB/B"}
@@ -0,0 +1,71 @@
1
+ import { relatedEntity, TUPLES } from "./policy-sql.js";
2
+ const MIN_FUSED_ARROWS = 4;
3
+ const MAX_FUSED_ARROWS = 32;
4
+ const MAX_TRAVERSED_STATES = 64;
5
+ const FALLBACK_THRESHOLD = MAX_TRAVERSED_STATES + 1;
6
+ export function compileLinearCheck(model, namespace, check) {
7
+ const path = readLinearPath(model, check.resource.type, check.permission);
8
+ if (path === undefined)
9
+ return undefined;
10
+ return {
11
+ kind: "check",
12
+ text: SQL,
13
+ values: [
14
+ namespace,
15
+ check.subject.type,
16
+ check.subject.id,
17
+ check.resource.type,
18
+ check.resource.id,
19
+ path,
20
+ ],
21
+ };
22
+ }
23
+ const terminal = "cardinality($6::text[])";
24
+ // UNION deduplicates each traversal state, including terminal userset cycles.
25
+ // Saturation produces NULL. Only a complete walk can produce a denial.
26
+ const SQL = `WITH RECURSIVE levels(ordinal, resource_kind, resource_id, relation) AS (
27
+ SELECT 1, $4::text, $5::text, ($6::text[])[1]
28
+ UNION
29
+ SELECT CASE WHEN levels.ordinal < ${terminal} THEN levels.ordinal + 1 ELSE levels.ordinal END,
30
+ tuple.subject_kind, tuple.subject_id,
31
+ CASE WHEN levels.ordinal < ${terminal} THEN ($6::text[])[levels.ordinal + 1] ELSE tuple.subject_relation END
32
+ FROM levels
33
+ JOIN ${TUPLES} tuple
34
+ ON tuple.namespace = $1 AND tuple.resource_kind = levels.resource_kind
35
+ AND tuple.resource_id = levels.resource_id AND tuple.relation = levels.relation
36
+ WHERE tuple.subject_is_wildcard = false
37
+ AND (levels.ordinal < ${terminal}) = (tuple.subject_relation = '')
38
+ ), bounded AS MATERIALIZED (
39
+ SELECT * FROM levels LIMIT ${String(FALLBACK_THRESHOLD)}
40
+ )
41
+ SELECT CASE WHEN EXISTS (
42
+ SELECT 1 FROM bounded
43
+ JOIN ${TUPLES} tuple
44
+ ON tuple.namespace = $1 AND tuple.resource_kind = bounded.resource_kind
45
+ AND tuple.resource_id = bounded.resource_id AND tuple.relation = bounded.relation
46
+ WHERE bounded.ordinal = ${terminal} AND tuple.subject_kind = $2
47
+ AND tuple.subject_relation = ''
48
+ AND ((tuple.subject_id = $3 AND tuple.subject_is_wildcard = false)
49
+ OR (tuple.subject_id = '' AND tuple.subject_is_wildcard = true))
50
+ ) THEN true
51
+ WHEN (SELECT count(*) FROM bounded) >= ${String(FALLBACK_THRESHOLD)} THEN NULL
52
+ ELSE false END AS allowed`;
53
+ export function readLinearPath(model, resourceEntity, permission) {
54
+ const arrows = [];
55
+ let entity = resourceEntity;
56
+ let name = permission;
57
+ while (arrows.length <= MAX_FUSED_ARROWS) {
58
+ const expression = model.permissions.find((candidate) => candidate.entity === entity && candidate.wireName === name)?.expression;
59
+ if (expression?.kind === "relation")
60
+ return arrows.length >= MIN_FUSED_ARROWS
61
+ ? [...arrows, expression.relation]
62
+ : undefined;
63
+ if (expression?.kind !== "through")
64
+ return undefined;
65
+ arrows.push(expression.relation);
66
+ entity = relatedEntity(model, entity, expression.relation);
67
+ name = expression.permission;
68
+ }
69
+ return undefined;
70
+ }
71
+ //# sourceMappingURL=compile-linear-check.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-linear-check.js","sourceRoot":"","sources":["../../src/policy/compile-linear-check.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAExD,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AAC,IAAI,CACvC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,MAAM,KAAK,MAAM,IAAI,SAAS,CAAC,QAAQ,KAAK,IAAI,CAC1E,EAAE,UAAU,CAAC;QACd,IAAI,UAAU,EAAE,IAAI,KAAK,UAAU;YACjC,OAAO,MAAM,CAAC,MAAM,IAAI,gBAAgB;gBACtC,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC;gBAClC,CAAC,CAAC,SAAS,CAAC;QAChB,IAAI,UAAU,EAAE,IAAI,KAAK,SAAS;YAAE,OAAO,SAAS,CAAC;QACrD,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACjC,MAAM,GAAG,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;QAC3D,IAAI,GAAG,UAAU,CAAC,UAAU,CAAC;IAC/B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -0,0 +1,28 @@
1
+ import { type AuthorizationModelDefinition, type NormalizedPermissionCheck, type NormalizedResourceLookup } from "./policy-model.js";
2
+ import type { PostgresQuery } from "../postgres-types.js";
3
+ export type CompiledAuthorizationQuery = PostgresQuery & {
4
+ readonly kind: "check" | "check-many" | "lookup" | "candidate-predicate";
5
+ };
6
+ export type PostgresAuthorizationCandidateBinding = {
7
+ readonly tableAlias: "dad_authorization_candidate";
8
+ readonly resourceIdColumn: "resource_id";
9
+ };
10
+ /** Fixed outer binding used only by named integration adapters. */
11
+ export declare const POSTGRES_AUTHORIZATION_CANDIDATE_BINDING: PostgresAuthorizationCandidateBinding;
12
+ /** Compiles one permission decision into a parameterized Postgres Boolean query. */
13
+ export declare function compilePostgresCheck(model: AuthorizationModelDefinition, namespace: string, request: NormalizedPermissionCheck): CompiledAuthorizationQuery & {
14
+ readonly canPrepare: boolean;
15
+ };
16
+ /** One ordinal input relation and one SQL statement preserve input ordering and duplicates. */
17
+ export declare function compilePostgresCheckMany(model: AuthorizationModelDefinition, namespace: string, requests: readonly NormalizedPermissionCheck[]): CompiledAuthorizationQuery;
18
+ /**
19
+ * Computes allowed IDs actor-first from the reverse subject index. It never
20
+ * enumerates every resource of a kind in application memory or SQL.
21
+ */
22
+ export declare function compilePostgresLookup(model: AuthorizationModelDefinition, namespace: string, request: NormalizedResourceLookup): CompiledAuthorizationQuery;
23
+ /**
24
+ * Compiles one descriptor's permission against the fixed integration candidate
25
+ * binding. The caller supplies neither SQL text nor an identifier.
26
+ */
27
+ export declare function compilePostgresCandidatePredicate(model: AuthorizationModelDefinition, namespace: string, request: NormalizedResourceLookup): CompiledAuthorizationQuery;
28
+ //# sourceMappingURL=compile-postgres-policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-postgres-policy.d.ts","sourceRoot":"","sources":["../../src/policy/compile-postgres-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,4BAA4B,EAEjC,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,EAC9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAY1D,MAAM,MAAM,0BAA0B,GAAG,aAAa,GAAG;IACvD,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,YAAY,GAAG,QAAQ,GAAG,qBAAqB,CAAC;CAC1E,CAAC;AAEF,MAAM,MAAM,qCAAqC,GAAG;IAClD,QAAQ,CAAC,UAAU,EAAE,6BAA6B,CAAC;IACnD,QAAQ,CAAC,gBAAgB,EAAE,aAAa,CAAC;CAC1C,CAAC;AAEF,mEAAmE;AACnE,eAAO,MAAM,wCAAwC,EAAE,qCAInD,CAAC;AAQL,oFAAoF;AACpF,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,4BAA4B,EACnC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,yBAAyB,GACjC,0BAA0B,GAAG;IAAE,QAAQ,CAAC,UAAU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