@friggframework/core 2.0.0-next.41 → 2.0.0-next.42
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 +693 -0
- package/README.md +931 -50
- package/application/commands/README.md +421 -0
- package/application/commands/credential-commands.js +224 -0
- package/application/commands/entity-commands.js +315 -0
- package/application/commands/integration-commands.js +160 -0
- package/application/commands/integration-commands.test.js +123 -0
- package/application/commands/user-commands.js +213 -0
- package/application/index.js +69 -0
- package/core/CLAUDE.md +690 -0
- package/core/create-handler.js +0 -6
- package/credential/repositories/credential-repository-factory.js +47 -0
- package/credential/repositories/credential-repository-interface.js +98 -0
- package/credential/repositories/credential-repository-mongo.js +301 -0
- package/credential/repositories/credential-repository-postgres.js +307 -0
- package/credential/repositories/credential-repository.js +307 -0
- package/credential/use-cases/get-credential-for-user.js +21 -0
- package/credential/use-cases/update-authentication-status.js +15 -0
- package/database/config.js +117 -0
- package/database/encryption/README.md +683 -0
- package/database/encryption/encryption-integration.test.js +553 -0
- package/database/encryption/encryption-schema-registry.js +141 -0
- package/database/encryption/encryption-schema-registry.test.js +392 -0
- package/database/encryption/field-encryption-service.js +226 -0
- package/database/encryption/field-encryption-service.test.js +525 -0
- package/database/encryption/logger.js +79 -0
- package/database/encryption/mongo-decryption-fix-verification.test.js +348 -0
- package/database/encryption/postgres-decryption-fix-verification.test.js +371 -0
- package/database/encryption/postgres-relation-decryption.test.js +245 -0
- package/database/encryption/prisma-encryption-extension.js +222 -0
- package/database/encryption/prisma-encryption-extension.test.js +439 -0
- package/database/index.js +25 -12
- package/database/models/readme.md +1 -0
- package/database/prisma.js +162 -0
- package/database/repositories/health-check-repository-factory.js +38 -0
- package/database/repositories/health-check-repository-interface.js +86 -0
- package/database/repositories/health-check-repository-mongodb.js +72 -0
- package/database/repositories/health-check-repository-postgres.js +75 -0
- package/database/repositories/health-check-repository.js +108 -0
- package/database/use-cases/check-database-health-use-case.js +34 -0
- package/database/use-cases/check-encryption-health-use-case.js +82 -0
- package/database/use-cases/test-encryption-use-case.js +252 -0
- package/encrypt/Cryptor.js +20 -152
- package/encrypt/index.js +1 -2
- package/encrypt/test-encrypt.js +0 -2
- package/handlers/app-definition-loader.js +38 -0
- package/handlers/app-handler-helpers.js +0 -3
- package/handlers/auth-flow.integration.test.js +147 -0
- package/handlers/backend-utils.js +25 -45
- package/handlers/integration-event-dispatcher.js +54 -0
- package/handlers/integration-event-dispatcher.test.js +141 -0
- package/handlers/routers/HEALTHCHECK.md +103 -1
- package/handlers/routers/auth.js +3 -14
- package/handlers/routers/health.js +63 -424
- package/handlers/routers/health.test.js +7 -0
- package/handlers/routers/integration-defined-routers.js +8 -5
- package/handlers/routers/user.js +25 -5
- package/handlers/routers/websocket.js +5 -3
- package/handlers/use-cases/check-external-apis-health-use-case.js +81 -0
- package/handlers/use-cases/check-integrations-health-use-case.js +32 -0
- package/handlers/workers/integration-defined-workers.js +6 -3
- package/index.js +45 -22
- package/integrations/index.js +12 -10
- package/integrations/integration-base.js +224 -53
- package/integrations/integration-router.js +386 -178
- package/integrations/options.js +1 -1
- package/integrations/repositories/integration-mapping-repository-factory.js +50 -0
- package/integrations/repositories/integration-mapping-repository-interface.js +106 -0
- package/integrations/repositories/integration-mapping-repository-mongo.js +161 -0
- package/integrations/repositories/integration-mapping-repository-postgres.js +227 -0
- package/integrations/repositories/integration-mapping-repository.js +156 -0
- package/integrations/repositories/integration-repository-factory.js +44 -0
- package/integrations/repositories/integration-repository-interface.js +115 -0
- package/integrations/repositories/integration-repository-mongo.js +271 -0
- package/integrations/repositories/integration-repository-postgres.js +319 -0
- package/integrations/tests/doubles/dummy-integration-class.js +90 -0
- package/integrations/tests/doubles/test-integration-repository.js +99 -0
- package/integrations/tests/use-cases/create-integration.test.js +131 -0
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +150 -0
- package/integrations/tests/use-cases/find-integration-context-by-external-entity-id.test.js +92 -0
- package/integrations/tests/use-cases/get-integration-for-user.test.js +150 -0
- package/integrations/tests/use-cases/get-integration-instance.test.js +176 -0
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +176 -0
- package/integrations/tests/use-cases/get-possible-integrations.test.js +188 -0
- package/integrations/tests/use-cases/update-integration-messages.test.js +142 -0
- package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
- package/integrations/tests/use-cases/update-integration.test.js +141 -0
- package/integrations/use-cases/create-integration.js +83 -0
- package/integrations/use-cases/delete-integration-for-user.js +73 -0
- package/integrations/use-cases/find-integration-context-by-external-entity-id.js +72 -0
- package/integrations/use-cases/get-integration-for-user.js +78 -0
- package/integrations/use-cases/get-integration-instance-by-definition.js +67 -0
- package/integrations/use-cases/get-integration-instance.js +83 -0
- package/integrations/use-cases/get-integrations-for-user.js +87 -0
- package/integrations/use-cases/get-possible-integrations.js +27 -0
- package/integrations/use-cases/index.js +11 -0
- package/integrations/use-cases/load-integration-context-full.test.js +329 -0
- package/integrations/use-cases/load-integration-context.js +71 -0
- package/integrations/use-cases/load-integration-context.test.js +114 -0
- package/integrations/use-cases/update-integration-messages.js +44 -0
- package/integrations/use-cases/update-integration-status.js +32 -0
- package/integrations/use-cases/update-integration.js +93 -0
- package/integrations/utils/map-integration-dto.js +36 -0
- package/jest-global-setup-noop.js +3 -0
- package/jest-global-teardown-noop.js +3 -0
- package/{module-plugin → modules}/entity.js +1 -0
- package/{module-plugin → modules}/index.js +0 -8
- package/modules/module-factory.js +56 -0
- package/modules/module-hydration.test.js +205 -0
- package/modules/module.js +221 -0
- package/modules/repositories/module-repository-factory.js +33 -0
- package/modules/repositories/module-repository-interface.js +129 -0
- package/modules/repositories/module-repository-mongo.js +386 -0
- package/modules/repositories/module-repository-postgres.js +437 -0
- package/modules/repositories/module-repository.js +327 -0
- package/{module-plugin → modules}/test/mock-api/api.js +8 -3
- package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
- package/modules/tests/doubles/test-module-factory.js +16 -0
- package/modules/tests/doubles/test-module-repository.js +39 -0
- package/modules/use-cases/get-entities-for-user.js +32 -0
- package/modules/use-cases/get-entity-options-by-id.js +59 -0
- package/modules/use-cases/get-entity-options-by-type.js +34 -0
- package/modules/use-cases/get-module-instance-from-type.js +31 -0
- package/modules/use-cases/get-module.js +56 -0
- package/modules/use-cases/process-authorization-callback.js +121 -0
- package/modules/use-cases/refresh-entity-options.js +59 -0
- package/modules/use-cases/test-module-auth.js +55 -0
- package/modules/utils/map-module-dto.js +18 -0
- package/package.json +14 -6
- package/prisma-mongodb/schema.prisma +321 -0
- package/prisma-postgresql/migrations/20250930193005_init/migration.sql +315 -0
- package/prisma-postgresql/migrations/20251006135218_init/migration.sql +9 -0
- package/prisma-postgresql/migrations/migration_lock.toml +3 -0
- package/prisma-postgresql/schema.prisma +303 -0
- package/syncs/manager.js +468 -443
- package/syncs/repositories/sync-repository-factory.js +38 -0
- package/syncs/repositories/sync-repository-interface.js +109 -0
- package/syncs/repositories/sync-repository-mongo.js +239 -0
- package/syncs/repositories/sync-repository-postgres.js +319 -0
- package/syncs/sync.js +0 -1
- package/token/repositories/token-repository-factory.js +33 -0
- package/token/repositories/token-repository-interface.js +131 -0
- package/token/repositories/token-repository-mongo.js +212 -0
- package/token/repositories/token-repository-postgres.js +257 -0
- package/token/repositories/token-repository.js +219 -0
- package/types/integrations/index.d.ts +2 -6
- package/types/module-plugin/index.d.ts +5 -57
- package/types/syncs/index.d.ts +0 -2
- package/user/repositories/user-repository-factory.js +46 -0
- package/user/repositories/user-repository-interface.js +198 -0
- package/user/repositories/user-repository-mongo.js +250 -0
- package/user/repositories/user-repository-postgres.js +311 -0
- package/user/tests/doubles/test-user-repository.js +72 -0
- package/user/tests/use-cases/create-individual-user.test.js +24 -0
- package/user/tests/use-cases/create-organization-user.test.js +28 -0
- package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
- package/user/tests/use-cases/login-user.test.js +140 -0
- package/user/use-cases/create-individual-user.js +61 -0
- package/user/use-cases/create-organization-user.js +47 -0
- package/user/use-cases/create-token-for-user-id.js +30 -0
- package/user/use-cases/get-user-from-bearer-token.js +77 -0
- package/user/use-cases/login-user.js +122 -0
- package/user/user.js +77 -0
- package/websocket/repositories/websocket-connection-repository-factory.js +37 -0
- package/websocket/repositories/websocket-connection-repository-interface.js +106 -0
- package/websocket/repositories/websocket-connection-repository-mongo.js +155 -0
- package/websocket/repositories/websocket-connection-repository-postgres.js +195 -0
- package/websocket/repositories/websocket-connection-repository.js +160 -0
- package/database/models/State.js +0 -9
- package/database/models/Token.js +0 -70
- package/database/mongo.js +0 -171
- package/encrypt/Cryptor.test.js +0 -32
- package/encrypt/encrypt.js +0 -104
- package/encrypt/encrypt.test.js +0 -1069
- package/handlers/routers/middleware/loadUser.js +0 -15
- package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
- package/integrations/create-frigg-backend.js +0 -31
- package/integrations/integration-factory.js +0 -251
- package/integrations/integration-mapping.js +0 -43
- package/integrations/integration-model.js +0 -46
- package/integrations/integration-user.js +0 -144
- package/integrations/test/integration-base.test.js +0 -144
- package/module-plugin/auther.js +0 -393
- package/module-plugin/credential.js +0 -22
- package/module-plugin/entity-manager.js +0 -70
- package/module-plugin/manager.js +0 -169
- package/module-plugin/module-factory.js +0 -61
- package/module-plugin/test/auther.test.js +0 -97
- /package/{module-plugin → modules}/ModuleConstants.js +0 -0
- /package/{module-plugin → modules}/requester/api-key.js +0 -0
- /package/{module-plugin → modules}/requester/basic.js +0 -0
- /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
- /package/{module-plugin → modules}/requester/requester.js +0 -0
- /package/{module-plugin → modules}/requester/requester.test.js +0 -0
- /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const {
|
|
2
|
+
GetUserFromBearerToken,
|
|
3
|
+
} = require('../../use-cases/get-user-from-bearer-token');
|
|
4
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
5
|
+
|
|
6
|
+
describe('GetUserFromBearerToken Use Case', () => {
|
|
7
|
+
let userRepository;
|
|
8
|
+
let getUserFromBearerToken;
|
|
9
|
+
let userConfig;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
userConfig = {
|
|
13
|
+
usePassword: true,
|
|
14
|
+
primary: 'individual',
|
|
15
|
+
individualUserRequired: true,
|
|
16
|
+
organizationUserRequired: false,
|
|
17
|
+
};
|
|
18
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
19
|
+
getUserFromBearerToken = new GetUserFromBearerToken({
|
|
20
|
+
userRepository,
|
|
21
|
+
userConfig
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should retrieve a user for a valid bearer token', async () => {
|
|
26
|
+
const userId = 'user-123';
|
|
27
|
+
const token = await userRepository.createToken(userId);
|
|
28
|
+
const createdUserData = await userRepository.createIndividualUser({
|
|
29
|
+
id: userId,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const user = await getUserFromBearerToken.execute(`Bearer ${token}`);
|
|
33
|
+
|
|
34
|
+
expect(user).toBeDefined();
|
|
35
|
+
expect(user.getId()).toBe(createdUserData.id);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should throw an unauthorized error if the bearer token is missing', async () => {
|
|
39
|
+
await expect(getUserFromBearerToken.execute(null)).rejects.toThrow(
|
|
40
|
+
'Missing Authorization Header'
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should throw an unauthorized error for an invalid token format', async () => {
|
|
45
|
+
await expect(
|
|
46
|
+
getUserFromBearerToken.execute('InvalidToken')
|
|
47
|
+
).rejects.toThrow('Invalid Token Format');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should throw an unauthorized error if the Session Token is not found', async () => {
|
|
51
|
+
userRepository.getSessionToken = jest.fn().mockResolvedValue(null);
|
|
52
|
+
await expect(
|
|
53
|
+
getUserFromBearerToken.execute('Bearer invalid-token')
|
|
54
|
+
).rejects.toThrow('Session Token Not Found');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should throw an unauthorized error if the token is valid but finds no user', async () => {
|
|
58
|
+
userRepository.getSessionToken = jest.fn().mockResolvedValue(null);
|
|
59
|
+
const token = await userRepository.createToken('user-dne');
|
|
60
|
+
await expect(
|
|
61
|
+
getUserFromBearerToken.execute(`Bearer ${token}`)
|
|
62
|
+
).rejects.toThrow('Session Token Not Found');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const bcrypt = require('bcryptjs');
|
|
2
|
+
const { LoginUser } = require('../../use-cases/login-user');
|
|
3
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
4
|
+
|
|
5
|
+
jest.mock('bcryptjs', () => ({
|
|
6
|
+
compareSync: jest.fn(),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
describe('LoginUser Use Case', () => {
|
|
10
|
+
let userRepository;
|
|
11
|
+
let loginUser;
|
|
12
|
+
let userConfig;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
userConfig = { usePassword: true, individualUserRequired: true, organizationUserRequired: false };
|
|
16
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
17
|
+
loginUser = new LoginUser({ userRepository, userConfig });
|
|
18
|
+
|
|
19
|
+
bcrypt.compareSync.mockClear();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('With Password Authentication', () => {
|
|
23
|
+
it('should successfully log in a user with correct credentials', async () => {
|
|
24
|
+
const username = 'test-user';
|
|
25
|
+
const password = 'password123';
|
|
26
|
+
await userRepository.createIndividualUser({
|
|
27
|
+
username,
|
|
28
|
+
hashword: 'hashed-password',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
bcrypt.compareSync.mockReturnValue(true);
|
|
32
|
+
|
|
33
|
+
const user = await loginUser.execute({ username, password });
|
|
34
|
+
|
|
35
|
+
expect(bcrypt.compareSync).toHaveBeenCalledWith(
|
|
36
|
+
password,
|
|
37
|
+
'hashed-password'
|
|
38
|
+
);
|
|
39
|
+
expect(user).toBeDefined();
|
|
40
|
+
expect(user.getIndividualUser().username).toBe(username);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should throw an unauthorized error for an incorrect password', async () => {
|
|
44
|
+
const username = 'test-user';
|
|
45
|
+
const password = 'wrong-password';
|
|
46
|
+
await userRepository.createIndividualUser({
|
|
47
|
+
username,
|
|
48
|
+
hashword: 'hashed-password',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
bcrypt.compareSync.mockReturnValue(false);
|
|
52
|
+
|
|
53
|
+
await expect(
|
|
54
|
+
loginUser.execute({ username, password })
|
|
55
|
+
).rejects.toThrow('Incorrect username or password');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should throw an unauthorized error for a non-existent user', async () => {
|
|
59
|
+
const username = 'non-existent-user';
|
|
60
|
+
const password = 'password123';
|
|
61
|
+
|
|
62
|
+
await expect(
|
|
63
|
+
loginUser.execute({ username, password })
|
|
64
|
+
).rejects.toThrow('user not found');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('Without Password (appUserId)', () => {
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
userConfig = { usePassword: false, individualUserRequired: true, organizationUserRequired: false };
|
|
71
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
72
|
+
loginUser = new LoginUser({
|
|
73
|
+
userRepository,
|
|
74
|
+
userConfig,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should successfully retrieve a user by appUserId', async () => {
|
|
79
|
+
const appUserId = 'app-user-123';
|
|
80
|
+
const createdUserData = await userRepository.createIndividualUser({
|
|
81
|
+
appUserId,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const result = await loginUser.execute({ appUserId });
|
|
85
|
+
expect(result.getId()).toBe(createdUserData.id);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('With Organization User', () => {
|
|
90
|
+
beforeEach(() => {
|
|
91
|
+
userConfig = {
|
|
92
|
+
primary: 'organization',
|
|
93
|
+
individualUserRequired: false,
|
|
94
|
+
organizationUserRequired: true,
|
|
95
|
+
};
|
|
96
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
97
|
+
loginUser = new LoginUser({
|
|
98
|
+
userRepository,
|
|
99
|
+
userConfig,
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should successfully retrieve an organization user by appOrgId', async () => {
|
|
104
|
+
const appOrgId = 'app-org-123';
|
|
105
|
+
const createdUserData = await userRepository.createOrganizationUser({
|
|
106
|
+
name: 'Test Org',
|
|
107
|
+
appOrgId,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const result = await loginUser.execute({ appOrgId });
|
|
111
|
+
expect(result.getId()).toBe(createdUserData.id);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should throw an unauthorized error for a non-existent organization user', async () => {
|
|
115
|
+
const appOrgId = 'non-existent-org';
|
|
116
|
+
|
|
117
|
+
await expect(loginUser.execute({ appOrgId })).rejects.toThrow(
|
|
118
|
+
'org user non-existent-org not found'
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('Required User Checks', () => {
|
|
124
|
+
it('should throw an error if a required individual user is not found', async () => {
|
|
125
|
+
userConfig = {
|
|
126
|
+
individualUserRequired: true,
|
|
127
|
+
usePassword: false,
|
|
128
|
+
};
|
|
129
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
130
|
+
loginUser = new LoginUser({
|
|
131
|
+
userRepository,
|
|
132
|
+
userConfig,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
await expect(
|
|
136
|
+
loginUser.execute({ appUserId: 'a-non-existent-user-id' })
|
|
137
|
+
).rejects.toThrow('user not found');
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const { get } = require('../../assertions');
|
|
2
|
+
const Boom = require('@hapi/boom');
|
|
3
|
+
const { User } = require('../user');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Use case for creating an individual user.
|
|
7
|
+
* @class CreateIndividualUser
|
|
8
|
+
*/
|
|
9
|
+
class CreateIndividualUser {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new CreateIndividualUser instance.
|
|
12
|
+
* @param {Object} params - Configuration parameters.
|
|
13
|
+
* @param {import('../user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
14
|
+
* @param {Object} params.userConfig - The user properties inside of the app definition.
|
|
15
|
+
*/
|
|
16
|
+
constructor({ userRepository, userConfig }) {
|
|
17
|
+
this.userRepository = userRepository;
|
|
18
|
+
this.userConfig = userConfig;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Executes the use case.
|
|
23
|
+
* @async
|
|
24
|
+
* @param {Object} params - The parameters for creating the user.
|
|
25
|
+
* @returns {Promise<import('../user').User>} The newly created user object.
|
|
26
|
+
*/
|
|
27
|
+
async execute(params) {
|
|
28
|
+
let hashword;
|
|
29
|
+
if (this.userConfig.usePassword) {
|
|
30
|
+
hashword = get(params, 'password');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const email = get(params, 'email', null);
|
|
34
|
+
const username = get(params, 'username', null);
|
|
35
|
+
if (!email && !username) {
|
|
36
|
+
throw Boom.badRequest('email or username is required');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const appUserId = get(params, 'appUserId', null);
|
|
40
|
+
const organizationUserId = get(params, 'organizationUserId', null);
|
|
41
|
+
|
|
42
|
+
const individualUserData = await this.userRepository.createIndividualUser({
|
|
43
|
+
email,
|
|
44
|
+
username,
|
|
45
|
+
hashword,
|
|
46
|
+
appUserId,
|
|
47
|
+
organizationUser: organizationUserId,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return new User(
|
|
51
|
+
individualUserData,
|
|
52
|
+
null,
|
|
53
|
+
this.userConfig.usePassword,
|
|
54
|
+
this.userConfig.primary,
|
|
55
|
+
this.userConfig.individualUserRequired,
|
|
56
|
+
this.userConfig.organizationUserRequired
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { CreateIndividualUser };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const { get } = require('../../assertions');
|
|
2
|
+
const { User } = require('../user');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Use case for creating an organization user.
|
|
6
|
+
* @class CreateOrganizationUser
|
|
7
|
+
*/
|
|
8
|
+
class CreateOrganizationUser {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new CreateOrganizationUser instance.
|
|
11
|
+
* @param {Object} params - Configuration parameters.
|
|
12
|
+
* @param {import('../user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
13
|
+
* @param {Object} params.userConfig - The user properties inside of the app definition.
|
|
14
|
+
*/
|
|
15
|
+
constructor({ userRepository, userConfig }) {
|
|
16
|
+
this.userRepository = userRepository;
|
|
17
|
+
this.userConfig = userConfig;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Executes the use case.
|
|
22
|
+
* @async
|
|
23
|
+
* @param {Object} params - The parameters for creating the user.
|
|
24
|
+
* @returns {Promise<import('../user').User>} The newly created user object.
|
|
25
|
+
*/
|
|
26
|
+
async execute(params) {
|
|
27
|
+
const name = get(params, 'name');
|
|
28
|
+
const appOrgId = get(params, 'appOrgId');
|
|
29
|
+
|
|
30
|
+
const organizationUserData =
|
|
31
|
+
await this.userRepository.createOrganizationUser({
|
|
32
|
+
name,
|
|
33
|
+
appOrgId,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return new User(
|
|
37
|
+
null,
|
|
38
|
+
organizationUserData,
|
|
39
|
+
this.userConfig.usePassword,
|
|
40
|
+
this.userConfig.primary,
|
|
41
|
+
this.userConfig.individualUserRequired,
|
|
42
|
+
this.userConfig.organizationUserRequired
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = { CreateOrganizationUser };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Use case for creating a token for a user ID.
|
|
5
|
+
* @class CreateTokenForUserId
|
|
6
|
+
*/
|
|
7
|
+
class CreateTokenForUserId {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new CreateTokenForUserId instance.
|
|
10
|
+
* @param {Object} params - Configuration parameters.
|
|
11
|
+
* @param {import('../user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
12
|
+
*/
|
|
13
|
+
constructor({ userRepository }) {
|
|
14
|
+
this.userRepository = userRepository;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Executes the use case.
|
|
19
|
+
* @async
|
|
20
|
+
* @param {string} userId - The ID of the user to create a token for.
|
|
21
|
+
* @param {number} minutes - The number of minutes until the token expires.
|
|
22
|
+
* @returns {Promise<string>} The user token.
|
|
23
|
+
*/
|
|
24
|
+
async execute(userId, minutes) {
|
|
25
|
+
const rawToken = crypto.randomBytes(20).toString('hex');
|
|
26
|
+
return this.userRepository.createToken(userId, rawToken, minutes);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = { CreateTokenForUserId };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const Boom = require('@hapi/boom');
|
|
2
|
+
const { User } = require('../user');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Use case for retrieving a user from a bearer token.
|
|
6
|
+
* @class GetUserFromBearerToken
|
|
7
|
+
*/
|
|
8
|
+
class GetUserFromBearerToken {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new GetUserFromBearerToken instance.
|
|
11
|
+
* @param {Object} params - Configuration parameters.
|
|
12
|
+
* @param {import('../user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
13
|
+
* @param {Object} params.userConfig - The user config in the app definition.
|
|
14
|
+
*/
|
|
15
|
+
constructor({ userRepository, userConfig }) {
|
|
16
|
+
this.userRepository = userRepository;
|
|
17
|
+
this.userConfig = userConfig;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Executes the use case.
|
|
22
|
+
* @async
|
|
23
|
+
* @param {string} bearerToken - The bearer token from the authorization header.
|
|
24
|
+
* @returns {Promise<import('../user').User>} The authenticated user object.
|
|
25
|
+
* @throws {Boom} 401 Unauthorized if the token is missing, malformed, or invalid.
|
|
26
|
+
*/
|
|
27
|
+
async execute(bearerToken) {
|
|
28
|
+
if (!bearerToken) {
|
|
29
|
+
throw Boom.unauthorized('Missing Authorization Header');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const token = bearerToken.split(' ')[1]?.trim();
|
|
33
|
+
if (!token) {
|
|
34
|
+
throw Boom.unauthorized('Invalid Token Format');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const sessionToken = await this.userRepository.getSessionToken(token);
|
|
38
|
+
|
|
39
|
+
if (!sessionToken) {
|
|
40
|
+
throw Boom.unauthorized('Session Token Not Found');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (this.userConfig.primary === 'organization') {
|
|
44
|
+
const organizationUserData = await this.userRepository.findOrganizationUserById(sessionToken.user);
|
|
45
|
+
|
|
46
|
+
if (!organizationUserData) {
|
|
47
|
+
throw Boom.unauthorized('Organization User Not Found');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return new User(
|
|
51
|
+
null,
|
|
52
|
+
organizationUserData,
|
|
53
|
+
this.userConfig.usePassword,
|
|
54
|
+
this.userConfig.primary,
|
|
55
|
+
this.userConfig.individualUserRequired,
|
|
56
|
+
this.userConfig.organizationUserRequired
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const individualUserData = await this.userRepository.findIndividualUserById(sessionToken.user);
|
|
61
|
+
|
|
62
|
+
if (!individualUserData) {
|
|
63
|
+
throw Boom.unauthorized('Individual User Not Found');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return new User(
|
|
67
|
+
individualUserData,
|
|
68
|
+
null,
|
|
69
|
+
this.userConfig.usePassword,
|
|
70
|
+
this.userConfig.primary,
|
|
71
|
+
this.userConfig.individualUserRequired,
|
|
72
|
+
this.userConfig.organizationUserRequired
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
module.exports = { GetUserFromBearerToken };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
const Boom = require('@hapi/boom');
|
|
2
|
+
const {
|
|
3
|
+
RequiredPropertyError,
|
|
4
|
+
} = require('../../errors');
|
|
5
|
+
const { User } = require('../user');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Use case for logging in a user.
|
|
9
|
+
* @class LoginUser
|
|
10
|
+
*/
|
|
11
|
+
class LoginUser {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new LoginUser instance.
|
|
14
|
+
* @param {Object} params - Configuration parameters.
|
|
15
|
+
* @param {import('../user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
16
|
+
* @param {Object} params.userConfig - The user properties inside of the app definition.
|
|
17
|
+
*/
|
|
18
|
+
constructor({ userRepository, userConfig }) {
|
|
19
|
+
this.userRepository = userRepository;
|
|
20
|
+
this.userConfig = userConfig;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Executes the use case.
|
|
25
|
+
* @async
|
|
26
|
+
* @param {Object} userCredentials - The user's credentials for authentication.
|
|
27
|
+
* @param {string} [userCredentials.username] - The username for authentication.
|
|
28
|
+
* @param {string} [userCredentials.password] - The password for authentication.
|
|
29
|
+
* @param {string} [userCredentials.appUserId] - The app user id for authentication if no username and password are provided.
|
|
30
|
+
* @param {string} [userCredentials.appOrgId] - The app organization id for authentication if no username and password are provided.
|
|
31
|
+
* @returns {Promise<import('../user').User>} The authenticated user object.
|
|
32
|
+
*/
|
|
33
|
+
async execute(userCredentials) {
|
|
34
|
+
const { username, password, appUserId, appOrgId } = userCredentials;
|
|
35
|
+
if (this.userConfig.individualUserRequired) {
|
|
36
|
+
if (this.userConfig.usePassword) {
|
|
37
|
+
if (!username) {
|
|
38
|
+
throw new RequiredPropertyError({
|
|
39
|
+
parent: this,
|
|
40
|
+
key: 'username',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (!password) {
|
|
44
|
+
throw new RequiredPropertyError({
|
|
45
|
+
parent: this,
|
|
46
|
+
key: 'password',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const individualUserData =
|
|
51
|
+
await this.userRepository.findIndividualUserByUsername(
|
|
52
|
+
username
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (!individualUserData) {
|
|
56
|
+
throw Boom.unauthorized('user not found');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const individualUser = new User(
|
|
60
|
+
individualUserData,
|
|
61
|
+
null,
|
|
62
|
+
this.userConfig.usePassword,
|
|
63
|
+
this.userConfig.primary,
|
|
64
|
+
this.userConfig.individualUserRequired,
|
|
65
|
+
this.userConfig.organizationUserRequired
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (!individualUser.isPasswordValid(password)) {
|
|
69
|
+
throw Boom.unauthorized('Incorrect username or password');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return individualUser;
|
|
73
|
+
} else {
|
|
74
|
+
const individualUserData =
|
|
75
|
+
await this.userRepository.findIndividualUserByAppUserId(
|
|
76
|
+
appUserId
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (!individualUserData) {
|
|
80
|
+
throw Boom.unauthorized('user not found');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const individualUser = new User(
|
|
84
|
+
individualUserData,
|
|
85
|
+
null,
|
|
86
|
+
this.userConfig.usePassword,
|
|
87
|
+
this.userConfig.primary,
|
|
88
|
+
this.userConfig.individualUserRequired,
|
|
89
|
+
this.userConfig.organizationUserRequired
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return individualUser;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if (this.userConfig.organizationUserRequired) {
|
|
98
|
+
|
|
99
|
+
const organizationUserData =
|
|
100
|
+
await this.userRepository.findOrganizationUserByAppOrgId(appOrgId);
|
|
101
|
+
|
|
102
|
+
if (!organizationUserData) {
|
|
103
|
+
throw Boom.unauthorized(`org user ${appOrgId} not found`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const organizationUser = new User(
|
|
107
|
+
null,
|
|
108
|
+
organizationUserData,
|
|
109
|
+
this.userConfig.usePassword,
|
|
110
|
+
this.userConfig.primary,
|
|
111
|
+
this.userConfig.individualUserRequired,
|
|
112
|
+
this.userConfig.organizationUserRequired
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return organizationUser;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
module.exports = { LoginUser };
|
package/user/user.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const bcrypt = require('bcryptjs');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a user in the system. The User class is a domain entity,
|
|
5
|
+
* @class User
|
|
6
|
+
*/
|
|
7
|
+
class User {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new User instance.
|
|
10
|
+
* @param {import('../database/models/IndividualUser').IndividualUser} [individualUser=null] - The individual user for the user.
|
|
11
|
+
* @param {import('../database/models/OrganizationUser').OrganizationUser} [organizationUser=null] - The organization user for the user.
|
|
12
|
+
* @param {boolean} [usePassword=false] - Whether the user has a password.
|
|
13
|
+
* @param {string} [primary='individual'] - The primary user type.
|
|
14
|
+
* @param {boolean} [individualUserRequired=true] - Whether the user is required to have an individual user.
|
|
15
|
+
* @param {boolean} [organizationUserRequired=false] - Whether the user is required to have an organization user.
|
|
16
|
+
*/
|
|
17
|
+
constructor(individualUser = null, organizationUser = null, usePassword = false, primary = 'individual', individualUserRequired = true, organizationUserRequired = false) {
|
|
18
|
+
this.individualUser = individualUser;
|
|
19
|
+
this.organizationUser = organizationUser;
|
|
20
|
+
this.usePassword = usePassword;
|
|
21
|
+
|
|
22
|
+
this.config = {
|
|
23
|
+
primary,
|
|
24
|
+
individualUserRequired,
|
|
25
|
+
organizationUserRequired,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getPrimaryUser() {
|
|
30
|
+
if (this.config.primary === 'organization') {
|
|
31
|
+
return this.organizationUser;
|
|
32
|
+
}
|
|
33
|
+
return this.individualUser;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getId() {
|
|
37
|
+
return this.getPrimaryUser()?.id;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
isPasswordRequired() {
|
|
41
|
+
return this.usePassword;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
isPasswordValid(password) {
|
|
45
|
+
if (!this.isPasswordRequired()) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return bcrypt.compareSync(password, this.getPrimaryUser().hashword);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
setIndividualUser(individualUser) {
|
|
53
|
+
this.individualUser = individualUser;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
setOrganizationUser(organizationUser) {
|
|
57
|
+
this.organizationUser = organizationUser;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
isOrganizationUserRequired() {
|
|
61
|
+
return this.config.organizationUserRequired;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
isIndividualUserRequired() {
|
|
65
|
+
return this.config.individualUserRequired;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getIndividualUser() {
|
|
69
|
+
return this.individualUser;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getOrganizationUser() {
|
|
73
|
+
return this.organizationUser;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
module.exports = { User };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const {
|
|
2
|
+
WebsocketConnectionRepositoryMongo,
|
|
3
|
+
} = require('./websocket-connection-repository-mongo');
|
|
4
|
+
const {
|
|
5
|
+
WebsocketConnectionRepositoryPostgres,
|
|
6
|
+
} = require('./websocket-connection-repository-postgres');
|
|
7
|
+
const config = require('../../database/config');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Websocket Connection Repository Factory
|
|
11
|
+
* Creates the appropriate repository adapter based on database type
|
|
12
|
+
*
|
|
13
|
+
* @returns {WebsocketConnectionRepositoryInterface} Configured repository adapter
|
|
14
|
+
*/
|
|
15
|
+
function createWebsocketConnectionRepository() {
|
|
16
|
+
const dbType = config.DB_TYPE;
|
|
17
|
+
|
|
18
|
+
switch (dbType) {
|
|
19
|
+
case 'mongodb':
|
|
20
|
+
return new WebsocketConnectionRepositoryMongo();
|
|
21
|
+
|
|
22
|
+
case 'postgresql':
|
|
23
|
+
return new WebsocketConnectionRepositoryPostgres();
|
|
24
|
+
|
|
25
|
+
default:
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'postgresql'`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = {
|
|
33
|
+
createWebsocketConnectionRepository,
|
|
34
|
+
// Export adapters for direct testing
|
|
35
|
+
WebsocketConnectionRepositoryMongo,
|
|
36
|
+
WebsocketConnectionRepositoryPostgres,
|
|
37
|
+
};
|