@etsoo/react 1.3.67 → 1.3.72

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.
@@ -3,6 +3,10 @@ import { IActionResult, IUser } from '@etsoo/appscript';
3
3
  * Service user interface
4
4
  */
5
5
  export interface IServiceUser extends IUser {
6
+ /**
7
+ * Service device id
8
+ */
9
+ serviceDeviceId: string;
6
10
  }
7
11
  /**
8
12
  * Service user login result
@@ -1,4 +1,4 @@
1
- import { IApi, InitCallDto, InitCallResult, RefreshTokenProps } from '@etsoo/appscript';
1
+ import { IApi, RefreshTokenProps } from '@etsoo/appscript';
2
2
  import { IServiceAppSettings } from './IServiceAppSettings';
3
3
  import { IServicePageData } from './IServicePage';
4
4
  import { IServiceUser } from './IServiceUser';
@@ -13,25 +13,23 @@ import { RefreshTokenRQ } from './RefreshTokenRQ';
13
13
  */
14
14
  export declare class ServiceApp<P extends IServicePageData = IServicePageData, U extends IServiceUser = IServiceUser, S extends IServiceAppSettings = IServiceAppSettings> extends ReactApp<S, U, P> {
15
15
  /**
16
- * Core system API
16
+ * Service API
17
17
  */
18
- readonly coreApi: IApi;
18
+ readonly serviceApi: IApi;
19
19
  /**
20
- * Core system user
20
+ * Service user
21
21
  */
22
- coreUser?: ISmartERPUser;
22
+ serviceUser?: ISmartERPUser;
23
+ /**
24
+ * Service passphrase
25
+ */
26
+ servicePassphrase: string;
23
27
  /**
24
28
  * Constructor
25
29
  * @param settings Settings
26
30
  * @param name Application name
27
31
  */
28
32
  constructor(settings: S, name: string);
29
- /**
30
- * Api init call, connect to the core Api
31
- * @param data Data
32
- * @returns Result
33
- */
34
- protected apiInitCall(data: InitCallDto): Promise<InitCallResult | undefined>;
35
33
  /**
36
34
  * Go to the login page
37
35
  */
@@ -42,15 +40,30 @@ export declare class ServiceApp<P extends IServicePageData = IServicePageData, U
42
40
  */
43
41
  refreshToken<D = RefreshTokenRQ>(props?: RefreshTokenProps<D>): Promise<boolean>;
44
42
  private loginFailed;
43
+ /**
44
+ * Service decrypt message
45
+ * @param messageEncrypted Encrypted message
46
+ * @param passphrase Secret passphrase
47
+ * @returns Pure text
48
+ */
49
+ serviceDecrypt(messageEncrypted: string, passphrase?: string): string | undefined;
50
+ /**
51
+ * Service encrypt message
52
+ * @param message Message
53
+ * @param passphrase Secret passphrase
54
+ * @param iterations Iterations, 1000 times, 1 - 99
55
+ * @returns Result
56
+ */
57
+ serviceEncrypt(message: string, passphrase?: string, iterations?: number): string;
45
58
  /**
46
59
  * Try login
47
60
  */
48
61
  tryLogin<D extends {} = {}>(data?: D): Promise<boolean>;
49
62
  /**
50
63
  * User login extended
51
- * @param user New user
64
+ * @param user Core system user
52
65
  * @param refreshToken Refresh token
53
- * @param coreUser Core system user
66
+ * @param serviceUser Service user
54
67
  */
55
- userLoginEx(user: IServiceUser, refreshToken: string, coreUser: ISmartERPUser): void;
68
+ userLoginEx(user: ISmartERPUser, refreshToken: string, serviceUser: IServiceUser): void;
56
69
  }
@@ -16,29 +16,25 @@ export class ServiceApp extends ReactApp {
16
16
  */
17
17
  constructor(settings, name) {
18
18
  super(settings, name);
19
+ /**
20
+ * Service passphrase
21
+ */
22
+ this.servicePassphrase = '';
19
23
  // Check
20
- if (settings.serviceId == null || settings.coreApi == null) {
24
+ if (settings.serviceId == null || settings.serviceEndpoint == null) {
21
25
  throw new Error('No service settings');
22
26
  }
23
- // Core API
27
+ // Service API
24
28
  const api = createClient();
25
- api.baseUrl = settings.coreApi;
29
+ api.baseUrl = settings.serviceEndpoint;
26
30
  this.setApi(api);
27
- this.coreApi = api;
28
- }
29
- /**
30
- * Api init call, connect to the core Api
31
- * @param data Data
32
- * @returns Result
33
- */
34
- async apiInitCall(data) {
35
- return await this.coreApi.put(this.initCallApi, data);
31
+ this.serviceApi = api;
36
32
  }
37
33
  /**
38
34
  * Go to the login page
39
35
  */
40
36
  toLoginPage() {
41
- const coreUrl = this.settings.coreUrl;
37
+ const coreUrl = this.settings.webUrl;
42
38
  window.location.href =
43
39
  coreUrl +
44
40
  '?serviceId=' +
@@ -92,8 +88,8 @@ export class ServiceApp extends ReactApp {
92
88
  // User data
93
89
  const userData = result.data;
94
90
  // Use core system access token to service api to exchange service access token
95
- const serviceResult = await this.api.put('Auth/ExchangeToken', {
96
- token: userData.token
91
+ const serviceResult = await this.serviceApi.put('Auth/ExchangeToken', {
92
+ token: this.encryptEnhanced(userData.token, this.settings.serviceId.toString())
97
93
  }, {
98
94
  showLoading,
99
95
  onError: (error) => {
@@ -116,14 +112,14 @@ export class ServiceApp extends ReactApp {
116
112
  return false;
117
113
  }
118
114
  // Login
119
- this.userLoginEx(serviceResult.data, refreshToken, userData);
115
+ this.userLoginEx(userData, refreshToken, serviceResult.data);
120
116
  // Success callback
121
117
  if (failCallback)
122
118
  failCallback(true);
123
119
  return true;
124
120
  };
125
121
  // Call API
126
- const result = await this.coreApi.put('Auth/RefreshToken', rq, payload);
122
+ const result = await this.api.put('Auth/RefreshToken', rq, payload);
127
123
  if (result == null)
128
124
  return false;
129
125
  if (!result.ok) {
@@ -172,6 +168,25 @@ export class ServiceApp extends ReactApp {
172
168
  this.userUnauthorized();
173
169
  this.toLoginPage();
174
170
  }
171
+ /**
172
+ * Service decrypt message
173
+ * @param messageEncrypted Encrypted message
174
+ * @param passphrase Secret passphrase
175
+ * @returns Pure text
176
+ */
177
+ serviceDecrypt(messageEncrypted, passphrase) {
178
+ return this.decrypt(messageEncrypted, passphrase !== null && passphrase !== void 0 ? passphrase : this.servicePassphrase);
179
+ }
180
+ /**
181
+ * Service encrypt message
182
+ * @param message Message
183
+ * @param passphrase Secret passphrase
184
+ * @param iterations Iterations, 1000 times, 1 - 99
185
+ * @returns Result
186
+ */
187
+ serviceEncrypt(message, passphrase, iterations) {
188
+ return this.encrypt(message, passphrase !== null && passphrase !== void 0 ? passphrase : this.servicePassphrase, iterations);
189
+ }
175
190
  /**
176
191
  * Try login
177
192
  */
@@ -201,16 +216,20 @@ export class ServiceApp extends ReactApp {
201
216
  }
202
217
  /**
203
218
  * User login extended
204
- * @param user New user
219
+ * @param user Core system user
205
220
  * @param refreshToken Refresh token
206
- * @param coreUser Core system user
221
+ * @param serviceUser Service user
207
222
  */
208
- userLoginEx(user, refreshToken, coreUser) {
223
+ userLoginEx(user, refreshToken, serviceUser) {
224
+ var _a;
209
225
  // Service user login
210
- super.userLogin(user, refreshToken, false);
211
- // Core system user
212
- this.coreUser = coreUser;
213
- // Core system API token
214
- this.coreApi.authorize(this.settings.authScheme, coreUser.token);
226
+ this.servicePassphrase =
227
+ (_a = this.decrypt(serviceUser.serviceDeviceId, this.settings.serviceId.toString())) !== null && _a !== void 0 ? _a : '';
228
+ // Keep = true, means service could hold the refresh token for long access
229
+ super.userLogin(user, refreshToken, true);
230
+ // Service user
231
+ this.serviceUser = serviceUser;
232
+ // Service API token
233
+ this.serviceApi.authorize(this.settings.authScheme, serviceUser.token);
215
234
  }
216
235
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.67",
3
+ "version": "1.3.72",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "@emotion/react": "^11.7.1",
51
51
  "@emotion/style": "^0.8.0",
52
52
  "@emotion/styled": "^11.6.0",
53
- "@etsoo/appscript": "^1.1.83",
53
+ "@etsoo/appscript": "^1.1.85",
54
54
  "@etsoo/notificationbase": "^1.0.94",
55
55
  "@etsoo/shared": "^1.0.78",
56
56
  "@mui/icons-material": "^5.2.4",
@@ -3,7 +3,12 @@ import { IActionResult, IUser } from '@etsoo/appscript';
3
3
  /**
4
4
  * Service user interface
5
5
  */
6
- export interface IServiceUser extends IUser {}
6
+ export interface IServiceUser extends IUser {
7
+ /**
8
+ * Service device id
9
+ */
10
+ serviceDeviceId: string;
11
+ }
7
12
 
8
13
  /**
9
14
  * Service user login result
@@ -2,8 +2,6 @@ import {
2
2
  createClient,
3
3
  IApi,
4
4
  IApiPayload,
5
- InitCallDto,
6
- InitCallResult,
7
5
  RefreshTokenProps,
8
6
  RefreshTokenResult
9
7
  } from '@etsoo/appscript';
@@ -28,14 +26,19 @@ export class ServiceApp<
28
26
  S extends IServiceAppSettings = IServiceAppSettings
29
27
  > extends ReactApp<S, U, P> {
30
28
  /**
31
- * Core system API
29
+ * Service API
32
30
  */
33
- readonly coreApi: IApi;
31
+ readonly serviceApi: IApi;
34
32
 
35
33
  /**
36
- * Core system user
34
+ * Service user
37
35
  */
38
- coreUser?: ISmartERPUser;
36
+ serviceUser?: ISmartERPUser;
37
+
38
+ /**
39
+ * Service passphrase
40
+ */
41
+ servicePassphrase: string = '';
39
42
 
40
43
  /**
41
44
  * Constructor
@@ -46,32 +49,23 @@ export class ServiceApp<
46
49
  super(settings, name);
47
50
 
48
51
  // Check
49
- if (settings.serviceId == null || settings.coreApi == null) {
52
+ if (settings.serviceId == null || settings.serviceEndpoint == null) {
50
53
  throw new Error('No service settings');
51
54
  }
52
55
 
53
- // Core API
56
+ // Service API
54
57
  const api = createClient();
55
- api.baseUrl = settings.coreApi;
58
+ api.baseUrl = settings.serviceEndpoint;
56
59
 
57
60
  this.setApi(api);
58
- this.coreApi = api;
59
- }
60
-
61
- /**
62
- * Api init call, connect to the core Api
63
- * @param data Data
64
- * @returns Result
65
- */
66
- protected override async apiInitCall(data: InitCallDto) {
67
- return await this.coreApi.put<InitCallResult>(this.initCallApi, data);
61
+ this.serviceApi = api;
68
62
  }
69
63
 
70
64
  /**
71
65
  * Go to the login page
72
66
  */
73
67
  override toLoginPage() {
74
- const coreUrl = this.settings.coreUrl;
68
+ const coreUrl = this.settings.webUrl;
75
69
  window.location.href =
76
70
  coreUrl +
77
71
  '?serviceId=' +
@@ -140,10 +134,13 @@ export class ServiceApp<
140
134
  const userData = result.data;
141
135
 
142
136
  // Use core system access token to service api to exchange service access token
143
- const serviceResult = await this.api.put<ServiceLoginResult>(
137
+ const serviceResult = await this.serviceApi.put<ServiceLoginResult>(
144
138
  'Auth/ExchangeToken',
145
139
  {
146
- token: userData.token
140
+ token: this.encryptEnhanced(
141
+ userData.token,
142
+ this.settings.serviceId.toString()
143
+ )
147
144
  },
148
145
  {
149
146
  showLoading,
@@ -169,7 +166,7 @@ export class ServiceApp<
169
166
  }
170
167
 
171
168
  // Login
172
- this.userLoginEx(serviceResult.data, refreshToken, userData);
169
+ this.userLoginEx(userData, refreshToken, serviceResult.data);
173
170
 
174
171
  // Success callback
175
172
  if (failCallback) failCallback(true);
@@ -178,7 +175,7 @@ export class ServiceApp<
178
175
  };
179
176
 
180
177
  // Call API
181
- const result = await this.coreApi.put<SmartERPLoginResult>(
178
+ const result = await this.api.put<SmartERPLoginResult>(
182
179
  'Auth/RefreshToken',
183
180
  rq,
184
181
  payload
@@ -253,6 +250,34 @@ export class ServiceApp<
253
250
  this.toLoginPage();
254
251
  }
255
252
 
253
+ /**
254
+ * Service decrypt message
255
+ * @param messageEncrypted Encrypted message
256
+ * @param passphrase Secret passphrase
257
+ * @returns Pure text
258
+ */
259
+ serviceDecrypt(messageEncrypted: string, passphrase?: string) {
260
+ return this.decrypt(
261
+ messageEncrypted,
262
+ passphrase ?? this.servicePassphrase
263
+ );
264
+ }
265
+
266
+ /**
267
+ * Service encrypt message
268
+ * @param message Message
269
+ * @param passphrase Secret passphrase
270
+ * @param iterations Iterations, 1000 times, 1 - 99
271
+ * @returns Result
272
+ */
273
+ serviceEncrypt(message: string, passphrase?: string, iterations?: number) {
274
+ return this.encrypt(
275
+ message,
276
+ passphrase ?? this.servicePassphrase,
277
+ iterations
278
+ );
279
+ }
280
+
256
281
  /**
257
282
  * Try login
258
283
  */
@@ -284,22 +309,29 @@ export class ServiceApp<
284
309
 
285
310
  /**
286
311
  * User login extended
287
- * @param user New user
312
+ * @param user Core system user
288
313
  * @param refreshToken Refresh token
289
- * @param coreUser Core system user
314
+ * @param serviceUser Service user
290
315
  */
291
316
  userLoginEx(
292
- user: IServiceUser,
317
+ user: ISmartERPUser,
293
318
  refreshToken: string,
294
- coreUser: ISmartERPUser
319
+ serviceUser: IServiceUser
295
320
  ): void {
296
321
  // Service user login
297
- super.userLogin(user, refreshToken, false);
322
+ this.servicePassphrase =
323
+ this.decrypt(
324
+ serviceUser.serviceDeviceId,
325
+ this.settings.serviceId.toString()
326
+ ) ?? '';
327
+
328
+ // Keep = true, means service could hold the refresh token for long access
329
+ super.userLogin(user, refreshToken, true);
298
330
 
299
- // Core system user
300
- this.coreUser = coreUser;
331
+ // Service user
332
+ this.serviceUser = serviceUser;
301
333
 
302
- // Core system API token
303
- this.coreApi.authorize(this.settings.authScheme, coreUser.token);
334
+ // Service API token
335
+ this.serviceApi.authorize(this.settings.authScheme, serviceUser.token);
304
336
  }
305
337
  }