@friggframework/core 2.0.0--canary.395.65f5f64.0 → 2.0.0--canary.398.24926ac.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 (97) hide show
  1. package/README.md +50 -931
  2. package/core/create-handler.js +0 -1
  3. package/database/models/WebsocketConnection.js +5 -0
  4. package/handlers/app-handler-helpers.js +3 -0
  5. package/handlers/backend-utils.js +44 -42
  6. package/handlers/routers/auth.js +14 -3
  7. package/handlers/routers/integration-defined-routers.js +5 -8
  8. package/handlers/routers/middleware/loadUser.js +15 -0
  9. package/handlers/routers/middleware/requireLoggedInUser.js +12 -0
  10. package/handlers/routers/user.js +5 -25
  11. package/handlers/workers/integration-defined-workers.js +3 -6
  12. package/index.js +16 -1
  13. package/integrations/create-frigg-backend.js +31 -0
  14. package/integrations/index.js +5 -0
  15. package/integrations/integration-base.js +46 -142
  16. package/integrations/integration-factory.js +251 -0
  17. package/integrations/integration-router.js +181 -303
  18. package/integrations/integration-user.js +144 -0
  19. package/integrations/options.js +1 -1
  20. package/integrations/test/integration-base.test.js +144 -0
  21. package/module-plugin/auther.js +393 -0
  22. package/module-plugin/entity-manager.js +70 -0
  23. package/{modules → module-plugin}/entity.js +0 -1
  24. package/{modules → module-plugin}/index.js +8 -0
  25. package/module-plugin/manager.js +169 -0
  26. package/module-plugin/module-factory.js +61 -0
  27. package/{modules → module-plugin}/test/mock-api/api.js +3 -8
  28. package/{modules → module-plugin}/test/mock-api/definition.js +8 -12
  29. package/package.json +5 -5
  30. package/syncs/sync.js +1 -0
  31. package/types/integrations/index.d.ts +6 -2
  32. package/types/module-plugin/index.d.ts +56 -4
  33. package/types/syncs/index.d.ts +2 -0
  34. package/credential/credential-repository.js +0 -56
  35. package/credential/use-cases/get-credential-for-user.js +0 -21
  36. package/credential/use-cases/update-authentication-status.js +0 -15
  37. package/handlers/app-definition-loader.js +0 -38
  38. package/integrations/integration-repository.js +0 -80
  39. package/integrations/tests/doubles/dummy-integration-class.js +0 -90
  40. package/integrations/tests/doubles/test-integration-repository.js +0 -89
  41. package/integrations/tests/use-cases/create-integration.test.js +0 -124
  42. package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -143
  43. package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -143
  44. package/integrations/tests/use-cases/get-integration-instance.test.js +0 -169
  45. package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -169
  46. package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -188
  47. package/integrations/tests/use-cases/update-integration-messages.test.js +0 -142
  48. package/integrations/tests/use-cases/update-integration-status.test.js +0 -103
  49. package/integrations/tests/use-cases/update-integration.test.js +0 -134
  50. package/integrations/use-cases/create-integration.js +0 -71
  51. package/integrations/use-cases/delete-integration-for-user.js +0 -72
  52. package/integrations/use-cases/get-integration-for-user.js +0 -78
  53. package/integrations/use-cases/get-integration-instance-by-definition.js +0 -67
  54. package/integrations/use-cases/get-integration-instance.js +0 -82
  55. package/integrations/use-cases/get-integrations-for-user.js +0 -76
  56. package/integrations/use-cases/get-possible-integrations.js +0 -27
  57. package/integrations/use-cases/index.js +0 -11
  58. package/integrations/use-cases/update-integration-messages.js +0 -31
  59. package/integrations/use-cases/update-integration-status.js +0 -28
  60. package/integrations/use-cases/update-integration.js +0 -91
  61. package/integrations/utils/map-integration-dto.js +0 -36
  62. package/modules/module-factory.js +0 -54
  63. package/modules/module-repository.js +0 -107
  64. package/modules/module.js +0 -218
  65. package/modules/tests/doubles/test-module-factory.js +0 -16
  66. package/modules/tests/doubles/test-module-repository.js +0 -19
  67. package/modules/use-cases/get-entities-for-user.js +0 -32
  68. package/modules/use-cases/get-entity-options-by-id.js +0 -58
  69. package/modules/use-cases/get-entity-options-by-type.js +0 -34
  70. package/modules/use-cases/get-module-instance-from-type.js +0 -31
  71. package/modules/use-cases/get-module.js +0 -56
  72. package/modules/use-cases/process-authorization-callback.js +0 -108
  73. package/modules/use-cases/refresh-entity-options.js +0 -58
  74. package/modules/use-cases/test-module-auth.js +0 -54
  75. package/modules/utils/map-module-dto.js +0 -18
  76. package/user/tests/doubles/test-user-repository.js +0 -72
  77. package/user/tests/use-cases/create-individual-user.test.js +0 -24
  78. package/user/tests/use-cases/create-organization-user.test.js +0 -28
  79. package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
  80. package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
  81. package/user/tests/use-cases/login-user.test.js +0 -140
  82. package/user/use-cases/create-individual-user.js +0 -61
  83. package/user/use-cases/create-organization-user.js +0 -47
  84. package/user/use-cases/create-token-for-user-id.js +0 -30
  85. package/user/use-cases/get-user-from-bearer-token.js +0 -77
  86. package/user/use-cases/login-user.js +0 -122
  87. package/user/user-repository.js +0 -62
  88. package/user/user.js +0 -77
  89. /package/{modules → module-plugin}/ModuleConstants.js +0 -0
  90. /package/{modules → module-plugin}/credential.js +0 -0
  91. /package/{modules → module-plugin}/requester/api-key.js +0 -0
  92. /package/{modules → module-plugin}/requester/basic.js +0 -0
  93. /package/{modules → module-plugin}/requester/oauth-2.js +0 -0
  94. /package/{modules → module-plugin}/requester/requester.js +0 -0
  95. /package/{modules → module-plugin}/requester/requester.test.js +0 -0
  96. /package/{modules → module-plugin}/test/auther.test.js +0 -0
  97. /package/{modules → module-plugin}/test/mock-api/mocks/hubspot.js +0 -0
@@ -1,18 +0,0 @@
1
- /**
2
- * @param {import('../module').Module} moduleInstance
3
- * Convert a Module domain instance to a plain DTO suitable for JSON responses.
4
- */
5
- function mapModuleClassToModuleDTO(moduleInstance) {
6
- if (!moduleInstance) return null;
7
-
8
- return {
9
- id: moduleInstance.entity.id,
10
- name: moduleInstance.name,
11
- userId: moduleInstance.userId,
12
- entity: moduleInstance.entity,
13
- credentialId: moduleInstance.credential?._id?.toString(),
14
- type: moduleInstance.getName()
15
- };
16
- }
17
-
18
- module.exports = { mapModuleClassToModuleDTO };
@@ -1,72 +0,0 @@
1
- const Boom = require('@hapi/boom');
2
- const { User } = require('../../user');
3
-
4
- class TestUserRepository {
5
- constructor({ userConfig }) {
6
- this.individualUsers = new Map();
7
- this.organizationUsers = new Map();
8
- this.tokens = new Map();
9
- this.userConfig = userConfig;
10
- }
11
-
12
- async getSessionToken(token) {
13
- return this.tokens.get(token);
14
- }
15
-
16
- async findOrganizationUserById(userId) {
17
- return this.organizationUsers.get(userId);
18
- }
19
-
20
- async findIndividualUserById(userId) {
21
- return this.individualUsers.get(userId);
22
- }
23
-
24
- async createToken(userId, rawToken, minutes = 120) {
25
- const token = `token-for-${userId}-for-${minutes}-mins`;
26
- this.tokens.set(token, { user: userId, rawToken });
27
- return token;
28
- }
29
-
30
- async createIndividualUser(params) {
31
- const individualUserData = { id: `individual-${Date.now()}`, ...params };
32
- this.individualUsers.set(individualUserData.id, individualUserData);
33
- return individualUserData;
34
- }
35
-
36
- async createOrganizationUser(params) {
37
- const orgUserData = { ...params, id: `org-${Date.now()}` };
38
- this.organizationUsers.set(orgUserData.id, orgUserData);
39
- return orgUserData;
40
- }
41
-
42
- async findIndividualUserByUsername(username) {
43
- for (const userDoc of this.individualUsers.values()) {
44
- if (userDoc.username === username) {
45
- return userDoc;
46
- }
47
- }
48
- return null;
49
- }
50
-
51
- async findIndividualUserByAppUserId(appUserId) {
52
- if (!appUserId) return null;
53
- for (const userDoc of this.individualUsers.values()) {
54
- if (userDoc.appUserId === appUserId) {
55
- return userDoc;
56
- }
57
- }
58
- return null;
59
- }
60
-
61
- async findOrganizationUserByAppOrgId(appOrgId) {
62
- if (!appOrgId) return null;
63
- for (const userDoc of this.organizationUsers.values()) {
64
- if (userDoc.appOrgId === appOrgId) {
65
- return userDoc;
66
- }
67
- }
68
- return null;
69
- }
70
- }
71
-
72
- module.exports = { TestUserRepository };
@@ -1,24 +0,0 @@
1
- const {
2
- CreateIndividualUser,
3
- } = require('../../use-cases/create-individual-user');
4
- const { TestUserRepository } = require('../doubles/test-user-repository');
5
-
6
- describe('CreateIndividualUser Use Case', () => {
7
- it('should create and return an individual user via the repository', async () => {
8
- const userConfig = { usePassword: true };
9
- const userRepository = new TestUserRepository({ userConfig });
10
- const createIndividualUser = new CreateIndividualUser({
11
- userRepository,
12
- userConfig,
13
- });
14
-
15
- const params = {
16
- username: 'test-user',
17
- password: 'password123',
18
- };
19
- const user = await createIndividualUser.execute(params);
20
-
21
- expect(user).toBeDefined();
22
- expect(user.getIndividualUser().username).toBe(params.username);
23
- });
24
- });
@@ -1,28 +0,0 @@
1
- const {
2
- CreateOrganizationUser,
3
- } = require('../../use-cases/create-organization-user');
4
- const { TestUserRepository } = require('../doubles/test-user-repository');
5
-
6
- describe('CreateOrganizationUser Use Case', () => {
7
- it('should create and return an organization user via the repository', async () => {
8
- const userConfig = {
9
- primary: 'organization',
10
- organizationUserRequired: true,
11
- individualUserRequired: false,
12
- };
13
- const userRepository = new TestUserRepository({ userConfig });
14
- const createOrganizationUser = new CreateOrganizationUser({
15
- userRepository,
16
- userConfig,
17
- });
18
-
19
- const params = {
20
- name: 'Test Org',
21
- appOrgId: 'org-123',
22
- };
23
- const user = await createOrganizationUser.execute(params);
24
-
25
- expect(user).toBeDefined();
26
- expect(user.getOrganizationUser().name).toBe(params.name);
27
- });
28
- });
@@ -1,19 +0,0 @@
1
- const {
2
- CreateTokenForUserId,
3
- } = require('../../use-cases/create-token-for-user-id');
4
- const { TestUserRepository } = require('../doubles/test-user-repository');
5
-
6
- describe('CreateTokenForUserId Use Case', () => {
7
- it('should create and return a token via the repository', async () => {
8
- const userConfig = {}; // Not used by this use case, but required by the test repo
9
- const userRepository = new TestUserRepository({ userConfig });
10
- const createTokenForUserId = new CreateTokenForUserId({ userRepository });
11
-
12
- const userId = 'user-123';
13
- const token = await createTokenForUserId.execute(userId);
14
-
15
- expect(token).toBeDefined();
16
- // The mock token is deterministic, so we can check it
17
- expect(token).toContain(`token-for-${userId}`);
18
- });
19
- });
@@ -1,64 +0,0 @@
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
- });
@@ -1,140 +0,0 @@
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
- });
@@ -1,61 +0,0 @@
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').UserRepository} 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 };
@@ -1,47 +0,0 @@
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').UserRepository} 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 };
@@ -1,30 +0,0 @@
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').UserRepository} 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 };
@@ -1,77 +0,0 @@
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').UserRepository} 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 };