@friggframework/core 2.0.0--canary.685.a13fdab.0 → 2.0.0--canary.679.a2bcc2f.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 (106) hide show
  1. package/CLAUDE.md +0 -25
  2. package/README.md +0 -105
  3. package/application/commands/integration-commands.js +2 -2
  4. package/application/commands/user-commands.js +5 -17
  5. package/credential/use-cases/credential-operations-for-user.js +25 -110
  6. package/credential/use-cases/reauthorize-credential-for-user.js +8 -15
  7. package/database/documentdb-utils.js +1 -6
  8. package/generated/prisma-mongodb/edge.js +4 -15
  9. package/generated/prisma-mongodb/index-browser.js +1 -12
  10. package/generated/prisma-mongodb/index.d.ts +86 -2027
  11. package/generated/prisma-mongodb/index.js +4 -15
  12. package/generated/prisma-mongodb/package.json +1 -1
  13. package/generated/prisma-mongodb/schema.prisma +0 -47
  14. package/generated/prisma-mongodb/wasm.js +4 -15
  15. package/generated/prisma-postgresql/edge.js +4 -15
  16. package/generated/prisma-postgresql/index-browser.js +1 -12
  17. package/generated/prisma-postgresql/index.d.ts +63 -2162
  18. package/generated/prisma-postgresql/index.js +4 -15
  19. package/generated/prisma-postgresql/package.json +1 -1
  20. package/generated/prisma-postgresql/schema.prisma +0 -44
  21. package/generated/prisma-postgresql/wasm.js +4 -15
  22. package/handlers/app-definition-loader.js +1 -24
  23. package/index.js +0 -22
  24. package/integrations/integration-router.js +73 -115
  25. package/integrations/repositories/integration-repository-documentdb.js +3 -35
  26. package/integrations/repositories/integration-repository-interface.js +1 -17
  27. package/integrations/repositories/integration-repository-mongo.js +76 -59
  28. package/integrations/repositories/integration-repository-postgres.js +76 -62
  29. package/integrations/tests/doubles/test-integration-repository.js +1 -12
  30. package/integrations/use-cases/create-integration.js +20 -55
  31. package/integrations/use-cases/get-integrations-for-user.js +40 -80
  32. package/integrations/use-cases/index.js +1 -5
  33. package/integrations/use-cases/integration-operations-for-user.js +24 -55
  34. package/management-api/management-api-config.js +2 -71
  35. package/management-api/openapi/components.js +0 -40
  36. package/management-api/openapi/v2-operations.js +4 -16
  37. package/management-api/v2/dependencies.js +15 -40
  38. package/management-api/v2/dto.js +0 -20
  39. package/management-api/v2/handlers/authorization.js +0 -1
  40. package/management-api/v2/handlers/credentials.js +2 -6
  41. package/management-api/v2/handlers/entities.js +7 -10
  42. package/management-api/v2/handlers/integrations.js +3 -6
  43. package/management-api/v2/request.js +1 -32
  44. package/modules/index.js +0 -8
  45. package/modules/repositories/module-repository-documentdb.js +6 -51
  46. package/modules/repositories/module-repository-interface.js +2 -19
  47. package/modules/repositories/module-repository-mongo.js +74 -67
  48. package/modules/repositories/module-repository-postgres.js +74 -66
  49. package/modules/repositories/module-repository.js +1 -2
  50. package/modules/use-cases/entity-operations-for-user.js +13 -50
  51. package/modules/use-cases/entity-types.js +7 -31
  52. package/modules/use-cases/execute-entity-proxy-request.js +3 -50
  53. package/modules/use-cases/get-entities-for-user.js +5 -16
  54. package/modules/use-cases/get-entity-module-for-user.js +5 -32
  55. package/modules/use-cases/get-entity-options-by-id.js +3 -9
  56. package/modules/use-cases/get-module.js +3 -9
  57. package/modules/use-cases/multi-step-authorization.js +12 -39
  58. package/modules/use-cases/process-authorization-callback.js +18 -52
  59. package/modules/use-cases/refresh-entity-options.js +3 -9
  60. package/modules/use-cases/test-module-auth.js +3 -9
  61. package/package.json +5 -6
  62. package/prisma-mongodb/schema.prisma +0 -47
  63. package/prisma-postgresql/schema.prisma +0 -44
  64. package/types/index.d.ts +0 -1
  65. package/types/modules/index.d.ts +0 -129
  66. package/user/repositories/user-repository-documentdb.js +2 -27
  67. package/user/repositories/user-repository-interface.js +0 -18
  68. package/user/repositories/user-repository-mongo.js +0 -24
  69. package/user/repositories/user-repository-postgres.js +0 -18
  70. package/user/use-cases/authenticate-user.js +1 -7
  71. package/user/use-cases/get-user-from-adopter-jwt.js +124 -95
  72. package/user/use-cases/get-user-from-x-frigg-headers.js +80 -83
  73. package/user/user.js +1 -64
  74. package/database/organization-where.js +0 -18
  75. package/database/unique-violation.js +0 -25
  76. package/integrations/repositories/integration-not-found.js +0 -14
  77. package/integrations/use-cases/resolve-integration-ownership.js +0 -177
  78. package/modules/entity-scope.js +0 -15
  79. package/modules/repositories/entity-not-found.js +0 -18
  80. package/modules/scope-config.js +0 -192
  81. package/modules/scope-policy.js +0 -112
  82. package/modules/scope-resolution.js +0 -288
  83. package/modules/scope-visibility.js +0 -247
  84. package/modules/tests/doubles/ownership-matching.js +0 -18
  85. package/modules/use-cases/authorize-entity-for-user.js +0 -61
  86. package/modules/use-cases/create-entity-from-credential.js +0 -113
  87. package/modules/use-cases/manage-access.js +0 -21
  88. package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +0 -26
  89. package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +0 -25
  90. package/types/user/index.d.ts +0 -134
  91. package/user/adopter-jwt-config.js +0 -177
  92. package/user/adopter-jwt-errors.js +0 -59
  93. package/user/adopter-token-verifier.js +0 -122
  94. package/user/app-user-helpers.js +0 -97
  95. package/user/create-user-authentication.js +0 -64
  96. package/user/membership.js +0 -51
  97. package/user/repositories/organization-membership-record.js +0 -19
  98. package/user/repositories/organization-membership-repository-documentdb.js +0 -106
  99. package/user/repositories/organization-membership-repository-factory.js +0 -42
  100. package/user/repositories/organization-membership-repository-interface.js +0 -57
  101. package/user/repositories/organization-membership-repository-mongo.js +0 -13
  102. package/user/repositories/organization-membership-repository-postgres.js +0 -29
  103. package/user/repositories/organization-membership-repository-prisma.js +0 -81
  104. package/user/tests/doubles/adopter-jwt-fixtures.js +0 -220
  105. package/user/use-cases/ensure-organization-membership.js +0 -92
  106. package/user/use-cases/find-individual-users-by-organization.js +0 -54
@@ -2,8 +2,6 @@ const Boom = require('@hapi/boom');
2
2
  const { getLogger } = require('../../logs');
3
3
  const { getTelemetry } = require('../../telemetry/telemetry-runtime');
4
4
  const { ProxyRequestError } = require('../proxy/proxy-errors');
5
- const { SCOPES } = require('../scope-resolution');
6
- const { grantsUse } = require('../scope-visibility');
7
5
  const { normalizeProxyPath, matchAllowRule } = require('../proxy/proxy-path');
8
6
  const {
9
7
  validateProxyRequest,
@@ -57,11 +55,9 @@ const authHeaderNamesOf = (api) =>
57
55
  * through its module with the stored credential, inside the limits the app
58
56
  * and the module set.
59
57
  *
60
- * Order of checks: request shape and size, path rules, entity visibility in
61
- * the active organization (404), `use` access (403), the entity's scope
62
- * against `managementApi.proxy.scopes` (403), the module's allow-list (403,
63
- * deny by default), credential readiness (401), then the call through
64
- * Requester._proxyRequest. Every call,
58
+ * Order of checks: request shape and size, path rules, entity ownership
59
+ * (404), the module's allow-list (403, deny by default), credential
60
+ * readiness (401), then the call through Requester._proxyRequest. Every call,
65
61
  * allowed or denied, writes one audit record and one usage count. Bodies,
66
62
  * headers, query values and the raw path are never logged.
67
63
  */
@@ -102,14 +98,9 @@ class ExecuteEntityProxyRequest {
102
98
  audit.requestBytes = validated.requestBytes;
103
99
  const path = normalizeProxyPath(validated.path);
104
100
 
105
- // Visible in the active organization, or 404 (ADR-056 §3.6): the
106
- // same clauses the v2 lists use, so a missing entity, another
107
- // person's and one paired with another organization look alike.
108
101
  const { entity, definition, module } =
109
102
  await this.getEntityModuleForUser.execute(entityId, user);
110
103
  audit.module = definition.moduleName;
111
- this.assertProxyable(entityId, entity, definition, user);
112
-
113
104
  const credentialRef = entity.credential;
114
105
  const credentialId =
115
106
  credentialRef && typeof credentialRef === 'object'
@@ -211,44 +202,6 @@ class ExecuteEntityProxyRequest {
211
202
  }
212
203
  }
213
204
 
214
- /**
215
- * ADR-056 §3.5: the caller needs `use` access to the entity in the active
216
- * organization, and the entity's scope must be one the app lets the proxy
217
- * accept. `access` and `scope` come from the description the v2 lists
218
- * return (GetEntityModuleForUser.describe), never from a second rule.
219
- *
220
- * Both refusals are 403 PROXY_NOT_ALLOWED: the caller can already see the
221
- * entity, so saying it exists leaks nothing. `details.reason` tells them
222
- * apart ('access' or 'scope').
223
- */
224
- assertProxyable(entityId, entity, definition, user) {
225
- const tenancy = this.getEntityModuleForUser.describe(entity, user);
226
- if (!tenancy) {
227
- // Not visible after all: the same 404 as a missing entity.
228
- throw Boom.notFound(`Entity ${entityId} not found`, {
229
- code: 'ENTITY_NOT_FOUND',
230
- });
231
- }
232
- if (!grantsUse(tenancy.access)) {
233
- throw new ProxyRequestError(
234
- 'PROXY_NOT_ALLOWED',
235
- 'Proxying through an entity needs use access to it; you can only view this one',
236
- { module: definition.moduleName, reason: 'access' }
237
- );
238
- }
239
- // Narrowest by default (§3.5): with no setting only individual
240
- // entities are proxied, because proxying through a shared credential
241
- // lets any member act on the organization's account.
242
- const acceptedScopes = this.proxyConfig.scopes ?? [SCOPES.INDIVIDUAL];
243
- if (!acceptedScopes.includes(tenancy.scope)) {
244
- throw new ProxyRequestError(
245
- 'PROXY_NOT_ALLOWED',
246
- `The entity proxy does not accept ${tenancy.scope} entities in this app`,
247
- { module: definition.moduleName, reason: 'scope', scope: tenancy.scope }
248
- );
249
- }
250
- }
251
-
252
205
  /** A module may pin its proxy base URL (string, or a function of the api). */
253
206
  resolveBaseUrl(definition, api) {
254
207
  const declared = definition.proxy?.baseUrl;
@@ -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,24 +11,16 @@ 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
19
  return entities.map((entity) => {
31
20
  const definition = this.definitionMap.get(entity.moduleName);
32
21
 
33
22
  const moduleInstance = new Module({
34
- userId: entity.userId,
23
+ userId,
35
24
  definition: definition,
36
25
  entity: entity,
37
26
  });
@@ -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
  */
@@ -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;
@@ -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
60
  const module = new Module({
67
- userId: entity.userId ?? userId,
61
+ userId,
68
62
  entity,
69
63
  definition: moduleDefinition,
70
64
  });
@@ -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
56
  const module = new Module({
63
- userId: entity.userId ?? userId,
57
+ userId,
64
58
  entity,
65
59
  definition: moduleDefinition,
66
60
  });
@@ -3,8 +3,6 @@ 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
 
@@ -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 = 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', {
@@ -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
61
  const module = new Module({
68
- userId: entity.userId ?? userId,
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
60
  const module = new Module({
67
- userId: entity.userId ?? userId,
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.685.a13fdab.0",
4
+ "version": "2.0.0--canary.679.a2bcc2f.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.685.a13fdab.0",
52
- "@friggframework/prettier-config": "2.0.0--canary.685.a13fdab.0",
53
- "@friggframework/test": "2.0.0--canary.685.a13fdab.0",
50
+ "@friggframework/eslint-config": "2.0.0--canary.679.a2bcc2f.0",
51
+ "@friggframework/prettier-config": "2.0.0--canary.679.a2bcc2f.0",
52
+ "@friggframework/test": "2.0.0--canary.679.a2bcc2f.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": "a13fdab0900eaf5edb30f284f6b598c8e20686d8"
92
+ "gitHead": "a2bcc2f124b0574c0d868beff77b4da848fd7849"
94
93
  }