@tomei/sso 0.66.1-test.2 → 0.66.1-test.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.66.1-test.2",
3
+ "version": "0.66.1-test.3",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -65,6 +65,7 @@
65
65
  "@tomei/general": "^0.21.10",
66
66
  "@tomei/mailer": "^0.6.2",
67
67
  "argon2": "^0.44.0",
68
+ "axios": "^1.13.2",
68
69
  "redis": "^5.8.2",
69
70
  "reflect-metadata": "^0.2.2",
70
71
  "sequelize": "^6.37.7",
@@ -12,13 +12,27 @@ import { createHash, randomBytes, randomUUID } from 'crypto';
12
12
  import { UserGroupRepository } from '../user-group/user-group.repository';
13
13
  import GroupSystemAccessModel from '../../models/group-system-access.entity';
14
14
  import SystemModel from '../../models/system.entity';
15
- import { ApplicationConfig } from '@tomei/config';
15
+ import { ApplicationConfig, ComponentConfig } from '@tomei/config';
16
+ import axios from 'axios';
16
17
 
17
18
  export class LoginUser extends User implements ILoginUser {
18
19
  session = {
19
20
  Id: null,
20
21
  };
21
22
 
23
+ private static isLoginApiEnabled(): boolean {
24
+ try {
25
+ const useLoginApiYN =
26
+ ComponentConfig.getComponentConfigValue(
27
+ '@tomei/sso',
28
+ 'useLoginApiYN',
29
+ ) || 'Y';
30
+ return String(useLoginApiYN).toUpperCase() !== 'N';
31
+ } catch (error) {
32
+ return true;
33
+ }
34
+ }
35
+
22
36
  static async init(
23
37
  sessionService: ISessionService,
24
38
  userId?: number,
@@ -26,20 +40,43 @@ export class LoginUser extends User implements ILoginUser {
26
40
  ): Promise<LoginUser> {
27
41
  User._RedisService = await RedisService.init();
28
42
  if (userId) {
29
- if (dbTransaction) {
30
- User._Repository = new UserRepository();
31
- }
32
- const user = await User._Repository.findOne({
33
- where: {
34
- UserId: userId,
35
- },
36
- include: [
43
+ let user: any;
44
+
45
+ if (LoginUser.isLoginApiEnabled()) {
46
+ const loginApiEndpoint = ComponentConfig.getComponentConfigValue(
47
+ '@tomei/sso',
48
+ 'loginApiEndpoint',
49
+ );
50
+
51
+ let payload = { UserId: userId };
52
+
53
+ const { data } = await axios.post(
54
+ `${loginApiEndpoint.baseUrl}${loginApiEndpoint.getProfile}`,
55
+ payload,
37
56
  {
38
- model: Staff,
57
+ headers: {
58
+ 'Content-Type': 'application/json',
59
+ 'x-api-key': loginApiEndpoint.apiKey,
60
+ },
39
61
  },
40
- ],
41
- transaction: dbTransaction,
42
- });
62
+ );
63
+ user = data.loginUser;
64
+ } else {
65
+ if (dbTransaction) {
66
+ User._Repository = new UserRepository();
67
+ }
68
+ user = await User._Repository.findOne({
69
+ where: {
70
+ UserId: userId,
71
+ },
72
+ include: [
73
+ {
74
+ model: Staff,
75
+ },
76
+ ],
77
+ transaction: dbTransaction,
78
+ });
79
+ }
43
80
 
44
81
  if (!user) {
45
82
  throw new Error('Invalid credentials.');
@@ -99,6 +136,30 @@ export class LoginUser extends User implements ILoginUser {
99
136
  throw new Error('Session name is not set');
100
137
  }
101
138
 
139
+ if (LoginUser.isLoginApiEnabled()) {
140
+ const loginApiEndpoint = ComponentConfig.getComponentConfigValue(
141
+ '@tomei/sso',
142
+ 'loginApiEndpoint',
143
+ );
144
+ let payload = {
145
+ SystemCode: systemCode,
146
+ SessionName: sessionName,
147
+ PrivilegeName: privilegeName,
148
+ UserId: this.ObjectId,
149
+ };
150
+ const { data } = await axios.post(
151
+ `${loginApiEndpoint.baseUrl}${loginApiEndpoint.checkPrivileges}`,
152
+ payload,
153
+ {
154
+ headers: {
155
+ 'Content-Type': 'application/json',
156
+ 'x-api-key': loginApiEndpoint.apiKey,
157
+ },
158
+ },
159
+ );
160
+ return data.hasPrivilege;
161
+ }
162
+
102
163
  const userSession = await this._SessionService.retrieveUserSession(
103
164
  this.ObjectId,
104
165
  sessionName,
@@ -113,8 +174,7 @@ export class LoginUser extends User implements ILoginUser {
113
174
  }
114
175
 
115
176
  const privileges = systemLogin.privileges;
116
- const hasPrivilege = privileges.includes(privilegeName);
117
- return hasPrivilege;
177
+ return privileges.includes(privilegeName);
118
178
  } catch (error) {
119
179
  throw error;
120
180
  }
@@ -131,6 +191,31 @@ export class LoginUser extends User implements ILoginUser {
131
191
  if (!sessionName) {
132
192
  throw new Error('Session name is not set');
133
193
  }
194
+
195
+ if (LoginUser.isLoginApiEnabled()) {
196
+ const loginApiEndpoint = ComponentConfig.getComponentConfigValue(
197
+ '@tomei/sso',
198
+ 'loginApiEndpoint',
199
+ );
200
+ let payload = {
201
+ SystemCode: systemCode,
202
+ SessionName: sessionName,
203
+ SessionId: sessionId,
204
+ UserId: userId,
205
+ };
206
+ const { data } = await axios.post(
207
+ `${loginApiEndpoint.baseUrl}${loginApiEndpoint.checkSession}`,
208
+ payload,
209
+ {
210
+ headers: {
211
+ 'Content-Type': 'application/json',
212
+ 'x-api-key': loginApiEndpoint.apiKey,
213
+ },
214
+ },
215
+ );
216
+ return data.systemLogin;
217
+ }
218
+
134
219
  const userSession = await this._SessionService.retrieveUserSession(
135
220
  userId,
136
221
  sessionName,
@@ -328,6 +413,31 @@ export class LoginUser extends User implements ILoginUser {
328
413
  throw new Error('Session name is not set in the configuration');
329
414
  }
330
415
 
416
+ if (LoginUser.isLoginApiEnabled()) {
417
+ const loginApiEndpoint = ComponentConfig.getComponentConfigValue(
418
+ '@tomei/sso',
419
+ 'loginApiEndpoint',
420
+ );
421
+ let payload = {
422
+ SystemCode: systemCode,
423
+ SessionName: sessionName,
424
+ SessionId: sessionId,
425
+ UserId: this.ObjectId,
426
+ };
427
+
428
+ await axios.post(
429
+ `${loginApiEndpoint.baseUrl}${loginApiEndpoint.setSession}`,
430
+ payload,
431
+ {
432
+ headers: {
433
+ 'Content-Type': 'application/json',
434
+ 'x-api-key': loginApiEndpoint.apiKey,
435
+ },
436
+ },
437
+ );
438
+ return;
439
+ }
440
+
331
441
  const userSession = await this._SessionService.retrieveUserSession(
332
442
  this.ObjectId,
333
443
  sessionName,
@@ -344,7 +454,6 @@ export class LoginUser extends User implements ILoginUser {
344
454
  system.code === systemCode ? systemLogin : system,
345
455
  );
346
456
  } else {
347
- // if not, add new system login into the userSession
348
457
  const newLogin = {
349
458
  id: systemCode,
350
459
  code: systemCode,
@@ -353,7 +462,6 @@ export class LoginUser extends User implements ILoginUser {
353
462
  };
354
463
  userSession.systemLogins.push(newLogin);
355
464
  }
356
- // then update userSession inside the redis storage with 1 day duration of time-to-live
357
465
  this._SessionService.setUserSession(
358
466
  this.ObjectId,
359
467
  userSession,
package/src/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from './session';
6
6
  export * from './enum';
7
7
  export * as ssoDb from './database';
8
8
  export * from './types';
9
+ export * from './models/user.entity';