@friggframework/core 2.0.0--canary.710.7cfcf12.0 → 2.0.0--canary.682.451c957.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 (174) hide show
  1. package/CLAUDE.md +3 -41
  2. package/README.md +105 -0
  3. package/application/commands/README.md +11 -17
  4. package/application/commands/entity-commands.js +60 -18
  5. package/application/commands/integration-commands.js +20 -73
  6. package/application/commands/user-commands.js +17 -5
  7. package/application/index.js +6 -2
  8. package/credential/repositories/credential-repository-documentdb.js +34 -0
  9. package/credential/repositories/credential-repository-interface.js +15 -0
  10. package/credential/repositories/credential-repository-mongo.js +23 -0
  11. package/credential/repositories/credential-repository-postgres.js +28 -0
  12. package/credential/use-cases/credential-operations-for-user.js +117 -0
  13. package/credential/use-cases/reauthorize-credential-for-user.js +66 -0
  14. package/database/documentdb-encryption-service.js +33 -2
  15. package/database/documentdb-utils.js +2 -0
  16. package/database/encryption/README.md +3 -3
  17. package/database/encryption/encryption-schema-registry.js +42 -109
  18. package/database/encryption/field-encryption-service.js +16 -13
  19. package/database/organization-where.js +18 -0
  20. package/database/prisma.js +1 -10
  21. package/database/unique-violation.js +25 -0
  22. package/errors/index.js +0 -4
  23. package/generated/prisma-mongodb/edge.js +31 -4
  24. package/generated/prisma-mongodb/index-browser.js +28 -1
  25. package/generated/prisma-mongodb/index.d.ts +3451 -62
  26. package/generated/prisma-mongodb/index.js +31 -4
  27. package/generated/prisma-mongodb/package.json +1 -1
  28. package/generated/prisma-mongodb/schema.prisma +74 -0
  29. package/generated/prisma-mongodb/wasm.js +31 -4
  30. package/generated/prisma-postgresql/edge.js +31 -4
  31. package/generated/prisma-postgresql/index-browser.js +28 -1
  32. package/generated/prisma-postgresql/index.d.ts +3633 -53
  33. package/generated/prisma-postgresql/index.js +31 -4
  34. package/generated/prisma-postgresql/package.json +1 -1
  35. package/generated/prisma-postgresql/schema.prisma +70 -0
  36. package/generated/prisma-postgresql/wasm.js +31 -4
  37. package/handlers/WEBHOOKS.md +0 -29
  38. package/handlers/app-definition-loader.js +33 -8
  39. package/handlers/app-handler-helpers.js +25 -3
  40. package/handlers/backend-utils.js +31 -25
  41. package/handlers/middleware/admin-auth.js +14 -1
  42. package/handlers/routers/auth.js +11 -3
  43. package/handlers/routers/health.js +11 -4
  44. package/handlers/workers/integration-defined-workers.js +5 -2
  45. package/index.js +23 -2
  46. package/infrastructure/scheduler/eventbridge-scheduler-adapter.js +13 -67
  47. package/integrations/index.js +1 -0
  48. package/integrations/integration-base.js +0 -29
  49. package/integrations/integration-router.js +136 -98
  50. package/integrations/repositories/integration-mapping-repository-documentdb.js +0 -50
  51. package/integrations/repositories/integration-mapping-repository-interface.js +0 -15
  52. package/integrations/repositories/integration-mapping-repository-mongo.js +0 -29
  53. package/integrations/repositories/integration-mapping-repository-postgres.js +0 -30
  54. package/integrations/repositories/integration-mapping-repository.js +0 -29
  55. package/integrations/repositories/integration-not-found.js +14 -0
  56. package/integrations/repositories/integration-repository-documentdb.js +35 -35
  57. package/integrations/repositories/integration-repository-interface.js +17 -31
  58. package/integrations/repositories/integration-repository-mongo.js +60 -129
  59. package/integrations/repositories/integration-repository-postgres.js +62 -134
  60. package/integrations/repositories/process-repository-mongo.js +1 -1
  61. package/integrations/tests/doubles/test-integration-repository.js +12 -1
  62. package/integrations/use-cases/create-integration.js +72 -75
  63. package/integrations/use-cases/get-integrations-for-user.js +80 -40
  64. package/integrations/use-cases/index.js +5 -1
  65. package/integrations/use-cases/integration-operations-for-user.js +246 -0
  66. package/integrations/use-cases/resolve-integration-ownership.js +177 -0
  67. package/integrations/utils/map-integration-dto.js +47 -15
  68. package/jest-setup.js +2 -13
  69. package/jest-teardown.js +2 -5
  70. package/jest.config.js +0 -4
  71. package/management-api/create-management-api-router.js +49 -0
  72. package/management-api/error-response.js +116 -0
  73. package/management-api/management-api-config.js +85 -0
  74. package/management-api/meta-router.js +57 -0
  75. package/management-api/openapi/build-openapi.js +144 -0
  76. package/management-api/openapi/components.js +407 -0
  77. package/management-api/openapi/v1-operations.js +83 -0
  78. package/management-api/openapi/v2-operations.js +261 -0
  79. package/management-api/route-registry.js +294 -0
  80. package/management-api/test-support/http-client.js +39 -0
  81. package/management-api/use-cases/get-management-api-meta.js +57 -0
  82. package/management-api/v1-deprecation.js +120 -0
  83. package/management-api/v2/create-v2-router.js +85 -0
  84. package/management-api/v2/dependencies.js +279 -0
  85. package/management-api/v2/dto.js +143 -0
  86. package/management-api/v2/handlers/authorization.js +94 -0
  87. package/management-api/v2/handlers/credentials.js +29 -0
  88. package/management-api/v2/handlers/entities.js +77 -0
  89. package/management-api/v2/handlers/integrations.js +128 -0
  90. package/management-api/v2/handlers/proxy.js +34 -0
  91. package/management-api/v2/request.js +85 -0
  92. package/management-api/v2/test-support/fake-dependencies.js +103 -0
  93. package/modules/domain/authorization-session.js +144 -0
  94. package/modules/entity-scope.js +15 -0
  95. package/modules/entity-type-registry.js +311 -0
  96. package/modules/index.js +14 -0
  97. package/modules/proxy/proxy-errors.js +20 -0
  98. package/modules/proxy/proxy-path.js +179 -0
  99. package/modules/proxy/proxy-policy.js +248 -0
  100. package/modules/repositories/authorization-session-repository-documentdb.js +112 -0
  101. package/modules/repositories/authorization-session-repository-factory.js +31 -0
  102. package/modules/repositories/authorization-session-repository-interface.js +47 -0
  103. package/modules/repositories/authorization-session-repository-prisma.js +87 -0
  104. package/modules/repositories/module-repository-documentdb.js +48 -4
  105. package/modules/repositories/module-repository-interface.js +19 -2
  106. package/modules/repositories/module-repository-mongo.js +65 -73
  107. package/modules/repositories/module-repository-postgres.js +64 -73
  108. package/modules/requester/proxy-request.js +244 -0
  109. package/modules/requester/requester.js +27 -2
  110. package/modules/scope-config.js +155 -0
  111. package/modules/scope-policy.js +80 -0
  112. package/modules/scope-resolution.js +277 -0
  113. package/modules/scope-visibility.js +165 -0
  114. package/modules/tests/doubles/in-memory-authorization-session-repository.js +45 -0
  115. package/modules/tests/doubles/ownership-matching.js +18 -0
  116. package/modules/use-cases/authorize-entity-for-user.js +59 -0
  117. package/modules/use-cases/create-entity-from-credential.js +113 -0
  118. package/modules/use-cases/entity-operations-for-user.js +157 -0
  119. package/modules/use-cases/entity-types.js +139 -0
  120. package/modules/use-cases/execute-entity-proxy-request.js +254 -0
  121. package/modules/use-cases/get-authorization-requirements.js +100 -0
  122. package/modules/use-cases/get-entities-for-user.js +16 -5
  123. package/modules/use-cases/get-entity-module-for-user.js +106 -0
  124. package/modules/use-cases/get-entity-options-by-id.js +16 -8
  125. package/modules/use-cases/get-module.js +27 -11
  126. package/modules/use-cases/multi-step-authorization.js +326 -0
  127. package/modules/use-cases/process-authorization-callback.js +52 -18
  128. package/modules/use-cases/refresh-entity-options.js +16 -8
  129. package/modules/use-cases/test-module-auth.js +16 -8
  130. package/modules/utils/find-module-definition.js +23 -0
  131. package/modules/utils/map-module-dto.js +10 -6
  132. package/modules/utils/public-entity.js +59 -0
  133. package/package.json +6 -7
  134. package/prisma-mongodb/schema.prisma +74 -0
  135. package/prisma-postgresql/migrations/20261003000000_add_authorization_session/migration.sql +28 -0
  136. package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +26 -0
  137. package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +25 -0
  138. package/prisma-postgresql/schema.prisma +70 -0
  139. package/queues/queuer-util.js +5 -6
  140. package/types/index.d.ts +2 -0
  141. package/types/modules/index.d.ts +155 -0
  142. package/types/user/index.d.ts +117 -0
  143. package/user/adopter-jwt-config.js +177 -0
  144. package/user/adopter-jwt-errors.js +59 -0
  145. package/user/adopter-token-verifier.js +122 -0
  146. package/user/app-user-helpers.js +97 -0
  147. package/user/create-user-authentication.js +64 -0
  148. package/user/membership.js +51 -0
  149. package/user/repositories/organization-membership-record.js +19 -0
  150. package/user/repositories/organization-membership-repository-documentdb.js +106 -0
  151. package/user/repositories/organization-membership-repository-factory.js +42 -0
  152. package/user/repositories/organization-membership-repository-interface.js +57 -0
  153. package/user/repositories/organization-membership-repository-mongo.js +13 -0
  154. package/user/repositories/organization-membership-repository-postgres.js +29 -0
  155. package/user/repositories/organization-membership-repository-prisma.js +81 -0
  156. package/user/repositories/user-repository-documentdb.js +27 -2
  157. package/user/repositories/user-repository-interface.js +18 -0
  158. package/user/repositories/user-repository-mongo.js +24 -0
  159. package/user/repositories/user-repository-postgres.js +18 -0
  160. package/user/tests/doubles/adopter-jwt-fixtures.js +220 -0
  161. package/user/tests/doubles/test-user-repository.js +1 -1
  162. package/user/use-cases/ensure-organization-membership.js +92 -0
  163. package/user/use-cases/find-individual-users-by-organization.js +54 -0
  164. package/user/use-cases/get-user-from-adopter-jwt.js +86 -125
  165. package/user/use-cases/get-user-from-bearer-token.js +2 -7
  166. package/user/use-cases/get-user-from-x-frigg-headers.js +83 -80
  167. package/user/user.js +27 -0
  168. package/database/encryption/ciphertext-format.js +0 -24
  169. package/errors/mapping-already-exists-error.js +0 -14
  170. package/integrations/use-cases/add-entities-to-integration.js +0 -92
  171. package/integrations/use-cases/remove-entities-from-integration.js +0 -70
  172. package/integrations/utils/assert-entity-ids-input.js +0 -42
  173. package/jest.db.config.js +0 -23
  174. package/modules/utils/to-credential-id.js +0 -14
package/user/user.js CHANGED
@@ -18,6 +18,9 @@ class User {
18
18
  this.individualUser = individualUser;
19
19
  this.organizationUser = organizationUser;
20
20
  this.usePassword = usePassword;
21
+ // Organization role from an adopter token claim (ADR-056 §2). Per request:
22
+ // the role stored on a membership is only what the first token carried.
23
+ this.orgRole = null;
21
24
 
22
25
  this.config = {
23
26
  primary,
@@ -26,6 +29,17 @@ class User {
26
29
  };
27
30
  }
28
31
 
32
+ /**
33
+ * The organization this request acts in (ADR-056 §3.6): the one the token or
34
+ * `x-frigg-appOrgId` named, not the person's legacy `organizationId`
35
+ * pointer. Null when the request has no organization.
36
+ * @returns {string|null}
37
+ */
38
+ getActiveOrganizationId() {
39
+ const id = this.organizationUser?.id;
40
+ return id === undefined || id === null || id === '' ? null : String(id);
41
+ }
42
+
29
43
  getPrimaryUser() {
30
44
  if (this.config.primary === 'organization') {
31
45
  return this.organizationUser;
@@ -33,6 +47,11 @@ class User {
33
47
  return this.individualUser;
34
48
  }
35
49
 
50
+ /**
51
+ * The id of the primary user. With `primary: 'organization'` this is the
52
+ * active organization (the one the request named), never the organization
53
+ * the person was first linked to.
54
+ */
36
55
  getId() {
37
56
  return this.getPrimaryUser()?.id;
38
57
  }
@@ -65,6 +84,14 @@ class User {
65
84
  return this.config.individualUserRequired;
66
85
  }
67
86
 
87
+ getOrgRole() {
88
+ return this.orgRole;
89
+ }
90
+
91
+ setOrgRole(orgRole) {
92
+ this.orgRole = orgRole ?? null;
93
+ }
94
+
68
95
  getIndividualUser() {
69
96
  return this.individualUser;
70
97
  }
@@ -1,24 +0,0 @@
1
- /**
2
- * Recognises values produced by Cryptor.encrypt():
3
- * keyId:iv:ciphertext:encryptedKey
4
- * keyId and encryptedKey are base64, iv and ciphertext are hex, so all four
5
- * parts match the base64 alphabet. Real envelopes are well over 50
6
- * characters (the encrypted data key alone is longer).
7
- *
8
- * Shared by the Prisma (FieldEncryptionService) and DocumentDB paths so a
9
- * plaintext value that happens to contain colons (`a:b:c:d`, a URL with
10
- * userinfo and a port) is never mistaken for ciphertext: on write it gets
11
- * encrypted, on read it is returned as plaintext.
12
- */
13
- const BASE64_PART = /^[A-Za-z0-9+/=]+$/;
14
- const MIN_ENVELOPE_LENGTH = 50;
15
-
16
- function isEncryptedEnvelope(value) {
17
- if (typeof value !== 'string' || value.length < MIN_ENVELOPE_LENGTH) {
18
- return false;
19
- }
20
- const parts = value.split(':');
21
- return parts.length === 4 && parts.every((part) => BASE64_PART.test(part));
22
- }
23
-
24
- module.exports = { isEncryptedEnvelope };
@@ -1,14 +0,0 @@
1
- const { BaseError } = require('./base-error');
2
-
3
- class MappingAlreadyExistsError extends BaseError {
4
- constructor(integrationId, sourceId, ...errorOptions) {
5
- super(
6
- `Mapping already exists for integration ${integrationId} and sourceId ${sourceId}`,
7
- ...errorOptions
8
- );
9
- this.integrationId = integrationId;
10
- this.sourceId = sourceId;
11
- }
12
- }
13
-
14
- module.exports = { MappingAlreadyExistsError };
@@ -1,92 +0,0 @@
1
- const {
2
- assertEntityIdsInput,
3
- integrationNotFound,
4
- loadIntegration,
5
- } = require('../utils/assert-entity-ids-input');
6
-
7
- /**
8
- * Attaches entities to an existing integration, e.g. when a user grants
9
- * access to more accounts after the integration was created. Every entity
10
- * must exist, belong to the integration's user, and be of a module the
11
- * integration already holds (so hydration can load it). Entities that are
12
- * already attached are left as they are.
13
- * @class AddEntitiesToIntegration
14
- */
15
- class AddEntitiesToIntegration {
16
- /**
17
- * @param {Object} params
18
- * @param {import('../repositories/integration-repository-interface').IntegrationRepositoryInterface} params.integrationRepository
19
- * @param {import('../../modules/repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
20
- */
21
- constructor({ integrationRepository, moduleRepository }) {
22
- this.integrationRepository = integrationRepository;
23
- this.moduleRepository = moduleRepository;
24
- }
25
-
26
- /**
27
- * @param {string} integrationId
28
- * @param {string[]} entityIds
29
- * @returns {Promise<Object>} The updated integration record.
30
- */
31
- async execute(integrationId, entityIds) {
32
- assertEntityIdsInput(integrationId, entityIds);
33
-
34
- const integration = await loadIntegration(
35
- this.integrationRepository,
36
- integrationId
37
- );
38
-
39
- const entities = await this.moduleRepository.findEntitiesByIds(
40
- entityIds
41
- );
42
- const foundIds = new Set(entities.map((e) => String(e.id)));
43
- const missing = entityIds.filter((id) => !foundIds.has(String(id)));
44
- if (missing.length > 0) {
45
- const error = new Error(
46
- `Entities not found: ${missing.join(', ')}`
47
- );
48
- error.code = 'ENTITIES_NOT_FOUND';
49
- throw error;
50
- }
51
-
52
- const foreign = entities.filter(
53
- (e) => String(e.userId) !== String(integration.userId)
54
- );
55
- if (foreign.length > 0) {
56
- const error = new Error(
57
- `Entities do not belong to the integration's user: ${foreign
58
- .map((e) => e.id)
59
- .join(', ')}`
60
- );
61
- error.code = 'ENTITY_USER_MISMATCH';
62
- throw error;
63
- }
64
-
65
- const attached = await this.moduleRepository.findEntitiesByIds(
66
- integration.entitiesIds ?? []
67
- );
68
- const heldModules = new Set(attached.map((e) => e.moduleName));
69
- const otherModules = entities.filter(
70
- (e) => !heldModules.has(e.moduleName)
71
- );
72
- if (otherModules.length > 0) {
73
- const error = new Error(
74
- `Entities belong to a module the integration does not hold: ${otherModules
75
- .map((e) => `${e.id} (${e.moduleName})`)
76
- .join(', ')}`
77
- );
78
- error.code = 'ENTITY_MODULE_MISMATCH';
79
- throw error;
80
- }
81
-
82
- const updated =
83
- await this.integrationRepository.addEntitiesToIntegration(
84
- integrationId,
85
- entityIds
86
- );
87
- if (!updated) throw integrationNotFound(integrationId);
88
- return updated;
89
- }
90
- }
91
-
92
- module.exports = { AddEntitiesToIntegration };
@@ -1,70 +0,0 @@
1
- const {
2
- assertEntityIdsInput,
3
- integrationNotFound,
4
- loadIntegration,
5
- } = require('../utils/assert-entity-ids-input');
6
-
7
- /**
8
- * Detaches entities from an integration. Entities that are not attached are
9
- * ignored, and detached entities are not deleted. Every module the
10
- * integration holds must keep at least one entity, so the integration can
11
- * still hydrate each of its modules.
12
- * @class RemoveEntitiesFromIntegration
13
- */
14
- class RemoveEntitiesFromIntegration {
15
- /**
16
- * @param {Object} params
17
- * @param {import('../repositories/integration-repository-interface').IntegrationRepositoryInterface} params.integrationRepository
18
- * @param {import('../../modules/repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
19
- */
20
- constructor({ integrationRepository, moduleRepository }) {
21
- this.integrationRepository = integrationRepository;
22
- this.moduleRepository = moduleRepository;
23
- }
24
-
25
- /**
26
- * @param {string} integrationId
27
- * @param {string[]} entityIds
28
- * @returns {Promise<Object>} The updated integration record.
29
- */
30
- async execute(integrationId, entityIds) {
31
- assertEntityIdsInput(integrationId, entityIds);
32
-
33
- const integration = await loadIntegration(
34
- this.integrationRepository,
35
- integrationId
36
- );
37
-
38
- const removing = new Set(entityIds.map(String));
39
- const attached = await this.moduleRepository.findEntitiesByIds(
40
- integration.entitiesIds ?? []
41
- );
42
- const emptied = [...new Set(attached.map((e) => e.moduleName))].filter(
43
- (moduleName) =>
44
- !attached.some(
45
- (e) =>
46
- e.moduleName === moduleName &&
47
- !removing.has(String(e.id))
48
- )
49
- );
50
- if (emptied.length > 0) {
51
- const error = new Error(
52
- `Removing these entities would leave no entity for module(s): ${emptied.join(
53
- ', '
54
- )}`
55
- );
56
- error.code = 'LAST_ENTITY_OF_MODULE';
57
- throw error;
58
- }
59
-
60
- const updated =
61
- await this.integrationRepository.removeEntitiesFromIntegration(
62
- integrationId,
63
- entityIds
64
- );
65
- if (!updated) throw integrationNotFound(integrationId);
66
- return updated;
67
- }
68
- }
69
-
70
- module.exports = { RemoveEntitiesFromIntegration };
@@ -1,42 +0,0 @@
1
- /**
2
- * Throws INVALID_INTEGRATION_DATA unless an integration id and a non-empty
3
- * list of entity ids are given.
4
- * @param {string} integrationId
5
- * @param {string[]} entityIds
6
- */
7
- function assertEntityIdsInput(integrationId, entityIds) {
8
- if (!integrationId || !Array.isArray(entityIds) || entityIds.length === 0) {
9
- const error = new Error(
10
- 'integrationId and a non-empty entityIds array are required'
11
- );
12
- error.code = 'INVALID_INTEGRATION_DATA';
13
- throw error;
14
- }
15
- }
16
-
17
- function integrationNotFound(integrationId) {
18
- const error = new Error(`Integration ${integrationId} not found`);
19
- error.code = 'INTEGRATION_NOT_FOUND';
20
- return error;
21
- }
22
-
23
- /**
24
- * Loads an integration, mapping the repositories' "not found" error to
25
- * INTEGRATION_NOT_FOUND. Any other failure (connection, invalid id) is
26
- * rethrown unchanged.
27
- * @param {Object} integrationRepository
28
- * @param {string} integrationId
29
- * @returns {Promise<Object>} The integration record.
30
- */
31
- async function loadIntegration(integrationRepository, integrationId) {
32
- try {
33
- return await integrationRepository.findIntegrationById(integrationId);
34
- } catch (error) {
35
- if (/^Integration with id .* not found$/.test(error?.message)) {
36
- throw integrationNotFound(integrationId);
37
- }
38
- throw error;
39
- }
40
- }
41
-
42
- module.exports = { assertEntityIdsInput, integrationNotFound, loadIntegration };
package/jest.db.config.js DELETED
@@ -1,23 +0,0 @@
1
- /*
2
- * Database integration suites (`*.db.test.js`).
3
- *
4
- * These talk to real databases instead of mocking Prisma, so they are kept
5
- * out of the default `npm test` run and executed with `npm run test:db`.
6
- * They need generated Prisma clients (`npm run prisma:generate`) and:
7
- * - a MongoDB replica set (Prisma requires one for MongoDB writes):
8
- * FRIGG_TEST_MONGODB_URL, default
9
- * mongodb://localhost:27017/frigg?replicaSet=rs0
10
- * - PostgreSQL with the prisma-postgresql migrations applied:
11
- * FRIGG_TEST_POSTGRES_URL, default
12
- * postgresql://postgres:postgres@localhost:5432/frigg?schema=public
13
- * The repository parity suites skip themselves unless their own URLs are
14
- * set (INTEGRATION_ENTITIES_PARITY_* / QUERY_MAPPINGS_PARITY_*).
15
- */
16
- const base = require('./jest.config');
17
-
18
- module.exports = {
19
- ...base,
20
- coverageThreshold: undefined,
21
- testMatch: ['**/*.db.test.js'],
22
- testPathIgnorePatterns: ['/node_modules/'],
23
- };
@@ -1,14 +0,0 @@
1
- /**
2
- * Resolve the credential id from an entity's or module's `credential`, which
3
- * repositories return as the decrypted credential object (or a bare id).
4
- * @param {Object|string|number|null|undefined} credential
5
- * @returns {string|null}
6
- */
7
- function toCredentialId(credential) {
8
- if (credential && typeof credential === 'object') {
9
- return credential.id?.toString() ?? null;
10
- }
11
- return credential?.toString() ?? null;
12
- }
13
-
14
- module.exports = { toCredentialId };