@tearleads/client-sdk 0.1.22 → 0.1.23

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 (54) hide show
  1. package/client/organizations/organizationGroupHistory.js +4 -7
  2. package/client/organizations/organizationGroupHistory.js.map +1 -1
  3. package/client/organizations/organizationReadModels.js +2 -86
  4. package/client/organizations/organizationReadModels.js.map +1 -1
  5. package/client/organizations/principalMutations.js +53 -133
  6. package/client/organizations/principalMutations.js.map +1 -1
  7. package/client/principalMutationDispatch.js +2 -2
  8. package/client/principalMutationDispatch.js.map +1 -1
  9. package/data/keyingProjectionVerification/principalPolicyVerification.js +5 -5
  10. package/data/keyingProjectionVerification/principalPolicyVerification.js.map +1 -1
  11. package/data/keyingProjectionVerification/resolvedPrincipalPolicy.js +21 -7
  12. package/data/keyingProjectionVerification/resolvedPrincipalPolicy.js.map +1 -1
  13. package/data/keyingProjectionVerification/types.d.ts +1 -8
  14. package/data/keyingProjectionVerification/types.js +0 -9
  15. package/data/keyingProjectionVerification/types.js.map +1 -1
  16. package/data/keyingProjectionVerification.d.ts +1 -1
  17. package/data/keyingProjectionVerification.js.map +1 -1
  18. package/data/persistence/principalHistoryPrefixPersistence.d.ts +5 -1
  19. package/data/persistence/principalHistoryPrefixPersistence.js +22 -5
  20. package/data/persistence/principalHistoryPrefixPersistence.js.map +1 -1
  21. package/data/principals/principalMutationJournalShape.d.ts +1 -1
  22. package/data/principals/principalMutationJournalShape.js +2 -2
  23. package/data/principals/principalMutationJournalShape.js.map +1 -1
  24. package/index.js.map +1 -1
  25. package/package.json +1 -1
  26. package/workflows/container-contents/container-state/groupGrantVerification.js +23 -74
  27. package/workflows/container-contents/container-state/groupGrantVerification.js.map +1 -1
  28. package/workflows/container-contents/container-state/groupShareEpoch.js +6 -23
  29. package/workflows/container-contents/container-state/groupShareEpoch.js.map +1 -1
  30. package/workflows/container-contents/container-state/remote.js +5 -3
  31. package/workflows/container-contents/container-state/remote.js.map +1 -1
  32. package/workflows/container-contents/container-state/types.js.map +1 -1
  33. package/workflows/container-contents/documentLinks.d.ts +1 -1
  34. package/workflows/container-contents/documentLinks.js.map +1 -1
  35. package/workflows/container-contents/documentPurge.d.ts +1 -1
  36. package/workflows/container-contents/documentPurge.js.map +1 -1
  37. package/workflows/container-contents/metadataRemoteSync.d.ts +1 -1
  38. package/workflows/container-contents/metadataRemoteSync.js.map +1 -1
  39. package/workflows/container-contents/remoteHydration/types.d.ts +2 -2
  40. package/workflows/container-contents/remoteHydration/types.js.map +1 -1
  41. package/workflows/container-contents/remoteHydration/verifiedDestination.js +6 -7
  42. package/workflows/container-contents/remoteHydration/verifiedDestination.js.map +1 -1
  43. package/workflows/containers/child/runtimeCurrentGroupShare.js +1 -1
  44. package/workflows/containers/child/runtimeCurrentGroupShare.js.map +1 -1
  45. package/workflows/organizations/groupMetadataRuntime.js +11 -8
  46. package/workflows/organizations/groupMetadataRuntime.js.map +1 -1
  47. package/workflows/organizations/organizationGroupNames.js +8 -55
  48. package/workflows/organizations/organizationGroupNames.js.map +1 -1
  49. package/workflows/organizations/principalOperationReceipt.js +1 -1
  50. package/workflows/organizations/principalOperationReceipt.js.map +1 -1
  51. package/workflows/organizations/runtimeGroupMetadataAuthority.js +16 -0
  52. package/workflows/organizations/runtimeGroupMetadataAuthority.js.map +1 -0
  53. package/workflows/principals/runtimePolicyWarmer.js +29 -27
  54. package/workflows/principals/runtimePolicyWarmer.js.map +1 -1
@@ -1,17 +1,14 @@
1
- import { KeyingVerificationError } from "../../internal/crypto/index.js";
1
+ import { ProjectionDependencyUnavailableError } from "../../data/keyingProjectionVerification/dependencyUnavailable.js";
2
2
  import { assertProjectionVerificationCurrent } from "../../data/keyingProjectionVerification/types.js";
3
3
  import { loadLocalOrganizationPolicyReference } from "../../workflows/organizations/index.js";
4
4
  import { buildOrganizationGroupPolicyHistoryPage } from "../../workflows/organizations/groupPolicyHistoryPage.js";
5
5
  import { createRuntimePrincipalPolicyCurrentResolver } from "../../workflows/principals/runtimePolicyRecovery.js";
6
- /** Undefined means the host lacks paged recovery; a failed capability never downgrades. */
6
+ /** All group history reads use the private paged verifier. */
7
7
  export async function loadBoundedOrganizationGroupHistory(input) {
8
8
  const { active } = input;
9
9
  const resolve = createRuntimePrincipalPolicyCurrentResolver(active.runtime);
10
- if (!resolve) {
11
- if (input.beforeVersion !== undefined)
12
- throw new KeyingVerificationError("invalid_shape", "This host cannot resolve a group history page cursor");
13
- return undefined;
14
- }
10
+ if (!resolve)
11
+ throw new ProjectionDependencyUnavailableError("Group history requires private paged recovery");
15
12
  assertProjectionVerificationCurrent(input.stillCurrent);
16
13
  const reference = await loadLocalOrganizationPolicyReference({
17
14
  currentUserId: active.userId,
@@ -1 +1 @@
1
- {"version":3,"file":"organizationGroupHistory.js","sourceRoot":"","sources":["organizationGroupHistory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,mCAAmC,EAAE,MAAM,+CAA+C,CAAC;AACpG,OAAO,EAAE,oCAAoC,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,EAAE,uCAAuC,EAAE,MAAM,sDAAsD,CAAC;AAC/G,OAAO,EAAE,2CAA2C,EAAE,MAAM,kDAAkD,CAAC;AAG/G,2FAA2F;AAC3F,MAAM,CAAC,KAAK,UAAU,mCAAmC,CAAC,KAKzD;IACC,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IACzB,MAAM,OAAO,GAAG,2CAA2C,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC5E,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,IAAI,KAAK,CAAC,aAAa,KAAK,SAAS;YACnC,MAAM,IAAI,uBAAuB,CAC/B,eAAe,EACf,sDAAsD,CACvD,CAAC;QACJ,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACxD,MAAM,SAAS,GAAG,MAAM,oCAAoC,CAAC;QAC3D,aAAa,EAAE,MAAM,CAAC,MAAM;QAC5B,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO;QACrC,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,WAAW,EAAE,KAAK,CAAC,OAAO;QAC1B,aAAa,EAAE,OAAO;KACvB,CAAC,CAAC;IACH,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACxD,IAAI,CAAC,SAAS;QAAE,OAAO,IAAI,CAAC;IAC5B,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC;QAC7B,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,SAAS;QACT,kBAAkB,EAAE,IAAI;QACxB,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,WAAW,EACT,KAAK,CAAC,aAAa,KAAK,SAAS;YAC/B,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,CAAC,aAAa,EAAE;KAC7C,CAAC,CAAC;IACH,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACxD,IAAI,CAAC,QAAQ,CAAC,WAAW;QACvB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACtD,OAAO,uCAAuC,CAAC;QAC7C,IAAI,EAAE,QAAQ,CAAC,WAAW;QAC1B,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,cAAc,EAAE,MAAM,CAAC,cAAc;KACtC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport { assertProjectionVerificationCurrent } from \"../../data/keyingProjectionVerification/types\";\nimport { loadLocalOrganizationPolicyReference } from \"../../workflows/organizations\";\nimport { buildOrganizationGroupPolicyHistoryPage } from \"../../workflows/organizations/groupPolicyHistoryPage\";\nimport { createRuntimePrincipalPolicyCurrentResolver } from \"../../workflows/principals/runtimePolicyRecovery\";\nimport type { ActiveOrganizationDataRuntime } from \"./organizationWorkflowRuntime\";\n\n/** Undefined means the host lacks paged recovery; a failed capability never downgrades. */\nexport async function loadBoundedOrganizationGroupHistory(input: {\n readonly active: ActiveOrganizationDataRuntime;\n readonly groupId: string;\n readonly beforeVersion?: number | undefined;\n readonly stillCurrent: () => boolean;\n}) {\n const { active } = input;\n const resolve = createRuntimePrincipalPolicyCurrentResolver(active.runtime);\n if (!resolve) {\n if (input.beforeVersion !== undefined)\n throw new KeyingVerificationError(\n \"invalid_shape\",\n \"This host cannot resolve a group history page cursor\",\n );\n return undefined;\n }\n assertProjectionVerificationCurrent(input.stillCurrent);\n const reference = await loadLocalOrganizationPolicyReference({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n principalId: input.groupId,\n principalType: \"group\",\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!reference) return null;\n const resolved = await resolve({\n organizationId: active.organizationId,\n reference,\n preferLocalCurrent: true,\n stillCurrent: input.stillCurrent,\n historyPage:\n input.beforeVersion === undefined\n ? {}\n : { beforeVersion: input.beforeVersion },\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!resolved.historyPage)\n throw new Error(\"Verified history page is missing\");\n return buildOrganizationGroupPolicyHistoryPage({\n page: resolved.historyPage,\n groupId: input.groupId,\n organizationId: active.organizationId,\n });\n}\n"]}
1
+ {"version":3,"file":"organizationGroupHistory.js","sourceRoot":"","sources":["organizationGroupHistory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oCAAoC,EAAE,MAAM,+DAA+D,CAAC;AACrH,OAAO,EAAE,mCAAmC,EAAE,MAAM,+CAA+C,CAAC;AACpG,OAAO,EAAE,oCAAoC,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,EAAE,uCAAuC,EAAE,MAAM,sDAAsD,CAAC;AAC/G,OAAO,EAAE,2CAA2C,EAAE,MAAM,kDAAkD,CAAC;AAG/G,8DAA8D;AAC9D,MAAM,CAAC,KAAK,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,CAAC","sourcesContent":["import { ProjectionDependencyUnavailableError } from \"../../data/keyingProjectionVerification/dependencyUnavailable\";\nimport { assertProjectionVerificationCurrent } from \"../../data/keyingProjectionVerification/types\";\nimport { loadLocalOrganizationPolicyReference } from \"../../workflows/organizations\";\nimport { buildOrganizationGroupPolicyHistoryPage } from \"../../workflows/organizations/groupPolicyHistoryPage\";\nimport { createRuntimePrincipalPolicyCurrentResolver } from \"../../workflows/principals/runtimePolicyRecovery\";\nimport type { ActiveOrganizationDataRuntime } from \"./organizationWorkflowRuntime\";\n\n/** All group history reads use the private paged verifier. */\nexport async function loadBoundedOrganizationGroupHistory(input: {\n readonly active: ActiveOrganizationDataRuntime;\n readonly groupId: string;\n readonly beforeVersion?: number | undefined;\n readonly stillCurrent: () => boolean;\n}) {\n const { active } = input;\n const resolve = createRuntimePrincipalPolicyCurrentResolver(active.runtime);\n if (!resolve)\n throw new ProjectionDependencyUnavailableError(\n \"Group history requires private paged recovery\",\n );\n assertProjectionVerificationCurrent(input.stillCurrent);\n const reference = await loadLocalOrganizationPolicyReference({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n principalId: input.groupId,\n principalType: \"group\",\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!reference) return null;\n const resolved = await resolve({\n organizationId: active.organizationId,\n reference,\n preferLocalCurrent: true,\n stillCurrent: input.stillCurrent,\n historyPage:\n input.beforeVersion === undefined\n ? {}\n : { beforeVersion: input.beforeVersion },\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!resolved.historyPage)\n throw new Error(\"Verified history page is missing\");\n return buildOrganizationGroupPolicyHistoryPage({\n page: resolved.historyPage,\n groupId: input.groupId,\n organizationId: active.organizationId,\n });\n}\n"]}
@@ -1,6 +1,5 @@
1
- import { loadLocalOrganizationContainerGrants, loadLocalOrganizationDirectoryAndGroups, loadLocalOrganizationGroupContainers, loadLocalOrganizationGroupMembers, loadLocalOrganizationGroupPolicyHistory, loadLocalOrganizationPolicyReference, loadLocalOrganizationUserDetail, reconcileOrganizationDirectoryAndGroups, } from "../../workflows/organizations/index.js";
1
+ import { loadLocalOrganizationContainerGrants, loadLocalOrganizationDirectoryAndGroups, loadLocalOrganizationGroupContainers, loadLocalOrganizationGroupMembers, loadLocalOrganizationUserDetail, reconcileOrganizationDirectoryAndGroups, } from "../../workflows/organizations/index.js";
2
2
  import { organizationAccessScopeKey, wasOrganizationPresentationAccessDeniedByServer, } from "../../workflows/organizations/organizationPresentationAccessState.js";
3
- import { createRuntimePrincipalPolicyWarmer } from "../../workflows/principals/runtimePolicyWarmer.js";
4
3
  import { recoverOrganizationAccess } from "./organizationAccessRestoration.js";
5
4
  import { loadBoundedOrganizationGroupHistory } from "./organizationGroupHistory.js";
6
5
  import { hydrateOrganizationGroupNamesForRuntime } from "./organizationGroupNameHydration.js";
@@ -10,7 +9,6 @@ const coordinatorsByRuntime = new WeakMap();
10
9
  class OrganizationReadModelCoordinatorImpl {
11
10
  runtimeService;
12
11
  reconciliationsByScope = new WeakMap();
13
- policyWarmersByScope = new WeakMap();
14
12
  constructor(runtimeService) {
15
13
  this.runtimeService = runtimeService;
16
14
  }
@@ -23,75 +21,6 @@ class OrganizationReadModelCoordinatorImpl {
23
21
  }
24
22
  return byKey;
25
23
  }
26
- policyWarmerMap(active) {
27
- const scope = active.runtime.state.domainScope;
28
- let byKey = this.policyWarmersByScope.get(scope);
29
- if (!byKey) {
30
- byKey = new Map();
31
- this.policyWarmersByScope.set(scope, byKey);
32
- }
33
- return byKey;
34
- }
35
- async warmPolicyReference(active, reference) {
36
- if (!active.runtime.state.online) {
37
- return;
38
- }
39
- const byKey = this.policyWarmerMap(active);
40
- const key = `${active.organizationId}\0${reference.principalType}\0${reference.principalId}\0${reference.stateHash}`;
41
- const existing = byKey.get(key);
42
- if (existing) {
43
- return existing;
44
- }
45
- const warming = createRuntimePrincipalPolicyWarmer(active.runtime)({
46
- organizationId: active.organizationId,
47
- references: [reference],
48
- }).finally(() => {
49
- if (byKey.get(key) === warming) {
50
- byKey.delete(key);
51
- }
52
- });
53
- byKey.set(key, warming);
54
- return warming;
55
- }
56
- async loadPolicyHistoryAfterWarm(input) {
57
- try {
58
- const local = await input.loadLocal();
59
- if (local) {
60
- return local;
61
- }
62
- }
63
- catch (error) {
64
- input.active.runtime.util.logError("Failed to load verified organization policy history", error);
65
- }
66
- if (!input.active.runtime.state.online) {
67
- return null;
68
- }
69
- let reference;
70
- try {
71
- reference = await loadLocalOrganizationPolicyReference({
72
- currentUserId: input.active.userId,
73
- execSql: input.active.runtime.infra.execSql,
74
- organizationId: input.active.organizationId,
75
- principalId: input.principalId,
76
- principalType: input.principalType,
77
- });
78
- }
79
- catch (error) {
80
- input.active.runtime.util.logError("Failed to load organization policy-head projection", error);
81
- return null;
82
- }
83
- if (!reference) {
84
- return null;
85
- }
86
- await this.warmPolicyReference(input.active, reference);
87
- try {
88
- return await input.loadLocal();
89
- }
90
- catch (error) {
91
- input.active.runtime.util.logError("Failed to reload verified organization policy history", error);
92
- return null;
93
- }
94
- }
95
24
  async loadLocal(organizationId) {
96
25
  const active = activeOrganizationDataRuntime(this.runtimeService, organizationId);
97
26
  if (!active) {
@@ -144,25 +73,12 @@ class OrganizationReadModelCoordinatorImpl {
144
73
  return null;
145
74
  }
146
75
  const domainScope = active.runtime.state.domainScope;
147
- const bounded = await loadBoundedOrganizationGroupHistory({
76
+ return loadBoundedOrganizationGroupHistory({
148
77
  active,
149
78
  groupId,
150
79
  beforeVersion,
151
80
  stillCurrent: () => isOrganizationDataRuntimeCurrent(this.runtimeService, active, domainScope),
152
81
  });
153
- if (bounded !== undefined)
154
- return bounded;
155
- return this.loadPolicyHistoryAfterWarm({
156
- active,
157
- loadLocal: () => loadLocalOrganizationGroupPolicyHistory({
158
- currentUserId: active.userId,
159
- execSql: active.runtime.infra.execSql,
160
- groupId,
161
- organizationId: active.organizationId,
162
- }),
163
- principalId: groupId,
164
- principalType: "group",
165
- });
166
82
  }
167
83
  async loadOrganizationPolicyHistory(organizationId, beforeVersion) {
168
84
  const active = activeOrganizationDataRuntime(this.runtimeService, organizationId);
@@ -1 +1 @@
1
- {"version":3,"file":"organizationReadModels.js","sourceRoot":"","sources":["organizationReadModels.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oCAAoC,EACpC,uCAAuC,EACvC,oCAAoC,EACpC,iCAAiC,EACjC,uCAAuC,EACvC,oCAAoC,EACpC,+BAA+B,EAQ/B,uCAAuC,GACxC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,0BAA0B,EAC1B,+CAA+C,GAChD,MAAM,mEAAmE,CAAC;AAC3E,OAAO,EAAE,kCAAkC,EAAE,MAAM,gDAAgD,CAAC;AAEpG,OAAO,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,uCAAuC,EAAE,MAAM,kCAAkC,CAAC;AAC3F,OAAO,EAAE,8BAA8B,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAEL,6BAA6B,EAC7B,gCAAgC,GACjC,MAAM,+BAA+B,CAAC;AAEvC,MAAM,qBAAqB,GAAG,IAAI,OAAO,EAGtC,CAAC;AA8CJ,MAAM,oCAAoC;IAYX;IATZ,sBAAsB,GAAG,IAAI,OAAO,EAGlD,CAAC;IACa,oBAAoB,GAAG,IAAI,OAAO,EAGhD,CAAC;IAEJ,YAA6B,cAA+B;QAA/B,mBAAc,GAAd,cAAc,CAAiB;IAAG,CAAC;IAExD,iBAAiB,CAAC,MAAqC;QAC7D,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC;QAC/C,IAAI,KAAK,GAAG,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACnD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;YAClB,I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AM,CAAC,CACjE,CAAC;QACF,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IAC/C,CAAC;CACF;AAED,MAAM,UAAU,sCAAsC,CACpD,cAA+B;IAE/B,MAAM,QAAQ,GAAG,qBAAqB,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAC3D,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,MAAM,WAAW,GAAG,IAAI,oCAAoC,CAAC,cAAc,CAAC,CAAC;IAC7E,qBAAqB,CAAC,GAAG,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;IACvD,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import type { DomainScope } from \"../../data/domainScope\";\nimport {\n loadLocalOrganizationContainerGrants,\n loadLocalOrganizationDirectoryAndGroups,\n loadLocalOrganizationGroupContainers,\n loadLocalOrganizationGroupMembers,\n loadLocalOrganizationGroupPolicyHistory,\n loadLocalOrganizationPolicyReference,\n loadLocalOrganizationUserDetail,\n type OrganizationContainerGrants,\n type OrganizationDirectoryAndGroups,\n type OrganizationGroupContainers,\n type OrganizationGroupMembers,\n type OrganizationGroupPolicyHistory,\n type OrganizationPolicyHistory,\n type OrganizationUserDetail,\n reconcileOrganizationDirectoryAndGroups,\n} from \"../../workflows/organizations\";\nimport {\n organizationAccessScopeKey,\n wasOrganizationPresentationAccessDeniedByServer,\n} from \"../../workflows/organizations/organizationPresentationAccessState\";\nimport { createRuntimePrincipalPolicyWarmer } from \"../../workflows/principals/runtimePolicyWarmer\";\nimport type { InternalRuntime } from \"../workflowRuntime\";\nimport { recoverOrganizationAccess } from \"./organizationAccessRestoration\";\nimport { loadBoundedOrganizationGroupHistory } from \"./organizationGroupHistory\";\nimport { hydrateOrganizationGroupNamesForRuntime } from \"./organizationGroupNameHydration\";\nimport { loadBoundedOrganizationHistory } from \"./organizationHistory\";\nimport {\n type ActiveOrganizationDataRuntime,\n activeOrganizationDataRuntime,\n isOrganizationDataRuntimeCurrent,\n} from \"./organizationWorkflowRuntime\";\n\nconst coordinatorsByRuntime = new WeakMap<\n InternalRuntime,\n OrganizationReadModelCoordinator\n>();\n\nexport interface OrganizationReadModelCoordinator {\n loadLocal(\n organizationId?: string | undefined,\n ): Promise<OrganizationDirectoryAndGroups | null>;\n loadLocalGroupMembers(\n groupId: string,\n organizationId?: string | undefined,\n ): Promise<OrganizationGroupMembers | null>;\n loadGroupPolicyHistory(\n groupId: string,\n organizationId?: string | undefined,\n beforeVersion?: number | undefined,\n ): Promise<OrganizationGroupPolicyHistory | null>;\n loadOrganizationPolicyHistory(\n organizationId?: string | undefined,\n beforeVersion?: number | undefined,\n ): Promise<OrganizationPolicyHistory | null>;\n loadLocalGrants(\n organizationId?: string | undefined,\n ): Promise<OrganizationContainerGrants | null>;\n loadLocalGroupContainers(\n groupId: string,\n organizationId?: string | undefined,\n ): Promise<OrganizationGroupContainers | null>;\n loadLocalUserDetail(\n userId: string,\n organizationId?: string | undefined,\n ): Promise<OrganizationUserDetail | null>;\n /**\n * Resolves `undefined` when the runtime declines without I/O (offline,\n * database not ready, organization mismatch) or when the feed failed with\n * no locally retained projection — passes that must not count as caught\n * up; `null` when the reconcile completed but produced nothing presentable\n * — including an authoritative denial that purged the projection, which\n * consumers must repaint.\n */\n reconcile(\n organizationId?: string | undefined,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined>;\n reconcileAfterMutation(\n organizationId?: string | undefined,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined>;\n}\n\nclass OrganizationReadModelCoordinatorImpl\n implements OrganizationReadModelCoordinator\n{\n private readonly reconciliationsByScope = new WeakMap<\n DomainScope,\n Map<string, Promise<OrganizationDirectoryAndGroups | null | undefined>>\n >();\n private readonly policyWarmersByScope = new WeakMap<\n DomainScope,\n Map<string, Promise<void>>\n >();\n\n constructor(private readonly runtimeService: InternalRuntime) {}\n\n private reconciliationMap(active: ActiveOrganizationDataRuntime) {\n const scope = active.runtime.state.domainScope;\n let byKey = this.reconciliationsByScope.get(scope);\n if (!byKey) {\n byKey = new Map();\n this.reconciliationsByScope.set(scope, byKey);\n }\n return byKey;\n }\n\n private policyWarmerMap(active: ActiveOrganizationDataRuntime) {\n const scope = active.runtime.state.domainScope;\n let byKey = this.policyWarmersByScope.get(scope);\n if (!byKey) {\n byKey = new Map();\n this.policyWarmersByScope.set(scope, byKey);\n }\n return byKey;\n }\n\n private async warmPolicyReference(\n active: ActiveOrganizationDataRuntime,\n reference: NonNullable<\n Awaited<ReturnType<typeof loadLocalOrganizationPolicyReference>>\n >,\n ): Promise<void> {\n if (!active.runtime.state.online) {\n return;\n }\n const byKey = this.policyWarmerMap(active);\n const key = `${active.organizationId}\\0${reference.principalType}\\0${reference.principalId}\\0${reference.stateHash}`;\n const existing = byKey.get(key);\n if (existing) {\n return existing;\n }\n const warming = createRuntimePrincipalPolicyWarmer(active.runtime)({\n organizationId: active.organizationId,\n references: [reference],\n }).finally(() => {\n if (byKey.get(key) === warming) {\n byKey.delete(key);\n }\n });\n byKey.set(key, warming);\n return warming;\n }\n\n private async loadPolicyHistoryAfterWarm<History>(input: {\n readonly active: ActiveOrganizationDataRuntime;\n readonly loadLocal: () => Promise<History | null>;\n readonly principalId: string;\n readonly principalType: \"group\" | \"organization\";\n }): Promise<History | null> {\n try {\n const local = await input.loadLocal();\n if (local) {\n return local;\n }\n } catch (error) {\n input.active.runtime.util.logError(\n \"Failed to load verified organization policy history\",\n error,\n );\n }\n if (!input.active.runtime.state.online) {\n return null;\n }\n\n let reference: Awaited<\n ReturnType<typeof loadLocalOrganizationPolicyReference>\n >;\n try {\n reference = await loadLocalOrganizationPolicyReference({\n currentUserId: input.active.userId,\n execSql: input.active.runtime.infra.execSql,\n organizationId: input.active.organizationId,\n principalId: input.principalId,\n principalType: input.principalType,\n });\n } catch (error) {\n input.active.runtime.util.logError(\n \"Failed to load organization policy-head projection\",\n error,\n );\n return null;\n }\n if (!reference) {\n return null;\n }\n\n await this.warmPolicyReference(input.active, reference);\n try {\n return await input.loadLocal();\n } catch (error) {\n input.active.runtime.util.logError(\n \"Failed to reload verified organization policy history\",\n error,\n );\n return null;\n }\n }\n\n async loadLocal(organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active) {\n return null;\n }\n return loadLocalOrganizationDirectoryAndGroups({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n });\n }\n\n async loadLocalGrants(organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active) {\n return null;\n }\n return loadLocalOrganizationContainerGrants({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n });\n }\n\n async loadLocalGroupContainers(groupId: string, organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || groupId.length === 0) {\n return null;\n }\n return loadLocalOrganizationGroupContainers({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n groupId,\n organizationId: active.organizationId,\n });\n }\n\n async loadLocalGroupMembers(groupId: string, organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || groupId.length === 0) {\n return null;\n }\n return loadLocalOrganizationGroupMembers({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n groupId,\n organizationId: active.organizationId,\n });\n }\n\n async loadGroupPolicyHistory(\n groupId: string,\n organizationId?: string,\n beforeVersion?: number,\n ) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || groupId.length === 0) {\n return null;\n }\n const domainScope = active.runtime.state.domainScope;\n const bounded = await loadBoundedOrganizationGroupHistory({\n active,\n groupId,\n beforeVersion,\n stillCurrent: () =>\n isOrganizationDataRuntimeCurrent(\n this.runtimeService,\n active,\n domainScope,\n ),\n });\n if (bounded !== undefined) return bounded;\n return this.loadPolicyHistoryAfterWarm({\n active,\n loadLocal: () =>\n loadLocalOrganizationGroupPolicyHistory({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n groupId,\n organizationId: active.organizationId,\n }),\n principalId: groupId,\n principalType: \"group\",\n });\n }\n\n async loadOrganizationPolicyHistory(\n organizationId?: string,\n beforeVersion?: number,\n ) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active) return null;\n const domainScope = active.runtime.state.domainScope;\n return loadBoundedOrganizationHistory({\n active,\n beforeVersion,\n stillCurrent: () =>\n isOrganizationDataRuntimeCurrent(\n this.runtimeService,\n active,\n domainScope,\n ),\n });\n }\n\n async loadLocalUserDetail(userId: string, organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || userId.length === 0) {\n return null;\n }\n return loadLocalOrganizationUserDetail({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n userId,\n });\n }\n\n reconcile(\n organizationId?: string,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined> {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active?.runtime.state.online) {\n return Promise.resolve(undefined);\n }\n\n const byKey = this.reconciliationMap(active);\n const key = organizationAccessScopeKey(\n active.organizationId,\n active.userId,\n );\n const existing = byKey.get(key);\n if (existing) {\n return existing;\n }\n\n // Snapshot the server-denied flag before reconciling. A reconcile that\n // flips it back to readable means org access was just restored (e.g. the\n // user was re-added to a group), and any write lanes idled by\n // permission-denied submissions have no self re-arm — regaining access is\n // their retry signal, mirroring how billing recovery re-requests all\n // lanes. Only a genuine server denial counts: a local session reset also\n // marks scopes denied, and re-arming on its first reconcile would race the\n // startup sync passes (e.g. pending-create adoption between panes).\n const accessWasDenied = wasOrganizationPresentationAccessDeniedByServer(\n {\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n requesterUserId: active.userId,\n },\n \"readModel\",\n );\n const domainScope = active.runtime.state.domainScope;\n const stillCurrent = () =>\n isOrganizationDataRuntimeCurrent(\n this.runtimeService,\n active,\n domainScope,\n );\n\n const reconciliation = reconcileOrganizationDirectoryAndGroups({\n apiClient: active.runtime.apiClient,\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n logError: active.runtime.util.logError,\n organizationId: active.organizationId,\n })\n .then(async (directoryAndGroups) => {\n if (!stillCurrent()) return null;\n // Feed success restores retry eligibility even if name decryption fails.\n const accessWasRestored =\n accessWasDenied &&\n directoryAndGroups !== null &&\n directoryAndGroups !== undefined;\n if (accessWasRestored)\n await recoverOrganizationAccess({\n active,\n domainScope,\n stillCurrent,\n });\n if (!stillCurrent()) return null;\n return hydrateOrganizationGroupNamesForRuntime(\n this.runtimeService,\n active,\n domainScope,\n directoryAndGroups,\n );\n })\n .finally(() => {\n if (byKey.get(key) === reconciliation) {\n byKey.delete(key);\n }\n });\n byKey.set(key, reconciliation);\n return reconciliation;\n }\n\n async reconcileAfterMutation(\n organizationId?: string,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined> {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active?.runtime.state.online) {\n return undefined;\n }\n const existing = this.reconciliationMap(active).get(\n organizationAccessScopeKey(active.organizationId, active.userId),\n );\n if (existing) {\n await existing.catch(() => null);\n }\n return this.reconcile(active.organizationId);\n }\n}\n\nexport function createOrganizationReadModelCoordinator(\n runtimeService: InternalRuntime,\n): OrganizationReadModelCoordinator {\n const existing = coordinatorsByRuntime.get(runtimeService);\n if (existing) {\n return existing;\n }\n\n const coordinator = new OrganizationReadModelCoordinatorImpl(runtimeService);\n coordinatorsByRuntime.set(runtimeService, coordinator);\n return coordinator;\n}\n"]}
1
+ {"version":3,"file":"organizationReadModels.js","sourceRoot":"","sources":["organiz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IACxB,CAAC;IAED,KAAK,CAAC,sBAAsB,CAC1B,cAAuB;QAEvB,MAAM,MAAM,GAAG,6BAA6B,CAC1C,IAAI,CAAC,cAAc,EACnB,cAAc,CACf,CAAC;QACF,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YAClC,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,MAAM,QAAQ,GAAG,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,GAAG,CACjD,0BAA0B,CAAC,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,CACjE,CAAC;QACF,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IAC/C,CAAC;CACF;AAED,MAAM,UAAU,sCAAsC,CACpD,cAA+B;IAE/B,MAAM,QAAQ,GAAG,qBAAqB,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAC3D,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,MAAM,WAAW,GAAG,IAAI,oCAAoC,CAAC,cAAc,CAAC,CAAC;IAC7E,qBAAqB,CAAC,GAAG,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;IACvD,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import type { DomainScope } from \"../../data/domainScope\";\nimport {\n loadLocalOrganizationContainerGrants,\n loadLocalOrganizationDirectoryAndGroups,\n loadLocalOrganizationGroupContainers,\n loadLocalOrganizationGroupMembers,\n loadLocalOrganizationUserDetail,\n type OrganizationContainerGrants,\n type OrganizationDirectoryAndGroups,\n type OrganizationGroupContainers,\n type OrganizationGroupMembers,\n type OrganizationGroupPolicyHistory,\n type OrganizationPolicyHistory,\n type OrganizationUserDetail,\n reconcileOrganizationDirectoryAndGroups,\n} from \"../../workflows/organizations\";\nimport {\n organizationAccessScopeKey,\n wasOrganizationPresentationAccessDeniedByServer,\n} from \"../../workflows/organizations/organizationPresentationAccessState\";\nimport type { InternalRuntime } from \"../workflowRuntime\";\nimport { recoverOrganizationAccess } from \"./organizationAccessRestoration\";\nimport { loadBoundedOrganizationGroupHistory } from \"./organizationGroupHistory\";\nimport { hydrateOrganizationGroupNamesForRuntime } from \"./organizationGroupNameHydration\";\nimport { loadBoundedOrganizationHistory } from \"./organizationHistory\";\nimport {\n type ActiveOrganizationDataRuntime,\n activeOrganizationDataRuntime,\n isOrganizationDataRuntimeCurrent,\n} from \"./organizationWorkflowRuntime\";\n\nconst coordinatorsByRuntime = new WeakMap<\n InternalRuntime,\n OrganizationReadModelCoordinator\n>();\n\nexport interface OrganizationReadModelCoordinator {\n loadLocal(\n organizationId?: string | undefined,\n ): Promise<OrganizationDirectoryAndGroups | null>;\n loadLocalGroupMembers(\n groupId: string,\n organizationId?: string | undefined,\n ): Promise<OrganizationGroupMembers | null>;\n loadGroupPolicyHistory(\n groupId: string,\n organizationId?: string | undefined,\n beforeVersion?: number | undefined,\n ): Promise<OrganizationGroupPolicyHistory | null>;\n loadOrganizationPolicyHistory(\n organizationId?: string | undefined,\n beforeVersion?: number | undefined,\n ): Promise<OrganizationPolicyHistory | null>;\n loadLocalGrants(\n organizationId?: string | undefined,\n ): Promise<OrganizationContainerGrants | null>;\n loadLocalGroupContainers(\n groupId: string,\n organizationId?: string | undefined,\n ): Promise<OrganizationGroupContainers | null>;\n loadLocalUserDetail(\n userId: string,\n organizationId?: string | undefined,\n ): Promise<OrganizationUserDetail | null>;\n /**\n * Resolves `undefined` when the runtime declines without I/O (offline,\n * database not ready, organization mismatch) or when the feed failed with\n * no locally retained projection — passes that must not count as caught\n * up; `null` when the reconcile completed but produced nothing presentable\n * — including an authoritative denial that purged the projection, which\n * consumers must repaint.\n */\n reconcile(\n organizationId?: string | undefined,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined>;\n reconcileAfterMutation(\n organizationId?: string | undefined,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined>;\n}\n\nclass OrganizationReadModelCoordinatorImpl\n implements OrganizationReadModelCoordinator\n{\n private readonly reconciliationsByScope = new WeakMap<\n DomainScope,\n Map<string, Promise<OrganizationDirectoryAndGroups | null | undefined>>\n >();\n constructor(private readonly runtimeService: InternalRuntime) {}\n\n private reconciliationMap(active: ActiveOrganizationDataRuntime) {\n const scope = active.runtime.state.domainScope;\n let byKey = this.reconciliationsByScope.get(scope);\n if (!byKey) {\n byKey = new Map();\n this.reconciliationsByScope.set(scope, byKey);\n }\n return byKey;\n }\n\n async loadLocal(organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active) {\n return null;\n }\n return loadLocalOrganizationDirectoryAndGroups({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n });\n }\n\n async loadLocalGrants(organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active) {\n return null;\n }\n return loadLocalOrganizationContainerGrants({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n });\n }\n\n async loadLocalGroupContainers(groupId: string, organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || groupId.length === 0) {\n return null;\n }\n return loadLocalOrganizationGroupContainers({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n groupId,\n organizationId: active.organizationId,\n });\n }\n\n async loadLocalGroupMembers(groupId: string, organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || groupId.length === 0) {\n return null;\n }\n return loadLocalOrganizationGroupMembers({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n groupId,\n organizationId: active.organizationId,\n });\n }\n\n async loadGroupPolicyHistory(\n groupId: string,\n organizationId?: string,\n beforeVersion?: number,\n ) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || groupId.length === 0) {\n return null;\n }\n const domainScope = active.runtime.state.domainScope;\n return loadBoundedOrganizationGroupHistory({\n active,\n groupId,\n beforeVersion,\n stillCurrent: () =>\n isOrganizationDataRuntimeCurrent(\n this.runtimeService,\n active,\n domainScope,\n ),\n });\n }\n\n async loadOrganizationPolicyHistory(\n organizationId?: string,\n beforeVersion?: number,\n ) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active) return null;\n const domainScope = active.runtime.state.domainScope;\n return loadBoundedOrganizationHistory({\n active,\n beforeVersion,\n stillCurrent: () =>\n isOrganizationDataRuntimeCurrent(\n this.runtimeService,\n active,\n domainScope,\n ),\n });\n }\n\n async loadLocalUserDetail(userId: string, organizationId?: string) {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active || userId.length === 0) {\n return null;\n }\n return loadLocalOrganizationUserDetail({\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n userId,\n });\n }\n\n reconcile(\n organizationId?: string,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined> {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active?.runtime.state.online) {\n return Promise.resolve(undefined);\n }\n\n const byKey = this.reconciliationMap(active);\n const key = organizationAccessScopeKey(\n active.organizationId,\n active.userId,\n );\n const existing = byKey.get(key);\n if (existing) {\n return existing;\n }\n\n // Snapshot the server-denied flag before reconciling. A reconcile that\n // flips it back to readable means org access was just restored (e.g. the\n // user was re-added to a group), and any write lanes idled by\n // permission-denied submissions have no self re-arm — regaining access is\n // their retry signal, mirroring how billing recovery re-requests all\n // lanes. Only a genuine server denial counts: a local session reset also\n // marks scopes denied, and re-arming on its first reconcile would race the\n // startup sync passes (e.g. pending-create adoption between panes).\n const accessWasDenied = wasOrganizationPresentationAccessDeniedByServer(\n {\n execSql: active.runtime.infra.execSql,\n organizationId: active.organizationId,\n requesterUserId: active.userId,\n },\n \"readModel\",\n );\n const domainScope = active.runtime.state.domainScope;\n const stillCurrent = () =>\n isOrganizationDataRuntimeCurrent(\n this.runtimeService,\n active,\n domainScope,\n );\n\n const reconciliation = reconcileOrganizationDirectoryAndGroups({\n apiClient: active.runtime.apiClient,\n currentUserId: active.userId,\n execSql: active.runtime.infra.execSql,\n logError: active.runtime.util.logError,\n organizationId: active.organizationId,\n })\n .then(async (directoryAndGroups) => {\n if (!stillCurrent()) return null;\n // Feed success restores retry eligibility even if name decryption fails.\n const accessWasRestored =\n accessWasDenied &&\n directoryAndGroups !== null &&\n directoryAndGroups !== undefined;\n if (accessWasRestored)\n await recoverOrganizationAccess({\n active,\n domainScope,\n stillCurrent,\n });\n if (!stillCurrent()) return null;\n return hydrateOrganizationGroupNamesForRuntime(\n this.runtimeService,\n active,\n domainScope,\n directoryAndGroups,\n );\n })\n .finally(() => {\n if (byKey.get(key) === reconciliation) {\n byKey.delete(key);\n }\n });\n byKey.set(key, reconciliation);\n return reconciliation;\n }\n\n async reconcileAfterMutation(\n organizationId?: string,\n ): Promise<OrganizationDirectoryAndGroups | null | undefined> {\n const active = activeOrganizationDataRuntime(\n this.runtimeService,\n organizationId,\n );\n if (!active?.runtime.state.online) {\n return undefined;\n }\n const existing = this.reconciliationMap(active).get(\n organizationAccessScopeKey(active.organizationId, active.userId),\n );\n if (existing) {\n await existing.catch(() => null);\n }\n return this.reconcile(active.organizationId);\n }\n}\n\nexport function createOrganizationReadModelCoordinator(\n runtimeService: InternalRuntime,\n): OrganizationReadModelCoordinator {\n const existing = coordinatorsByRuntime.get(runtimeService);\n if (existing) {\n return existing;\n }\n\n const coordinator = new OrganizationReadModelCoordinatorImpl(runtimeService);\n coordinatorsByRuntime.set(runtimeService, coordinator);\n return coordinator;\n}\n"]}
@@ -1,5 +1,6 @@
1
+ import { ProjectionDependencyUnavailableError } from "../../data/keyingProjectionVerification/dependencyUnavailable.js";
1
2
  import { runWithSecurityIncidentReporting } from "../../data/keyingProjectionVerification/error.js";
2
- import { addOrganizationGroupUser, createOrganizationGroup, deleteOrganizationGroup, removeOrganizationGroupUser, revokeOrganizationContainerGrant, rotateOrganizationGroupForAccessSetShrink, } from "../../workflows/organizations/index.js";
3
+ import { revokeOrganizationContainerGrant } from "../../workflows/organizations/index.js";
3
4
  import { createCurrentOrganizationGroup } from "../../workflows/organizations/createCurrentOrganizationGroup.js";
4
5
  import { createSelectedCurrentGroupMetadataContainerVerifier } from "../../workflows/organizations/currentGroupMetadataAuthority.js";
5
6
  import { deleteCurrentOrganizationGroup } from "../../workflows/organizations/deleteCurrentOrganizationGroup.js";
@@ -8,7 +9,6 @@ import { createRuntimeCurrentOrganizationMutation } from "../../workflows/organi
8
9
  import { createRuntimePrincipalPolicyWarmer } from "../../workflows/principals/runtimePolicyWarmer.js";
9
10
  import { createCurrentPrincipalMutation } from "./currentPrincipalMutations.js";
10
11
  import { syncOrganizationMetadataProfile } from "./organizationMetadataProfileSync.js";
11
- import { preparePrincipalContainerMutations } from "./principalContainerMutations.js";
12
12
  function requireSigningContext(runtime) {
13
13
  if (!runtime.auth.organizationId ||
14
14
  !runtime.auth.userId ||
@@ -37,45 +37,18 @@ export async function addUserToOrganizationGroup(input) {
37
37
  operation: "group.member.add",
38
38
  organizationId: signingContext.organizationId,
39
39
  }, async () => {
40
- let memberGroupId = null;
41
40
  const mutateCurrent = createCurrentPrincipalMutation({
42
41
  ...signingContext,
43
42
  runtime: input.runtime,
44
43
  stillCurrent: input.stillCurrent,
45
44
  });
46
- const current = mutateCurrent
47
- ? await mutateCurrent(input.groupId, {
48
- kind: "add",
49
- expectedGroupName: input.expectedGroupName,
50
- targetUserId: input.targetUserId,
51
- })
52
- : undefined;
53
- memberGroupId = current?.memberGroupId ?? null;
54
- const bundle = current?.response ??
55
- (await addOrganizationGroupUser({
56
- apiClient: input.runtime.apiClient,
57
- beforePolicyCommit: (_head, authority) => {
58
- memberGroupId = authority.memberGroupId;
59
- },
60
- currentUserSecretKey: requireEncapsulationKeyPair(input.runtime)
61
- .secretKey,
62
- execSql: input.runtime.infra.execSql,
63
- expectedGroupName: input.expectedGroupName,
64
- readEncryptedName: createRuntimeGroupMetadataAccess(input.runtime, signingContext.organizationId, input.stillCurrent).readName,
65
- groupId: input.groupId,
66
- prepareContainerMutations: ({ currentPolicy, nextPolicy }) => preparePrincipalContainerMutations({
67
- currentPolicy,
68
- groupId: input.groupId,
69
- nextPolicy,
70
- organizationId: signingContext.organizationId,
71
- runtime: input.runtime,
72
- stillCurrent: input.stillCurrent,
73
- }),
74
- reportSecurityIncident: input.runtime.util.reportSecurityIncident,
75
- resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
76
- targetUserId: input.targetUserId,
77
- ...signingContext,
78
- }));
45
+ if (!mutateCurrent)
46
+ throw new ProjectionDependencyUnavailableError("Principal history recovery is unavailable");
47
+ const { memberGroupId, response: bundle } = await mutateCurrent(input.groupId, {
48
+ kind: "add",
49
+ expectedGroupName: input.expectedGroupName,
50
+ targetUserId: input.targetUserId,
51
+ });
79
52
  if (input.groupId === memberGroupId) {
80
53
  await syncOrganizationMetadataProfile({
81
54
  containerContents: input.containerContents,
@@ -97,33 +70,23 @@ export function createGroupForOrganization(input) {
97
70
  }, () => {
98
71
  const creatorEncapsulationKeyPair = requireEncapsulationKeyPair(input.runtime);
99
72
  const mutate = createRuntimeCurrentOrganizationMutation(input.runtime);
100
- if (mutate)
101
- return mutate({ ...signingContext, stillCurrent: input.stillCurrent }, (context) => createCurrentOrganizationGroup({
102
- ...signingContext,
103
- context,
104
- apiClient: input.runtime.apiClient,
105
- creatorEncapsulationKeyPair,
106
- execSql: input.runtime.infra.execSql,
107
- name: input.name,
108
- metadataAccess: createRuntimeGroupMetadataAccess(input.runtime, signingContext.organizationId, context.stillCurrent, createSelectedCurrentGroupMetadataContainerVerifier({
109
- authority: context,
110
- organizationId: signingContext.organizationId,
111
- stillCurrent: context.stillCurrent,
112
- })),
113
- reportSecurityIncident: input.runtime.util.reportSecurityIncident,
114
- resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
115
- }));
116
- return createOrganizationGroup({
117
- stillCurrent: input.stillCurrent,
73
+ if (!mutate)
74
+ throw new ProjectionDependencyUnavailableError("Principal history recovery is unavailable");
75
+ return mutate({ ...signingContext, stillCurrent: input.stillCurrent }, (context) => createCurrentOrganizationGroup({
76
+ ...signingContext,
77
+ context,
118
78
  apiClient: input.runtime.apiClient,
119
79
  creatorEncapsulationKeyPair,
120
80
  execSql: input.runtime.infra.execSql,
121
81
  name: input.name,
122
- metadataAccess: createRuntimeGroupMetadataAccess(input.runtime, signingContext.organizationId, input.stillCurrent),
82
+ metadataAccess: createRuntimeGroupMetadataAccess(input.runtime, signingContext.organizationId, context.stillCurrent, createSelectedCurrentGroupMetadataContainerVerifier({
83
+ authority: context,
84
+ organizationId: signingContext.organizationId,
85
+ stillCurrent: context.stillCurrent,
86
+ })),
123
87
  reportSecurityIncident: input.runtime.util.reportSecurityIncident,
124
88
  resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
125
- ...signingContext,
126
- });
89
+ }));
127
90
  });
128
91
  }
129
92
  export function deleteGroupForOrganization(input) {
@@ -135,22 +98,15 @@ export function deleteGroupForOrganization(input) {
135
98
  organizationId: signingContext.organizationId,
136
99
  }, () => {
137
100
  const mutate = createRuntimeCurrentOrganizationMutation(input.runtime);
138
- if (mutate)
139
- return mutate({ ...signingContext, stillCurrent: input.stillCurrent }, (context) => deleteCurrentOrganizationGroup({
140
- ...signingContext,
141
- context,
142
- groupId: input.groupId,
143
- apiClient: input.runtime.apiClient,
144
- resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
145
- }));
146
- return deleteOrganizationGroup({
147
- stillCurrent: input.stillCurrent,
148
- apiClient: input.runtime.apiClient,
149
- execSql: input.runtime.infra.execSql,
101
+ if (!mutate)
102
+ throw new ProjectionDependencyUnavailableError("Principal history recovery is unavailable");
103
+ return mutate({ ...signingContext, stillCurrent: input.stillCurrent }, (context) => deleteCurrentOrganizationGroup({
104
+ ...signingContext,
105
+ context,
150
106
  groupId: input.groupId,
107
+ apiClient: input.runtime.apiClient,
151
108
  resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
152
- ...signingContext,
153
- });
109
+ }));
154
110
  });
155
111
  }
156
112
  export async function removeUserFromOrganizationGroup(input) {
@@ -161,42 +117,18 @@ export async function removeUserFromOrganizationGroup(input) {
161
117
  operation: "group.member.remove",
162
118
  organizationId: signingContext.organizationId,
163
119
  }, async () => {
164
- let memberGroupId = null;
165
120
  const mutateCurrent = createCurrentPrincipalMutation({
166
121
  ...signingContext,
167
122
  runtime: input.runtime,
168
123
  stillCurrent: input.stillCurrent,
169
124
  });
170
- const current = mutateCurrent
171
- ? await mutateCurrent(input.groupId, {
172
- kind: "remove",
173
- expectedGroupName: input.expectedGroupName,
174
- removedUserId: input.removedUserId,
175
- })
176
- : undefined;
177
- memberGroupId = current?.memberGroupId ?? null;
178
- const bundle = current?.response ??
179
- (await removeOrganizationGroupUser({
180
- apiClient: input.runtime.apiClient,
181
- beforePolicyCommit: (_head, authority) => {
182
- memberGroupId = authority.memberGroupId;
183
- },
184
- execSql: input.runtime.infra.execSql,
185
- expectedGroupName: input.expectedGroupName,
186
- readEncryptedName: createRuntimeGroupMetadataAccess(input.runtime, signingContext.organizationId, input.stillCurrent).readName,
187
- groupId: input.groupId,
188
- prepareContainerMutations: ({ currentPolicy, nextPolicy }) => preparePrincipalContainerMutations({
189
- currentPolicy,
190
- groupId: input.groupId,
191
- nextPolicy,
192
- organizationId: signingContext.organizationId,
193
- runtime: input.runtime,
194
- stillCurrent: input.stillCurrent,
195
- }),
196
- removedUserId: input.removedUserId,
197
- resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
198
- ...signingContext,
199
- }));
125
+ if (!mutateCurrent)
126
+ throw new ProjectionDependencyUnavailableError("Principal history recovery is unavailable");
127
+ const { memberGroupId, response: bundle } = await mutateCurrent(input.groupId, {
128
+ kind: "remove",
129
+ expectedGroupName: input.expectedGroupName,
130
+ removedUserId: input.removedUserId,
131
+ });
200
132
  if (input.groupId === memberGroupId) {
201
133
  await syncOrganizationMetadataProfile({
202
134
  containerContents: input.containerContents,
@@ -220,37 +152,22 @@ export async function revokeOrganizationGrant(input) {
220
152
  operation: "principal.grant.revoke",
221
153
  organizationId: signingContext.organizationId,
222
154
  }, async () => {
223
- const mutateCurrent = createCurrentPrincipalMutation({
224
- ...signingContext,
225
- runtime: input.runtime,
226
- stillCurrent: input.stillCurrent,
227
- });
228
- const current = input.subjectType === "group" && mutateCurrent
229
- ? await mutateCurrent(input.subjectId, {
230
- kind: "revoke",
231
- revokedContainerId: input.containerId,
232
- })
233
- : undefined;
234
- const response = current?.response ??
235
- (input.subjectType === "group"
236
- ? await rotateOrganizationGroupForAccessSetShrink({
237
- apiClient: input.runtime.apiClient,
238
- execSql: input.runtime.infra.execSql,
239
- groupId: input.subjectId,
240
- revokedContainerId: input.containerId,
241
- prepareContainerMutations: ({ currentPolicy, nextPolicy }) => preparePrincipalContainerMutations({
242
- currentPolicy,
243
- groupId: input.subjectId,
244
- nextPolicy,
245
- organizationId: signingContext.organizationId,
246
- revokedContainerId: input.containerId,
247
- runtime: input.runtime,
248
- stillCurrent: input.stillCurrent,
249
- }),
250
- resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
155
+ const revoke = async () => {
156
+ if (input.subjectType === "group") {
157
+ const mutateCurrent = createCurrentPrincipalMutation({
251
158
  ...signingContext,
252
- })
253
- : await revokeOrganizationContainerGrant({
159
+ runtime: input.runtime,
160
+ stillCurrent: input.stillCurrent,
161
+ });
162
+ if (!mutateCurrent)
163
+ throw new ProjectionDependencyUnavailableError("Principal history recovery is unavailable");
164
+ return (await mutateCurrent(input.subjectId, {
165
+ kind: "revoke",
166
+ revokedContainerId: input.containerId,
167
+ })).response;
168
+ }
169
+ else {
170
+ return revokeOrganizationContainerGrant({
254
171
  stillCurrent: input.stillCurrent,
255
172
  reportSecurityIncident: input.runtime.util.reportSecurityIncident,
256
173
  apiClient: input.runtime.apiClient,
@@ -264,7 +181,10 @@ export async function revokeOrganizationGrant(input) {
264
181
  resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,
265
182
  warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(input.runtime),
266
183
  ...signingContext,
267
- }));
184
+ });
185
+ }
186
+ };
187
+ const response = await revoke();
268
188
  await input.readModelCoordinator.reconcileAfterMutation(signingContext.organizationId);
269
189
  return response;
270
190
  });
@@ -1 +1 @@
1
- {"version":3,"file":"principalMutations.js","sourceRoot":"","sources":["principalMutations.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gCAAgC,EAAE,MAAM,+CAA+C,CAAC;AACjG,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACvB,uBAAuB,EACvB,2BAA2B,EAC3B,gCAAgC,EAChC,yCAAyC,GAC1C,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,8BAA8B,EAAE,MAAM,8DAA8D,CAAC;AAC9G,OAAO,EAAE,mDAAmD,EAAE,MAAM,6DAA6D,CAAC;AAClI,OAAO,EAAE,8BAA8B,EAAE,MAAM,8DAA8D,CAAC;AAC9G,OAAO,EAAE,gCAAgC,EAAE,MAAM,oDAAoD,CAAC;AACtG,OAAO,EAAE,wCAAwC,EAAE,MAAM,kEAAkE,CAAC;AAC5H,OAAO,EAAE,kCAAkC,EAAE,MAAM,gDAAgD,CAAC;AAGpG,OAAO,EAAE,8BAA8B,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC;AAEpF,OAAO,EAAE,kCAAkC,EAAE,MAAM,+BAA+B,CAAC;AAgCnF,SAAS,qBAAqB,CAC5B,OAAqC;IAErC,IACE,CAAC,OAAO,CAAC,IAAI,C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E;wBACd,SAAS,EAAE,KAAK,CAAC,SAAS;wBAC1B,WAAW,EAAE,KAAK,CAAC,WAAW;qBAC/B;oBACD,0BAA0B,EACxB,KAAK,CAAC,OAAO,CAAC,0BAA0B;oBAC1C,+BAA+B,EAC7B,kCAAkC,CAAC,KAAK,CAAC,OAAO,CAAC;oBACnD,GAAG,cAAc;iBAClB,CAAC,CAAC,CAAC;QACV,MAAM,KAAK,CAAC,oBAAoB,CAAC,sBAAsB,CACrD,cAAc,CAAC,cAAc,CAC9B,CAAC;QACF,OAAO,QAAQ,CAAC;IAClB,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import type { ContainerGrantSubjectType } from \"@tearleads/crypto\";\nimport { runWithSecurityIncidentReporting } from \"../../data/keyingProjectionVerification/error\";\nimport {\n addOrganizationGroupUser,\n createOrganizationGroup,\n deleteOrganizationGroup,\n removeOrganizationGroupUser,\n revokeOrganizationContainerGrant,\n rotateOrganizationGroupForAccessSetShrink,\n} from \"../../workflows/organizations\";\nimport { createCurrentOrganizationGroup } from \"../../workflows/organizations/createCurrentOrganizationGroup\";\nimport { createSelectedCurrentGroupMetadataContainerVerifier } from \"../../workflows/organizations/currentGroupMetadataAuthority\";\nimport { deleteCurrentOrganizationGroup } from \"../../workflows/organizations/deleteCurrentOrganizationGroup\";\nimport { createRuntimeGroupMetadataAccess } from \"../../workflows/organizations/groupMetadataRuntime\";\nimport { createRuntimeCurrentOrganizationMutation } from \"../../workflows/organizations/runtimeCurrentOrganizationMutation\";\nimport { createRuntimePrincipalPolicyWarmer } from \"../../workflows/principals/runtimePolicyWarmer\";\nimport type { ContainerContents } from \"../containerContents\";\nimport type { InternalWorkflowRuntimeInput } from \"../workflowRuntime\";\nimport { createCurrentPrincipalMutation } from \"./currentPrincipalMutations\";\nimport { syncOrganizationMetadataProfile } from \"./organizationMetadataProfileSync\";\nimport type { OrganizationReadModelCoordinator } from \"./organizationReadModels\";\nimport { preparePrincipalContainerMutations } from \"./principalContainerMutations\";\n\nexport interface OrganizationGrantRef {\n containerId: string;\n subjectId: string;\n subjectType: ContainerGrantSubjectType;\n}\n\nexport interface OrganizationGroupUserMutationInput {\n groupId: string;\n expectedGroupName: string;\n}\n\nexport interface AddOrganizationGroupUserInput\n extends OrganizationGroupUserMutationInput {\n targetUserId: string;\n}\n\nexport interface RemoveOrganizationGroupUserInput\n extends OrganizationGroupUserMutationInput {\n removedUserId: string;\n}\n\ninterface OrganizationSigningContext {\n organizationId: string;\n signerUserId: string;\n signingFingerprint: string;\n signingKeyPair: NonNullable<\n InternalWorkflowRuntimeInput[\"crypto\"][\"signingKeyPair\"]\n >;\n}\n\nfunction requireSigningContext(\n runtime: InternalWorkflowRuntimeInput,\n): OrganizationSigningContext {\n if (\n !runtime.auth.organizationId ||\n !runtime.auth.userId ||\n !runtime.crypto.signingFingerprint ||\n !runtime.crypto.signingKeyPair\n ) {\n throw new Error(\"Organization signing context is unavailable\");\n }\n\n return {\n organizationId: runtime.auth.organizationId,\n signerUserId: runtime.auth.userId,\n signingFingerprint: runtime.crypto.signingFingerprint,\n signingKeyPair: runtime.crypto.signingKeyPair,\n };\n}\n\nfunction requireEncapsulationKeyPair(\n runtime: InternalWorkflowRuntimeInput,\n): NonNullable<InternalWorkflowRuntimeInput[\"crypto\"][\"encapsulationKeyPair\"]> {\n if (!runtime.crypto.encapsulationKeyPair) {\n throw new Error(\"Organization encryption context is unavailable\");\n }\n\n return runtime.crypto.encapsulationKeyPair;\n}\n\ninterface PrincipalMutationRuntime {\n readonly stillCurrent: () => boolean;\n readonly containerContents: ContainerContents;\n readonly readModelCoordinator: OrganizationReadModelCoordinator;\n readonly runtime: InternalWorkflowRuntimeInput;\n}\n\nexport async function addUserToOrganizationGroup(\n input: PrincipalMutationRuntime & AddOrganizationGroupUserInput,\n) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.groupId,\n objectKind: \"principal\",\n operation: \"group.member.add\",\n organizationId: signingContext.organizationId,\n },\n async () => {\n let memberGroupId: string | null = null;\n const mutateCurrent = createCurrentPrincipalMutation({\n ...signingContext,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n });\n const current = mutateCurrent\n ? await mutateCurrent(input.groupId, {\n kind: \"add\",\n expectedGroupName: input.expectedGroupName,\n targetUserId: input.targetUserId,\n })\n : undefined;\n memberGroupId = current?.memberGroupId ?? null;\n const bundle =\n current?.response ??\n (await addOrganizationGroupUser({\n apiClient: input.runtime.apiClient,\n beforePolicyCommit: (_head, authority) => {\n memberGroupId = authority.memberGroupId;\n },\n currentUserSecretKey: requireEncapsulationKeyPair(input.runtime)\n .secretKey,\n execSql: input.runtime.infra.execSql,\n expectedGroupName: input.expectedGroupName,\n readEncryptedName: createRuntimeGroupMetadataAccess(\n input.runtime,\n signingContext.organizationId,\n input.stillCurrent,\n ).readName,\n groupId: input.groupId,\n prepareContainerMutations: ({ currentPolicy, nextPolicy }) =>\n preparePrincipalContainerMutations({\n currentPolicy,\n groupId: input.groupId,\n nextPolicy,\n organizationId: signingContext.organizationId,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n }),\n reportSecurityIncident: input.runtime.util.reportSecurityIncident,\n resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,\n targetUserId: input.targetUserId,\n ...signingContext,\n }));\n if (input.groupId === memberGroupId) {\n await syncOrganizationMetadataProfile({\n containerContents: input.containerContents,\n log: input.runtime.util.log,\n organizationId: signingContext.organizationId,\n });\n }\n await input.readModelCoordinator.reconcileAfterMutation(\n signingContext.organizationId,\n );\n return bundle;\n },\n );\n}\n\nexport function createGroupForOrganization(input: {\n readonly name: string;\n readonly runtime: InternalWorkflowRuntimeInput;\n readonly stillCurrent: () => boolean;\n}) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: signingContext.organizationId,\n objectKind: \"principal\",\n operation: \"group.create\",\n organizationId: signingContext.organizationId,\n },\n () => {\n const creatorEncapsulationKeyPair = requireEncapsulationKeyPair(\n input.runtime,\n );\n const mutate = createRuntimeCurrentOrganizationMutation(input.runtime);\n if (mutate)\n return mutate(\n { ...signingContext, stillCurrent: input.stillCurrent },\n (context) =>\n createCurrentOrganizationGroup({\n ...signingContext,\n context,\n apiClient: input.runtime.apiClient,\n creatorEncapsulationKeyPair,\n execSql: input.runtime.infra.execSql,\n name: input.name,\n metadataAccess: createRuntimeGroupMetadataAccess(\n input.runtime,\n signingContext.organizationId,\n context.stillCurrent,\n createSelectedCurrentGroupMetadataContainerVerifier({\n authority: context,\n organizationId: signingContext.organizationId,\n stillCurrent: context.stillCurrent,\n }),\n ),\n reportSecurityIncident: input.runtime.util.reportSecurityIncident,\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n }),\n );\n return createOrganizationGroup({\n stillCurrent: input.stillCurrent,\n apiClient: input.runtime.apiClient,\n creatorEncapsulationKeyPair,\n execSql: input.runtime.infra.execSql,\n name: input.name,\n metadataAccess: createRuntimeGroupMetadataAccess(\n input.runtime,\n signingContext.organizationId,\n input.stillCurrent,\n ),\n reportSecurityIncident: input.runtime.util.reportSecurityIncident,\n resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,\n ...signingContext,\n });\n },\n );\n}\n\nexport function deleteGroupForOrganization(input: {\n readonly groupId: string;\n readonly runtime: InternalWorkflowRuntimeInput;\n readonly stillCurrent: () => boolean;\n}) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.groupId,\n objectKind: \"principal\",\n operation: \"group.delete\",\n organizationId: signingContext.organizationId,\n },\n () => {\n const mutate = createRuntimeCurrentOrganizationMutation(input.runtime);\n if (mutate)\n return mutate(\n { ...signingContext, stillCurrent: input.stillCurrent },\n (context) =>\n deleteCurrentOrganizationGroup({\n ...signingContext,\n context,\n groupId: input.groupId,\n apiClient: input.runtime.apiClient,\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n }),\n );\n return deleteOrganizationGroup({\n stillCurrent: input.stillCurrent,\n apiClient: input.runtime.apiClient,\n execSql: input.runtime.infra.execSql,\n groupId: input.groupId,\n resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,\n ...signingContext,\n });\n },\n );\n}\n\nexport async function removeUserFromOrganizationGroup(\n input: PrincipalMutationRuntime & RemoveOrganizationGroupUserInput,\n) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.groupId,\n objectKind: \"principal\",\n operation: \"group.member.remove\",\n organizationId: signingContext.organizationId,\n },\n async () => {\n let memberGroupId: string | null = null;\n const mutateCurrent = createCurrentPrincipalMutation({\n ...signingContext,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n });\n const current = mutateCurrent\n ? await mutateCurrent(input.groupId, {\n kind: \"remove\",\n expectedGroupName: input.expectedGroupName,\n removedUserId: input.removedUserId,\n })\n : undefined;\n memberGroupId = current?.memberGroupId ?? null;\n const bundle =\n current?.response ??\n (await removeOrganizationGroupUser({\n apiClient: input.runtime.apiClient,\n beforePolicyCommit: (_head, authority) => {\n memberGroupId = authority.memberGroupId;\n },\n execSql: input.runtime.infra.execSql,\n expectedGroupName: input.expectedGroupName,\n readEncryptedName: createRuntimeGroupMetadataAccess(\n input.runtime,\n signingContext.organizationId,\n input.stillCurrent,\n ).readName,\n groupId: input.groupId,\n prepareContainerMutations: ({ currentPolicy, nextPolicy }) =>\n preparePrincipalContainerMutations({\n currentPolicy,\n groupId: input.groupId,\n nextPolicy,\n organizationId: signingContext.organizationId,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n }),\n removedUserId: input.removedUserId,\n resolveTrustedUserIdentity: input.runtime.resolveTrustedUserIdentity,\n ...signingContext,\n }));\n if (input.groupId === memberGroupId) {\n await syncOrganizationMetadataProfile({\n containerContents: input.containerContents,\n log: input.runtime.util.log,\n organizationId: signingContext.organizationId,\n });\n }\n await input.readModelCoordinator.reconcileAfterMutation(\n signingContext.organizationId,\n );\n return bundle;\n },\n );\n}\n\nexport async function revokeOrganizationGrant(\n input: PrincipalMutationRuntime & OrganizationGrantRef,\n) {\n const signingContext = requireSigningContext(input.runtime);\n const encapsulationKeyPair = requireEncapsulationKeyPair(input.runtime);\n if (input.runtime.infra.dbStatus !== \"ready\") {\n throw new Error(\"Organization local database is unavailable\");\n }\n\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.subjectId,\n objectKind: \"principal\",\n operation: \"principal.grant.revoke\",\n organizationId: signingContext.organizationId,\n },\n async () => {\n const mutateCurrent = createCurrentPrincipalMutation({\n ...signingContext,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n });\n const current =\n input.subjectType === \"group\" && mutateCurrent\n ? await mutateCurrent(input.subjectId, {\n kind: \"revoke\",\n revokedContainerId: input.containerId,\n })\n : undefined;\n const response =\n current?.response ??\n (input.subjectType === \"group\"\n ? await rotateOrganizationGroupForAccessSetShrink({\n apiClient: input.runtime.apiClient,\n execSql: input.runtime.infra.execSql,\n groupId: input.subjectId,\n revokedContainerId: input.containerId,\n prepareContainerMutations: ({ currentPolicy, nextPolicy }) =>\n preparePrincipalContainerMutations({\n currentPolicy,\n groupId: input.subjectId,\n nextPolicy,\n organizationId: signingContext.organizationId,\n revokedContainerId: input.containerId,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n }),\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n ...signingContext,\n })\n : await revokeOrganizationContainerGrant({\n stillCurrent: input.stillCurrent,\n reportSecurityIncident: input.runtime.util.reportSecurityIncident,\n apiClient: input.runtime.apiClient,\n containerId: input.containerId,\n encapsulationKeyPair,\n execSql: input.runtime.infra.execSql,\n revokedSubject: {\n subjectId: input.subjectId,\n subjectType: input.subjectType,\n },\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n warmReferencedPrincipalPolicies:\n createRuntimePrincipalPolicyWarmer(input.runtime),\n ...signingContext,\n }));\n await input.readModelCoordinator.reconcileAfterMutation(\n signingContext.organizationId,\n );\n return response;\n },\n );\n}\n"]}
1
+ {"version":3,"file":"principalMutations.js","sourceRoot":"","sources":["principalMutations.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oCAAoC,EAAE,MAAM,+DAA+D,CAAC;AACrH,OAAO,EAAE,gCAAgC,EAAE,MAAM,+CAA+C,CAAC;AACjG,OAAO,EAAE,gCAAgC,EAAE,MAAM,+BAA+B,CAAC;AACjF,OAAO,EAAE,8BAA8B,EAAE,MAAM,8DAA8D,CAAC;AAC9G,OAAO,EAAE,mDAAmD,EAAE,MAAM,6DAA6D,CAAC;AAClI,OAAO,EAAE,8BAA8B,EAAE,MAAM,8DAA8D,CAAC;AAC9G,OAAO,EAAE,gCAAgC,EAAE,MAAM,oDAAoD,CAAC;AACtG,OAAO,EAAE,wCAAwC,EAAE,MAAM,kEAAkE,CAAC;AAC5H,OAAO,EAAE,kCAAkC,EAAE,MAAM,gDAAgD,CAAC;AAGpG,OAAO,EAAE,8BAA8B,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC;AAiCpF,SAAS,qBAAqB,CAC5B,OAAqC;IAErC,IACE,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc;QAC5B,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM;QACpB,CAAC,OAAO,CAAC,MAAM,CAAC,kBAAkB;QAClC,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,EAC9B,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IACjE,CAAC;IAED,OAAO;QACL,cAAc,EAAE,OAAO,CAAC,IAAI,CAAC,cAAc;QAC3C,YAAY,EAAE,OAAO,CAAC,IAAI,CAAC,MAAM;QACjC,kBAAkB,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AK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO;oBACpC,cAAc,EAAE;wBACd,SAAS,EAAE,KAAK,CAAC,SAAS;wBAC1B,WAAW,EAAE,KAAK,CAAC,WAAW;qBAC/B;oBACD,0BAA0B,EACxB,KAAK,CAAC,OAAO,CAAC,0BAA0B;oBAC1C,+BAA+B,EAAE,kCAAkC,CACjE,KAAK,CAAC,OAAO,CACd;oBACD,GAAG,cAAc;iBAClB,CAAC,CAAC;YACL,CAAC;QACH,CAAC,CAAC;QACF,MAAM,QAAQ,GAAG,MAAM,MAAM,EAAE,CAAC;QAChC,MAAM,KAAK,CAAC,oBAAoB,CAAC,sBAAsB,CACrD,cAAc,CAAC,cAAc,CAC9B,CAAC;QACF,OAAO,QAAQ,CAAC;IAClB,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import type { ContainerGrantSubjectType } from \"@tearleads/crypto\";\nimport { ProjectionDependencyUnavailableError } from \"../../data/keyingProjectionVerification/dependencyUnavailable\";\nimport { runWithSecurityIncidentReporting } from \"../../data/keyingProjectionVerification/error\";\nimport { revokeOrganizationContainerGrant } from \"../../workflows/organizations\";\nimport { createCurrentOrganizationGroup } from \"../../workflows/organizations/createCurrentOrganizationGroup\";\nimport { createSelectedCurrentGroupMetadataContainerVerifier } from \"../../workflows/organizations/currentGroupMetadataAuthority\";\nimport { deleteCurrentOrganizationGroup } from \"../../workflows/organizations/deleteCurrentOrganizationGroup\";\nimport { createRuntimeGroupMetadataAccess } from \"../../workflows/organizations/groupMetadataRuntime\";\nimport { createRuntimeCurrentOrganizationMutation } from \"../../workflows/organizations/runtimeCurrentOrganizationMutation\";\nimport { createRuntimePrincipalPolicyWarmer } from \"../../workflows/principals/runtimePolicyWarmer\";\nimport type { ContainerContents } from \"../containerContents\";\nimport type { InternalWorkflowRuntimeInput } from \"../workflowRuntime\";\nimport { createCurrentPrincipalMutation } from \"./currentPrincipalMutations\";\nimport { syncOrganizationMetadataProfile } from \"./organizationMetadataProfileSync\";\nimport type { OrganizationReadModelCoordinator } from \"./organizationReadModels\";\n\nexport interface OrganizationGrantRef {\n containerId: string;\n subjectId: string;\n subjectType: ContainerGrantSubjectType;\n}\n\nexport interface OrganizationGroupUserMutationInput {\n groupId: string;\n expectedGroupName: string;\n}\n\nexport interface AddOrganizationGroupUserInput\n extends OrganizationGroupUserMutationInput {\n targetUserId: string;\n}\n\nexport interface RemoveOrganizationGroupUserInput\n extends OrganizationGroupUserMutationInput {\n removedUserId: string;\n}\n\ninterface OrganizationSigningContext {\n organizationId: string;\n signerUserId: string;\n signingFingerprint: string;\n signingKeyPair: NonNullable<\n InternalWorkflowRuntimeInput[\"crypto\"][\"signingKeyPair\"]\n >;\n}\n\nfunction requireSigningContext(\n runtime: InternalWorkflowRuntimeInput,\n): OrganizationSigningContext {\n if (\n !runtime.auth.organizationId ||\n !runtime.auth.userId ||\n !runtime.crypto.signingFingerprint ||\n !runtime.crypto.signingKeyPair\n ) {\n throw new Error(\"Organization signing context is unavailable\");\n }\n\n return {\n organizationId: runtime.auth.organizationId,\n signerUserId: runtime.auth.userId,\n signingFingerprint: runtime.crypto.signingFingerprint,\n signingKeyPair: runtime.crypto.signingKeyPair,\n };\n}\n\nfunction requireEncapsulationKeyPair(\n runtime: InternalWorkflowRuntimeInput,\n): NonNullable<InternalWorkflowRuntimeInput[\"crypto\"][\"encapsulationKeyPair\"]> {\n if (!runtime.crypto.encapsulationKeyPair) {\n throw new Error(\"Organization encryption context is unavailable\");\n }\n\n return runtime.crypto.encapsulationKeyPair;\n}\n\ninterface PrincipalMutationRuntime {\n readonly stillCurrent: () => boolean;\n readonly containerContents: ContainerContents;\n readonly readModelCoordinator: OrganizationReadModelCoordinator;\n readonly runtime: InternalWorkflowRuntimeInput;\n}\n\nexport async function addUserToOrganizationGroup(\n input: PrincipalMutationRuntime & AddOrganizationGroupUserInput,\n) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.groupId,\n objectKind: \"principal\",\n operation: \"group.member.add\",\n organizationId: signingContext.organizationId,\n },\n async () => {\n const mutateCurrent = createCurrentPrincipalMutation({\n ...signingContext,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n });\n if (!mutateCurrent)\n throw new ProjectionDependencyUnavailableError(\n \"Principal history recovery is unavailable\",\n );\n const { memberGroupId, response: bundle } = await mutateCurrent(\n input.groupId,\n {\n kind: \"add\",\n expectedGroupName: input.expectedGroupName,\n targetUserId: input.targetUserId,\n },\n );\n if (input.groupId === memberGroupId) {\n await syncOrganizationMetadataProfile({\n containerContents: input.containerContents,\n log: input.runtime.util.log,\n organizationId: signingContext.organizationId,\n });\n }\n await input.readModelCoordinator.reconcileAfterMutation(\n signingContext.organizationId,\n );\n return bundle;\n },\n );\n}\n\nexport function createGroupForOrganization(input: {\n readonly name: string;\n readonly runtime: InternalWorkflowRuntimeInput;\n readonly stillCurrent: () => boolean;\n}) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: signingContext.organizationId,\n objectKind: \"principal\",\n operation: \"group.create\",\n organizationId: signingContext.organizationId,\n },\n () => {\n const creatorEncapsulationKeyPair = requireEncapsulationKeyPair(\n input.runtime,\n );\n const mutate = createRuntimeCurrentOrganizationMutation(input.runtime);\n if (!mutate)\n throw new ProjectionDependencyUnavailableError(\n \"Principal history recovery is unavailable\",\n );\n return mutate(\n { ...signingContext, stillCurrent: input.stillCurrent },\n (context) =>\n createCurrentOrganizationGroup({\n ...signingContext,\n context,\n apiClient: input.runtime.apiClient,\n creatorEncapsulationKeyPair,\n execSql: input.runtime.infra.execSql,\n name: input.name,\n metadataAccess: createRuntimeGroupMetadataAccess(\n input.runtime,\n signingContext.organizationId,\n context.stillCurrent,\n createSelectedCurrentGroupMetadataContainerVerifier({\n authority: context,\n organizationId: signingContext.organizationId,\n stillCurrent: context.stillCurrent,\n }),\n ),\n reportSecurityIncident: input.runtime.util.reportSecurityIncident,\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n }),\n );\n },\n );\n}\n\nexport function deleteGroupForOrganization(input: {\n readonly groupId: string;\n readonly runtime: InternalWorkflowRuntimeInput;\n readonly stillCurrent: () => boolean;\n}) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.groupId,\n objectKind: \"principal\",\n operation: \"group.delete\",\n organizationId: signingContext.organizationId,\n },\n () => {\n const mutate = createRuntimeCurrentOrganizationMutation(input.runtime);\n if (!mutate)\n throw new ProjectionDependencyUnavailableError(\n \"Principal history recovery is unavailable\",\n );\n return mutate(\n { ...signingContext, stillCurrent: input.stillCurrent },\n (context) =>\n deleteCurrentOrganizationGroup({\n ...signingContext,\n context,\n groupId: input.groupId,\n apiClient: input.runtime.apiClient,\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n }),\n );\n },\n );\n}\n\nexport async function removeUserFromOrganizationGroup(\n input: PrincipalMutationRuntime & RemoveOrganizationGroupUserInput,\n) {\n const signingContext = requireSigningContext(input.runtime);\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.groupId,\n objectKind: \"principal\",\n operation: \"group.member.remove\",\n organizationId: signingContext.organizationId,\n },\n async () => {\n const mutateCurrent = createCurrentPrincipalMutation({\n ...signingContext,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n });\n if (!mutateCurrent)\n throw new ProjectionDependencyUnavailableError(\n \"Principal history recovery is unavailable\",\n );\n const { memberGroupId, response: bundle } = await mutateCurrent(\n input.groupId,\n {\n kind: \"remove\",\n expectedGroupName: input.expectedGroupName,\n removedUserId: input.removedUserId,\n },\n );\n if (input.groupId === memberGroupId) {\n await syncOrganizationMetadataProfile({\n containerContents: input.containerContents,\n log: input.runtime.util.log,\n organizationId: signingContext.organizationId,\n });\n }\n await input.readModelCoordinator.reconcileAfterMutation(\n signingContext.organizationId,\n );\n return bundle;\n },\n );\n}\n\nexport async function revokeOrganizationGrant(\n input: PrincipalMutationRuntime & OrganizationGrantRef,\n) {\n const signingContext = requireSigningContext(input.runtime);\n const encapsulationKeyPair = requireEncapsulationKeyPair(input.runtime);\n if (input.runtime.infra.dbStatus !== \"ready\") {\n throw new Error(\"Organization local database is unavailable\");\n }\n\n return runWithSecurityIncidentReporting(\n input.runtime.util.reportSecurityIncident,\n {\n objectId: input.subjectId,\n objectKind: \"principal\",\n operation: \"principal.grant.revoke\",\n organizationId: signingContext.organizationId,\n },\n async () => {\n const revoke = async () => {\n if (input.subjectType === \"group\") {\n const mutateCurrent = createCurrentPrincipalMutation({\n ...signingContext,\n runtime: input.runtime,\n stillCurrent: input.stillCurrent,\n });\n if (!mutateCurrent)\n throw new ProjectionDependencyUnavailableError(\n \"Principal history recovery is unavailable\",\n );\n return (\n await mutateCurrent(input.subjectId, {\n kind: \"revoke\",\n revokedContainerId: input.containerId,\n })\n ).response;\n } else {\n return revokeOrganizationContainerGrant({\n stillCurrent: input.stillCurrent,\n reportSecurityIncident: input.runtime.util.reportSecurityIncident,\n apiClient: input.runtime.apiClient,\n containerId: input.containerId,\n encapsulationKeyPair,\n execSql: input.runtime.infra.execSql,\n revokedSubject: {\n subjectId: input.subjectId,\n subjectType: input.subjectType,\n },\n resolveTrustedUserIdentity:\n input.runtime.resolveTrustedUserIdentity,\n warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(\n input.runtime,\n ),\n ...signingContext,\n });\n }\n };\n const response = await revoke();\n await input.readModelCoordinator.reconcileAfterMutation(\n signingContext.organizationId,\n );\n return response;\n },\n );\n}\n"]}