@tomei/sso 0.61.0 → 0.61.1

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 (134) hide show
  1. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.d.ts +1 -0
  2. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js +71 -0
  3. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js.map +1 -0
  4. package/dist/__tests__/unit/components/login-user/login-user.spec.d.ts +0 -0
  5. package/dist/__tests__/unit/components/login-user/login-user.spec.js +6 -0
  6. package/dist/__tests__/unit/components/login-user/login-user.spec.js.map +1 -0
  7. package/dist/src/components/login-history/login-history.d.ts +23 -0
  8. package/dist/src/components/login-history/login-history.js +88 -0
  9. package/dist/src/components/login-history/login-history.js.map +1 -0
  10. package/dist/src/components/login-user/user.js +3 -2
  11. package/dist/src/components/login-user/user.js.map +1 -1
  12. package/dist/src/interfaces/login-history-search-attr.interface.d.ts +8 -0
  13. package/dist/src/interfaces/login-history-search-attr.interface.js +3 -0
  14. package/dist/src/interfaces/login-history-search-attr.interface.js.map +1 -0
  15. package/dist/src/interfaces/login-history.interface.d.ts +11 -0
  16. package/dist/src/interfaces/login-history.interface.js +3 -0
  17. package/dist/src/interfaces/login-history.interface.js.map +1 -0
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/src/components/api-key/api-key.repository.ts +15 -15
  21. package/src/components/api-key/api-key.ts +448 -448
  22. package/src/components/api-key/index.ts +4 -4
  23. package/src/components/building/building.repository.ts +27 -27
  24. package/src/components/building/index.ts +2 -2
  25. package/src/components/group/group.repository.ts +26 -26
  26. package/src/components/group/group.ts +2284 -2284
  27. package/src/components/group/index.ts +3 -3
  28. package/src/components/group-object-privilege/group-object-privilege.repository.ts +25 -25
  29. package/src/components/group-object-privilege/group-object-privilege.ts +278 -278
  30. package/src/components/group-object-privilege/index.ts +2 -2
  31. package/src/components/group-privilege/group-privilege.repository.ts +29 -29
  32. package/src/components/group-privilege/group-privilege.ts +84 -84
  33. package/src/components/group-privilege/index.ts +2 -2
  34. package/src/components/group-reporting-user/group-reporting-user.repository.ts +23 -23
  35. package/src/components/group-reporting-user/group-reporting-user.ts +506 -506
  36. package/src/components/group-reporting-user/index.ts +3 -3
  37. package/src/components/group-system-access/group-system-access.repository.ts +43 -43
  38. package/src/components/group-system-access/group-system-access.ts +90 -90
  39. package/src/components/group-system-access/index.ts +2 -2
  40. package/src/components/index.ts +20 -20
  41. package/src/components/login-user/index.ts +5 -5
  42. package/src/components/login-user/interfaces/check-user-info-duplicated.interface.ts +7 -7
  43. package/src/components/login-user/interfaces/index.ts +1 -1
  44. package/src/components/login-user/interfaces/system-access.interface.ts +13 -13
  45. package/src/components/login-user/interfaces/user-info.interface.ts +34 -34
  46. package/src/components/login-user/login-user.ts +362 -362
  47. package/src/components/login-user/user.repository.ts +11 -11
  48. package/src/components/login-user/user.ts +3 -2
  49. package/src/components/password-hash/index.ts +2 -2
  50. package/src/components/password-hash/interfaces/index.ts +1 -1
  51. package/src/components/password-hash/interfaces/password-hash-service.interface.ts +4 -4
  52. package/src/components/password-hash/password-hash.service.ts +14 -14
  53. package/src/components/staff/index.ts +2 -2
  54. package/src/components/staff/staff.repository.ts +27 -27
  55. package/src/components/system/index.ts +3 -3
  56. package/src/components/system/system.repository.ts +11 -11
  57. package/src/components/system/system.ts +456 -456
  58. package/src/components/system-privilege/index.ts +4 -4
  59. package/src/components/system-privilege/system-privilege.repository.ts +18 -18
  60. package/src/components/system-privilege/system-privilege.ts +541 -541
  61. package/src/components/user-group/index.ts +2 -2
  62. package/src/components/user-group/user-group.repository.ts +19 -19
  63. package/src/components/user-group/user-group.ts +764 -764
  64. package/src/components/user-object-privilege/index.ts +2 -2
  65. package/src/components/user-object-privilege/user-object-privilege.repository.ts +11 -11
  66. package/src/components/user-object-privilege/user-object-privilege.ts +79 -79
  67. package/src/components/user-password-history/index.ts +2 -2
  68. package/src/components/user-password-history/user-password-history.repository.ts +39 -39
  69. package/src/components/user-password-history/user-password-history.ts +187 -187
  70. package/src/components/user-privilege/index.ts +2 -2
  71. package/src/components/user-privilege/user-privilege.repository.ts +25 -25
  72. package/src/components/user-privilege/user-privilege.ts +662 -662
  73. package/src/components/user-reporting-hierarchy/index.ts +2 -2
  74. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.repository.ts +30 -30
  75. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.ts +505 -505
  76. package/src/components/user-system-access/index.ts +2 -2
  77. package/src/components/user-system-access/user-system-access.repository.ts +41 -41
  78. package/src/database.ts +15 -15
  79. package/src/enum/api-key.enum.ts +5 -5
  80. package/src/enum/building-type.enum.ts +6 -6
  81. package/src/enum/group-type.enum.ts +8 -8
  82. package/src/enum/index.ts +6 -6
  83. package/src/enum/login-status.enum.ts +4 -4
  84. package/src/enum/object-status.enum.ts +4 -4
  85. package/src/enum/user-status.enum.ts +7 -7
  86. package/src/enum/yn.enum.ts +4 -4
  87. package/src/index.ts +8 -8
  88. package/src/interfaces/api-key-attr.interface.ts +16 -16
  89. package/src/interfaces/group-object-privilege.interface.ts +14 -14
  90. package/src/interfaces/group-privilege.interface.ts +10 -10
  91. package/src/interfaces/group-reporting-user.interface.ts +11 -11
  92. package/src/interfaces/group-search-attr.interface.ts +9 -9
  93. package/src/interfaces/group-system-access.interface.ts +10 -10
  94. package/src/interfaces/group.interface.ts +17 -17
  95. package/src/interfaces/index.ts +13 -13
  96. package/src/interfaces/system-login.interface.ts +6 -6
  97. package/src/interfaces/system-privilege-search.interface.ts +5 -5
  98. package/src/interfaces/system-privilege.interface.ts +11 -11
  99. package/src/interfaces/system-search-attr.interface.ts +5 -5
  100. package/src/interfaces/system.interface.ts +15 -15
  101. package/src/interfaces/user-group.interface.ts +12 -12
  102. package/src/interfaces/user-object-privilege.interface.ts +14 -14
  103. package/src/interfaces/user-password-history.interface.ts +6 -6
  104. package/src/interfaces/user-privilege.interface.ts +10 -10
  105. package/src/interfaces/user-reporting-hierarchy.interface.ts +11 -11
  106. package/src/interfaces/user-session.interface.ts +5 -5
  107. package/src/interfaces/user-system-access.interface.ts +10 -10
  108. package/src/models/api-key-entity.ts +101 -101
  109. package/src/models/building.entity.ts +103 -103
  110. package/src/models/group-object-privilege.entity.ts +91 -91
  111. package/src/models/group-privilege.entity.ts +78 -78
  112. package/src/models/group-reporting-user.entity.ts +95 -95
  113. package/src/models/group-system-access.entity.ts +81 -81
  114. package/src/models/group.entity.ts +127 -127
  115. package/src/models/staff.entity.ts +91 -91
  116. package/src/models/system-privilege.entity.ts +90 -90
  117. package/src/models/system.entity.ts +113 -113
  118. package/src/models/user-group.entity.ts +91 -91
  119. package/src/models/user-object-privilege.entity.ts +90 -90
  120. package/src/models/user-password-history.ts +51 -51
  121. package/src/models/user-privilege.entity.ts +78 -78
  122. package/src/models/user-reporting-hierarchy.entity.ts +102 -102
  123. package/src/models/user-system-access.entity.ts +87 -87
  124. package/src/models/user.entity.ts +193 -193
  125. package/src/redis-client/__mocks__/jest-initial-setup.ts +2 -2
  126. package/src/redis-client/__mocks__/redis-mock.ts +28 -28
  127. package/src/redis-client/index.ts +1 -1
  128. package/src/redis-client/redis.service.ts +75 -75
  129. package/src/session/index.ts +2 -2
  130. package/src/session/interfaces/index.ts +1 -1
  131. package/src/session/interfaces/session-service.interface.ts +26 -26
  132. package/src/session/session.service.ts +96 -96
  133. package/src/types/auth-context.ts +10 -10
  134. package/src/types/index.ts +1 -1
@@ -1,448 +1,448 @@
1
- import { ClassError, ObjectBase } from '@tomei/general';
2
- import { APIKeyStatusEnum } from '../../enum/api-key.enum';
3
- import { APIKeyRepository } from './api-key.repository';
4
- import { IAPIKeyAttr } from '../../interfaces/api-key-attr.interface';
5
- import { LoginUser } from '../login-user/login-user';
6
- import { ApplicationConfig } from '@tomei/config';
7
- import { randomBytes } from 'crypto';
8
- import { ActionEnum, Activity } from '@tomei/activity-history';
9
- import { Op } from 'sequelize';
10
- import { System } from '../system/system';
11
-
12
- export class APIKey extends ObjectBase {
13
- ObjectId: string;
14
- ObjectName: string;
15
- ObjectType: 'ApiKey';
16
- TableName: 'sso_APIKey';
17
-
18
- ApiKey: string;
19
- Name: string;
20
- SystemCode: string;
21
- Description: string;
22
- Status: APIKeyStatusEnum;
23
- ExpirationDate: Date;
24
- RevokedReason: string;
25
- _RevokedById: number;
26
- _RevokedAt: Date;
27
- _CreatedAt: Date;
28
- _CreatedById: number;
29
-
30
- get APIKeyId(): number {
31
- return parseInt(this.ObjectId);
32
- }
33
-
34
- set APIKeyId(value: number) {
35
- this.ObjectId = value.toString();
36
- }
37
-
38
- get RevokedById(): number {
39
- return this._RevokedById;
40
- }
41
-
42
- get RevokedAt(): Date {
43
- return this._RevokedAt;
44
- }
45
-
46
- get CreatedAt(): Date {
47
- return this._CreatedAt;
48
- }
49
-
50
- get CreatedById(): number {
51
- return this._CreatedById;
52
- }
53
-
54
- protected static _Repo = new APIKeyRepository();
55
-
56
- protected constructor(apiKeyAttr?: IAPIKeyAttr) {
57
- super();
58
- if (apiKeyAttr) {
59
- this.APIKeyId = apiKeyAttr.APIKeyId;
60
- this.ApiKey = apiKeyAttr.ApiKey;
61
- this.Name = apiKeyAttr.Name;
62
- this.SystemCode = apiKeyAttr.SystemCode;
63
- this.Description = apiKeyAttr.Description;
64
- this.Status = apiKeyAttr.Status;
65
- this.ExpirationDate = apiKeyAttr.ExpirationDate;
66
- this._RevokedById = apiKeyAttr.RevokedById;
67
- this._RevokedAt = apiKeyAttr.RevokedAt;
68
- this._CreatedAt = apiKeyAttr.CreatedAt;
69
- this._CreatedById = apiKeyAttr.CreatedById;
70
- }
71
- }
72
-
73
- public static async init(ApiKeyId?: number, dbTransaction?: any) {
74
- try {
75
- if (ApiKeyId) {
76
- const apiKeyAttr = await this._Repo.findByPk(
77
- ApiKeyId.toString(),
78
- dbTransaction,
79
- );
80
- if (apiKeyAttr) {
81
- return new APIKey(apiKeyAttr);
82
- } else {
83
- throw new ClassError(
84
- 'APIKey',
85
- 'APIKeyErrMsgO1',
86
- 'APIKey not found',
87
- 'init',
88
- );
89
- }
90
- }
91
- return new APIKey();
92
- } catch (error) {
93
- throw error;
94
- }
95
- }
96
-
97
- async generate(loginUser: LoginUser, dbTransaction?: any) {
98
- //This method generates a new API key and saves it into the database.
99
- try {
100
- // Part 2: Privilege Checking
101
- // Call loginUser.checkPrivileges() method by passing:
102
- // SystemCode: <get_from_app_config>
103
- // PrivilegeCode: "API_KEY_CREATE"
104
- const systemCode =
105
- ApplicationConfig.getComponentConfigValue('system-code');
106
- const isPrivileged = await loginUser.checkPrivileges(
107
- systemCode,
108
- 'API_KEY_CREATE',
109
- );
110
-
111
- if (!isPrivileged) {
112
- throw new ClassError(
113
- 'APIKey',
114
- 'APIKeyErrMsgO2',
115
- 'User does not have privilege to generate API key.',
116
- 'generate',
117
- );
118
- }
119
-
120
- // Part 3: System Existence Check
121
- // Call System.init(dbTransaction, this.SystemCode) to verify the System exists.
122
- // If the group does not exist, throw a NotFoundError.
123
-
124
- await System.init(dbTransaction, this.SystemCode);
125
-
126
- // Part 3: Generate API Key
127
- // Populate the following attributes:
128
- // CreatedById: Set to loginUser.UserId.
129
- this._CreatedById = loginUser.UserId;
130
- // CreatedAt: Set to current date time.
131
- this._CreatedAt = new Date();
132
- // Generate a unique API key string:
133
- // Use a secure random function or crypto module to generate a 64-character hexadecimal string.
134
- // Assign the generated string to this.ApiKey.
135
- this.ApiKey = randomBytes(64).toString('hex');
136
-
137
- // Part 4: Save API Key to Database
138
- // Call APIKey._Repo.create() by passing:
139
- // loginUser
140
- // dbTransaction
141
- // This APIKey instance.
142
- const EntityValueAfter: any = {
143
- ApiKey: this.ApiKey,
144
- Name: this.Name,
145
- SystemCode: this.SystemCode,
146
- Status: this.Status,
147
- Description: this.Description,
148
- ExpirationDate: this.ExpirationDate,
149
- CreatedAt: this.CreatedAt,
150
- CreatedById: this.CreatedById,
151
- RevokedAt: this.RevokedAt,
152
- RevokedById: this.RevokedById,
153
- RevokedReason: this.RevokedReason,
154
- };
155
-
156
- const data = await APIKey._Repo.create(EntityValueAfter, {
157
- transaction: dbTransaction,
158
- });
159
- this.APIKeyId = data.APIKeyId;
160
- EntityValueAfter.ApiKeyId = data.APIKeyId;
161
-
162
- // Part 5: Record Generate API Key Activity
163
- // Initialise EntityValueBefore variable and set to empty object.
164
- const EntityValueBefore = {};
165
- // Initialise EntityValueAfter variable and set to this APIKey instance.
166
- // Instantiate new activity from Activity class, call createId() method, then set:
167
- const activity = new Activity();
168
- activity.ActivityId = activity.createId();
169
- // Action: ActionEnum.Create
170
- // Description: "Generate API key."
171
- // EntityType: "APIKey"
172
- // EntityId: <this.APIKeyId>
173
- // EntityValueBefore: EntityValueBefore
174
- // EntityValueAfter: EntityValueAfter
175
- activity.Action = ActionEnum.CREATE;
176
- activity.Description = 'Generate API key.';
177
- activity.EntityType = 'APIKey';
178
- activity.EntityId = this.ObjectId;
179
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
180
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
181
- // Call new activity create method by passing:
182
- // dbTransaction
183
- // userId: loginUser.ObjectId
184
- await activity.create(loginUser.ObjectId, dbTransaction);
185
-
186
- // Part 6: Returns
187
- // Translate the created APIKey entity into an object and return the following fields:
188
- // ApiKey
189
- // Name
190
- // Status
191
- // ExpirationDate
192
- // CreatedAt
193
- // CreatedById
194
- // InvokedAt
195
- // InvokedById
196
- return {
197
- ApiKey: this.ApiKey,
198
- Name: this.Name,
199
- Status: this.Status,
200
- Description: this.Description,
201
- SystemCode: this.SystemCode,
202
- ExpirationDate: this.ExpirationDate,
203
- CreatedAt: this.CreatedAt,
204
- CreatedById: this.CreatedById,
205
- RevokedAt: this.RevokedAt,
206
- RevokedById: this.RevokedById,
207
- RevokedReason: this.RevokedReason,
208
- };
209
- } catch (error) {
210
- throw error;
211
- }
212
- }
213
-
214
- public static async findAll(
215
- pagination: { page: number; limit: number },
216
- loginUser: LoginUser,
217
- dbTransaction: any,
218
- whereOptions: {
219
- SystemCode?: string;
220
- Status?: APIKeyStatusEnum;
221
- ExpirationDate?: {
222
- FromDate: Date;
223
- ToDate: Date;
224
- };
225
- CreatedById?: number;
226
- },
227
- sortOptions?: {
228
- SortBy: string;
229
- SortOrder: 'ASC' | 'DESC';
230
- },
231
- ) {
232
- try {
233
- // Part 1: Privilege Checking
234
- // Call the loginUser.checkPrivileges() method by passing:
235
- // SystemCode: Retrieved from the application configuration.
236
- // PrivilegeCode: 'API_KEY_VIEW'.
237
- // Ensure the user has the necessary privileges to view API keys.
238
- const systemCode =
239
- ApplicationConfig.getComponentConfigValue('system-code');
240
- const isPrivileged = await loginUser.checkPrivileges(
241
- systemCode,
242
- 'API_KEY_VIEW',
243
- );
244
-
245
- if (!isPrivileged) {
246
- throw new ClassError(
247
- 'APIKey',
248
- 'APIKeyErrMsgO2',
249
- 'User does not have privilege to generate API key.',
250
- 'generate',
251
- );
252
- }
253
-
254
- // Part 2: Prepare Filters
255
- // Ensure that whereOptions includes the necessary filters such as:
256
- // Status: Filter for specific API key statuses (e.g., "Active", "Revoked").
257
- // ExpirationDate: Filter for expiration date ranges (e.g., { [Op.between]: [fromDate, toDate] }).
258
- // CreatedById: Filter by the user who created the API key.
259
- const where = {};
260
- if (whereOptions) {
261
- if (whereOptions.SystemCode) {
262
- where['SystemCode'] = whereOptions.SystemCode;
263
- }
264
- if (whereOptions.Status) {
265
- where['Status'] = whereOptions.Status;
266
- }
267
- if (whereOptions.ExpirationDate) {
268
- where['ExpirationDate'] = {
269
- [Op.between]: [
270
- whereOptions.ExpirationDate.FromDate,
271
- whereOptions.ExpirationDate.ToDate,
272
- ],
273
- };
274
- }
275
- if (whereOptions.CreatedById) {
276
- where['CreatedById'] = whereOptions.CreatedById;
277
- }
278
- }
279
-
280
- const order = [];
281
- if (sortOptions) {
282
- // Sort the results based on the provided SortBy and SortOrder.
283
- // Ensure that the SortBy field is valid and that the SortOrder is either "ASC" or "DESC".
284
- if (sortOptions.SortBy) {
285
- order.push([sortOptions.SortBy, sortOptions.SortOrder]);
286
- }
287
- } else {
288
- order.push(['CreatedAt', 'DESC']);
289
- }
290
-
291
- let offset = 0;
292
- if (pagination) {
293
- offset = (pagination.page - 1) * pagination.limit;
294
- }
295
-
296
- // Ensure that the pagination object includes the page and limit values.
297
- // Use Sequelize's findAll() method with the provided whereOptions, sortOptions, and pagination to retrieve matching API keys.
298
- const data = await APIKey._Repo.findAllWithPagination({
299
- where,
300
- order,
301
- offset,
302
- limit: pagination.limit,
303
- transaction: dbTransaction,
304
- distinct: true,
305
- });
306
-
307
- // Return the list of API keys including:
308
- // ApiKeyId
309
- // ApiKey
310
- // Name
311
- // Status
312
- // ExpirationDate
313
- // CreatedAt
314
- // CreatedById
315
- // RevokedAt
316
- // RevokedById
317
- // RevokedReason
318
- // Include pagination information such as:
319
- // total: Total number of records,
320
- // page: Current page,
321
- // limit: Number of records per page.
322
-
323
- return {
324
- total: data.count,
325
- ApiKeys: data.rows.map((row) => {
326
- return {
327
- ApiKeyId: row.APIKeyId,
328
- ApiKey: row.ApiKey,
329
- Name: row.Name,
330
- SystemCode: row.SystemCode,
331
- Description: row.Description,
332
- Status: row.Status,
333
- ExpirationDate: row.ExpirationDate,
334
- CreatedAt: row.CreatedAt,
335
- CreatedById: row.CreatedById,
336
- RevokedAt: row.RevokedAt,
337
- RevokedById: row.RevokedById,
338
- RevokedReason: row.RevokedReason,
339
- };
340
- }),
341
- page: pagination.page,
342
- limit: pagination.limit,
343
- };
344
- } catch (error) {
345
- throw error;
346
- }
347
- }
348
-
349
- public async revoke(
350
- apiKey: string,
351
- loginUser: LoginUser,
352
- dbTransaction: any,
353
- reason?: string,
354
- ) {
355
- try {
356
- // Part 1: Prepare Required Params
357
- // Ensure apiKey, loginUser, and dbTransaction are provided.
358
- // Retrieve the existing API key record from the database using the provided apiKey.
359
- const apiKeyRecord = await APIKey._Repo.findOne({
360
- where: { ApiKey: apiKey },
361
- transaction: dbTransaction,
362
- });
363
- if (!apiKeyRecord) {
364
- throw new ClassError(
365
- 'APIKey',
366
- 'APIKeyErrMsgO3',
367
- 'API key not found.',
368
- 'revoke',
369
- );
370
- }
371
- const EntityValueBefore = {
372
- ...apiKeyRecord.get({ plain: true }),
373
- };
374
-
375
- // Part 2: Revoke API Key
376
- // Mark the API key as revoked:
377
- // Set the Status to "Revoked".
378
- apiKeyRecord.Status = APIKeyStatusEnum.REVOKED;
379
- // Set the RevokedAt timestamp to the current date and time.\
380
- apiKeyRecord.RevokedAt = new Date();
381
- // Set the RevokedById to loginUser.UserId.
382
- apiKeyRecord.RevokedById = loginUser.UserId;
383
- // Optionally, set the revocation reason:
384
- // If the reason parameter is provided, store it in the RevokedReason attribute.
385
- if (reason) {
386
- apiKeyRecord.RevokedReason = reason;
387
- }
388
-
389
- // Part 3: Save API Key to Database
390
- // Call APIKey._Repo.update() by passing:
391
- // The updated APIKey instance
392
- // dbTransaction.
393
- await APIKey._Repo.update(
394
- {
395
- ...apiKeyRecord.get({ plain: true }),
396
- },
397
- {
398
- where: { APIKeyId: apiKeyRecord.APIKeyId },
399
- transaction: dbTransaction,
400
- },
401
- );
402
-
403
- // Part 4: Record Update API Key Activity
404
- // Initialise EntityValueBefore variable and set to empty object.
405
- // Initialise EntityValueAfter variable and set to this APIKey instance.
406
- const EntityValueAfter = {
407
- ...apiKeyRecord.get({ plain: true }),
408
- };
409
- // Instantiate new activity from Activity class, call createId() method, then set:
410
- // Action: ActionEnum.Create
411
- // Description: "Revoke API key."
412
- // EntityType: "APIKey"
413
- // EntityId: <this.APIKeyId>
414
- // EntityValueBefore: EntityValueBefore
415
- // EntityValueAfter: EntityValueAfter
416
- const activity = new Activity();
417
- activity.ActivityId = activity.createId();
418
- activity.Action = ActionEnum.UPDATE;
419
- activity.Description = 'Revoke API key.';
420
- activity.EntityType = 'APIKey';
421
- activity.EntityId = apiKeyRecord.APIKeyId.toString();
422
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
423
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
424
- // Call new activity create method by passing:
425
- // dbTransaction
426
- // userId: loginUser.ObjectId
427
- await activity.create(loginUser.ObjectId, dbTransaction);
428
-
429
- // Part 5: Returns
430
- // Translate the updated APIKey entity into an object and return the following fields:
431
- // ApiKey
432
- // Status: "Revoked"
433
- // RevokedAt
434
- // RevokedById
435
- // RevokedByName
436
- // RevokedReason
437
- return {
438
- ApiKey: apiKeyRecord.ApiKey,
439
- Status: apiKeyRecord.Status,
440
- RevokedAt: apiKeyRecord.RevokedAt,
441
- RevokedById: apiKeyRecord.RevokedById,
442
- RevokedReason: apiKeyRecord.RevokedReason,
443
- };
444
- } catch (error) {
445
- throw error;
446
- }
447
- }
448
- }
1
+ import { ClassError, ObjectBase } from '@tomei/general';
2
+ import { APIKeyStatusEnum } from '../../enum/api-key.enum';
3
+ import { APIKeyRepository } from './api-key.repository';
4
+ import { IAPIKeyAttr } from '../../interfaces/api-key-attr.interface';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { ApplicationConfig } from '@tomei/config';
7
+ import { randomBytes } from 'crypto';
8
+ import { ActionEnum, Activity } from '@tomei/activity-history';
9
+ import { Op } from 'sequelize';
10
+ import { System } from '../system/system';
11
+
12
+ export class APIKey extends ObjectBase {
13
+ ObjectId: string;
14
+ ObjectName: string;
15
+ ObjectType: 'ApiKey';
16
+ TableName: 'sso_APIKey';
17
+
18
+ ApiKey: string;
19
+ Name: string;
20
+ SystemCode: string;
21
+ Description: string;
22
+ Status: APIKeyStatusEnum;
23
+ ExpirationDate: Date;
24
+ RevokedReason: string;
25
+ _RevokedById: number;
26
+ _RevokedAt: Date;
27
+ _CreatedAt: Date;
28
+ _CreatedById: number;
29
+
30
+ get APIKeyId(): number {
31
+ return parseInt(this.ObjectId);
32
+ }
33
+
34
+ set APIKeyId(value: number) {
35
+ this.ObjectId = value.toString();
36
+ }
37
+
38
+ get RevokedById(): number {
39
+ return this._RevokedById;
40
+ }
41
+
42
+ get RevokedAt(): Date {
43
+ return this._RevokedAt;
44
+ }
45
+
46
+ get CreatedAt(): Date {
47
+ return this._CreatedAt;
48
+ }
49
+
50
+ get CreatedById(): number {
51
+ return this._CreatedById;
52
+ }
53
+
54
+ protected static _Repo = new APIKeyRepository();
55
+
56
+ protected constructor(apiKeyAttr?: IAPIKeyAttr) {
57
+ super();
58
+ if (apiKeyAttr) {
59
+ this.APIKeyId = apiKeyAttr.APIKeyId;
60
+ this.ApiKey = apiKeyAttr.ApiKey;
61
+ this.Name = apiKeyAttr.Name;
62
+ this.SystemCode = apiKeyAttr.SystemCode;
63
+ this.Description = apiKeyAttr.Description;
64
+ this.Status = apiKeyAttr.Status;
65
+ this.ExpirationDate = apiKeyAttr.ExpirationDate;
66
+ this._RevokedById = apiKeyAttr.RevokedById;
67
+ this._RevokedAt = apiKeyAttr.RevokedAt;
68
+ this._CreatedAt = apiKeyAttr.CreatedAt;
69
+ this._CreatedById = apiKeyAttr.CreatedById;
70
+ }
71
+ }
72
+
73
+ public static async init(ApiKeyId?: number, dbTransaction?: any) {
74
+ try {
75
+ if (ApiKeyId) {
76
+ const apiKeyAttr = await this._Repo.findByPk(
77
+ ApiKeyId.toString(),
78
+ dbTransaction,
79
+ );
80
+ if (apiKeyAttr) {
81
+ return new APIKey(apiKeyAttr);
82
+ } else {
83
+ throw new ClassError(
84
+ 'APIKey',
85
+ 'APIKeyErrMsgO1',
86
+ 'APIKey not found',
87
+ 'init',
88
+ );
89
+ }
90
+ }
91
+ return new APIKey();
92
+ } catch (error) {
93
+ throw error;
94
+ }
95
+ }
96
+
97
+ async generate(loginUser: LoginUser, dbTransaction?: any) {
98
+ //This method generates a new API key and saves it into the database.
99
+ try {
100
+ // Part 2: Privilege Checking
101
+ // Call loginUser.checkPrivileges() method by passing:
102
+ // SystemCode: <get_from_app_config>
103
+ // PrivilegeCode: "API_KEY_CREATE"
104
+ const systemCode =
105
+ ApplicationConfig.getComponentConfigValue('system-code');
106
+ const isPrivileged = await loginUser.checkPrivileges(
107
+ systemCode,
108
+ 'API_KEY_CREATE',
109
+ );
110
+
111
+ if (!isPrivileged) {
112
+ throw new ClassError(
113
+ 'APIKey',
114
+ 'APIKeyErrMsgO2',
115
+ 'User does not have privilege to generate API key.',
116
+ 'generate',
117
+ );
118
+ }
119
+
120
+ // Part 3: System Existence Check
121
+ // Call System.init(dbTransaction, this.SystemCode) to verify the System exists.
122
+ // If the group does not exist, throw a NotFoundError.
123
+
124
+ await System.init(dbTransaction, this.SystemCode);
125
+
126
+ // Part 3: Generate API Key
127
+ // Populate the following attributes:
128
+ // CreatedById: Set to loginUser.UserId.
129
+ this._CreatedById = loginUser.UserId;
130
+ // CreatedAt: Set to current date time.
131
+ this._CreatedAt = new Date();
132
+ // Generate a unique API key string:
133
+ // Use a secure random function or crypto module to generate a 64-character hexadecimal string.
134
+ // Assign the generated string to this.ApiKey.
135
+ this.ApiKey = randomBytes(64).toString('hex');
136
+
137
+ // Part 4: Save API Key to Database
138
+ // Call APIKey._Repo.create() by passing:
139
+ // loginUser
140
+ // dbTransaction
141
+ // This APIKey instance.
142
+ const EntityValueAfter: any = {
143
+ ApiKey: this.ApiKey,
144
+ Name: this.Name,
145
+ SystemCode: this.SystemCode,
146
+ Status: this.Status,
147
+ Description: this.Description,
148
+ ExpirationDate: this.ExpirationDate,
149
+ CreatedAt: this.CreatedAt,
150
+ CreatedById: this.CreatedById,
151
+ RevokedAt: this.RevokedAt,
152
+ RevokedById: this.RevokedById,
153
+ RevokedReason: this.RevokedReason,
154
+ };
155
+
156
+ const data = await APIKey._Repo.create(EntityValueAfter, {
157
+ transaction: dbTransaction,
158
+ });
159
+ this.APIKeyId = data.APIKeyId;
160
+ EntityValueAfter.ApiKeyId = data.APIKeyId;
161
+
162
+ // Part 5: Record Generate API Key Activity
163
+ // Initialise EntityValueBefore variable and set to empty object.
164
+ const EntityValueBefore = {};
165
+ // Initialise EntityValueAfter variable and set to this APIKey instance.
166
+ // Instantiate new activity from Activity class, call createId() method, then set:
167
+ const activity = new Activity();
168
+ activity.ActivityId = activity.createId();
169
+ // Action: ActionEnum.Create
170
+ // Description: "Generate API key."
171
+ // EntityType: "APIKey"
172
+ // EntityId: <this.APIKeyId>
173
+ // EntityValueBefore: EntityValueBefore
174
+ // EntityValueAfter: EntityValueAfter
175
+ activity.Action = ActionEnum.CREATE;
176
+ activity.Description = 'Generate API key.';
177
+ activity.EntityType = 'APIKey';
178
+ activity.EntityId = this.ObjectId;
179
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
180
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
181
+ // Call new activity create method by passing:
182
+ // dbTransaction
183
+ // userId: loginUser.ObjectId
184
+ await activity.create(loginUser.ObjectId, dbTransaction);
185
+
186
+ // Part 6: Returns
187
+ // Translate the created APIKey entity into an object and return the following fields:
188
+ // ApiKey
189
+ // Name
190
+ // Status
191
+ // ExpirationDate
192
+ // CreatedAt
193
+ // CreatedById
194
+ // InvokedAt
195
+ // InvokedById
196
+ return {
197
+ ApiKey: this.ApiKey,
198
+ Name: this.Name,
199
+ Status: this.Status,
200
+ Description: this.Description,
201
+ SystemCode: this.SystemCode,
202
+ ExpirationDate: this.ExpirationDate,
203
+ CreatedAt: this.CreatedAt,
204
+ CreatedById: this.CreatedById,
205
+ RevokedAt: this.RevokedAt,
206
+ RevokedById: this.RevokedById,
207
+ RevokedReason: this.RevokedReason,
208
+ };
209
+ } catch (error) {
210
+ throw error;
211
+ }
212
+ }
213
+
214
+ public static async findAll(
215
+ pagination: { page: number; limit: number },
216
+ loginUser: LoginUser,
217
+ dbTransaction: any,
218
+ whereOptions: {
219
+ SystemCode?: string;
220
+ Status?: APIKeyStatusEnum;
221
+ ExpirationDate?: {
222
+ FromDate: Date;
223
+ ToDate: Date;
224
+ };
225
+ CreatedById?: number;
226
+ },
227
+ sortOptions?: {
228
+ SortBy: string;
229
+ SortOrder: 'ASC' | 'DESC';
230
+ },
231
+ ) {
232
+ try {
233
+ // Part 1: Privilege Checking
234
+ // Call the loginUser.checkPrivileges() method by passing:
235
+ // SystemCode: Retrieved from the application configuration.
236
+ // PrivilegeCode: 'API_KEY_VIEW'.
237
+ // Ensure the user has the necessary privileges to view API keys.
238
+ const systemCode =
239
+ ApplicationConfig.getComponentConfigValue('system-code');
240
+ const isPrivileged = await loginUser.checkPrivileges(
241
+ systemCode,
242
+ 'API_KEY_VIEW',
243
+ );
244
+
245
+ if (!isPrivileged) {
246
+ throw new ClassError(
247
+ 'APIKey',
248
+ 'APIKeyErrMsgO2',
249
+ 'User does not have privilege to generate API key.',
250
+ 'generate',
251
+ );
252
+ }
253
+
254
+ // Part 2: Prepare Filters
255
+ // Ensure that whereOptions includes the necessary filters such as:
256
+ // Status: Filter for specific API key statuses (e.g., "Active", "Revoked").
257
+ // ExpirationDate: Filter for expiration date ranges (e.g., { [Op.between]: [fromDate, toDate] }).
258
+ // CreatedById: Filter by the user who created the API key.
259
+ const where = {};
260
+ if (whereOptions) {
261
+ if (whereOptions.SystemCode) {
262
+ where['SystemCode'] = whereOptions.SystemCode;
263
+ }
264
+ if (whereOptions.Status) {
265
+ where['Status'] = whereOptions.Status;
266
+ }
267
+ if (whereOptions.ExpirationDate) {
268
+ where['ExpirationDate'] = {
269
+ [Op.between]: [
270
+ whereOptions.ExpirationDate.FromDate,
271
+ whereOptions.ExpirationDate.ToDate,
272
+ ],
273
+ };
274
+ }
275
+ if (whereOptions.CreatedById) {
276
+ where['CreatedById'] = whereOptions.CreatedById;
277
+ }
278
+ }
279
+
280
+ const order = [];
281
+ if (sortOptions) {
282
+ // Sort the results based on the provided SortBy and SortOrder.
283
+ // Ensure that the SortBy field is valid and that the SortOrder is either "ASC" or "DESC".
284
+ if (sortOptions.SortBy) {
285
+ order.push([sortOptions.SortBy, sortOptions.SortOrder]);
286
+ }
287
+ } else {
288
+ order.push(['CreatedAt', 'DESC']);
289
+ }
290
+
291
+ let offset = 0;
292
+ if (pagination) {
293
+ offset = (pagination.page - 1) * pagination.limit;
294
+ }
295
+
296
+ // Ensure that the pagination object includes the page and limit values.
297
+ // Use Sequelize's findAll() method with the provided whereOptions, sortOptions, and pagination to retrieve matching API keys.
298
+ const data = await APIKey._Repo.findAllWithPagination({
299
+ where,
300
+ order,
301
+ offset,
302
+ limit: pagination.limit,
303
+ transaction: dbTransaction,
304
+ distinct: true,
305
+ });
306
+
307
+ // Return the list of API keys including:
308
+ // ApiKeyId
309
+ // ApiKey
310
+ // Name
311
+ // Status
312
+ // ExpirationDate
313
+ // CreatedAt
314
+ // CreatedById
315
+ // RevokedAt
316
+ // RevokedById
317
+ // RevokedReason
318
+ // Include pagination information such as:
319
+ // total: Total number of records,
320
+ // page: Current page,
321
+ // limit: Number of records per page.
322
+
323
+ return {
324
+ total: data.count,
325
+ ApiKeys: data.rows.map((row) => {
326
+ return {
327
+ ApiKeyId: row.APIKeyId,
328
+ ApiKey: row.ApiKey,
329
+ Name: row.Name,
330
+ SystemCode: row.SystemCode,
331
+ Description: row.Description,
332
+ Status: row.Status,
333
+ ExpirationDate: row.ExpirationDate,
334
+ CreatedAt: row.CreatedAt,
335
+ CreatedById: row.CreatedById,
336
+ RevokedAt: row.RevokedAt,
337
+ RevokedById: row.RevokedById,
338
+ RevokedReason: row.RevokedReason,
339
+ };
340
+ }),
341
+ page: pagination.page,
342
+ limit: pagination.limit,
343
+ };
344
+ } catch (error) {
345
+ throw error;
346
+ }
347
+ }
348
+
349
+ public async revoke(
350
+ apiKey: string,
351
+ loginUser: LoginUser,
352
+ dbTransaction: any,
353
+ reason?: string,
354
+ ) {
355
+ try {
356
+ // Part 1: Prepare Required Params
357
+ // Ensure apiKey, loginUser, and dbTransaction are provided.
358
+ // Retrieve the existing API key record from the database using the provided apiKey.
359
+ const apiKeyRecord = await APIKey._Repo.findOne({
360
+ where: { ApiKey: apiKey },
361
+ transaction: dbTransaction,
362
+ });
363
+ if (!apiKeyRecord) {
364
+ throw new ClassError(
365
+ 'APIKey',
366
+ 'APIKeyErrMsgO3',
367
+ 'API key not found.',
368
+ 'revoke',
369
+ );
370
+ }
371
+ const EntityValueBefore = {
372
+ ...apiKeyRecord.get({ plain: true }),
373
+ };
374
+
375
+ // Part 2: Revoke API Key
376
+ // Mark the API key as revoked:
377
+ // Set the Status to "Revoked".
378
+ apiKeyRecord.Status = APIKeyStatusEnum.REVOKED;
379
+ // Set the RevokedAt timestamp to the current date and time.\
380
+ apiKeyRecord.RevokedAt = new Date();
381
+ // Set the RevokedById to loginUser.UserId.
382
+ apiKeyRecord.RevokedById = loginUser.UserId;
383
+ // Optionally, set the revocation reason:
384
+ // If the reason parameter is provided, store it in the RevokedReason attribute.
385
+ if (reason) {
386
+ apiKeyRecord.RevokedReason = reason;
387
+ }
388
+
389
+ // Part 3: Save API Key to Database
390
+ // Call APIKey._Repo.update() by passing:
391
+ // The updated APIKey instance
392
+ // dbTransaction.
393
+ await APIKey._Repo.update(
394
+ {
395
+ ...apiKeyRecord.get({ plain: true }),
396
+ },
397
+ {
398
+ where: { APIKeyId: apiKeyRecord.APIKeyId },
399
+ transaction: dbTransaction,
400
+ },
401
+ );
402
+
403
+ // Part 4: Record Update API Key Activity
404
+ // Initialise EntityValueBefore variable and set to empty object.
405
+ // Initialise EntityValueAfter variable and set to this APIKey instance.
406
+ const EntityValueAfter = {
407
+ ...apiKeyRecord.get({ plain: true }),
408
+ };
409
+ // Instantiate new activity from Activity class, call createId() method, then set:
410
+ // Action: ActionEnum.Create
411
+ // Description: "Revoke API key."
412
+ // EntityType: "APIKey"
413
+ // EntityId: <this.APIKeyId>
414
+ // EntityValueBefore: EntityValueBefore
415
+ // EntityValueAfter: EntityValueAfter
416
+ const activity = new Activity();
417
+ activity.ActivityId = activity.createId();
418
+ activity.Action = ActionEnum.UPDATE;
419
+ activity.Description = 'Revoke API key.';
420
+ activity.EntityType = 'APIKey';
421
+ activity.EntityId = apiKeyRecord.APIKeyId.toString();
422
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
423
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
424
+ // Call new activity create method by passing:
425
+ // dbTransaction
426
+ // userId: loginUser.ObjectId
427
+ await activity.create(loginUser.ObjectId, dbTransaction);
428
+
429
+ // Part 5: Returns
430
+ // Translate the updated APIKey entity into an object and return the following fields:
431
+ // ApiKey
432
+ // Status: "Revoked"
433
+ // RevokedAt
434
+ // RevokedById
435
+ // RevokedByName
436
+ // RevokedReason
437
+ return {
438
+ ApiKey: apiKeyRecord.ApiKey,
439
+ Status: apiKeyRecord.Status,
440
+ RevokedAt: apiKeyRecord.RevokedAt,
441
+ RevokedById: apiKeyRecord.RevokedById,
442
+ RevokedReason: apiKeyRecord.RevokedReason,
443
+ };
444
+ } catch (error) {
445
+ throw error;
446
+ }
447
+ }
448
+ }