@friggframework/core 2.0.0--canary.704.79d9a63.0 → 2.0.0--canary.686.e94de4d.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.
- package/CLAUDE.md +28 -40
- package/README.md +105 -0
- package/application/commands/entity-commands.js +60 -18
- package/application/commands/integration-commands.js +20 -73
- package/application/commands/scheduler-commands.js +11 -7
- package/application/commands/user-commands.js +17 -5
- package/application/index.js +6 -2
- package/core/Worker.js +31 -7
- package/core/create-handler.js +9 -0
- package/credential/repositories/credential-repository-documentdb.js +34 -0
- package/credential/repositories/credential-repository-interface.js +15 -0
- package/credential/repositories/credential-repository-mongo.js +23 -0
- package/credential/repositories/credential-repository-postgres.js +28 -0
- package/credential/use-cases/credential-operations-for-user.js +202 -0
- package/credential/use-cases/reauthorize-credential-for-user.js +73 -0
- package/database/config.js +48 -24
- package/database/documentdb-encryption-service.js +9 -22
- package/database/documentdb-utils.js +2 -0
- package/database/encryption/README.md +76 -14
- package/database/encryption/documentdb-encryption-service.md +78 -1
- package/database/encryption/encryption-config.js +219 -0
- package/database/encryption/encryption-schema-registry.js +93 -12
- package/database/encryption/integration-mapping-encryption.js +1 -1
- package/database/organization-where.js +18 -0
- package/database/prisma.js +15 -38
- package/database/unique-violation.js +25 -0
- package/database/use-cases/check-encryption-health-use-case.js +43 -52
- package/errors/index.js +0 -4
- package/extensions/bootstrap-runner.js +43 -0
- package/extensions/extension-loader.js +187 -0
- package/extensions/index.js +41 -0
- package/extensions/initialize-app.js +123 -0
- package/extensions/route-mounter.js +103 -0
- package/extensions/schema-composer.js +188 -0
- package/generated/prisma-mongodb/edge.js +43 -4
- package/generated/prisma-mongodb/index-browser.js +40 -1
- package/generated/prisma-mongodb/index.d.ts +4761 -133
- package/generated/prisma-mongodb/index.js +43 -4
- package/generated/prisma-mongodb/package.json +1 -1
- package/generated/prisma-mongodb/schema.prisma +92 -0
- package/generated/prisma-mongodb/wasm.js +43 -4
- package/generated/prisma-postgresql/edge.js +43 -4
- package/generated/prisma-postgresql/index-browser.js +40 -1
- package/generated/prisma-postgresql/index.d.ts +4961 -28
- package/generated/prisma-postgresql/index.js +43 -4
- package/generated/prisma-postgresql/package.json +1 -1
- package/generated/prisma-postgresql/schema.prisma +87 -0
- package/generated/prisma-postgresql/wasm.js +43 -4
- package/handlers/WEBHOOKS.md +0 -29
- package/handlers/app-definition-loader.js +121 -19
- package/handlers/app-handler-helpers.js +25 -3
- package/handlers/backend-utils.js +31 -25
- package/handlers/middleware/admin-auth.js +14 -1
- package/handlers/routers/auth.js +46 -10
- package/handlers/routers/health.js +13 -4
- package/handlers/routers/integration-defined-routers.js +144 -92
- package/handlers/routers/integration-webhook-routers.js +102 -64
- package/handlers/workers/integration-defined-workers.js +5 -2
- package/index.js +65 -3
- package/infrastructure/scheduler/index.js +19 -6
- package/infrastructure/scheduler/netlify-scheduler-adapter.js +216 -0
- package/infrastructure/scheduler/scheduled-job-repository.js +118 -0
- package/infrastructure/scheduler/scheduler-service-factory.js +55 -7
- package/integrations/index.js +1 -0
- package/integrations/integration-base.js +0 -29
- package/integrations/integration-router.js +131 -96
- package/integrations/repositories/integration-mapping-repository-documentdb.js +0 -50
- package/integrations/repositories/integration-mapping-repository-interface.js +0 -15
- package/integrations/repositories/integration-mapping-repository-mongo.js +0 -29
- package/integrations/repositories/integration-mapping-repository-postgres.js +0 -30
- package/integrations/repositories/integration-mapping-repository.js +0 -29
- package/integrations/repositories/integration-not-found.js +14 -0
- package/integrations/repositories/integration-repository-documentdb.js +35 -35
- package/integrations/repositories/integration-repository-interface.js +17 -31
- package/integrations/repositories/integration-repository-mongo.js +60 -129
- package/integrations/repositories/integration-repository-postgres.js +62 -134
- package/integrations/repositories/process-repository-mongo.js +1 -1
- package/integrations/schedule-config.js +80 -0
- package/integrations/tests/doubles/test-integration-repository.js +12 -1
- package/integrations/use-cases/create-integration.js +72 -75
- package/integrations/use-cases/dispatch-ongoing-syncs.js +100 -0
- package/integrations/use-cases/get-integrations-for-user.js +80 -40
- package/integrations/use-cases/index.js +7 -1
- package/integrations/use-cases/integration-operations-for-user.js +256 -0
- package/integrations/use-cases/resolve-integration-ownership.js +177 -0
- package/integrations/utils/map-integration-dto.js +43 -14
- package/management-api/create-management-api-router.js +49 -0
- package/management-api/error-response.js +116 -0
- package/management-api/management-api-config.js +151 -0
- package/management-api/meta-router.js +76 -0
- package/management-api/openapi/build-openapi.js +144 -0
- package/management-api/openapi/components.js +408 -0
- package/management-api/openapi/v1-operations.js +83 -0
- package/management-api/openapi/v2-operations.js +264 -0
- package/management-api/route-registry.js +411 -0
- package/management-api/test-support/http-client.js +39 -0
- package/management-api/use-cases/get-management-api-meta.js +57 -0
- package/management-api/v1-deprecation.js +120 -0
- package/management-api/v2/create-v2-router.js +85 -0
- package/management-api/v2/dependencies.js +281 -0
- package/management-api/v2/dto.js +143 -0
- package/management-api/v2/handlers/authorization.js +94 -0
- package/management-api/v2/handlers/credentials.js +33 -0
- package/management-api/v2/handlers/entities.js +77 -0
- package/management-api/v2/handlers/integrations.js +128 -0
- package/management-api/v2/handlers/proxy.js +34 -0
- package/management-api/v2/request.js +85 -0
- package/management-api/v2/test-support/fake-dependencies.js +103 -0
- package/modules/domain/authorization-session.js +144 -0
- package/modules/entity-scope.js +15 -0
- package/modules/entity-type-registry.js +311 -0
- package/modules/index.js +14 -0
- package/modules/module-factory.js +12 -0
- package/modules/module.js +53 -2
- package/modules/proxy/proxy-errors.js +20 -0
- package/modules/proxy/proxy-path.js +179 -0
- package/modules/proxy/proxy-policy.js +248 -0
- package/modules/repositories/authorization-session-repository-documentdb.js +112 -0
- package/modules/repositories/authorization-session-repository-factory.js +31 -0
- package/modules/repositories/authorization-session-repository-interface.js +47 -0
- package/modules/repositories/authorization-session-repository-prisma.js +87 -0
- package/modules/repositories/entity-not-found.js +18 -0
- package/modules/repositories/module-repository-documentdb.js +51 -6
- package/modules/repositories/module-repository-interface.js +19 -2
- package/modules/repositories/module-repository-mongo.js +67 -74
- package/modules/repositories/module-repository-postgres.js +66 -74
- package/modules/repositories/module-repository.js +2 -1
- package/modules/requester/proxy-request.js +244 -0
- package/modules/requester/requester.js +27 -2
- package/modules/scope-config.js +192 -0
- package/modules/scope-policy.js +112 -0
- package/modules/scope-resolution.js +288 -0
- package/modules/scope-visibility.js +247 -0
- package/modules/tests/doubles/in-memory-authorization-session-repository.js +45 -0
- package/modules/tests/doubles/ownership-matching.js +18 -0
- package/modules/use-cases/authorize-entity-for-user.js +61 -0
- package/modules/use-cases/create-entity-from-credential.js +113 -0
- package/modules/use-cases/entity-operations-for-user.js +162 -0
- package/modules/use-cases/entity-types.js +139 -0
- package/modules/use-cases/execute-entity-proxy-request.js +286 -0
- package/modules/use-cases/get-authorization-requirements.js +100 -0
- package/modules/use-cases/get-entities-for-user.js +26 -13
- package/modules/use-cases/get-entity-module-for-user.js +113 -0
- package/modules/use-cases/get-entity-options-by-id.js +10 -4
- package/modules/use-cases/get-entity-options-by-type.js +1 -1
- package/modules/use-cases/get-module-instance-from-type.js +1 -1
- package/modules/use-cases/get-module.js +10 -4
- package/modules/use-cases/manage-access.js +21 -0
- package/modules/use-cases/multi-step-authorization.js +329 -0
- package/modules/use-cases/process-authorization-callback.js +53 -19
- package/modules/use-cases/refresh-entity-options.js +10 -4
- package/modules/use-cases/test-module-auth.js +10 -4
- package/modules/utils/map-module-dto.js +3 -5
- package/package.json +6 -5
- package/prisma-mongodb/schema.prisma +92 -0
- package/prisma-postgresql/migrations/20261003000000_add_authorization_session/migration.sql +28 -0
- package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +26 -0
- package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +25 -0
- package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +28 -0
- package/prisma-postgresql/schema.prisma +87 -0
- package/providers/index.js +23 -0
- package/providers/resolve-provider.js +93 -0
- package/providers/runtime-detection.js +124 -0
- package/queues/index.js +25 -0
- package/queues/providers/index.js +11 -0
- package/queues/providers/netlify-background-provider.js +150 -0
- package/queues/providers/qstash-queue-provider.js +219 -0
- package/queues/providers/sqs-queue-provider.js +70 -0
- package/queues/queue-client-interface.js +55 -0
- package/queues/queue-message-auth.js +193 -0
- package/queues/queue-provider-factory.js +101 -0
- package/queues/queue-provider.js +65 -0
- package/queues/sqs-queue-client.js +81 -0
- package/types/index.d.ts +2 -0
- package/types/modules/index.d.ts +197 -0
- package/types/user/index.d.ts +134 -0
- package/user/adopter-jwt-config.js +177 -0
- package/user/adopter-jwt-errors.js +59 -0
- package/user/adopter-token-verifier.js +122 -0
- package/user/app-user-helpers.js +97 -0
- package/user/create-user-authentication.js +64 -0
- package/user/membership.js +51 -0
- package/user/repositories/organization-membership-record.js +19 -0
- package/user/repositories/organization-membership-repository-documentdb.js +106 -0
- package/user/repositories/organization-membership-repository-factory.js +42 -0
- package/user/repositories/organization-membership-repository-interface.js +57 -0
- package/user/repositories/organization-membership-repository-mongo.js +13 -0
- package/user/repositories/organization-membership-repository-postgres.js +29 -0
- package/user/repositories/organization-membership-repository-prisma.js +81 -0
- package/user/repositories/user-repository-documentdb.js +27 -2
- package/user/repositories/user-repository-interface.js +18 -0
- package/user/repositories/user-repository-mongo.js +24 -0
- package/user/repositories/user-repository-postgres.js +18 -0
- package/user/tests/doubles/adopter-jwt-fixtures.js +220 -0
- package/user/tests/doubles/test-user-repository.js +1 -1
- package/user/use-cases/authenticate-user.js +7 -1
- package/user/use-cases/ensure-organization-membership.js +92 -0
- package/user/use-cases/find-individual-users-by-organization.js +54 -0
- package/user/use-cases/get-user-from-adopter-jwt.js +95 -124
- package/user/use-cases/get-user-from-bearer-token.js +2 -7
- package/user/use-cases/get-user-from-x-frigg-headers.js +83 -80
- package/user/user.js +64 -1
- package/errors/mapping-already-exists-error.js +0 -14
- package/integrations/use-cases/add-entities-to-integration.js +0 -92
- package/integrations/use-cases/remove-entities-from-integration.js +0 -70
- package/integrations/utils/assert-entity-ids-input.js +0 -42
- package/modules/utils/to-credential-id.js +0 -14
|
@@ -40,13 +40,8 @@ class GetUserFromBearerToken {
|
|
|
40
40
|
throw Boom.unauthorized('Session Token Not Found');
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const tokenUserId = sessionToken.userId;
|
|
44
|
-
if (!tokenUserId) {
|
|
45
|
-
throw Boom.unauthorized('Session Token Has No User');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
43
|
if (this.userConfig.primary === 'organization') {
|
|
49
|
-
const organizationUserData = await this.userRepository.findOrganizationUserById(
|
|
44
|
+
const organizationUserData = await this.userRepository.findOrganizationUserById(sessionToken.user);
|
|
50
45
|
|
|
51
46
|
if (!organizationUserData) {
|
|
52
47
|
throw Boom.unauthorized('Organization User Not Found');
|
|
@@ -62,7 +57,7 @@ class GetUserFromBearerToken {
|
|
|
62
57
|
);
|
|
63
58
|
}
|
|
64
59
|
|
|
65
|
-
const individualUserData = await this.userRepository.findIndividualUserById(
|
|
60
|
+
const individualUserData = await this.userRepository.findIndividualUserById(sessionToken.user);
|
|
66
61
|
|
|
67
62
|
if (!individualUserData) {
|
|
68
63
|
throw Boom.unauthorized('Individual User Not Found');
|
|
@@ -1,10 +1,38 @@
|
|
|
1
1
|
const Boom = require('@hapi/boom');
|
|
2
|
-
const {
|
|
2
|
+
const {
|
|
3
|
+
findAppUsers,
|
|
4
|
+
createMissingAppUsers,
|
|
5
|
+
buildUser,
|
|
6
|
+
wantsIndividual,
|
|
7
|
+
} = require('../app-user-helpers');
|
|
8
|
+
const { getLogger } = require('../../logs');
|
|
9
|
+
|
|
10
|
+
const log = getLogger('frigg.user.x_frigg_headers');
|
|
11
|
+
|
|
12
|
+
const NOT_A_MEMBER_MESSAGE =
|
|
13
|
+
'Not a member: the user identified by x-frigg-appUserId is not a member of the ' +
|
|
14
|
+
'organization identified by x-frigg-appOrgId, and strict membership does not ' +
|
|
15
|
+
'allow these headers to add them.';
|
|
3
16
|
|
|
4
17
|
/**
|
|
5
18
|
* Use case for retrieving or creating a user from x-frigg header identifiers.
|
|
6
19
|
* Supports backend-to-backend API communication using application user IDs.
|
|
7
20
|
*
|
|
21
|
+
* `x-frigg-appOrgId` names the request's active organization (ADR-056 §3.6).
|
|
22
|
+
* A person the organization does not know yet is added to it as a member; the
|
|
23
|
+
* person is never moved out of another organization, and
|
|
24
|
+
* `IndividualUser.organizationId` is set only on a first link.
|
|
25
|
+
*
|
|
26
|
+
* With `user.strictMembership: true` the headers cannot make anyone a member of
|
|
27
|
+
* an organization that already existed: a person who is not already a member
|
|
28
|
+
* (a brand-new `appUserId` included) is refused with 400. The one thing strict
|
|
29
|
+
* mode still allows is naming an organization this request creates, so a first
|
|
30
|
+
* login can bootstrap its own organization. Apps that run strict add people to
|
|
31
|
+
* existing organizations themselves, through the membership repository.
|
|
32
|
+
*
|
|
33
|
+
* `strictUserValidation` is a deprecated alias for `strictMembership`; its
|
|
34
|
+
* meaning changed (it used to reject a mismatched individual/organization pair).
|
|
35
|
+
*
|
|
8
36
|
* @class GetUserFromXFriggHeaders
|
|
9
37
|
*/
|
|
10
38
|
class GetUserFromXFriggHeaders {
|
|
@@ -12,11 +40,22 @@ class GetUserFromXFriggHeaders {
|
|
|
12
40
|
* Creates a new GetUserFromXFriggHeaders instance.
|
|
13
41
|
* @param {Object} params - Configuration parameters.
|
|
14
42
|
* @param {import('../repositories/user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
43
|
+
* @param {import('./ensure-organization-membership').EnsureOrganizationMembership} params.ensureOrganizationMembership
|
|
15
44
|
* @param {Object} params.userConfig - The user config in the app definition.
|
|
16
45
|
*/
|
|
17
|
-
constructor({ userRepository, userConfig }) {
|
|
46
|
+
constructor({ userRepository, ensureOrganizationMembership, userConfig }) {
|
|
18
47
|
this.userRepository = userRepository;
|
|
48
|
+
this.ensureOrganizationMembership = ensureOrganizationMembership;
|
|
19
49
|
this.userConfig = userConfig;
|
|
50
|
+
|
|
51
|
+
const { strictMembership, strictUserValidation } = userConfig ?? {};
|
|
52
|
+
if (strictUserValidation !== undefined && strictMembership === undefined) {
|
|
53
|
+
log.warn(
|
|
54
|
+
'user.strictUserValidation is deprecated and now means strictMembership; rename it',
|
|
55
|
+
{ eventName: `${log.name}.strict_user_validation_deprecated` }
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
this.strict = Boolean(strictMembership ?? strictUserValidation);
|
|
20
59
|
}
|
|
21
60
|
|
|
22
61
|
/**
|
|
@@ -25,7 +64,7 @@ class GetUserFromXFriggHeaders {
|
|
|
25
64
|
* @param {string} [appUserId] - The app user ID from x-frigg-appUserId header.
|
|
26
65
|
* @param {string} [appOrgId] - The app organization ID from x-frigg-appOrgId header.
|
|
27
66
|
* @returns {Promise<import('../user').User>} The authenticated user object.
|
|
28
|
-
* @throws {Boom} 400 Bad Request if neither ID is provided or if
|
|
67
|
+
* @throws {Boom} 400 Bad Request if neither ID is provided or, in strict mode, if the person is not already a member of the existing organization named.
|
|
29
68
|
*/
|
|
30
69
|
async execute(appUserId, appOrgId) {
|
|
31
70
|
// At least one header must be provided
|
|
@@ -36,96 +75,60 @@ class GetUserFromXFriggHeaders {
|
|
|
36
75
|
}
|
|
37
76
|
|
|
38
77
|
// Find users by both IDs if both are provided
|
|
39
|
-
let individualUserData =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
await this.userRepository.findIndividualUserByAppUserId(
|
|
45
|
-
appUserId
|
|
46
|
-
);
|
|
47
|
-
}
|
|
78
|
+
let { individual: individualUserData, organization: organizationUserData } =
|
|
79
|
+
await findAppUsers(this.userRepository, this.userConfig, {
|
|
80
|
+
appUserId,
|
|
81
|
+
appOrgId,
|
|
82
|
+
});
|
|
48
83
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
);
|
|
54
|
-
}
|
|
84
|
+
// Strict mode refuses to make anyone a member of an organization that
|
|
85
|
+
// existed before this request. Only an organization created below is
|
|
86
|
+
// exempt, since it has no members to disagree with.
|
|
87
|
+
const refuseNewMembers = this.strict && Boolean(organizationUserData);
|
|
55
88
|
|
|
56
|
-
//
|
|
89
|
+
// A person who does not exist yet cannot already be a member, so refuse
|
|
90
|
+
// now, before anything is persisted: refusing after creating them would
|
|
91
|
+
// leave an orphan individual behind on every refused request.
|
|
57
92
|
if (
|
|
93
|
+
refuseNewMembers &&
|
|
58
94
|
appUserId &&
|
|
59
|
-
|
|
60
|
-
individualUserData
|
|
61
|
-
organizationUserData
|
|
95
|
+
wantsIndividual(this.userConfig) &&
|
|
96
|
+
!individualUserData
|
|
62
97
|
) {
|
|
63
|
-
|
|
64
|
-
const individualOrgId =
|
|
65
|
-
individualUserData.organizationUser?.toString();
|
|
66
|
-
const expectedOrgId = organizationUserData.id?.toString();
|
|
67
|
-
|
|
68
|
-
if (individualOrgId !== expectedOrgId) {
|
|
69
|
-
// Default behavior: Auto-link disconnected users
|
|
70
|
-
// Opt-in strict mode: Throw error on mismatch
|
|
71
|
-
if (this.userConfig.strictUserValidation) {
|
|
72
|
-
throw Boom.badRequest(
|
|
73
|
-
'User ID mismatch: x-frigg-appUserId and x-frigg-appOrgId refer to different users. ' +
|
|
74
|
-
'Provide only one identifier or ensure they belong to the same user.'
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Auto-link the users
|
|
79
|
-
individualUserData = await this.userRepository.linkIndividualToOrganization(
|
|
80
|
-
individualUserData.id,
|
|
81
|
-
organizationUserData.id
|
|
82
|
-
);
|
|
83
|
-
}
|
|
98
|
+
throw Boom.badRequest(NOT_A_MEMBER_MESSAGE);
|
|
84
99
|
}
|
|
85
100
|
|
|
86
101
|
// Auto-create users independently if they don't exist and are required
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
username: `app-user-${appUserId}`,
|
|
96
|
-
email: `${appUserId}@app.local`,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
!organizationUserData &&
|
|
102
|
-
appOrgId &&
|
|
103
|
-
this.userConfig.organizationUserRequired
|
|
104
|
-
) {
|
|
105
|
-
organizationUserData =
|
|
106
|
-
await this.userRepository.createOrganizationUser({
|
|
107
|
-
appOrgId,
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// Link individual user to newly created org user if individual exists
|
|
111
|
-
if (individualUserData && organizationUserData) {
|
|
112
|
-
individualUserData = await this.userRepository.linkIndividualToOrganization(
|
|
113
|
-
individualUserData.id,
|
|
114
|
-
organizationUserData.id
|
|
115
|
-
);
|
|
102
|
+
const created = await createMissingAppUsers(
|
|
103
|
+
this.userRepository,
|
|
104
|
+
this.userConfig,
|
|
105
|
+
{
|
|
106
|
+
appUserId,
|
|
107
|
+
appOrgId,
|
|
108
|
+
individual: individualUserData,
|
|
109
|
+
organization: organizationUserData,
|
|
116
110
|
}
|
|
111
|
+
);
|
|
112
|
+
individualUserData = created.individual;
|
|
113
|
+
organizationUserData = created.organization;
|
|
114
|
+
|
|
115
|
+
// The organization named by the header is the active one: join it
|
|
116
|
+
// rather than re-linking the person's single organizationId.
|
|
117
|
+
const ensured = await this.ensureOrganizationMembership.execute({
|
|
118
|
+
individual: individualUserData,
|
|
119
|
+
organization: organizationUserData,
|
|
120
|
+
requireExisting: refuseNewMembers,
|
|
121
|
+
});
|
|
122
|
+
if (refuseNewMembers && individualUserData && !ensured.membership) {
|
|
123
|
+
throw Boom.badRequest(NOT_A_MEMBER_MESSAGE);
|
|
117
124
|
}
|
|
125
|
+
individualUserData = ensured.individual;
|
|
118
126
|
|
|
119
|
-
|
|
127
|
+
return buildUser(
|
|
128
|
+
this.userConfig,
|
|
120
129
|
individualUserData,
|
|
121
|
-
organizationUserData
|
|
122
|
-
this.userConfig.usePassword,
|
|
123
|
-
this.userConfig.primary,
|
|
124
|
-
this.userConfig.individualUserRequired,
|
|
125
|
-
this.userConfig.organizationUserRequired
|
|
130
|
+
organizationUserData
|
|
126
131
|
);
|
|
127
|
-
|
|
128
|
-
return user;
|
|
129
132
|
}
|
|
130
133
|
}
|
|
131
134
|
|
package/user/user.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
const bcrypt = require('bcryptjs');
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* How the request that produced a `User` proved who it is. Set by
|
|
5
|
+
* AuthenticateUser, never inferred from a missing value. Organization roles
|
|
6
|
+
* (ADR-056 §3, §3.6) depend on it: a shared-secret caller is the app's own
|
|
7
|
+
* backend and is trusted to manage; a Frigg bearer token keeps its pre-roles
|
|
8
|
+
* behaviour unless the app opts in (`user.orgAdminGateBearerTokens`); an
|
|
9
|
+
* adopter token has the role it carries, else the stored membership role,
|
|
10
|
+
* else `MEMBER`.
|
|
11
|
+
*/
|
|
12
|
+
const AUTH_SOURCES = Object.freeze({
|
|
13
|
+
SHARED_SECRET: 'shared-secret',
|
|
14
|
+
ADOPTER_JWT: 'adopter-jwt',
|
|
15
|
+
BEARER_TOKEN: 'bearer-token',
|
|
16
|
+
});
|
|
17
|
+
|
|
3
18
|
/**
|
|
4
19
|
* Represents a user in the system. The User class is a domain entity,
|
|
5
20
|
* @class User
|
|
@@ -18,6 +33,14 @@ class User {
|
|
|
18
33
|
this.individualUser = individualUser;
|
|
19
34
|
this.organizationUser = organizationUser;
|
|
20
35
|
this.usePassword = usePassword;
|
|
36
|
+
// Organization role from an adopter token claim (ADR-056 §2), per
|
|
37
|
+
// request. When present it wins over the stored membership role.
|
|
38
|
+
this.orgRole = null;
|
|
39
|
+
// The role stored on the person's membership of the active organization
|
|
40
|
+
// (OrganizationMembership.orgRole); used when the token carries none.
|
|
41
|
+
this.membershipRole = null;
|
|
42
|
+
// How this request authenticated (one of AUTH_SOURCES); null until AuthenticateUser says.
|
|
43
|
+
this.authSource = null;
|
|
21
44
|
|
|
22
45
|
this.config = {
|
|
23
46
|
primary,
|
|
@@ -26,6 +49,17 @@ class User {
|
|
|
26
49
|
};
|
|
27
50
|
}
|
|
28
51
|
|
|
52
|
+
/**
|
|
53
|
+
* The organization this request acts in (ADR-056 §3.6): the one the token or
|
|
54
|
+
* `x-frigg-appOrgId` named, not the person's legacy `organizationId`
|
|
55
|
+
* pointer. Null when the request has no organization.
|
|
56
|
+
* @returns {string|null}
|
|
57
|
+
*/
|
|
58
|
+
getActiveOrganizationId() {
|
|
59
|
+
const id = this.organizationUser?.id;
|
|
60
|
+
return id === undefined || id === null || id === '' ? null : String(id);
|
|
61
|
+
}
|
|
62
|
+
|
|
29
63
|
getPrimaryUser() {
|
|
30
64
|
if (this.config.primary === 'organization') {
|
|
31
65
|
return this.organizationUser;
|
|
@@ -33,6 +67,11 @@ class User {
|
|
|
33
67
|
return this.individualUser;
|
|
34
68
|
}
|
|
35
69
|
|
|
70
|
+
/**
|
|
71
|
+
* The id of the primary user. With `primary: 'organization'` this is the
|
|
72
|
+
* active organization (the one the request named), never the organization
|
|
73
|
+
* the person was first linked to.
|
|
74
|
+
*/
|
|
36
75
|
getId() {
|
|
37
76
|
return this.getPrimaryUser()?.id;
|
|
38
77
|
}
|
|
@@ -65,6 +104,30 @@ class User {
|
|
|
65
104
|
return this.config.individualUserRequired;
|
|
66
105
|
}
|
|
67
106
|
|
|
107
|
+
getAuthSource() {
|
|
108
|
+
return this.authSource;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
setAuthSource(authSource) {
|
|
112
|
+
this.authSource = authSource ?? null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
getOrgRole() {
|
|
116
|
+
return this.orgRole;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
setOrgRole(orgRole) {
|
|
120
|
+
this.orgRole = orgRole ?? null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
getMembershipRole() {
|
|
124
|
+
return this.membershipRole;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
setMembershipRole(membershipRole) {
|
|
128
|
+
this.membershipRole = membershipRole ?? null;
|
|
129
|
+
}
|
|
130
|
+
|
|
68
131
|
getIndividualUser() {
|
|
69
132
|
return this.individualUser;
|
|
70
133
|
}
|
|
@@ -122,4 +185,4 @@ class User {
|
|
|
122
185
|
}
|
|
123
186
|
}
|
|
124
187
|
|
|
125
|
-
module.exports = { User };
|
|
188
|
+
module.exports = { User, AUTH_SOURCES };
|
|
@@ -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 };
|
|
@@ -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 };
|