@friggframework/core 2.0.0--canary.707.df80bfd.0 → 2.0.0--canary.680.d670696.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 (161) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/CLAUDE.md +0 -28
  3. package/README.md +0 -105
  4. package/application/commands/integration-commands.js +2 -2
  5. package/application/commands/scheduler-commands.js +7 -11
  6. package/application/commands/user-commands.js +5 -17
  7. package/core/Worker.js +7 -31
  8. package/core/create-handler.js +0 -9
  9. package/credential/use-cases/credential-operations-for-user.js +25 -110
  10. package/credential/use-cases/reauthorize-credential-for-user.js +8 -15
  11. package/database/config.js +24 -48
  12. package/database/documentdb-encryption-service.js +0 -1
  13. package/database/documentdb-utils.js +1 -6
  14. package/database/encryption/encryption-config.js +44 -13
  15. package/database/encryption/encryption-schema-registry.js +31 -88
  16. package/database/prisma.js +2 -4
  17. package/generated/prisma-mongodb/edge.js +4 -27
  18. package/generated/prisma-mongodb/index-browser.js +1 -24
  19. package/generated/prisma-mongodb/index.d.ts +48 -3228
  20. package/generated/prisma-mongodb/index.js +4 -27
  21. package/generated/prisma-mongodb/package.json +1 -1
  22. package/generated/prisma-mongodb/schema.prisma +0 -65
  23. package/generated/prisma-mongodb/wasm.js +4 -27
  24. package/generated/prisma-postgresql/edge.js +4 -27
  25. package/generated/prisma-postgresql/index-browser.js +1 -24
  26. package/generated/prisma-postgresql/index.d.ts +51 -3503
  27. package/generated/prisma-postgresql/index.js +4 -27
  28. package/generated/prisma-postgresql/package.json +1 -1
  29. package/generated/prisma-postgresql/schema.prisma +0 -61
  30. package/generated/prisma-postgresql/wasm.js +4 -27
  31. package/handlers/app-definition-loader.js +12 -110
  32. package/handlers/routers/auth.js +12 -40
  33. package/handlers/routers/health.js +2 -4
  34. package/handlers/routers/integration-defined-routers.js +92 -144
  35. package/handlers/routers/integration-webhook-routers.js +64 -102
  36. package/index.js +1 -64
  37. package/infrastructure/scheduler/index.js +6 -19
  38. package/infrastructure/scheduler/scheduler-service-factory.js +7 -55
  39. package/integrations/index.js +0 -5
  40. package/integrations/integration-router.js +73 -115
  41. package/integrations/repositories/integration-repository-documentdb.js +3 -35
  42. package/integrations/repositories/integration-repository-interface.js +1 -17
  43. package/integrations/repositories/integration-repository-mongo.js +76 -59
  44. package/integrations/repositories/integration-repository-postgres.js +76 -62
  45. package/integrations/tests/doubles/test-integration-repository.js +1 -12
  46. package/integrations/use-cases/create-integration.js +20 -55
  47. package/integrations/use-cases/get-integrations-for-user.js +40 -80
  48. package/integrations/use-cases/index.js +1 -7
  49. package/integrations/use-cases/integration-operations-for-user.js +24 -55
  50. package/integrations/utils/map-integration-dto.js +1 -44
  51. package/management-api/management-api-config.js +2 -71
  52. package/management-api/meta-router.js +2 -21
  53. package/management-api/openapi/components.js +3 -50
  54. package/management-api/openapi/v2-operations.js +5 -17
  55. package/management-api/route-registry.js +6 -123
  56. package/management-api/v2/dependencies.js +15 -40
  57. package/management-api/v2/dto.js +0 -20
  58. package/management-api/v2/handlers/authorization.js +0 -1
  59. package/management-api/v2/handlers/credentials.js +2 -6
  60. package/management-api/v2/handlers/entities.js +7 -10
  61. package/management-api/v2/handlers/integrations.js +3 -6
  62. package/management-api/v2/request.js +1 -32
  63. package/modules/index.js +0 -8
  64. package/modules/module-factory.js +0 -12
  65. package/modules/module.js +2 -53
  66. package/modules/proxy/proxy-errors.js +1 -12
  67. package/modules/proxy/proxy-path.js +3 -7
  68. package/modules/proxy/proxy-policy.js +5 -34
  69. package/modules/repositories/module-repository-documentdb.js +6 -51
  70. package/modules/repositories/module-repository-interface.js +2 -19
  71. package/modules/repositories/module-repository-mongo.js +74 -67
  72. package/modules/repositories/module-repository-postgres.js +74 -66
  73. package/modules/repositories/module-repository.js +1 -2
  74. package/modules/requester/proxy-request.js +1 -2
  75. package/modules/requester/requester.js +3 -8
  76. package/modules/use-cases/entity-operations-for-user.js +13 -50
  77. package/modules/use-cases/entity-types.js +7 -31
  78. package/modules/use-cases/execute-entity-proxy-request.js +15 -107
  79. package/modules/use-cases/get-authorization-requirements.js +2 -2
  80. package/modules/use-cases/get-entities-for-user.js +13 -26
  81. package/modules/use-cases/get-entity-module-for-user.js +7 -34
  82. package/modules/use-cases/get-entity-options-by-id.js +4 -10
  83. package/modules/use-cases/get-entity-options-by-type.js +1 -1
  84. package/modules/use-cases/get-module-instance-from-type.js +1 -1
  85. package/modules/use-cases/get-module.js +4 -10
  86. package/modules/use-cases/multi-step-authorization.js +13 -40
  87. package/modules/use-cases/process-authorization-callback.js +19 -53
  88. package/modules/use-cases/refresh-entity-options.js +4 -10
  89. package/modules/use-cases/test-module-auth.js +4 -10
  90. package/package.json +5 -6
  91. package/prisma-mongodb/schema.prisma +0 -65
  92. package/prisma-postgresql/schema.prisma +0 -61
  93. package/queues/index.js +0 -25
  94. package/types/index.d.ts +0 -1
  95. package/types/modules/index.d.ts +1 -157
  96. package/user/repositories/user-repository-documentdb.js +2 -27
  97. package/user/repositories/user-repository-interface.js +0 -18
  98. package/user/repositories/user-repository-mongo.js +0 -24
  99. package/user/repositories/user-repository-postgres.js +0 -18
  100. package/user/use-cases/authenticate-user.js +1 -7
  101. package/user/use-cases/get-user-from-adopter-jwt.js +124 -95
  102. package/user/use-cases/get-user-from-x-frigg-headers.js +80 -83
  103. package/user/user.js +1 -64
  104. package/database/organization-where.js +0 -18
  105. package/database/unique-violation.js +0 -25
  106. package/extensions/bootstrap-runner.js +0 -43
  107. package/extensions/extension-loader.js +0 -230
  108. package/extensions/index.js +0 -44
  109. package/extensions/initialize-app.js +0 -123
  110. package/extensions/prisma-composer.js +0 -270
  111. package/extensions/route-mounter.js +0 -103
  112. package/extensions/schema-composer.js +0 -188
  113. package/infrastructure/scheduler/netlify-scheduler-adapter.js +0 -216
  114. package/infrastructure/scheduler/scheduled-job-repository.js +0 -118
  115. package/integrations/repositories/integration-not-found.js +0 -14
  116. package/integrations/schedule-config.js +0 -80
  117. package/integrations/use-cases/dispatch-ongoing-syncs.js +0 -100
  118. package/integrations/use-cases/resolve-integration-ownership.js +0 -177
  119. package/modules/entity-scope.js +0 -15
  120. package/modules/proxy/proxy-bases.js +0 -169
  121. package/modules/repositories/entity-not-found.js +0 -18
  122. package/modules/scope-config.js +0 -192
  123. package/modules/scope-policy.js +0 -112
  124. package/modules/scope-resolution.js +0 -288
  125. package/modules/scope-visibility.js +0 -247
  126. package/modules/tests/doubles/ownership-matching.js +0 -18
  127. package/modules/use-cases/authorize-entity-for-user.js +0 -61
  128. package/modules/use-cases/create-entity-from-credential.js +0 -113
  129. package/modules/use-cases/manage-access.js +0 -21
  130. package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +0 -26
  131. package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +0 -25
  132. package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +0 -28
  133. package/providers/index.js +0 -23
  134. package/providers/resolve-provider.js +0 -93
  135. package/providers/runtime-detection.js +0 -124
  136. package/queues/providers/index.js +0 -11
  137. package/queues/providers/netlify-background-provider.js +0 -150
  138. package/queues/providers/qstash-queue-provider.js +0 -219
  139. package/queues/providers/sqs-queue-provider.js +0 -70
  140. package/queues/queue-client-interface.js +0 -55
  141. package/queues/queue-message-auth.js +0 -193
  142. package/queues/queue-provider-factory.js +0 -101
  143. package/queues/queue-provider.js +0 -65
  144. package/queues/sqs-queue-client.js +0 -81
  145. package/types/user/index.d.ts +0 -134
  146. package/user/adopter-jwt-config.js +0 -177
  147. package/user/adopter-jwt-errors.js +0 -59
  148. package/user/adopter-token-verifier.js +0 -122
  149. package/user/app-user-helpers.js +0 -97
  150. package/user/create-user-authentication.js +0 -64
  151. package/user/membership.js +0 -51
  152. package/user/repositories/organization-membership-record.js +0 -19
  153. package/user/repositories/organization-membership-repository-documentdb.js +0 -106
  154. package/user/repositories/organization-membership-repository-factory.js +0 -42
  155. package/user/repositories/organization-membership-repository-interface.js +0 -57
  156. package/user/repositories/organization-membership-repository-mongo.js +0 -13
  157. package/user/repositories/organization-membership-repository-postgres.js +0 -29
  158. package/user/repositories/organization-membership-repository-prisma.js +0 -81
  159. package/user/tests/doubles/adopter-jwt-fixtures.js +0 -220
  160. package/user/use-cases/ensure-organization-membership.js +0 -92
  161. package/user/use-cases/find-individual-users-by-organization.js +0 -54
@@ -1,11 +1,8 @@
1
1
  const { Module } = require('../module');
2
2
  const { mapModuleClassToModuleDTO } = require('../utils/map-module-dto');
3
- const { describeRecord, visibilityClauses } = require('../scope-visibility');
4
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
5
3
 
6
4
  class GetEntitiesForUser {
7
- constructor({ moduleRepository, moduleDefinitions, scopePolicy }) {
8
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
5
+ constructor({ moduleRepository, moduleDefinitions }) {
9
6
  this.moduleRepository = moduleRepository;
10
7
 
11
8
  this.definitionMap = new Map();
@@ -14,31 +11,21 @@ class GetEntitiesForUser {
14
11
  }
15
12
  }
16
13
 
17
- /**
18
- * The entities a `User` may see in the active organization (ADR-056 §3.6).
19
- * @param {Object} user - authenticated User
20
- */
21
- async execute(user) {
22
- const ctx = this.scopePolicy.accessContext(user);
23
- const found = await this.moduleRepository.findEntitiesByOwnership(
24
- visibilityClauses(ctx, 'entity')
25
- );
26
- const entities = (found || []).filter((entity) =>
27
- describeRecord(entity, ctx, 'entity')
14
+ async execute(userId) {
15
+ const entities = await this.moduleRepository.findEntitiesByUserId(
16
+ userId
28
17
  );
29
18
 
30
- return Promise.all(
31
- entities.map(async (entity) => {
32
- const definition = this.definitionMap.get(entity.moduleName);
19
+ return entities.map((entity) => {
20
+ const definition = this.definitionMap.get(entity.moduleName);
33
21
 
34
- const moduleInstance = await Module.create({
35
- userId: entity.userId,
36
- definition: definition,
37
- entity: entity,
38
- });
39
- return mapModuleClassToModuleDTO(moduleInstance);
40
- })
41
- );
22
+ const moduleInstance = new Module({
23
+ userId,
24
+ definition: definition,
25
+ entity: entity,
26
+ });
27
+ return mapModuleClassToModuleDTO(moduleInstance);
28
+ });
42
29
  }
43
30
  }
44
31
 
@@ -1,14 +1,8 @@
1
1
  const Boom = require('@hapi/boom');
2
- const { describeRecord } = require('../scope-visibility');
3
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
4
- const { isEntityNotFound } = require('../repositories/entity-not-found');
5
2
 
6
3
  /**
7
4
  * True when `user` may act on records owned by `ownerId`. Uses the User
8
5
  * domain object's rule (individual or organisation user model) when present.
9
- * This is the owner-only rule: it ignores the organization a record belongs
10
- * to, so it no longer decides access to an entity or an integration
11
- * (modules/scope-visibility.js does). Credentials still use it.
12
6
  * @param {Object} user - authenticated User (or a plain { getId } double)
13
7
  * @param {string|number} ownerId
14
8
  */
@@ -20,7 +14,7 @@ function userOwns(user, ownerId) {
20
14
 
21
15
  const defaultCreateModule = (params) => {
22
16
  const { Module } = require('../module');
23
- return Module.create(params);
17
+ return new Module(params);
24
18
  };
25
19
 
26
20
  /**
@@ -36,11 +30,8 @@ class GetEntityModuleForUser {
36
30
  * @param {import('../repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
37
31
  * @param {Array<Object>} params.moduleDefinitions
38
32
  * @param {Function} [params.createModule] - builds a Module; injectable for tests.
39
- * @param {Object} [params.scopePolicy] - from createScopePolicy; with none the
40
- * app configures nothing (ADR-056 §3.1).
41
33
  */
42
- constructor({ moduleRepository, moduleDefinitions, createModule, scopePolicy }) {
43
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
34
+ constructor({ moduleRepository, moduleDefinitions, createModule }) {
44
35
  this.moduleRepository = moduleRepository;
45
36
  this.moduleDefinitions = moduleDefinitions || [];
46
37
  this.createModule = createModule || defaultCreateModule;
@@ -62,7 +53,7 @@ class GetEntityModuleForUser {
62
53
  { code: 'ENTITY_TYPE_NOT_FOUND' }
63
54
  );
64
55
  }
65
- const module = await this.createModule({
56
+ const module = this.createModule({
66
57
  userId: entity.userId,
67
58
  entity,
68
59
  definition,
@@ -70,44 +61,26 @@ class GetEntityModuleForUser {
70
61
  return { entity, definition, module };
71
62
  }
72
63
 
73
- /**
74
- * The entity record, or a 404 when it is missing or the caller may not use
75
- * it: not theirs, or paired with another organization (ADR-056 §3.6). A
76
- * `global` entity is never the tenant's to load.
77
- */
64
+ /** The entity record, or a 404 when it is missing or not the caller's. */
78
65
  async loadOwnedEntity(entityId, user) {
79
66
  let entity = null;
80
67
  try {
81
68
  entity = await this.moduleRepository.findEntityById(entityId);
82
69
  } catch (error) {
83
- // Every adapter throws for a missing row, and a malformed id (e.g.
84
- // not an integer on Postgres) is "not found" too. Both must be the
85
- // 404 another tenant's entity gets, or the status says which ids
86
- // exist. Anything else is a real failure.
87
- if (
88
- isEntityNotFound(error) ||
89
- /Invalid ID|ObjectId|Malformed/i.test(error?.message || '')
90
- ) {
70
+ // A malformed id (e.g. not an integer on Postgres) is "not found".
71
+ if (/Invalid ID|ObjectId|Malformed/i.test(error?.message || '')) {
91
72
  entity = null;
92
73
  } else {
93
74
  throw error;
94
75
  }
95
76
  }
96
- if (!entity || !this.describe(entity, user)) {
77
+ if (!entity || !userOwns(user, entity.userId)) {
97
78
  throw Boom.notFound(`Entity ${entityId} not found`, {
98
79
  code: 'ENTITY_NOT_FOUND',
99
80
  });
100
81
  }
101
82
  return entity;
102
83
  }
103
-
104
- /**
105
- * `{ scope, owner, organizationId, access }` of an entity to this caller,
106
- * or null when they may not see it (§3.4).
107
- */
108
- describe(entity, user) {
109
- return describeRecord(entity, this.scopePolicy.accessContext(user), 'entity');
110
- }
111
84
  }
112
85
 
113
86
  module.exports = { GetEntityModuleForUser, userOwns };
@@ -1,5 +1,4 @@
1
1
  const { Module } = require('../module');
2
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
3
2
 
4
3
  class GetEntityOptionsById {
5
4
  /**
@@ -7,11 +6,9 @@ class GetEntityOptionsById {
7
6
  * @param {import('../repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
8
7
  * @param {} params.moduleDefinitions
9
8
  */
10
- constructor({ moduleRepository, moduleDefinitions, scopePolicy }) {
9
+ constructor({ moduleRepository, moduleDefinitions }) {
11
10
  this.moduleRepository = moduleRepository;
12
11
  this.moduleDefinitions = moduleDefinitions;
13
- // Who may load an entity follows its scope (ADR-056 §3.6).
14
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
15
12
  }
16
13
 
17
14
  /**
@@ -38,7 +35,7 @@ class GetEntityOptionsById {
38
35
 
39
36
  // Validate entity ownership
40
37
  const isOwned = typeof userIdOrUser === 'object' && userIdOrUser?.ownsUserId
41
- ? this.scopePolicy.canUseEntity(userIdOrUser, entity)
38
+ ? userIdOrUser.ownsUserId(entity.userId)
42
39
  : entity.userId?.toString() === userId?.toString();
43
40
 
44
41
  if (!isOwned) {
@@ -60,11 +57,8 @@ class GetEntityOptionsById {
60
57
  );
61
58
  }
62
59
 
63
- // Built for the entity's OWNER, not the caller: a caller may use an
64
- // organization's entity, and a token refresh during this call must
65
- // update the owner's credential, not create one under the caller.
66
- const module = await Module.create({
67
- userId: entity.userId ?? userId,
60
+ const module = new Module({
61
+ userId,
68
62
  entity,
69
63
  definition: moduleDefinition,
70
64
  });
@@ -21,7 +21,7 @@ class GetEntityOptionsByType {
21
21
  if (!moduleDefinition) {
22
22
  throw new Error(`Module definition not found for type: ${type}`);
23
23
  }
24
- const moduleInstance = await Module.create({
24
+ const moduleInstance = new Module({
25
25
  userId,
26
26
  definition: moduleDefinition,
27
27
  });
@@ -23,7 +23,7 @@ class GetModuleInstanceFromType {
23
23
  if (!moduleDefinition) {
24
24
  throw new Error(`Module definition not found for type: ${type}`);
25
25
  }
26
- return Module.create({
26
+ return new Module({
27
27
  userId,
28
28
  definition: moduleDefinition,
29
29
  state: options.state,
@@ -1,12 +1,9 @@
1
1
  const { Module } = require('../module');
2
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
3
2
 
4
3
  class GetModule {
5
- constructor({ moduleRepository, moduleDefinitions, scopePolicy }) {
4
+ constructor({ moduleRepository, moduleDefinitions }) {
6
5
  this.moduleRepository = moduleRepository;
7
6
  this.moduleDefinitions = moduleDefinitions;
8
- // Who may load an entity follows its scope (ADR-056 §3.6).
9
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
10
7
  }
11
8
 
12
9
  /**
@@ -35,7 +32,7 @@ class GetModule {
35
32
  // If User object provided, use ownsUserId to check linked users
36
33
  // Otherwise fall back to simple equality check
37
34
  const isOwned = typeof userIdOrUser === 'object' && userIdOrUser?.ownsUserId
38
- ? this.scopePolicy.canUseEntity(userIdOrUser, entity)
35
+ ? userIdOrUser.ownsUserId(entity.userId)
39
36
  : entity.userId?.toString() === userId?.toString();
40
37
 
41
38
  if (!isOwned) {
@@ -56,11 +53,8 @@ class GetModule {
56
53
  );
57
54
  }
58
55
 
59
- // Built for the entity's OWNER, not the caller: a caller may use an
60
- // organization's entity, and a token refresh during this call must
61
- // update the owner's credential, not create one under the caller.
62
- const module = await Module.create({
63
- userId: entity.userId ?? userId,
56
+ const module = new Module({
57
+ userId,
64
58
  entity,
65
59
  definition: moduleDefinition,
66
60
  });
@@ -3,14 +3,12 @@ const { FetchError } = require('../../errors/fetch-error');
3
3
  const { getLogger } = require('../../logs');
4
4
  const { AuthorizationSession } = require('../domain/authorization-session');
5
5
  const { findDefinition, getAuthStepCount } = require('./entity-types');
6
- const { describeRecord } = require('../scope-visibility');
7
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
8
6
 
9
7
  const log = getLogger('frigg.api.authorize');
10
8
 
11
9
  const defaultCreateModule = (params) => {
12
10
  const { Module } = require('../module');
13
- return Module.create(params);
11
+ return new Module(params);
14
12
  };
15
13
 
16
14
  const isMultiStep = (definition) =>
@@ -146,30 +144,21 @@ class SubmitAuthorizationStep {
146
144
  moduleDefinitions,
147
145
  getAuthorizationRequirements,
148
146
  authorizationSessionRepository,
149
- authorizeEntityForUser,
147
+ processAuthorizationCallback,
150
148
  moduleRepository,
151
149
  credentialRepository,
152
150
  createModule,
153
- scopePolicy,
154
151
  }) {
155
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
156
152
  this.moduleDefinitions = moduleDefinitions || [];
157
153
  this.getAuthorizationRequirements = getAuthorizationRequirements;
158
154
  this.sessions = authorizationSessionRepository;
159
- this.authorizeEntityForUser = authorizeEntityForUser;
155
+ this.processAuthorizationCallback = processAuthorizationCallback;
160
156
  this.moduleRepository = moduleRepository;
161
157
  this.credentialRepository = credentialRepository;
162
158
  this.createModule = createModule || defaultCreateModule;
163
159
  }
164
160
 
165
- /**
166
- * @param {Object} params
167
- * @param {string} [params.scope] - the scope the caller asks for, from the
168
- * deciding level's allow list (ADR-056 §3.1). Never `global`.
169
- * @param {string} [params.fixedScope] - re-authorization keeps the scope the
170
- * credential already has, whatever the config says now.
171
- */
172
- async execute({ user, entityType, data, step = 1, sessionId, credentialId = null, scope, fixedScope }) {
161
+ async execute({ user, entityType, data, step = 1, sessionId, credentialId = null }) {
173
162
  if (!data || typeof data !== 'object' || Array.isArray(data)) {
174
163
  throw Boom.badRequest('data must be an object', {
175
164
  code: 'VALIDATION_ERROR',
@@ -177,16 +166,7 @@ class SubmitAuthorizationStep {
177
166
  });
178
167
  }
179
168
  const definition = findDefinition(this.moduleDefinitions, entityType);
180
- // `userId` binds a session to the caller and is what GET /authorize
181
- // computes too. The owner of the entity this creates is decided by
182
- // scope (§3.2), not by user.primary, and is `ownership.ownerId`.
183
169
  const userId = user.getId();
184
- const ownership = this.authorizeEntityForUser.resolveOwnership({
185
- user,
186
- entityType,
187
- scope,
188
- fixedScope,
189
- });
190
170
 
191
171
  if (!isMultiStep(definition)) {
192
172
  if (step !== 1) {
@@ -194,7 +174,7 @@ class SubmitAuthorizationStep {
194
174
  code: 'INVALID_STEP',
195
175
  });
196
176
  }
197
- return this.complete({ ownership, user, entityType, params: data, credentialId });
177
+ return this.complete({ userId, entityType, params: data, credentialId });
198
178
  }
199
179
 
200
180
  const totalSteps = getAuthStepCount(definition);
@@ -225,7 +205,7 @@ class SubmitAuthorizationStep {
225
205
  );
226
206
  }
227
207
 
228
- const module = await this.createModule({ userId: ownership.ownerId, definition });
208
+ const module = this.createModule({ userId, definition });
229
209
  let result;
230
210
  try {
231
211
  result = await definition.processAuthorizationStep(
@@ -244,8 +224,7 @@ class SubmitAuthorizationStep {
244
224
  session.markComplete();
245
225
  await this.sessions.update(session);
246
226
  const outcome = await this.complete({
247
- ownership,
248
- user,
227
+ userId,
249
228
  entityType,
250
229
  params: result.authData || {},
251
230
  credentialId,
@@ -282,15 +261,14 @@ class SubmitAuthorizationStep {
282
261
  };
283
262
  }
284
263
 
285
- async complete({ ownership, user, entityType, params, credentialId }) {
264
+ async complete({ userId, entityType, params, credentialId }) {
286
265
  let callback;
287
266
  try {
288
- ({ result: callback } = await this.authorizeEntityForUser.execute({
289
- user,
267
+ callback = await this.processAuthorizationCallback.execute(
268
+ userId,
290
269
  entityType,
291
- data: params,
292
- ownership,
293
- }));
270
+ params
271
+ );
294
272
  } catch (error) {
295
273
  throw toAuthorizationError(error);
296
274
  }
@@ -298,18 +276,13 @@ class SubmitAuthorizationStep {
298
276
  this.moduleRepository.findEntityById(callback.entity_id),
299
277
  this.credentialRepository.findCredentialById(callback.credential_id),
300
278
  ]);
301
- // What the new entity is to this caller (§3.4), so a client need not list again.
302
- const tenancy = entity
303
- ? describeRecord(entity, this.scopePolicy.accessContext(user), 'entity')
304
- : null;
305
279
  const outcome = {
306
280
  status: 'complete',
307
- entity: entity && tenancy ? { ...entity, tenancy } : entity,
281
+ entity,
308
282
  credential: credential && {
309
283
  ...credential,
310
284
  type: entityType,
311
285
  entityIds: entity ? [String(entity.id)] : [],
312
- ...(tenancy && { tenancy }),
313
286
  },
314
287
  };
315
288
  if (credentialId && String(callback.credential_id) !== String(credentialId)) {
@@ -1,16 +1,5 @@
1
1
  const { Module } = require('../module');
2
2
  const { ModuleConstants } = require('../ModuleConstants');
3
- const { pickExistingEntity } = require('../scope-resolution');
4
-
5
- /**
6
- * What an entity belongs to when the caller says nothing: the person's own,
7
- * outside any organization.
8
- */
9
- const DEFAULT_TENANCY = Object.freeze({
10
- scope: 'individual',
11
- organizationId: null,
12
- legacyOrgId: null,
13
- });
14
3
  const { getLogger } = require('../../logs');
15
4
 
16
5
  const log = getLogger('frigg.modules.authorization_callback');
@@ -41,17 +30,7 @@ class ProcessAuthorizationCallback {
41
30
  this.integrationRepository = integrationRepository;
42
31
  }
43
32
 
44
- /**
45
- * @param {string} userId - the OWNER of what this creates (ADR-056 §3.2): the
46
- * person for `individual`, the organization's user for `organization`.
47
- * Not necessarily the caller; callers resolve it from the scope.
48
- * @param {string} entityType
49
- * @param {Object} params - what the module's authorization needs
50
- * @param {{scope: string, organizationId: (string|null), legacyOrgId: (string|null)}} [tenancy]
51
- * the entity's scope and organization (§3.6); default: the person's own,
52
- * with no organization
53
- */
54
- async execute(userId, entityType, params, tenancy = DEFAULT_TENANCY) {
33
+ async execute(userId, entityType, params) {
55
34
  log.debug('Authorization callback started', {
56
35
  eventName: `${log.name}.started`,
57
36
  userId,
@@ -78,7 +57,10 @@ class ProcessAuthorizationCallback {
78
57
  userId,
79
58
  entityType
80
59
  );
81
- const existingEntity = pickExistingEntity(existingEntities, tenancy);
60
+ const existingEntity =
61
+ existingEntities && existingEntities.length > 0
62
+ ? existingEntities[0]
63
+ : null;
82
64
 
83
65
  if (existingEntity) {
84
66
  log.debug('Existing entity found', {
@@ -88,7 +70,7 @@ class ProcessAuthorizationCallback {
88
70
  });
89
71
  }
90
72
 
91
- const module = await Module.create({
73
+ const module = new Module({
92
74
  userId,
93
75
  entity: existingEntity,
94
76
  definition: moduleDefinition,
@@ -174,8 +156,7 @@ class ProcessAuthorizationCallback {
174
156
  const persistedEntity = await this.findOrCreateEntity(
175
157
  entityDetails,
176
158
  entityType,
177
- module.credential.id,
178
- tenancy
159
+ module.credential.id
179
160
  );
180
161
  log.debug('Authorization callback completed', {
181
162
  eventName: `${log.name}.completed`,
@@ -252,12 +233,7 @@ class ProcessAuthorizationCallback {
252
233
  module.credential = persisted;
253
234
  }
254
235
 
255
- async findOrCreateEntity(
256
- entityDetails,
257
- moduleName,
258
- credentialId,
259
- tenancy = DEFAULT_TENANCY
260
- ) {
236
+ async findOrCreateEntity(entityDetails, moduleName, credentialId) {
261
237
  const { identifiers, details } = entityDetails;
262
238
 
263
239
  // Support both 'user' and 'userId' field names from module definitions
@@ -271,16 +247,11 @@ class ProcessAuthorizationCallback {
271
247
  );
272
248
  }
273
249
 
274
- // The same provider account in two organizations is two entities
275
- // (ADR-056 §3.6), so the match is per organization.
276
- const existingEntity = pickExistingEntity(
277
- await this.moduleRepository.findEntities({
278
- externalId: identifiers.externalId,
279
- user: userId,
280
- moduleName: moduleName,
281
- }),
282
- tenancy
283
- );
250
+ const existingEntity = await this.moduleRepository.findEntity({
251
+ externalId: identifiers.externalId,
252
+ user: userId,
253
+ moduleName: moduleName,
254
+ });
284
255
 
285
256
  if (existingEntity) {
286
257
  // Repoint the entity's credentialId when re-auth produced a
@@ -311,17 +282,12 @@ class ProcessAuthorizationCallback {
311
282
  return existingEntity;
312
283
  }
313
284
 
314
- // `organizationId` is not part of the entity data: that carries provider
315
- // fields, and one that happens to be called organizationId is theirs.
316
- return await this.moduleRepository.createEntity(
317
- {
318
- ...identifiers,
319
- ...details,
320
- moduleName: moduleName,
321
- credential: credentialId,
322
- },
323
- { organizationId: tenancy.organizationId ?? null }
324
- );
285
+ return await this.moduleRepository.createEntity({
286
+ ...identifiers,
287
+ ...details,
288
+ moduleName: moduleName,
289
+ credential: credentialId,
290
+ });
325
291
  }
326
292
  }
327
293
 
@@ -1,5 +1,4 @@
1
1
  const { Module } = require('../module');
2
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
3
2
 
4
3
  class RefreshEntityOptions {
5
4
  /**
@@ -7,11 +6,9 @@ class RefreshEntityOptions {
7
6
  * @param {import('../repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
8
7
  * @param {} params.moduleDefinitions
9
8
  */
10
- constructor({ moduleRepository, moduleDefinitions, scopePolicy }) {
9
+ constructor({ moduleRepository, moduleDefinitions }) {
11
10
  this.moduleRepository = moduleRepository;
12
11
  this.moduleDefinitions = moduleDefinitions;
13
- // Who may load an entity follows its scope (ADR-056 §3.6).
14
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
15
12
  }
16
13
 
17
14
  /**
@@ -39,7 +36,7 @@ class RefreshEntityOptions {
39
36
 
40
37
  // Validate entity ownership
41
38
  const isOwned = typeof userIdOrUser === 'object' && userIdOrUser?.ownsUserId
42
- ? this.scopePolicy.canUseEntity(userIdOrUser, entity)
39
+ ? userIdOrUser.ownsUserId(entity.userId)
43
40
  : entity.userId?.toString() === userId?.toString();
44
41
 
45
42
  if (!isOwned) {
@@ -61,11 +58,8 @@ class RefreshEntityOptions {
61
58
  );
62
59
  }
63
60
 
64
- // Built for the entity's OWNER, not the caller: a caller may use an
65
- // organization's entity, and a token refresh during this call must
66
- // update the owner's credential, not create one under the caller.
67
- const module = await Module.create({
68
- userId: entity.userId ?? userId,
61
+ const module = new Module({
62
+ userId,
69
63
  entity,
70
64
  definition: moduleDefinition,
71
65
  });
@@ -1,5 +1,4 @@
1
1
  const { Module } = require('../module');
2
- const { DEFAULT_SCOPE_POLICY } = require('../scope-policy');
3
2
 
4
3
  class TestModuleAuth {
5
4
  /**
@@ -7,11 +6,9 @@ class TestModuleAuth {
7
6
  * @param {import('../repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository - Repository for module data operations.
8
7
  * @param {Array<Object>} params.moduleDefinitions - Array of module definitions.
9
8
  */
10
- constructor({ moduleRepository, moduleDefinitions, scopePolicy }) {
9
+ constructor({ moduleRepository, moduleDefinitions }) {
11
10
  this.moduleRepository = moduleRepository;
12
11
  this.moduleDefinitions = moduleDefinitions;
13
- // Who may load an entity follows its scope (ADR-056 §3.6).
14
- this.scopePolicy = scopePolicy || DEFAULT_SCOPE_POLICY;
15
12
  }
16
13
 
17
14
  /**
@@ -38,7 +35,7 @@ class TestModuleAuth {
38
35
 
39
36
  // Validate entity ownership
40
37
  const isOwned = typeof userIdOrUser === 'object' && userIdOrUser?.ownsUserId
41
- ? this.scopePolicy.canUseEntity(userIdOrUser, entity)
38
+ ? userIdOrUser.ownsUserId(entity.userId)
42
39
  : entity.userId?.toString() === userId?.toString();
43
40
 
44
41
  if (!isOwned) {
@@ -60,11 +57,8 @@ class TestModuleAuth {
60
57
  );
61
58
  }
62
59
 
63
- // Built for the entity's OWNER, not the caller: a caller may use an
64
- // organization's entity, and a token refresh during this call must
65
- // update the owner's credential, not create one under the caller.
66
- const module = await Module.create({
67
- userId: entity.userId ?? userId,
60
+ const module = new Module({
61
+ userId,
68
62
  entity,
69
63
  definition: moduleDefinition,
70
64
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.707.df80bfd.0",
4
+ "version": "2.0.0--canary.680.d670696.0",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
7
7
  "@aws-sdk/client-kms": "^3.588.0",
@@ -28,7 +28,6 @@
28
28
  "express-async-handler": "^1.2.0",
29
29
  "form-data": "^4.0.6",
30
30
  "fs-extra": "^11.2.0",
31
- "jose": "^5.10.0",
32
31
  "lodash": "4.18.1",
33
32
  "lodash.get": "^4.4.2",
34
33
  "node-fetch": "^2.6.7",
@@ -48,9 +47,9 @@
48
47
  }
49
48
  },
50
49
  "devDependencies": {
51
- "@friggframework/eslint-config": "2.0.0--canary.707.df80bfd.0",
52
- "@friggframework/prettier-config": "2.0.0--canary.707.df80bfd.0",
53
- "@friggframework/test": "2.0.0--canary.707.df80bfd.0",
50
+ "@friggframework/eslint-config": "2.0.0--canary.680.d670696.0",
51
+ "@friggframework/prettier-config": "2.0.0--canary.680.d670696.0",
52
+ "@friggframework/test": "2.0.0--canary.680.d670696.0",
54
53
  "@prisma/client": "^6.19.3",
55
54
  "@types/lodash": "4.17.15",
56
55
  "@typescript-eslint/eslint-plugin": "^8.0.0",
@@ -90,5 +89,5 @@
90
89
  "publishConfig": {
91
90
  "access": "public"
92
91
  },
93
- "gitHead": "df80bfd35bba914a7b20184bd2d5aa88f9f406ff"
92
+ "gitHead": "d6706963c56674519299fca904c380268b721911"
94
93
  }