@friggframework/core 2.0.0--canary.679.a2bcc2f.0 → 2.0.0--canary.686.87e9875.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 (136) hide show
  1. package/CLAUDE.md +25 -0
  2. package/README.md +105 -0
  3. package/application/commands/integration-commands.js +2 -2
  4. package/application/commands/user-commands.js +17 -5
  5. package/core/Worker.js +31 -7
  6. package/credential/use-cases/credential-operations-for-user.js +110 -25
  7. package/credential/use-cases/reauthorize-credential-for-user.js +15 -8
  8. package/database/config.js +48 -24
  9. package/database/documentdb-utils.js +6 -1
  10. package/database/encryption/encryption-schema-registry.js +80 -12
  11. package/database/organization-where.js +18 -0
  12. package/database/prisma.js +4 -2
  13. package/database/unique-violation.js +25 -0
  14. package/extensions/bootstrap-runner.js +43 -0
  15. package/extensions/extension-loader.js +167 -0
  16. package/extensions/index.js +41 -0
  17. package/extensions/initialize-app.js +123 -0
  18. package/extensions/route-mounter.js +103 -0
  19. package/extensions/schema-composer.js +188 -0
  20. package/generated/prisma-mongodb/edge.js +27 -4
  21. package/generated/prisma-mongodb/index-browser.js +24 -1
  22. package/generated/prisma-mongodb/index.d.ts +3227 -47
  23. package/generated/prisma-mongodb/index.js +27 -4
  24. package/generated/prisma-mongodb/package.json +1 -1
  25. package/generated/prisma-mongodb/schema.prisma +65 -0
  26. package/generated/prisma-mongodb/wasm.js +27 -4
  27. package/generated/prisma-postgresql/edge.js +27 -4
  28. package/generated/prisma-postgresql/index-browser.js +24 -1
  29. package/generated/prisma-postgresql/index.d.ts +3519 -67
  30. package/generated/prisma-postgresql/index.js +27 -4
  31. package/generated/prisma-postgresql/package.json +1 -1
  32. package/generated/prisma-postgresql/schema.prisma +61 -0
  33. package/generated/prisma-postgresql/wasm.js +27 -4
  34. package/handlers/app-definition-loader.js +93 -12
  35. package/handlers/routers/auth.js +40 -12
  36. package/handlers/routers/integration-defined-routers.js +144 -92
  37. package/handlers/routers/integration-webhook-routers.js +102 -64
  38. package/index.js +58 -1
  39. package/infrastructure/scheduler/index.js +19 -6
  40. package/infrastructure/scheduler/netlify-scheduler-adapter.js +185 -0
  41. package/infrastructure/scheduler/scheduler-service-factory.js +36 -7
  42. package/integrations/integration-router.js +115 -73
  43. package/integrations/repositories/integration-not-found.js +14 -0
  44. package/integrations/repositories/integration-repository-documentdb.js +35 -3
  45. package/integrations/repositories/integration-repository-interface.js +17 -1
  46. package/integrations/repositories/integration-repository-mongo.js +59 -76
  47. package/integrations/repositories/integration-repository-postgres.js +62 -76
  48. package/integrations/tests/doubles/test-integration-repository.js +12 -1
  49. package/integrations/use-cases/create-integration.js +55 -20
  50. package/integrations/use-cases/get-integrations-for-user.js +80 -40
  51. package/integrations/use-cases/index.js +5 -1
  52. package/integrations/use-cases/integration-operations-for-user.js +55 -24
  53. package/integrations/use-cases/resolve-integration-ownership.js +177 -0
  54. package/management-api/management-api-config.js +71 -2
  55. package/management-api/openapi/components.js +40 -0
  56. package/management-api/openapi/v2-operations.js +16 -4
  57. package/management-api/v2/dependencies.js +40 -15
  58. package/management-api/v2/dto.js +20 -0
  59. package/management-api/v2/handlers/authorization.js +1 -0
  60. package/management-api/v2/handlers/credentials.js +6 -2
  61. package/management-api/v2/handlers/entities.js +10 -7
  62. package/management-api/v2/handlers/integrations.js +6 -3
  63. package/management-api/v2/request.js +32 -1
  64. package/modules/entity-scope.js +15 -0
  65. package/modules/index.js +8 -0
  66. package/modules/module-factory.js +12 -0
  67. package/modules/module.js +35 -2
  68. package/modules/repositories/entity-not-found.js +18 -0
  69. package/modules/repositories/module-repository-documentdb.js +51 -6
  70. package/modules/repositories/module-repository-interface.js +19 -2
  71. package/modules/repositories/module-repository-mongo.js +67 -74
  72. package/modules/repositories/module-repository-postgres.js +66 -74
  73. package/modules/repositories/module-repository.js +2 -1
  74. package/modules/scope-config.js +192 -0
  75. package/modules/scope-policy.js +112 -0
  76. package/modules/scope-resolution.js +288 -0
  77. package/modules/scope-visibility.js +247 -0
  78. package/modules/tests/doubles/ownership-matching.js +18 -0
  79. package/modules/use-cases/authorize-entity-for-user.js +61 -0
  80. package/modules/use-cases/create-entity-from-credential.js +113 -0
  81. package/modules/use-cases/entity-operations-for-user.js +50 -13
  82. package/modules/use-cases/entity-types.js +31 -7
  83. package/modules/use-cases/execute-entity-proxy-request.js +50 -3
  84. package/modules/use-cases/get-entities-for-user.js +16 -5
  85. package/modules/use-cases/get-entity-module-for-user.js +32 -5
  86. package/modules/use-cases/get-entity-options-by-id.js +9 -3
  87. package/modules/use-cases/get-module.js +9 -3
  88. package/modules/use-cases/manage-access.js +21 -0
  89. package/modules/use-cases/multi-step-authorization.js +39 -12
  90. package/modules/use-cases/process-authorization-callback.js +52 -18
  91. package/modules/use-cases/refresh-entity-options.js +9 -3
  92. package/modules/use-cases/test-module-auth.js +9 -3
  93. package/package.json +6 -5
  94. package/prisma-mongodb/schema.prisma +65 -0
  95. package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +26 -0
  96. package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +25 -0
  97. package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +28 -0
  98. package/prisma-postgresql/schema.prisma +61 -0
  99. package/providers/index.js +13 -0
  100. package/providers/resolve-provider.js +93 -0
  101. package/queues/index.js +25 -0
  102. package/queues/providers/index.js +11 -0
  103. package/queues/providers/netlify-background-provider.js +76 -0
  104. package/queues/providers/qstash-queue-provider.js +122 -0
  105. package/queues/providers/sqs-queue-provider.js +61 -0
  106. package/queues/queue-client-interface.js +55 -0
  107. package/queues/queue-provider-factory.js +101 -0
  108. package/queues/queue-provider.js +46 -0
  109. package/queues/sqs-queue-client.js +81 -0
  110. package/types/index.d.ts +1 -0
  111. package/types/modules/index.d.ts +129 -0
  112. package/types/user/index.d.ts +134 -0
  113. package/user/adopter-jwt-config.js +177 -0
  114. package/user/adopter-jwt-errors.js +59 -0
  115. package/user/adopter-token-verifier.js +122 -0
  116. package/user/app-user-helpers.js +97 -0
  117. package/user/create-user-authentication.js +64 -0
  118. package/user/membership.js +51 -0
  119. package/user/repositories/organization-membership-record.js +19 -0
  120. package/user/repositories/organization-membership-repository-documentdb.js +106 -0
  121. package/user/repositories/organization-membership-repository-factory.js +42 -0
  122. package/user/repositories/organization-membership-repository-interface.js +57 -0
  123. package/user/repositories/organization-membership-repository-mongo.js +13 -0
  124. package/user/repositories/organization-membership-repository-postgres.js +29 -0
  125. package/user/repositories/organization-membership-repository-prisma.js +81 -0
  126. package/user/repositories/user-repository-documentdb.js +27 -2
  127. package/user/repositories/user-repository-interface.js +18 -0
  128. package/user/repositories/user-repository-mongo.js +24 -0
  129. package/user/repositories/user-repository-postgres.js +18 -0
  130. package/user/tests/doubles/adopter-jwt-fixtures.js +220 -0
  131. package/user/use-cases/authenticate-user.js +7 -1
  132. package/user/use-cases/ensure-organization-membership.js +92 -0
  133. package/user/use-cases/find-individual-users-by-organization.js +54 -0
  134. package/user/use-cases/get-user-from-adopter-jwt.js +95 -124
  135. package/user/use-cases/get-user-from-x-frigg-headers.js +83 -80
  136. package/user/user.js +64 -1
@@ -27,13 +27,19 @@ class CreateIntegration {
27
27
  * and entity set, instead of creating a duplicate.
28
28
  * @async
29
29
  * @param {string[]} entities - Array of entity IDs to associate with the integration.
30
- * @param {string} userId - ID of the user creating the integration.
30
+ * @param {string} userId - ID of the OWNER of the integration (ADR-056 §3):
31
+ * the person or the organization's user, as ResolveIntegrationOwnership decided.
31
32
  * @param {Object} config - Configuration object for the integration.
32
33
  * @param {string} config.type - Type of integration to create.
34
+ * @param {{organizationId?: (string|null), adoptUnscoped?: boolean}} [tenancy]
35
+ * the organization it is created in (§3.6). A duplicate is only reused in
36
+ * the same organization, so one person's integration in organization A is
37
+ * not handed back as the one they asked for in B. `adoptUnscoped` also
38
+ * accepts a duplicate written before scoping (no organization).
33
39
  * @returns {Promise<Object>} The created or reused integration DTO.
34
40
  * @throws {Error} When integration class is not found for the specified type.
35
41
  */
36
- async execute(entities, userId, config) {
42
+ async execute(entities, userId, config, tenancy = {}) {
37
43
  console.log(
38
44
  `[Frigg] Creating ${config?.type} integration for user ${userId} with entities [${entities}]`
39
45
  );
@@ -41,7 +47,8 @@ class CreateIntegration {
41
47
  const existing = await this._findDuplicate(
42
48
  userId,
43
49
  config?.type,
44
- entities
50
+ entities,
51
+ tenancy
45
52
  );
46
53
  if (existing) {
47
54
  console.log(
@@ -50,11 +57,18 @@ class CreateIntegration {
50
57
  return this._reuseExisting(existing, config);
51
58
  }
52
59
 
60
+ // Everything that can be refused is refused before a row is written: an
61
+ // unknown type, or an entity the owner cannot load, would otherwise
62
+ // leave an IN_CREATION integration behind that nothing moves on.
63
+ const integrationClass = this._requireClass(config?.type);
64
+ const modules = await this._loadModules(entities, userId);
65
+
53
66
  const integrationRecord =
54
67
  await this.integrationRepository.createIntegration(
55
68
  entities,
56
69
  userId,
57
- config
70
+ config,
71
+ { organizationId: tenancy.organizationId ?? null }
58
72
  );
59
73
  console.log(
60
74
  `[Frigg] Created integration ${integrationRecord.id} for user ${userId}`
@@ -65,7 +79,8 @@ class CreateIntegration {
65
79
  const survivor = await this._findDuplicate(
66
80
  userId,
67
81
  config?.type,
68
- entities
82
+ entities,
83
+ tenancy
69
84
  );
70
85
  if (survivor && String(survivor.id) !== String(integrationRecord.id)) {
71
86
  console.log(
@@ -78,7 +93,8 @@ class CreateIntegration {
78
93
  }
79
94
 
80
95
  const integrationInstance = await this._buildInstance(
81
- integrationRecord
96
+ integrationRecord,
97
+ { integrationClass, modules }
82
98
  );
83
99
  console.log(
84
100
  `[Frigg] Sending ON_CREATE for integration ${integrationRecord.id}`
@@ -135,7 +151,7 @@ class CreateIntegration {
135
151
  return mapIntegrationClassToIntegrationDTO(integrationInstance);
136
152
  }
137
153
 
138
- async _findDuplicate(userId, type, entityIds) {
154
+ async _findDuplicate(userId, type, entityIds, tenancy = {}) {
139
155
  const candidates =
140
156
  await this.integrationRepository.findIntegrationsByUserId(userId);
141
157
  const target = [...(entityIds ?? [])].map(String).sort();
@@ -150,6 +166,7 @@ class CreateIntegration {
150
166
  // matching how teardown failures are already handled here.
151
167
  if (integration.status === 'IN_DELETION') return false;
152
168
  if (integration.config?.type !== type) return false;
169
+ if (!this._sameOrganization(integration, tenancy)) return false;
153
170
  const current = [...(integration.entitiesIds ?? [])]
154
171
  .map(String)
155
172
  .sort();
@@ -162,6 +179,15 @@ class CreateIntegration {
162
179
  return matches.length ? this._pickOldest(matches) : null;
163
180
  }
164
181
 
182
+ /** Same organization, or a row from before scoping that belongs here. */
183
+ _sameOrganization(integration, tenancy) {
184
+ const wanted = tenancy.organizationId ?? null;
185
+ const has = integration.organizationId ?? null;
186
+ if (wanted === null && has === null) return true;
187
+ if (wanted !== null && has !== null) return String(wanted) === String(has);
188
+ return has === null && tenancy.adoptUnscoped === true;
189
+ }
190
+
165
191
  _pickOldest(records) {
166
192
  return [...records].sort((a, b) => {
167
193
  const diff =
@@ -173,27 +199,36 @@ class CreateIntegration {
173
199
  })[0];
174
200
  }
175
201
 
176
- async _buildInstance(integrationRecord) {
202
+ _requireClass(type) {
177
203
  const integrationClass = this.integrationClasses.find(
178
- (integrationClass) =>
179
- integrationClass.Definition.name ===
180
- integrationRecord.config.type
204
+ (integrationClass) => integrationClass.Definition.name === type
181
205
  );
182
-
183
206
  if (!integrationClass) {
184
- throw new Error(
185
- `No integration class found for type: ${integrationRecord.config.type}`
186
- );
207
+ throw new Error(`No integration class found for type: ${type}`);
187
208
  }
209
+ return integrationClass;
210
+ }
188
211
 
212
+ async _loadModules(entityIds, userId) {
189
213
  const modules = [];
190
- for (const entityId of integrationRecord.entitiesIds) {
191
- const moduleInstance = await this.moduleFactory.getModuleInstance(
192
- entityId,
193
- integrationRecord.userId
214
+ for (const entityId of entityIds) {
215
+ modules.push(
216
+ await this.moduleFactory.getModuleInstance(entityId, userId)
194
217
  );
195
- modules.push(moduleInstance);
196
218
  }
219
+ return modules;
220
+ }
221
+
222
+ async _buildInstance(integrationRecord, preloaded) {
223
+ const integrationClass =
224
+ preloaded?.integrationClass ??
225
+ this._requireClass(integrationRecord.config.type);
226
+ const modules =
227
+ preloaded?.modules ??
228
+ (await this._loadModules(
229
+ integrationRecord.entitiesIds,
230
+ integrationRecord.userId
231
+ ));
197
232
 
198
233
  const integrationInstance = new integrationClass({
199
234
  id: integrationRecord.id,
@@ -2,9 +2,17 @@
2
2
  const {
3
3
  mapIntegrationClassToIntegrationDTO,
4
4
  } = require('../utils/map-integration-dto');
5
+ const {
6
+ describeRecord,
7
+ visibilityClauses,
8
+ matchesListFilters,
9
+ } = require('../../modules/scope-visibility');
10
+ const { DEFAULT_SCOPE_POLICY } = require('../../modules/scope-policy');
5
11
 
6
12
  /**
7
- * Use case for retrieving all integrations for a specific user.
13
+ * Use case for retrieving the integrations a caller may see: the ones they own
14
+ * or their active organization owns, created in that organization (ADR-056
15
+ * §3.6), each with `tenancy` ({scope, owner, organizationId, access}).
8
16
  * @class GetIntegrationsForUser
9
17
  */
10
18
  class GetIntegrationsForUser {
@@ -15,13 +23,16 @@ class GetIntegrationsForUser {
15
23
  * @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes.
16
24
  * @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory - Service for module instantiation and management.
17
25
  * @param {import('../../modules/repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository - Repository for module and entity data operations.
26
+ * @param {Object} [params.scopePolicy] - from createScopePolicy
18
27
  */
19
28
  constructor({
20
29
  integrationRepository,
21
30
  integrationClasses,
22
31
  moduleFactory,
23
32
  moduleRepository,
33
+ scopePolicy,
24
34
  }) {
35
+ this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
25
36
  /**
26
37
  * @type {import('../repositories/integration-repository-interface').IntegrationRepositoryInterface}
27
38
  */
@@ -32,57 +43,86 @@ class GetIntegrationsForUser {
32
43
  }
33
44
 
34
45
  /**
35
- * Executes the retrieval of all integrations for a user.
36
46
  * @async
37
- * @param {string} userId - ID of the user whose integrations to retrieve.
38
- * @returns {Promise<Object[]>} Array of integration DTOs for the specified user.
47
+ * @param {Object} user - authenticated User
48
+ * @param {{scope?: string, owner?: string}} [filters] - `?scope=` and `?owner=`
49
+ * @returns {Promise<Object[]>} Array of integration DTOs.
39
50
  */
40
- async execute(userId) {
41
- const integrationRecords =
42
- await this.integrationRepository.findIntegrationsByUserId(userId);
51
+ async execute(user, filters = {}) {
52
+ const ctx = this.scopePolicy.accessContext(user);
53
+ const records = await this.integrationRepository.findIntegrationsByOwnership(
54
+ visibilityClauses(ctx, 'integration')
55
+ );
43
56
 
44
57
  const integrations = [];
58
+ for (const record of records) {
59
+ // The query is the filter; the same rule again means a repository
60
+ // that returns too much cannot widen the list.
61
+ const tenancy = describeRecord(record, ctx, 'integration');
62
+ if (!tenancy || !matchesListFilters(tenancy, filters)) continue;
63
+ integrations.push({ ...(await this.toDto(record)), tenancy });
64
+ }
65
+ return integrations;
66
+ }
45
67
 
46
- for (const integrationRecord of integrationRecords) {
47
- const entities = await this.moduleRepository.findEntitiesByIds(
48
- integrationRecord.entitiesIds
49
- );
68
+ /** Hydrates one stored integration record into its DTO. */
69
+ async toDto(integrationRecord) {
70
+ const entities = await this.moduleRepository.findEntitiesByIds(
71
+ integrationRecord.entitiesIds
72
+ );
73
+
74
+ const integrationClass = this.integrationClasses.find(
75
+ (integrationClass) =>
76
+ integrationClass.Definition.name ===
77
+ integrationRecord.config.type
78
+ );
50
79
 
51
- const integrationClass = this.integrationClasses.find(
52
- (integrationClass) =>
53
- integrationClass.Definition.name ===
54
- integrationRecord.config.type
80
+ const modules = [];
81
+ for (const entity of entities) {
82
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
83
+ entity.id,
84
+ integrationRecord.userId
55
85
  );
86
+ modules.push(moduleInstance);
87
+ }
56
88
 
57
- const modules = [];
58
- for (const entity of entities) {
59
- const moduleInstance =
60
- await this.moduleFactory.getModuleInstance(
61
- entity.id,
62
- integrationRecord.userId
63
- );
64
- modules.push(moduleInstance);
65
- }
89
+ return mapIntegrationClassToIntegrationDTO({
90
+ id: integrationRecord.id,
91
+ userId: integrationRecord.userId,
92
+ entities: entities,
93
+ config: integrationRecord.config,
94
+ status: integrationRecord.status,
95
+ version: integrationRecord.version,
96
+ messages: integrationRecord.messages || { errors: [], warnings: [] },
97
+ modules,
98
+ options: integrationClass.getOptionDetails(),
99
+ });
100
+ }
101
+ }
66
102
 
67
- const integrationData = {
68
- id: integrationRecord.id,
69
- userId: integrationRecord.userId,
70
- entities: entities,
71
- config: integrationRecord.config,
72
- status: integrationRecord.status,
73
- version: integrationRecord.version,
74
- messages: integrationRecord.messages || { errors: [], warnings: [] },
75
- modules,
76
- options: integrationClass.getOptionDetails(),
77
- };
103
+ /**
104
+ * Every integration one user id owns, with no organization rules: the lookup
105
+ * behind the app-level `friggCommands.findIntegrationsByUserId`, which has a
106
+ * user id and no request.
107
+ */
108
+ class GetIntegrationsByUserId {
109
+ /** @param {ConstructorParameters<typeof GetIntegrationsForUser>[0]} params */
110
+ constructor(params) {
111
+ this.integrationRepository = params.integrationRepository;
112
+ this.hydrator = new GetIntegrationsForUser(params);
113
+ }
78
114
 
79
- integrations.push(
80
- mapIntegrationClassToIntegrationDTO(integrationData)
81
- );
115
+ /** @param {string} userId */
116
+ async execute(userId) {
117
+ const records = await this.integrationRepository.findIntegrationsByUserId(
118
+ userId
119
+ );
120
+ const integrations = [];
121
+ for (const record of records) {
122
+ integrations.push(await this.hydrator.toDto(record));
82
123
  }
83
-
84
124
  return integrations;
85
125
  }
86
126
  }
87
127
 
88
- module.exports = { GetIntegrationsForUser };
128
+ module.exports = { GetIntegrationsForUser, GetIntegrationsByUserId };
@@ -1,4 +1,7 @@
1
- const { GetIntegrationsForUser } = require('./get-integrations-for-user');
1
+ const {
2
+ GetIntegrationsForUser,
3
+ GetIntegrationsByUserId,
4
+ } = require('./get-integrations-for-user');
2
5
  const { DeleteIntegrationForUser } = require('./delete-integration-for-user');
3
6
  const { CreateIntegration } = require('./create-integration');
4
7
  const { GetIntegration } = require('./get-integration');
@@ -9,6 +12,7 @@ const { GetProcess } = require('./get-process');
9
12
 
10
13
  module.exports = {
11
14
  GetIntegrationsForUser,
15
+ GetIntegrationsByUserId,
12
16
  DeleteIntegrationForUser,
13
17
  CreateIntegration,
14
18
  GetIntegration,
@@ -1,11 +1,15 @@
1
1
  const Boom = require('@hapi/boom');
2
- const { userOwns } = require('../../modules/use-cases/get-entity-module-for-user');
2
+ const { describeRecord } = require('../../modules/scope-visibility');
3
+ const { DEFAULT_SCOPE_POLICY } = require('../../modules/scope-policy');
4
+ const { assertCanManage } = require('../../modules/use-cases/manage-access');
5
+ const { isIntegrationNotFound } = require('../repositories/integration-not-found');
3
6
 
4
7
  /**
5
- * Management API v2 integration operations. They put an ownership check
6
- * (missing or foreign integration = 404) in front of the existing
7
- * integration use cases, and pass the record's owner id on, so the
8
- * organisation user model works the same as for entities.
8
+ * Management API v2 integration operations. They put an access check
9
+ * (missing or unavailable integration = 404) in front of the existing
10
+ * integration use cases, and pass the record's owner id on. Access follows
11
+ * the owner and the organization the integration was created in (ADR-056
12
+ * §3.6), not the owner alone.
9
13
  */
10
14
 
11
15
  const notFound = (integrationId) =>
@@ -13,40 +17,61 @@ const notFound = (integrationId) =>
13
17
  code: 'INTEGRATION_NOT_FOUND',
14
18
  });
15
19
 
16
- /** The integration record, or 404 when it is missing or not the caller's. */
20
+ /**
21
+ * The integration record, or 404 when it is missing or the caller may not
22
+ * use it: not theirs or their active organization's, or created in another
23
+ * organization.
24
+ */
17
25
  class GetOwnedIntegrationRecord {
18
- constructor({ integrationRepository }) {
26
+ constructor({ integrationRepository, scopePolicy }) {
19
27
  this.integrationRepository = integrationRepository;
28
+ this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
20
29
  }
21
30
 
22
- async execute(integrationId, user) {
31
+ /** `{ scope, owner, organizationId, access }` of a record to this caller, or null. */
32
+ describe(record, user) {
33
+ return describeRecord(record, this.scopePolicy.accessContext(user), 'integration');
34
+ }
35
+
36
+ /**
37
+ * @param {string} integrationId
38
+ * @param {Object} user - authenticated User
39
+ * @param {{manage?: boolean}} [options] - `manage: true` for changing or
40
+ * deleting: a person may use an organization's integration but only an
41
+ * admin may change it (403 ORG_ADMIN_REQUIRED)
42
+ */
43
+ async execute(integrationId, user, { manage = false } = {}) {
23
44
  let record = null;
24
45
  try {
25
46
  record = await this.integrationRepository.findIntegrationById(
26
47
  integrationId
27
48
  );
28
49
  } catch (error) {
29
- if (/Invalid ID|ObjectId|Malformed/i.test(error?.message || '')) {
50
+ if (
51
+ /Invalid ID|ObjectId|Malformed/i.test(error?.message || '') ||
52
+ isIntegrationNotFound(error)
53
+ ) {
30
54
  throw notFound(integrationId);
31
55
  }
32
56
  throw error;
33
57
  }
34
- if (!record || !userOwns(user, record.userId)) {
35
- throw notFound(integrationId);
36
- }
58
+ const tenancy = record && this.describe(record, user);
59
+ if (!tenancy) throw notFound(integrationId);
60
+ if (manage) assertCanManage(tenancy.access, `integration ${integrationId}`);
37
61
  return record;
38
62
  }
39
63
  }
40
64
 
41
65
  /**
42
- * Creates (or reuses) an integration between entities the caller owns.
43
- * Unlike v1, the entities' ownership and the integration type are checked
44
- * before anything is written.
66
+ * Creates (or reuses) an integration between entities the caller may use.
67
+ * The entities' access, the binding scopes and the integration type are
68
+ * checked before anything is written, and the integration is owned and
69
+ * paired with an organization by `resolveIntegrationOwnership` (§3, §3.6).
45
70
  */
46
71
  class CreateIntegrationForUser {
47
- constructor({ createIntegration, getEntityModuleForUser, integrationClasses }) {
72
+ constructor({ createIntegration, resolveIntegrationOwnership, integrationClasses }) {
48
73
  this.createIntegration = createIntegration;
49
- this.getEntityModuleForUser = getEntityModuleForUser;
74
+ this.resolveIntegrationOwnership = resolveIntegrationOwnership;
50
75
  this.integrationClasses = integrationClasses || [];
51
76
  }
52
77
 
@@ -74,13 +99,17 @@ class CreateIntegrationForUser {
74
99
  details: { field: 'entities' },
75
100
  });
76
101
  }
77
- for (const entityId of entities) {
78
- await this.getEntityModuleForUser.loadOwnedEntity(String(entityId), user);
79
- }
102
+ const { ownerId, organizationId, adoptUnscoped } =
103
+ await this.resolveIntegrationOwnership.execute({
104
+ entityIds: entities,
105
+ config,
106
+ user,
107
+ });
80
108
  return this.createIntegration.execute(
81
109
  entities.map(String),
82
- user.getId(),
83
- config
110
+ ownerId,
111
+ config,
112
+ { organizationId, adoptUnscoped }
84
113
  );
85
114
  }
86
115
  }
@@ -117,7 +146,8 @@ class UpdateIntegrationForUser {
117
146
  }
118
147
  const record = await this.getOwnedIntegrationRecord.execute(
119
148
  integrationId,
120
- user
149
+ user,
150
+ { manage: true }
121
151
  );
122
152
  return this.updateIntegration.execute(record.id, record.userId, config);
123
153
  }
@@ -133,7 +163,8 @@ class RemoveIntegrationForUser {
133
163
  async execute(integrationId, user) {
134
164
  const record = await this.getOwnedIntegrationRecord.execute(
135
165
  integrationId,
136
- user
166
+ user,
167
+ { manage: true }
137
168
  );
138
169
  await this.deleteIntegrationForUser.execute(record.id, record.userId);
139
170
  }
@@ -0,0 +1,177 @@
1
+ const Boom = require('@hapi/boom');
2
+ const { SCOPES, resolveScope } = require('../../modules/scope-resolution');
3
+ const {
4
+ integrationOwnerKind,
5
+ resolveIntegrationOwner,
6
+ } = require('../../modules/scope-resolution');
7
+ const {
8
+ describeRecord,
9
+ unscopedRowsBelongHere,
10
+ } = require('../../modules/scope-visibility');
11
+ const {
12
+ bindingScopeSetting,
13
+ readIntegrationOwnership,
14
+ } = require('../../modules/scope-config');
15
+ const { DEFAULT_SCOPE_POLICY } = require('../../modules/scope-policy');
16
+ const { entityTypeNameOf } = require('../../modules/entity-type-registry');
17
+
18
+ const notFound = (entityId) =>
19
+ Boom.notFound(`Entity ${entityId} not found`, { code: 'ENTITY_NOT_FOUND' });
20
+
21
+ /**
22
+ * Who owns an integration about to be created, and in which organization
23
+ * (ADR-056 §3, §3.3, §3.6). Shared by the v1 and v2 create paths. Nothing is
24
+ * persisted before this has answered, so a refusal leaves no row behind.
25
+ *
26
+ * Every entity must be one the caller may use in the active organization. A
27
+ * missing one, another organization's and a `global` one are the same 404, so
28
+ * ids cannot be probed. When the integration declares a scope (a binding's
29
+ * `scope` or `Definition.entities[key].scope`, or `Definition.ownership.scope`)
30
+ * the entity's scope must be in it. Levels below the integration (entity type,
31
+ * app, default) decide where a connection is CREATED and do not restrict what
32
+ * an integration may bind, so an app that adds a registry scope later does not
33
+ * break integrations that already use older connections.
34
+ *
35
+ * The owner follows the entities (all organization: the organization;
36
+ * otherwise the person) unless `Definition.ownership.owner` says. Every entity
37
+ * must then be of the owner's own scope: an integration is loaded by its owner,
38
+ * who must own what it uses, so an integration that mixes an organization
39
+ * entity with a person's own, or that names an owner that does not match, is
40
+ * refused with a 400 here, before anything is written.
41
+ */
42
+ class ResolveIntegrationOwnership {
43
+ /**
44
+ * @param {Object} params
45
+ * @param {import('../../modules/repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
46
+ * @param {Array<Function>} params.integrationClasses
47
+ * @param {Object} [params.scopePolicy] - from createScopePolicy
48
+ */
49
+ constructor({ moduleRepository, integrationClasses, scopePolicy }) {
50
+ this.moduleRepository = moduleRepository;
51
+ this.integrationClasses = integrationClasses || [];
52
+ this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
53
+ }
54
+
55
+ /**
56
+ * @param {Object} params
57
+ * @param {Array<string|number>} params.entityIds
58
+ * @param {Object} params.config - `{ type }`
59
+ * @param {Object} params.user - authenticated User
60
+ * @returns {Promise<{ownerId: string, organizationId: (string|null), ownerKind: string, adoptUnscoped: boolean}>}
61
+ */
62
+ async execute({ entityIds, config, user }) {
63
+ const ctx = this.scopePolicy.accessContext(user);
64
+ const integrationClass = this.integrationClasses.find(
65
+ (IntegrationClass) => IntegrationClass.Definition?.name === config?.type
66
+ );
67
+ const ownership = readIntegrationOwnership(integrationClass);
68
+
69
+ const entities = await this.loadEntities(entityIds);
70
+ const scoped = entities.map((entity) => ({
71
+ entity,
72
+ scope: this.acceptedScope({ entity, ctx, integrationClass, ownership }),
73
+ }));
74
+
75
+ const ownerKind = integrationOwnerKind(
76
+ scoped.map(({ scope }) => scope),
77
+ ownership.owner
78
+ );
79
+ this.assertOneScope(scoped, ownerKind, config?.type);
80
+ return {
81
+ ...resolveIntegrationOwner(ownerKind, ctx),
82
+ ownerKind,
83
+ // Whether an existing integration with no organization (written
84
+ // before scoping) is this caller's to reuse. An organization-owned
85
+ // one is unambiguous: its owner is the organization, whoever asks
86
+ // and whatever their own legacy pointer says. A person's is theirs
87
+ // only where scope-visibility says (their legacy organization).
88
+ adoptUnscoped:
89
+ ownerKind === SCOPES.ORGANIZATION ||
90
+ unscopedRowsBelongHere(ctx, 'integration'),
91
+ };
92
+ }
93
+
94
+ /** Every entity must be of the integration owner's own scope (see the class comment). */
95
+ assertOneScope(scoped, ownerKind, type) {
96
+ for (const { entity, scope } of scoped) {
97
+ if (scope === ownerKind) continue;
98
+ // The load-time check (resolveOwnershipConfig) only sees declared
99
+ // scopes; this one sees the entities actually named.
100
+ if (ownerKind === SCOPES.ORGANIZATION) {
101
+ throw Boom.badRequest(
102
+ "An organization-owned integration cannot use an 'individual' entity: a shared integration must not run on one person's credential",
103
+ {
104
+ code: 'ORG_INTEGRATION_INDIVIDUAL_ENTITY',
105
+ details: { entityId: String(entity.id) },
106
+ }
107
+ );
108
+ }
109
+ throw Boom.badRequest(
110
+ `Integration '${type}' would be owned by the person but uses the organization entity ${entity.id}. An integration must use entities of one scope: all of the person's, or all of the organization's.`,
111
+ {
112
+ code: 'MIXED_ENTITY_SCOPES',
113
+ details: { entityId: String(entity.id), scope },
114
+ }
115
+ );
116
+ }
117
+ }
118
+
119
+ async loadEntities(entityIds) {
120
+ const ids = (entityIds || []).map(String);
121
+ if (ids.length === 0) return [];
122
+ let found;
123
+ try {
124
+ found = await this.moduleRepository.findEntitiesByIds(ids);
125
+ } catch (error) {
126
+ // A malformed id (e.g. not an integer on PostgreSQL) is "not found".
127
+ if (/Invalid ID|ObjectId|Malformed/i.test(error?.message || '')) {
128
+ throw notFound(ids[0]);
129
+ }
130
+ throw error;
131
+ }
132
+ const byId = new Map((found || []).filter(Boolean).map((e) => [String(e.id), e]));
133
+ return ids.map((id) => {
134
+ if (!byId.has(id)) throw notFound(id);
135
+ return byId.get(id);
136
+ });
137
+ }
138
+
139
+ /** The entity's scope for this caller, or a refusal. */
140
+ acceptedScope({ entity, ctx, integrationClass, ownership }) {
141
+ const described = describeRecord(entity, ctx, 'entity');
142
+ if (!described) throw notFound(entity.id);
143
+ const { scope } = described;
144
+
145
+ const declared = this.declaredScope({ entity, integrationClass, ownership });
146
+ if (declared && !declared.allow.includes(scope)) {
147
+ throw Boom.badRequest(
148
+ `Entity ${entity.id} is ${scope}-scoped, but this integration binds ${entity.moduleName} at ${declared.allow.join(' or ')} scope`,
149
+ {
150
+ code: 'ENTITY_SCOPE_MISMATCH',
151
+ details: { entityId: String(entity.id), scope, allowed: declared.allow },
152
+ }
153
+ );
154
+ }
155
+ return scope;
156
+ }
157
+
158
+ /**
159
+ * What the integration itself declares for this entity's binding (levels 2
160
+ * and 3 only), or null when it declares nothing.
161
+ */
162
+ declaredScope({ entity, integrationClass, ownership }) {
163
+ const Definition = integrationClass?.Definition;
164
+ const key = Object.keys(Definition?.modules || {}).find(
165
+ (candidate) =>
166
+ entityTypeNameOf(Definition.modules[candidate]?.definition) ===
167
+ entity.moduleName
168
+ );
169
+ const resolved = resolveScope({
170
+ binding: key === undefined ? undefined : bindingScopeSetting(Definition, key),
171
+ integration: ownership.scope,
172
+ });
173
+ return resolved.decidedBy === 'default' ? null : resolved;
174
+ }
175
+ }
176
+
177
+ module.exports = { ResolveIntegrationOwnership };