@friggframework/core 2.0.0--canary.405.1f6792c.0 → 2.0.0--canary.396.6862738.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 (59) hide show
  1. package/credential/credential-repository.js +9 -0
  2. package/credential/use-cases/get-credential-for-user.js +21 -0
  3. package/encrypt/encrypt.js +27 -4
  4. package/handlers/app-definition-loader.js +38 -0
  5. package/handlers/app-handler-helpers.js +0 -3
  6. package/handlers/backend-utils.js +29 -34
  7. package/handlers/routers/auth.js +14 -11
  8. package/handlers/routers/integration-defined-routers.js +8 -5
  9. package/handlers/routers/user.js +25 -5
  10. package/handlers/workers/integration-defined-workers.js +6 -3
  11. package/index.js +0 -11
  12. package/integrations/index.js +0 -5
  13. package/integrations/integration-base.js +10 -7
  14. package/integrations/integration-repository.js +44 -0
  15. package/integrations/integration-router.js +230 -132
  16. package/integrations/integration.js +233 -0
  17. package/integrations/options.js +1 -1
  18. package/integrations/use-cases/create-integration.js +58 -0
  19. package/integrations/use-cases/delete-integration-for-user.js +53 -0
  20. package/integrations/use-cases/get-integration-for-user.js +63 -0
  21. package/integrations/use-cases/get-integration-instance.js +73 -0
  22. package/integrations/use-cases/get-integrations-for-user.js +64 -0
  23. package/integrations/use-cases/index.js +11 -0
  24. package/integrations/use-cases/update-integration.js +81 -0
  25. package/integrations/utils/map-integration-dto.js +37 -0
  26. package/module-plugin/index.js +0 -4
  27. package/module-plugin/module-factory.js +13 -32
  28. package/module-plugin/module-repository.js +70 -0
  29. package/module-plugin/module-service.js +50 -0
  30. package/module-plugin/{auther.js → module.js} +109 -173
  31. package/module-plugin/test/mock-api/api.js +8 -3
  32. package/module-plugin/test/mock-api/definition.js +12 -8
  33. package/module-plugin/use-cases/get-entities-for-user.js +32 -0
  34. package/module-plugin/utils/map-module-dto.js +18 -0
  35. package/package.json +5 -5
  36. package/types/integrations/index.d.ts +2 -6
  37. package/types/module-plugin/index.d.ts +4 -21
  38. package/user/tests/doubles/test-user-repository.js +72 -0
  39. package/user/tests/use-cases/create-individual-user.test.js +24 -0
  40. package/user/tests/use-cases/create-organization-user.test.js +28 -0
  41. package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
  42. package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
  43. package/user/tests/use-cases/login-user.test.js +140 -0
  44. package/user/use-cases/create-individual-user.js +61 -0
  45. package/user/use-cases/create-organization-user.js +47 -0
  46. package/user/use-cases/create-token-for-user-id.js +30 -0
  47. package/user/use-cases/get-user-from-bearer-token.js +77 -0
  48. package/user/use-cases/login-user.js +122 -0
  49. package/user/user-repository.js +62 -0
  50. package/user/user.js +77 -0
  51. package/handlers/routers/HEALTHCHECK.md +0 -240
  52. package/handlers/routers/health.js +0 -459
  53. package/handlers/routers/health.test.js +0 -203
  54. package/handlers/routers/middleware/loadUser.js +0 -15
  55. package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
  56. package/integrations/create-frigg-backend.js +0 -31
  57. package/integrations/integration-factory.js +0 -251
  58. package/integrations/integration-user.js +0 -144
  59. package/module-plugin/entity-manager.js +0 -70
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.405.1f6792c.0",
4
+ "version": "2.0.0--canary.396.6862738.0",
5
5
  "dependencies": {
6
6
  "@hapi/boom": "^10.0.1",
7
7
  "aws-sdk": "^2.1200.0",
@@ -22,9 +22,9 @@
22
22
  "uuid": "^9.0.1"
23
23
  },
24
24
  "devDependencies": {
25
- "@friggframework/eslint-config": "2.0.0--canary.405.1f6792c.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.405.1f6792c.0",
27
- "@friggframework/test": "2.0.0--canary.405.1f6792c.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.396.6862738.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.396.6862738.0",
27
+ "@friggframework/test": "2.0.0--canary.396.6862738.0",
28
28
  "@types/lodash": "4.17.15",
29
29
  "@typescript-eslint/eslint-plugin": "^8.0.0",
30
30
  "chai": "^4.3.6",
@@ -53,5 +53,5 @@
53
53
  },
54
54
  "homepage": "https://github.com/friggframework/frigg#readme",
55
55
  "description": "",
56
- "gitHead": "1f6792ccfcbd08502e14072f07ccbebdf99394cb"
56
+ "gitHead": "6862738f1b370d1bb37353809b30586267e7deb0"
57
57
  }
@@ -1,7 +1,6 @@
1
1
  declare module "@friggframework/integrations" {
2
2
  import { Delegate, IFriggDelegate } from "@friggframework/core";
3
3
  import { Model } from "mongoose";
4
- import { EntityManager } from "@friggframework/module-plugin";
5
4
 
6
5
  export class Integration extends Model {
7
6
  entities: any[];
@@ -19,8 +18,7 @@ declare module "@friggframework/integrations" {
19
18
 
20
19
  export class IntegrationManager
21
20
  extends Delegate
22
- implements IFriggIntegrationManager
23
- {
21
+ implements IFriggIntegrationManager {
24
22
  integration: Integration;
25
23
  primaryInstance: any;
26
24
  targetInstance: any;
@@ -56,7 +54,6 @@ declare module "@friggframework/integrations" {
56
54
  entities: { id: string; user: any },
57
55
  userId: string,
58
56
  config: any,
59
- EntityManager: EntityManager
60
57
  ): Promise<any>;
61
58
 
62
59
  static getFormattedIntegration(
@@ -116,8 +113,7 @@ declare module "@friggframework/integrations" {
116
113
  }
117
114
 
118
115
  export class IntegrationConfigManager
119
- implements IFriggIntegrationConfigManager
120
- {
116
+ implements IFriggIntegrationConfigManager {
121
117
  options: IntegrationOptions[];
122
118
  primary: any;
123
119
 
@@ -9,21 +9,7 @@ declare module "@friggframework/module-plugin" {
9
9
  externalId: string;
10
10
  }
11
11
 
12
- export class EntityManager implements IFriggEntityManager {
13
- static primaryEntityClass: any;
14
- static entityManagerClasses: any[];
15
- static entityTypes: string[];
16
- static getEntitiesForUser(userId: string): Promise<any[]>;
17
- static checkIsValidType(entityType: string): boolean;
18
- static getEntityManagerClass(entityType?: string): any;
19
-
20
- static getEntityManagerInstanceFromEntityId(
21
- entityId: string,
22
- userId: string
23
- ): Promise<any>;
24
- }
25
-
26
- interface IFriggEntityManager {}
12
+ interface IFriggEntityManager { }
27
13
 
28
14
  export class Entity extends Model {
29
15
  credentialId: string;
@@ -138,8 +124,7 @@ declare module "@friggframework/module-plugin" {
138
124
 
139
125
  export class ApiKeyRequester
140
126
  extends Requester
141
- implements IFriggApiKeyRequester
142
- {
127
+ implements IFriggApiKeyRequester {
143
128
  API_KEY_NAME: string;
144
129
  API_KEY_VALUE: any;
145
130
 
@@ -160,8 +145,7 @@ declare module "@friggframework/module-plugin" {
160
145
 
161
146
  export class BasicAuthRequester
162
147
  extends Requester
163
- implements IFriggBasicAuthRequester
164
- {
148
+ implements IFriggBasicAuthRequester {
165
149
  password: string;
166
150
  username: string;
167
151
 
@@ -189,8 +173,7 @@ declare module "@friggframework/module-plugin" {
189
173
 
190
174
  export class OAuth2Requester
191
175
  extends Requester
192
- implements IFriggOAuth2Requester
193
- {
176
+ implements IFriggOAuth2Requester {
194
177
  DLGT_TOKEN_DEAUTHORIZED: string;
195
178
  DLGT_TOKEN_UPDATE: string;
196
179
  accessTokenExpire: any;
@@ -0,0 +1,72 @@
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 };
@@ -0,0 +1,24 @@
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
+ });
@@ -0,0 +1,28 @@
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
+ });
@@ -0,0 +1,19 @@
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
+ });
@@ -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').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 };
@@ -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').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 };
@@ -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').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 };
@@ -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').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 };