@tomei/sso 0.58.9 → 0.58.10

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.58.9",
3
+ "version": "0.58.10",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,6 +12,7 @@ import { createHash, randomBytes } 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
16
 
16
17
  export class LoginUser extends User implements ILoginUser {
17
18
  session = {
@@ -91,8 +92,15 @@ export class LoginUser extends User implements ILoginUser {
91
92
  throw new Error('ObjectId(UserId) is not set');
92
93
  }
93
94
 
95
+ const sessionName =
96
+ ApplicationConfig.getComponentConfigValue('sessionName');
97
+ if (!sessionName) {
98
+ throw new Error('Session name is not set');
99
+ }
100
+
94
101
  const userSession = await this._SessionService.retrieveUserSession(
95
102
  this.ObjectId,
103
+ sessionName,
96
104
  );
97
105
 
98
106
  const systemLogin = userSession.systemLogins.find(
@@ -117,8 +125,15 @@ export class LoginUser extends User implements ILoginUser {
117
125
  userId: string,
118
126
  ): Promise<ISystemLogin> {
119
127
  try {
120
- const userSession =
121
- await this._SessionService.retrieveUserSession(userId);
128
+ const sessionName =
129
+ ApplicationConfig.getComponentConfigValue('sessionName');
130
+ if (!sessionName) {
131
+ throw new Error('Session name is not set');
132
+ }
133
+ const userSession = await this._SessionService.retrieveUserSession(
134
+ userId,
135
+ sessionName,
136
+ );
122
137
 
123
138
  if (userSession.systemLogins.length === 0) {
124
139
  throw new Error('Session expired.');
@@ -136,7 +151,7 @@ export class LoginUser extends User implements ILoginUser {
136
151
  throw new Error('Session expired.');
137
152
  }
138
153
 
139
- await this._SessionService.refreshDuration(userId);
154
+ await this._SessionService.refreshDuration(userId, sessionName);
140
155
 
141
156
  return systemLogin;
142
157
  } catch (error) {
@@ -149,14 +164,26 @@ export class LoginUser extends User implements ILoginUser {
149
164
  if (!this.ObjectId) {
150
165
  throw new Error('ObjectId(UserId) is not set');
151
166
  }
167
+
168
+ const sessionName =
169
+ ApplicationConfig.getComponentConfigValue('sessionName');
170
+ if (!sessionName) {
171
+ throw new Error('Session name is not set');
172
+ }
173
+
152
174
  const userSession = await this._SessionService.retrieveUserSession(
153
175
  this.ObjectId,
176
+ sessionName,
154
177
  );
155
178
  const index = userSession.systemLogins.findIndex(
156
179
  (system) => system.code === systemCode,
157
180
  );
158
181
  userSession.systemLogins.splice(index, 1);
159
- this._SessionService.setUserSession(this.ObjectId, userSession);
182
+ this._SessionService.setUserSession(
183
+ this.ObjectId,
184
+ userSession,
185
+ sessionName,
186
+ );
160
187
  } catch (error) {
161
188
  throw error;
162
189
  }
@@ -656,8 +656,16 @@ export class User extends UserBase {
656
656
  );
657
657
 
658
658
  // fetch user session if exists
659
+ const sessionName =
660
+ ApplicationConfig.getComponentConfigValue('sessionName');
661
+
662
+ if (!sessionName) {
663
+ throw new Error('Session name is not set in the configuration');
664
+ }
665
+
659
666
  const userSession = await this._SessionService.retrieveUserSession(
660
667
  this.ObjectId,
668
+ sessionName,
661
669
  );
662
670
  const systemLogin = userSession.systemLogins.find(
663
671
  (system) => system.code === systemCode,
@@ -690,7 +698,11 @@ export class User extends UserBase {
690
698
  userSession.systemLogins.push(newLogin);
691
699
  }
692
700
  // then update userSession inside the redis storage with 1 day duration of time-to-live
693
- this._SessionService.setUserSession(this.ObjectId, userSession);
701
+ this._SessionService.setUserSession(
702
+ this.ObjectId,
703
+ userSession,
704
+ sessionName,
705
+ );
694
706
 
695
707
  // record new login history
696
708
  await User._LoginHistoryRepository.create(
@@ -810,8 +822,14 @@ export class User extends UserBase {
810
822
  throw new Error('ObjectId(UserId) is not set');
811
823
  }
812
824
 
825
+ const sessionName =
826
+ ApplicationConfig.getComponentConfigValue('sessionName');
827
+ if (!sessionName) {
828
+ throw new Error('Session name is not set in the configuration');
829
+ }
813
830
  const userSession = await this._SessionService.retrieveUserSession(
814
831
  this.ObjectId,
832
+ sessionName,
815
833
  );
816
834
 
817
835
  const systemLogin = userSession.systemLogins.find(
@@ -1960,8 +1978,19 @@ export class User extends UserBase {
1960
1978
  await user.save({ transaction: dbTransaction });
1961
1979
 
1962
1980
  // 5. Retrieve Session
1981
+ const sessionName =
1982
+ ApplicationConfig.getComponentConfigValue('sessionName');
1983
+
1984
+ if (!sessionName) {
1985
+ throw new ClassError(
1986
+ 'LoginUser',
1987
+ 'LoginUserErrMsg0X',
1988
+ 'Session name is not set in config file',
1989
+ );
1990
+ }
1963
1991
  const userSession = await this._SessionService.retrieveUserSession(
1964
1992
  `${userId}`,
1993
+ sessionName,
1965
1994
  );
1966
1995
 
1967
1996
  if (!systemCode) {
@@ -2021,8 +2050,16 @@ export class User extends UserBase {
2021
2050
  }
2022
2051
 
2023
2052
  // 5. Retrieve Session
2053
+ const sessionName =
2054
+ ApplicationConfig.getComponentConfigValue('sessionName');
2055
+
2056
+ if (!sessionName) {
2057
+ throw new Error('Session name is not set in config file');
2058
+ }
2059
+
2024
2060
  const userSession = await this._SessionService.retrieveUserSession(
2025
2061
  `${userId}`,
2062
+ sessionName,
2026
2063
  );
2027
2064
 
2028
2065
  if (!systemCode) {
@@ -2055,8 +2092,16 @@ export class User extends UserBase {
2055
2092
  }
2056
2093
 
2057
2094
  //Retrieve user session
2095
+ const sessionName =
2096
+ ApplicationConfig.getComponentConfigValue('sessionName');
2097
+
2098
+ if (!sessionName) {
2099
+ throw new Error('Session name is not set in config file');
2100
+ }
2101
+
2058
2102
  const userSession = await this._SessionService.retrieveUserSession(
2059
2103
  `${this.UserId}`,
2104
+ sessionName,
2060
2105
  );
2061
2106
 
2062
2107
  //Retrieve system code
@@ -1,9 +1,21 @@
1
1
  import { IUserSession } from '../../interfaces/user-session.interface';
2
2
 
3
3
  export interface ISessionService {
4
- retrieveUserSession(userId: string): Promise<IUserSession>;
5
- setUserSession(userId: string, sessionData: IUserSession): Promise<void>;
6
- refreshDuration(userId: string): Promise<void>;
4
+ retrieveUserSession(
5
+ userId: string,
6
+ sessionName: string,
7
+ ): Promise<IUserSession>;
8
+ setUserSession(
9
+ userId: string,
10
+ sessionData: IUserSession,
11
+ sessionName: string,
12
+ duration?: number,
13
+ ): Promise<void>;
14
+ refreshDuration(
15
+ userId: string,
16
+ sessionName: string,
17
+ duration?: number,
18
+ ): Promise<void>;
7
19
  setAuthorizationCode(
8
20
  token: string,
9
21
  value: string,
@@ -22,19 +22,23 @@ export class SessionService implements ISessionService {
22
22
  async setUserSession(
23
23
  userId: string,
24
24
  sessionData: IUserSession,
25
+ sessionName: string = 'tomei-sid',
25
26
  duration: number = 60 * 60, //1 hour
26
27
  ): Promise<void> {
27
28
  try {
28
- const key = `${this.environment}tomei-sid:${userId}`;
29
+ const key = `${this.environment}${sessionName}:${userId}`;
29
30
  await SessionService._RedisService.set(key, sessionData, duration);
30
31
  } catch (error) {
31
32
  throw error;
32
33
  }
33
34
  }
34
35
 
35
- async retrieveUserSession(userId: string): Promise<IUserSession> {
36
+ async retrieveUserSession(
37
+ userId: string,
38
+ sessionName: string = 'tomei-sid',
39
+ ): Promise<IUserSession> {
36
40
  try {
37
- const key = `${this.environment}tomei-sid:${userId}`;
41
+ const key = `${this.environment}${sessionName}:${userId}`;
38
42
  const stringData = await SessionService._RedisService.get(key);
39
43
  if (!stringData)
40
44
  return {
@@ -48,11 +52,12 @@ export class SessionService implements ISessionService {
48
52
 
49
53
  async refreshDuration(
50
54
  userid: string,
55
+ sessionName: string = 'tomei-sid',
51
56
  duration: number = 60 * 60,
52
57
  ): Promise<void> {
53
58
  try {
54
- const userSession = await this.retrieveUserSession(userid);
55
- await this.setUserSession(userid, userSession, duration);
59
+ const userSession = await this.retrieveUserSession(userid, sessionName);
60
+ await this.setUserSession(userid, userSession, sessionName, duration);
56
61
  } catch (error) {
57
62
  throw error;
58
63
  }