@tomei/sso 0.31.6 → 0.32.1

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.31.6",
3
+ "version": "0.32.1",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -72,7 +72,8 @@
72
72
  "reflect-metadata": "^0.1.13",
73
73
  "sequelize": "^6.32.1",
74
74
  "sequelize-typescript": "^2.1.5",
75
- "speakeasy": "^2.0.0"
75
+ "speakeasy": "^2.0.0",
76
+ "uuid": "^10.0.0"
76
77
  },
77
78
  "lint-staged": {
78
79
  "*/**/*.{js,ts,tsx}": [
@@ -8,6 +8,7 @@ import { ApplicationConfig } from '@tomei/config';
8
8
  import { Op } from 'sequelize';
9
9
  import { ActionEnum, Activity } from '@tomei/activity-history';
10
10
  import { GroupSystemAccessRepository } from '../group-system-access/group-system-access.repository';
11
+ import SystemModel from '../../models/system.entity';
11
12
 
12
13
  export class Group extends ObjectBase {
13
14
  ObjectId: string;
@@ -511,4 +512,76 @@ export class Group extends ObjectBase {
511
512
  return error;
512
513
  }
513
514
  }
515
+
516
+ private static async getInheritedSystemAccess(
517
+ dbTransaction: any,
518
+ group: Group,
519
+ ): Promise<any[]> {
520
+ const options: any = {
521
+ where: {
522
+ GroupCode: group.GroupCode,
523
+ Status: 'Active',
524
+ },
525
+ include: [
526
+ {
527
+ model: SystemModel,
528
+ },
529
+ ],
530
+ transaction: dbTransaction,
531
+ };
532
+ let systemAccess = await Group._SystemAccessRepo.findAll(options);
533
+
534
+ if (group.InheritParentSystemAccessYN === 'Y') {
535
+ const parentGroup = await Group.init(
536
+ dbTransaction,
537
+ group.ParentGroupCode,
538
+ );
539
+ const parentSystemAccesses = await this.getInheritedSystemAccess(
540
+ dbTransaction,
541
+ parentGroup,
542
+ );
543
+ systemAccess = systemAccess.concat(parentSystemAccesses);
544
+ }
545
+ return systemAccess;
546
+ }
547
+
548
+ public static async getParentSystemAccesses(
549
+ loginUser: LoginUser,
550
+ dbTransaction: any,
551
+ GroupCode: string,
552
+ ) {
553
+ // Part 1: Privilege Checking
554
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
555
+ const isPrivileged = await loginUser.checkPrivileges(
556
+ systemCode,
557
+ 'SYSTEM_ACCESS_VIEW',
558
+ );
559
+
560
+ if (!isPrivileged) {
561
+ throw new ClassError(
562
+ 'Group',
563
+ 'GroupErrMsg06',
564
+ 'You do not have the privilege to view system access',
565
+ );
566
+ }
567
+
568
+ try {
569
+ const group = await Group.init(dbTransaction, GroupCode);
570
+ if (group.InheritParentSystemAccessYN !== 'Y' && !group.ParentGroupCode) {
571
+ return [];
572
+ } else {
573
+ const parentGroup = await Group.init(
574
+ dbTransaction,
575
+ group.ParentGroupCode,
576
+ );
577
+ const inheritSystemAccess = await Group.getInheritedSystemAccess(
578
+ dbTransaction,
579
+ parentGroup,
580
+ );
581
+ return inheritSystemAccess;
582
+ }
583
+ } catch (error) {
584
+ throw error;
585
+ }
586
+ }
514
587
  }
@@ -6,6 +6,7 @@ import { ApplicationConfig } from '@tomei/config';
6
6
  import { ActionEnum, Activity } from '@tomei/activity-history';
7
7
  import { ISystemSearchAttr } from '../../interfaces/system-search-attr.interface';
8
8
  import { Op } from 'sequelize';
9
+ import { v4 as uuidv4 } from 'uuid';
9
10
 
10
11
  export class System extends ObjectBase {
11
12
  ObjectId: string;
@@ -272,4 +273,116 @@ export class System extends ObjectBase {
272
273
  throw error;
273
274
  }
274
275
  }
276
+
277
+ public static async renewApiKeyAndSecret(
278
+ loginUser: LoginUser,
279
+ dbTransaction: any,
280
+ systemCode: string,
281
+ ) {
282
+ try {
283
+ //Part 1: Privilege Checking
284
+ //Call loginUser.checkPrivilege() method to check if the user has the privilege to renew API Key and Secret.
285
+ const sc = ApplicationConfig.getComponentConfigValue('system-code');
286
+ const isPrivileged = await loginUser.checkPrivileges(sc, 'SYSTEM_UPDATE');
287
+
288
+ if (!isPrivileged) {
289
+ throw new ClassError(
290
+ 'System',
291
+ 'SystemErrMsg06',
292
+ 'You do not have permission to renew API Key and Secret.',
293
+ );
294
+ }
295
+
296
+ //Part 2: Validation
297
+ //Instantiate existing System
298
+ const system = await System.init(dbTransaction, systemCode);
299
+
300
+ //Check if system.AccessURL got value. If not, throw new ClassError
301
+ if (!system.AccessURL) {
302
+ throw new ClassError(
303
+ 'System',
304
+ 'SystemErrMsg07',
305
+ 'AccessURL is required for callback',
306
+ );
307
+ }
308
+
309
+ //Check if system.Status is "Active". If not, throw new ClassError
310
+ if (system.Status !== 'Active') {
311
+ throw new ClassError(
312
+ 'System',
313
+ 'SystemErrMsg08',
314
+ 'Cannot do this operation on inactive system.',
315
+ );
316
+ }
317
+
318
+ //Set EntityValueBefore to system instance.
319
+ const entityValueBefore = {
320
+ SystemCode: system.SystemCode,
321
+ Name: system.Name,
322
+ Description: system.Description,
323
+ AccessURL: system.AccessURL,
324
+ GooglePlayURL: system.GooglePlayURL,
325
+ AppleStoreURL: system.AppleStoreURL,
326
+ APIKey: system.APIKey,
327
+ APISecret: system.APISecret,
328
+ Status: system.Status,
329
+ };
330
+
331
+ //Part 3: Generate API key and secret
332
+ //Use https://www.npmjs.com/package/uuid package to generate both the api key and api secret.
333
+ const apiKey = uuidv4();
334
+ const apiSecret = uuidv4();
335
+
336
+ //Update the system instance with new API key and secret.
337
+ system.APIKey = apiKey;
338
+ system.APISecret = apiSecret;
339
+ system._UpdatedById = loginUser.UserId;
340
+ system._UpdatedAt = new Date();
341
+ //Call System._Repo update() method to update the system record.
342
+ await System._Repo.update(
343
+ {
344
+ APIKey: apiKey,
345
+ APISecret: apiSecret,
346
+ UpdatedById: system._UpdatedById,
347
+ UpdatedAt: system._UpdatedAt,
348
+ },
349
+ {
350
+ where: {
351
+ SystemCode: systemCode,
352
+ },
353
+ transaction: dbTransaction,
354
+ },
355
+ );
356
+
357
+ //Part 4: Record Renew API Key and Secret Activity
358
+ //Set EntityValueAfter to system instance.
359
+ const entityValueAfter = {
360
+ SystemCode: system.SystemCode,
361
+ Name: system.Name,
362
+ Description: system.Description,
363
+ AccessURL: system.AccessURL,
364
+ GooglePlayURL: system.GooglePlayURL,
365
+ AppleStoreURL: system.AppleStoreURL,
366
+ APIKey: system.APIKey,
367
+ APISecret: system.APISecret,
368
+ Status: system.Status,
369
+ };
370
+
371
+ //Instantiate new activity from Activity class, call createId() method, then set the properties.
372
+ const activity = new Activity();
373
+ activity.ActivityId = activity.createId();
374
+ activity.Action = ActionEnum.UPDATE;
375
+ activity.Description = 'Renew API key and secret for a system';
376
+ activity.EntityType = 'System';
377
+ activity.EntityId = system.SystemCode;
378
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
379
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
380
+
381
+ await activity.create(loginUser.ObjectId, dbTransaction);
382
+ //Return the updated system instance.
383
+ return system;
384
+ } catch (error) {
385
+ throw error;
386
+ }
387
+ }
275
388
  }