@etsoo/appscript 1.2.44 → 1.2.48
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/lib/cjs/app/CoreApp.d.ts +25 -22
- package/lib/cjs/app/CoreApp.js +48 -49
- package/lib/mjs/app/CoreApp.d.ts +25 -22
- package/lib/mjs/app/CoreApp.js +48 -49
- package/package.json +1 -1
- package/src/app/CoreApp.ts +69 -54
package/lib/cjs/app/CoreApp.d.ts
CHANGED
|
@@ -43,6 +43,16 @@ export interface RefreshTokenProps<D extends {}> {
|
|
|
43
43
|
*/
|
|
44
44
|
showLoading?: boolean;
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* App fields
|
|
48
|
+
*/
|
|
49
|
+
declare const appFields: readonly ["headerToken", "serversideDeviceId", "deviceId", "devices", "devicePassphrase"];
|
|
50
|
+
/**
|
|
51
|
+
* Basic type template
|
|
52
|
+
*/
|
|
53
|
+
export declare type IAppFields = {
|
|
54
|
+
[key in typeof appFields[number]]: string;
|
|
55
|
+
};
|
|
46
56
|
/**
|
|
47
57
|
* Core application interface
|
|
48
58
|
*/
|
|
@@ -51,6 +61,10 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
|
|
|
51
61
|
* Settings
|
|
52
62
|
*/
|
|
53
63
|
readonly settings: S;
|
|
64
|
+
/**
|
|
65
|
+
* Fields
|
|
66
|
+
*/
|
|
67
|
+
readonly fields: IAppFields;
|
|
54
68
|
/**
|
|
55
69
|
* API
|
|
56
70
|
*/
|
|
@@ -371,6 +385,10 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
|
|
|
371
385
|
* Settings
|
|
372
386
|
*/
|
|
373
387
|
readonly settings: S;
|
|
388
|
+
/**
|
|
389
|
+
* Fields
|
|
390
|
+
*/
|
|
391
|
+
readonly fields: IAppFields;
|
|
374
392
|
/**
|
|
375
393
|
* API
|
|
376
394
|
*/
|
|
@@ -467,6 +485,12 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
|
|
|
467
485
|
protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
|
|
468
486
|
private getDeviceId;
|
|
469
487
|
private resetKeys;
|
|
488
|
+
/**
|
|
489
|
+
* Add app name as identifier
|
|
490
|
+
* @param field Field
|
|
491
|
+
* @returns Result
|
|
492
|
+
*/
|
|
493
|
+
protected addIdentifier(field: string): string;
|
|
470
494
|
/**
|
|
471
495
|
* Restore settings from persisted source
|
|
472
496
|
*/
|
|
@@ -773,25 +797,4 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
|
|
|
773
797
|
*/
|
|
774
798
|
warning(message: NotificationContent<N>, align?: NotificationAlign): void;
|
|
775
799
|
}
|
|
776
|
-
export
|
|
777
|
-
/**
|
|
778
|
-
* Response token header field name
|
|
779
|
-
*/
|
|
780
|
-
const headerTokenField = "SmartERPRefreshToken";
|
|
781
|
-
/**
|
|
782
|
-
* Serverside device id encrypted field name
|
|
783
|
-
*/
|
|
784
|
-
const serversideDeviceIdField = "SmartERPServersideDeviceId";
|
|
785
|
-
/**
|
|
786
|
-
* Device id field name
|
|
787
|
-
*/
|
|
788
|
-
const deviceIdField = "SmartERPDeviceId";
|
|
789
|
-
/**
|
|
790
|
-
* Devices field name
|
|
791
|
-
*/
|
|
792
|
-
const devicesField = "SmartERPDevices";
|
|
793
|
-
/**
|
|
794
|
-
* Device passphrase field name
|
|
795
|
-
*/
|
|
796
|
-
const devicePassphraseField = "SmartERPDevicePassphrase";
|
|
797
|
-
}
|
|
800
|
+
export {};
|
package/lib/cjs/app/CoreApp.js
CHANGED
|
@@ -9,6 +9,16 @@ const AddressRegion_1 = require("../address/AddressRegion");
|
|
|
9
9
|
const AddressUtils_1 = require("../address/AddressUtils");
|
|
10
10
|
const BusinessUtils_1 = require("../business/BusinessUtils");
|
|
11
11
|
const ActionResultError_1 = require("../result/ActionResultError");
|
|
12
|
+
/**
|
|
13
|
+
* App fields
|
|
14
|
+
*/
|
|
15
|
+
const appFields = [
|
|
16
|
+
'headerToken',
|
|
17
|
+
'serversideDeviceId',
|
|
18
|
+
'deviceId',
|
|
19
|
+
'devices',
|
|
20
|
+
'devicePassphrase'
|
|
21
|
+
];
|
|
12
22
|
/**
|
|
13
23
|
* Core application
|
|
14
24
|
*/
|
|
@@ -45,8 +55,10 @@ class CoreApp {
|
|
|
45
55
|
this.notifier = notifier;
|
|
46
56
|
this.storage = storage;
|
|
47
57
|
this.name = name;
|
|
58
|
+
// Fields, attach with the name identifier
|
|
59
|
+
this.fields = appFields.reduce((a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }), {});
|
|
48
60
|
// Device id
|
|
49
|
-
this._deviceId = storage.getData(
|
|
61
|
+
this._deviceId = storage.getData(this.fields.deviceId, '');
|
|
50
62
|
// Restore
|
|
51
63
|
this.restore();
|
|
52
64
|
this.setApi(api);
|
|
@@ -118,10 +130,10 @@ class CoreApp {
|
|
|
118
130
|
*/
|
|
119
131
|
get persistedFields() {
|
|
120
132
|
return [
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
133
|
+
this.fields.deviceId,
|
|
134
|
+
this.fields.devicePassphrase,
|
|
135
|
+
this.fields.serversideDeviceId,
|
|
136
|
+
this.fields.headerToken
|
|
125
137
|
];
|
|
126
138
|
}
|
|
127
139
|
getDeviceId() {
|
|
@@ -129,23 +141,31 @@ class CoreApp {
|
|
|
129
141
|
}
|
|
130
142
|
resetKeys() {
|
|
131
143
|
this.storage.clear([
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
144
|
+
this.fields.devicePassphrase,
|
|
145
|
+
this.fields.headerToken,
|
|
146
|
+
this.fields.serversideDeviceId
|
|
135
147
|
], false);
|
|
136
148
|
this.passphrase = '';
|
|
137
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Add app name as identifier
|
|
152
|
+
* @param field Field
|
|
153
|
+
* @returns Result
|
|
154
|
+
*/
|
|
155
|
+
addIdentifier(field) {
|
|
156
|
+
return field + '-' + this.name;
|
|
157
|
+
}
|
|
138
158
|
/**
|
|
139
159
|
* Restore settings from persisted source
|
|
140
160
|
*/
|
|
141
161
|
restore() {
|
|
142
162
|
// Devices
|
|
143
|
-
const devices = this.storage.getPersistedData(
|
|
163
|
+
const devices = this.storage.getPersistedData(this.fields.devices, []);
|
|
144
164
|
if (this.deviceId === '') {
|
|
145
165
|
// First vist, restore and keep the source
|
|
146
166
|
this.storage.copyFrom(this.persistedFields, false);
|
|
147
167
|
// Reset device id
|
|
148
|
-
this._deviceId = this.storage.getData(
|
|
168
|
+
this._deviceId = this.storage.getData(this.fields.deviceId, '');
|
|
149
169
|
// Totally new, no data restored
|
|
150
170
|
if (this._deviceId === '')
|
|
151
171
|
return false;
|
|
@@ -158,13 +178,14 @@ class CoreApp {
|
|
|
158
178
|
this.resetKeys();
|
|
159
179
|
return false;
|
|
160
180
|
}
|
|
161
|
-
|
|
181
|
+
// this.name to identifier different app's secret
|
|
182
|
+
const passphraseEncrypted = this.storage.getData(this.fields.devicePassphrase);
|
|
162
183
|
if (passphraseEncrypted) {
|
|
163
184
|
const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
|
|
164
185
|
if (passphraseDecrypted != null) {
|
|
165
186
|
// Add the device to the list
|
|
166
187
|
devices.push(d);
|
|
167
|
-
this.storage.setPersistedData(
|
|
188
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
168
189
|
this.passphrase = passphraseDecrypted;
|
|
169
190
|
return true;
|
|
170
191
|
}
|
|
@@ -178,13 +199,13 @@ class CoreApp {
|
|
|
178
199
|
*/
|
|
179
200
|
persist() {
|
|
180
201
|
// Devices
|
|
181
|
-
const devices = this.storage.getPersistedData(
|
|
202
|
+
const devices = this.storage.getPersistedData(this.fields.devices);
|
|
182
203
|
if (devices != null) {
|
|
183
204
|
const index = devices.indexOf(this.getDeviceId());
|
|
184
205
|
if (index !== -1) {
|
|
185
206
|
// Remove current device from the list
|
|
186
207
|
devices.splice(index, 1);
|
|
187
|
-
this.storage.setPersistedData(
|
|
208
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
188
209
|
}
|
|
189
210
|
}
|
|
190
211
|
if (!this.authorized)
|
|
@@ -247,7 +268,7 @@ class CoreApp {
|
|
|
247
268
|
return;
|
|
248
269
|
}
|
|
249
270
|
// Serverside encrypted device id
|
|
250
|
-
const identifier = this.storage.getData(
|
|
271
|
+
const identifier = this.storage.getData(this.fields.serversideDeviceId);
|
|
251
272
|
// Timestamp
|
|
252
273
|
const timestamp = new Date().getTime();
|
|
253
274
|
// Request data
|
|
@@ -283,7 +304,7 @@ class CoreApp {
|
|
|
283
304
|
if (callback)
|
|
284
305
|
callback(false);
|
|
285
306
|
// Clear device id
|
|
286
|
-
this.storage.setData(
|
|
307
|
+
this.storage.setData(this.fields.deviceId, undefined);
|
|
287
308
|
return;
|
|
288
309
|
}
|
|
289
310
|
this.initCallUpdate(result.data, data.timestamp);
|
|
@@ -306,14 +327,14 @@ class CoreApp {
|
|
|
306
327
|
return;
|
|
307
328
|
// Update device id and cache it
|
|
308
329
|
this._deviceId = data.deviceId;
|
|
309
|
-
this.storage.setData(
|
|
330
|
+
this.storage.setData(this.fields.deviceId, this._deviceId);
|
|
310
331
|
// Devices
|
|
311
|
-
const devices = this.storage.getPersistedData(
|
|
332
|
+
const devices = this.storage.getPersistedData(this.fields.devices, []);
|
|
312
333
|
devices.push(this.getDeviceId());
|
|
313
|
-
this.storage.setPersistedData(
|
|
334
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
314
335
|
// Current passphrase
|
|
315
336
|
this.passphrase = passphrase;
|
|
316
|
-
this.storage.setData(
|
|
337
|
+
this.storage.setData(this.fields.devicePassphrase, this.encrypt(passphrase, this.name));
|
|
317
338
|
// Previous passphrase
|
|
318
339
|
if (data.previousPassphrase) {
|
|
319
340
|
const prev = this.decrypt(data.previousPassphrase, timestamp.toString());
|
|
@@ -345,7 +366,7 @@ class CoreApp {
|
|
|
345
366
|
* @returns Fields
|
|
346
367
|
*/
|
|
347
368
|
initCallEncryptedUpdateFields() {
|
|
348
|
-
return [
|
|
369
|
+
return [this.fields.headerToken];
|
|
349
370
|
}
|
|
350
371
|
/**
|
|
351
372
|
* Alert action result
|
|
@@ -370,7 +391,7 @@ class CoreApp {
|
|
|
370
391
|
if (refreshToken !== '') {
|
|
371
392
|
if (refreshToken != null)
|
|
372
393
|
refreshToken = this.encrypt(refreshToken);
|
|
373
|
-
this.storage.setData(
|
|
394
|
+
this.storage.setData(this.fields.headerToken, refreshToken);
|
|
374
395
|
}
|
|
375
396
|
// Reset tryLogin state
|
|
376
397
|
this._isTryingLogin = false;
|
|
@@ -443,14 +464,14 @@ class CoreApp {
|
|
|
443
464
|
*/
|
|
444
465
|
clearCacheData() {
|
|
445
466
|
this.clearCacheToken();
|
|
446
|
-
this.storage.setData(
|
|
467
|
+
this.storage.setData(this.fields.devicePassphrase, undefined);
|
|
447
468
|
}
|
|
448
469
|
/**
|
|
449
470
|
* Clear cached token
|
|
450
471
|
*/
|
|
451
472
|
clearCacheToken() {
|
|
452
473
|
this.cachedRefreshToken = undefined;
|
|
453
|
-
this.storage.setPersistedData(
|
|
474
|
+
this.storage.setPersistedData(this.fields.headerToken, undefined);
|
|
454
475
|
}
|
|
455
476
|
/**
|
|
456
477
|
* Decrypt message
|
|
@@ -705,7 +726,7 @@ class CoreApp {
|
|
|
705
726
|
// Temp refresh token
|
|
706
727
|
if (this.cachedRefreshToken)
|
|
707
728
|
return this.cachedRefreshToken;
|
|
708
|
-
return this.storage.getData(
|
|
729
|
+
return this.storage.getData(this.fields.headerToken);
|
|
709
730
|
}
|
|
710
731
|
/**
|
|
711
732
|
* Get all regions
|
|
@@ -732,7 +753,7 @@ class CoreApp {
|
|
|
732
753
|
*/
|
|
733
754
|
getResponseToken(rawResponse) {
|
|
734
755
|
const response = this.api.transformResponse(rawResponse);
|
|
735
|
-
return this.api.getHeaderValue(response.headers,
|
|
756
|
+
return this.api.getHeaderValue(response.headers, 'SmartERPRefreshToken');
|
|
736
757
|
}
|
|
737
758
|
/**
|
|
738
759
|
* Get time zone
|
|
@@ -951,7 +972,7 @@ class CoreApp {
|
|
|
951
972
|
userLogin(user, refreshToken, keep) {
|
|
952
973
|
this.userData = user;
|
|
953
974
|
// Cache the encrypted serverside device id
|
|
954
|
-
this.storage.setData(
|
|
975
|
+
this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
|
|
955
976
|
if (keep) {
|
|
956
977
|
this.authorize(user.token, refreshToken);
|
|
957
978
|
}
|
|
@@ -989,25 +1010,3 @@ class CoreApp {
|
|
|
989
1010
|
}
|
|
990
1011
|
}
|
|
991
1012
|
exports.CoreApp = CoreApp;
|
|
992
|
-
(function (CoreApp) {
|
|
993
|
-
/**
|
|
994
|
-
* Response token header field name
|
|
995
|
-
*/
|
|
996
|
-
CoreApp.headerTokenField = 'SmartERPRefreshToken';
|
|
997
|
-
/**
|
|
998
|
-
* Serverside device id encrypted field name
|
|
999
|
-
*/
|
|
1000
|
-
CoreApp.serversideDeviceIdField = 'SmartERPServersideDeviceId';
|
|
1001
|
-
/**
|
|
1002
|
-
* Device id field name
|
|
1003
|
-
*/
|
|
1004
|
-
CoreApp.deviceIdField = 'SmartERPDeviceId';
|
|
1005
|
-
/**
|
|
1006
|
-
* Devices field name
|
|
1007
|
-
*/
|
|
1008
|
-
CoreApp.devicesField = 'SmartERPDevices';
|
|
1009
|
-
/**
|
|
1010
|
-
* Device passphrase field name
|
|
1011
|
-
*/
|
|
1012
|
-
CoreApp.devicePassphraseField = 'SmartERPDevicePassphrase';
|
|
1013
|
-
})(CoreApp = exports.CoreApp || (exports.CoreApp = {}));
|
package/lib/mjs/app/CoreApp.d.ts
CHANGED
|
@@ -43,6 +43,16 @@ export interface RefreshTokenProps<D extends {}> {
|
|
|
43
43
|
*/
|
|
44
44
|
showLoading?: boolean;
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* App fields
|
|
48
|
+
*/
|
|
49
|
+
declare const appFields: readonly ["headerToken", "serversideDeviceId", "deviceId", "devices", "devicePassphrase"];
|
|
50
|
+
/**
|
|
51
|
+
* Basic type template
|
|
52
|
+
*/
|
|
53
|
+
export declare type IAppFields = {
|
|
54
|
+
[key in typeof appFields[number]]: string;
|
|
55
|
+
};
|
|
46
56
|
/**
|
|
47
57
|
* Core application interface
|
|
48
58
|
*/
|
|
@@ -51,6 +61,10 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
|
|
|
51
61
|
* Settings
|
|
52
62
|
*/
|
|
53
63
|
readonly settings: S;
|
|
64
|
+
/**
|
|
65
|
+
* Fields
|
|
66
|
+
*/
|
|
67
|
+
readonly fields: IAppFields;
|
|
54
68
|
/**
|
|
55
69
|
* API
|
|
56
70
|
*/
|
|
@@ -371,6 +385,10 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
|
|
|
371
385
|
* Settings
|
|
372
386
|
*/
|
|
373
387
|
readonly settings: S;
|
|
388
|
+
/**
|
|
389
|
+
* Fields
|
|
390
|
+
*/
|
|
391
|
+
readonly fields: IAppFields;
|
|
374
392
|
/**
|
|
375
393
|
* API
|
|
376
394
|
*/
|
|
@@ -467,6 +485,12 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
|
|
|
467
485
|
protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
|
|
468
486
|
private getDeviceId;
|
|
469
487
|
private resetKeys;
|
|
488
|
+
/**
|
|
489
|
+
* Add app name as identifier
|
|
490
|
+
* @param field Field
|
|
491
|
+
* @returns Result
|
|
492
|
+
*/
|
|
493
|
+
protected addIdentifier(field: string): string;
|
|
470
494
|
/**
|
|
471
495
|
* Restore settings from persisted source
|
|
472
496
|
*/
|
|
@@ -773,25 +797,4 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
|
|
|
773
797
|
*/
|
|
774
798
|
warning(message: NotificationContent<N>, align?: NotificationAlign): void;
|
|
775
799
|
}
|
|
776
|
-
export
|
|
777
|
-
/**
|
|
778
|
-
* Response token header field name
|
|
779
|
-
*/
|
|
780
|
-
const headerTokenField = "SmartERPRefreshToken";
|
|
781
|
-
/**
|
|
782
|
-
* Serverside device id encrypted field name
|
|
783
|
-
*/
|
|
784
|
-
const serversideDeviceIdField = "SmartERPServersideDeviceId";
|
|
785
|
-
/**
|
|
786
|
-
* Device id field name
|
|
787
|
-
*/
|
|
788
|
-
const deviceIdField = "SmartERPDeviceId";
|
|
789
|
-
/**
|
|
790
|
-
* Devices field name
|
|
791
|
-
*/
|
|
792
|
-
const devicesField = "SmartERPDevices";
|
|
793
|
-
/**
|
|
794
|
-
* Device passphrase field name
|
|
795
|
-
*/
|
|
796
|
-
const devicePassphraseField = "SmartERPDevicePassphrase";
|
|
797
|
-
}
|
|
800
|
+
export {};
|
package/lib/mjs/app/CoreApp.js
CHANGED
|
@@ -6,6 +6,16 @@ import { AddressRegion } from '../address/AddressRegion';
|
|
|
6
6
|
import { AddressUtils } from '../address/AddressUtils';
|
|
7
7
|
import { BusinessUtils } from '../business/BusinessUtils';
|
|
8
8
|
import { ActionResultError } from '../result/ActionResultError';
|
|
9
|
+
/**
|
|
10
|
+
* App fields
|
|
11
|
+
*/
|
|
12
|
+
const appFields = [
|
|
13
|
+
'headerToken',
|
|
14
|
+
'serversideDeviceId',
|
|
15
|
+
'deviceId',
|
|
16
|
+
'devices',
|
|
17
|
+
'devicePassphrase'
|
|
18
|
+
];
|
|
9
19
|
/**
|
|
10
20
|
* Core application
|
|
11
21
|
*/
|
|
@@ -42,8 +52,10 @@ export class CoreApp {
|
|
|
42
52
|
this.notifier = notifier;
|
|
43
53
|
this.storage = storage;
|
|
44
54
|
this.name = name;
|
|
55
|
+
// Fields, attach with the name identifier
|
|
56
|
+
this.fields = appFields.reduce((a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }), {});
|
|
45
57
|
// Device id
|
|
46
|
-
this._deviceId = storage.getData(
|
|
58
|
+
this._deviceId = storage.getData(this.fields.deviceId, '');
|
|
47
59
|
// Restore
|
|
48
60
|
this.restore();
|
|
49
61
|
this.setApi(api);
|
|
@@ -115,10 +127,10 @@ export class CoreApp {
|
|
|
115
127
|
*/
|
|
116
128
|
get persistedFields() {
|
|
117
129
|
return [
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
130
|
+
this.fields.deviceId,
|
|
131
|
+
this.fields.devicePassphrase,
|
|
132
|
+
this.fields.serversideDeviceId,
|
|
133
|
+
this.fields.headerToken
|
|
122
134
|
];
|
|
123
135
|
}
|
|
124
136
|
getDeviceId() {
|
|
@@ -126,23 +138,31 @@ export class CoreApp {
|
|
|
126
138
|
}
|
|
127
139
|
resetKeys() {
|
|
128
140
|
this.storage.clear([
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
141
|
+
this.fields.devicePassphrase,
|
|
142
|
+
this.fields.headerToken,
|
|
143
|
+
this.fields.serversideDeviceId
|
|
132
144
|
], false);
|
|
133
145
|
this.passphrase = '';
|
|
134
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Add app name as identifier
|
|
149
|
+
* @param field Field
|
|
150
|
+
* @returns Result
|
|
151
|
+
*/
|
|
152
|
+
addIdentifier(field) {
|
|
153
|
+
return field + '-' + this.name;
|
|
154
|
+
}
|
|
135
155
|
/**
|
|
136
156
|
* Restore settings from persisted source
|
|
137
157
|
*/
|
|
138
158
|
restore() {
|
|
139
159
|
// Devices
|
|
140
|
-
const devices = this.storage.getPersistedData(
|
|
160
|
+
const devices = this.storage.getPersistedData(this.fields.devices, []);
|
|
141
161
|
if (this.deviceId === '') {
|
|
142
162
|
// First vist, restore and keep the source
|
|
143
163
|
this.storage.copyFrom(this.persistedFields, false);
|
|
144
164
|
// Reset device id
|
|
145
|
-
this._deviceId = this.storage.getData(
|
|
165
|
+
this._deviceId = this.storage.getData(this.fields.deviceId, '');
|
|
146
166
|
// Totally new, no data restored
|
|
147
167
|
if (this._deviceId === '')
|
|
148
168
|
return false;
|
|
@@ -155,13 +175,14 @@ export class CoreApp {
|
|
|
155
175
|
this.resetKeys();
|
|
156
176
|
return false;
|
|
157
177
|
}
|
|
158
|
-
|
|
178
|
+
// this.name to identifier different app's secret
|
|
179
|
+
const passphraseEncrypted = this.storage.getData(this.fields.devicePassphrase);
|
|
159
180
|
if (passphraseEncrypted) {
|
|
160
181
|
const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
|
|
161
182
|
if (passphraseDecrypted != null) {
|
|
162
183
|
// Add the device to the list
|
|
163
184
|
devices.push(d);
|
|
164
|
-
this.storage.setPersistedData(
|
|
185
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
165
186
|
this.passphrase = passphraseDecrypted;
|
|
166
187
|
return true;
|
|
167
188
|
}
|
|
@@ -175,13 +196,13 @@ export class CoreApp {
|
|
|
175
196
|
*/
|
|
176
197
|
persist() {
|
|
177
198
|
// Devices
|
|
178
|
-
const devices = this.storage.getPersistedData(
|
|
199
|
+
const devices = this.storage.getPersistedData(this.fields.devices);
|
|
179
200
|
if (devices != null) {
|
|
180
201
|
const index = devices.indexOf(this.getDeviceId());
|
|
181
202
|
if (index !== -1) {
|
|
182
203
|
// Remove current device from the list
|
|
183
204
|
devices.splice(index, 1);
|
|
184
|
-
this.storage.setPersistedData(
|
|
205
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
185
206
|
}
|
|
186
207
|
}
|
|
187
208
|
if (!this.authorized)
|
|
@@ -244,7 +265,7 @@ export class CoreApp {
|
|
|
244
265
|
return;
|
|
245
266
|
}
|
|
246
267
|
// Serverside encrypted device id
|
|
247
|
-
const identifier = this.storage.getData(
|
|
268
|
+
const identifier = this.storage.getData(this.fields.serversideDeviceId);
|
|
248
269
|
// Timestamp
|
|
249
270
|
const timestamp = new Date().getTime();
|
|
250
271
|
// Request data
|
|
@@ -280,7 +301,7 @@ export class CoreApp {
|
|
|
280
301
|
if (callback)
|
|
281
302
|
callback(false);
|
|
282
303
|
// Clear device id
|
|
283
|
-
this.storage.setData(
|
|
304
|
+
this.storage.setData(this.fields.deviceId, undefined);
|
|
284
305
|
return;
|
|
285
306
|
}
|
|
286
307
|
this.initCallUpdate(result.data, data.timestamp);
|
|
@@ -303,14 +324,14 @@ export class CoreApp {
|
|
|
303
324
|
return;
|
|
304
325
|
// Update device id and cache it
|
|
305
326
|
this._deviceId = data.deviceId;
|
|
306
|
-
this.storage.setData(
|
|
327
|
+
this.storage.setData(this.fields.deviceId, this._deviceId);
|
|
307
328
|
// Devices
|
|
308
|
-
const devices = this.storage.getPersistedData(
|
|
329
|
+
const devices = this.storage.getPersistedData(this.fields.devices, []);
|
|
309
330
|
devices.push(this.getDeviceId());
|
|
310
|
-
this.storage.setPersistedData(
|
|
331
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
311
332
|
// Current passphrase
|
|
312
333
|
this.passphrase = passphrase;
|
|
313
|
-
this.storage.setData(
|
|
334
|
+
this.storage.setData(this.fields.devicePassphrase, this.encrypt(passphrase, this.name));
|
|
314
335
|
// Previous passphrase
|
|
315
336
|
if (data.previousPassphrase) {
|
|
316
337
|
const prev = this.decrypt(data.previousPassphrase, timestamp.toString());
|
|
@@ -342,7 +363,7 @@ export class CoreApp {
|
|
|
342
363
|
* @returns Fields
|
|
343
364
|
*/
|
|
344
365
|
initCallEncryptedUpdateFields() {
|
|
345
|
-
return [
|
|
366
|
+
return [this.fields.headerToken];
|
|
346
367
|
}
|
|
347
368
|
/**
|
|
348
369
|
* Alert action result
|
|
@@ -367,7 +388,7 @@ export class CoreApp {
|
|
|
367
388
|
if (refreshToken !== '') {
|
|
368
389
|
if (refreshToken != null)
|
|
369
390
|
refreshToken = this.encrypt(refreshToken);
|
|
370
|
-
this.storage.setData(
|
|
391
|
+
this.storage.setData(this.fields.headerToken, refreshToken);
|
|
371
392
|
}
|
|
372
393
|
// Reset tryLogin state
|
|
373
394
|
this._isTryingLogin = false;
|
|
@@ -440,14 +461,14 @@ export class CoreApp {
|
|
|
440
461
|
*/
|
|
441
462
|
clearCacheData() {
|
|
442
463
|
this.clearCacheToken();
|
|
443
|
-
this.storage.setData(
|
|
464
|
+
this.storage.setData(this.fields.devicePassphrase, undefined);
|
|
444
465
|
}
|
|
445
466
|
/**
|
|
446
467
|
* Clear cached token
|
|
447
468
|
*/
|
|
448
469
|
clearCacheToken() {
|
|
449
470
|
this.cachedRefreshToken = undefined;
|
|
450
|
-
this.storage.setPersistedData(
|
|
471
|
+
this.storage.setPersistedData(this.fields.headerToken, undefined);
|
|
451
472
|
}
|
|
452
473
|
/**
|
|
453
474
|
* Decrypt message
|
|
@@ -702,7 +723,7 @@ export class CoreApp {
|
|
|
702
723
|
// Temp refresh token
|
|
703
724
|
if (this.cachedRefreshToken)
|
|
704
725
|
return this.cachedRefreshToken;
|
|
705
|
-
return this.storage.getData(
|
|
726
|
+
return this.storage.getData(this.fields.headerToken);
|
|
706
727
|
}
|
|
707
728
|
/**
|
|
708
729
|
* Get all regions
|
|
@@ -729,7 +750,7 @@ export class CoreApp {
|
|
|
729
750
|
*/
|
|
730
751
|
getResponseToken(rawResponse) {
|
|
731
752
|
const response = this.api.transformResponse(rawResponse);
|
|
732
|
-
return this.api.getHeaderValue(response.headers,
|
|
753
|
+
return this.api.getHeaderValue(response.headers, 'SmartERPRefreshToken');
|
|
733
754
|
}
|
|
734
755
|
/**
|
|
735
756
|
* Get time zone
|
|
@@ -948,7 +969,7 @@ export class CoreApp {
|
|
|
948
969
|
userLogin(user, refreshToken, keep) {
|
|
949
970
|
this.userData = user;
|
|
950
971
|
// Cache the encrypted serverside device id
|
|
951
|
-
this.storage.setData(
|
|
972
|
+
this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
|
|
952
973
|
if (keep) {
|
|
953
974
|
this.authorize(user.token, refreshToken);
|
|
954
975
|
}
|
|
@@ -985,25 +1006,3 @@ export class CoreApp {
|
|
|
985
1006
|
});
|
|
986
1007
|
}
|
|
987
1008
|
}
|
|
988
|
-
(function (CoreApp) {
|
|
989
|
-
/**
|
|
990
|
-
* Response token header field name
|
|
991
|
-
*/
|
|
992
|
-
CoreApp.headerTokenField = 'SmartERPRefreshToken';
|
|
993
|
-
/**
|
|
994
|
-
* Serverside device id encrypted field name
|
|
995
|
-
*/
|
|
996
|
-
CoreApp.serversideDeviceIdField = 'SmartERPServersideDeviceId';
|
|
997
|
-
/**
|
|
998
|
-
* Device id field name
|
|
999
|
-
*/
|
|
1000
|
-
CoreApp.deviceIdField = 'SmartERPDeviceId';
|
|
1001
|
-
/**
|
|
1002
|
-
* Devices field name
|
|
1003
|
-
*/
|
|
1004
|
-
CoreApp.devicesField = 'SmartERPDevices';
|
|
1005
|
-
/**
|
|
1006
|
-
* Device passphrase field name
|
|
1007
|
-
*/
|
|
1008
|
-
CoreApp.devicePassphraseField = 'SmartERPDevicePassphrase';
|
|
1009
|
-
})(CoreApp || (CoreApp = {}));
|
package/package.json
CHANGED
package/src/app/CoreApp.ts
CHANGED
|
@@ -83,6 +83,22 @@ export interface RefreshTokenProps<D extends {}> {
|
|
|
83
83
|
showLoading?: boolean;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/**
|
|
87
|
+
* App fields
|
|
88
|
+
*/
|
|
89
|
+
const appFields = [
|
|
90
|
+
'headerToken',
|
|
91
|
+
'serversideDeviceId',
|
|
92
|
+
'deviceId',
|
|
93
|
+
'devices',
|
|
94
|
+
'devicePassphrase'
|
|
95
|
+
] as const;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Basic type template
|
|
99
|
+
*/
|
|
100
|
+
export type IAppFields = { [key in typeof appFields[number]]: string };
|
|
101
|
+
|
|
86
102
|
/**
|
|
87
103
|
* Core application interface
|
|
88
104
|
*/
|
|
@@ -96,6 +112,11 @@ export interface ICoreApp<
|
|
|
96
112
|
*/
|
|
97
113
|
readonly settings: S;
|
|
98
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Fields
|
|
117
|
+
*/
|
|
118
|
+
readonly fields: IAppFields;
|
|
119
|
+
|
|
99
120
|
/**
|
|
100
121
|
* API
|
|
101
122
|
*/
|
|
@@ -502,6 +523,11 @@ export abstract class CoreApp<
|
|
|
502
523
|
*/
|
|
503
524
|
readonly settings: S;
|
|
504
525
|
|
|
526
|
+
/**
|
|
527
|
+
* Fields
|
|
528
|
+
*/
|
|
529
|
+
readonly fields: IAppFields;
|
|
530
|
+
|
|
505
531
|
/**
|
|
506
532
|
* API
|
|
507
533
|
*/
|
|
@@ -632,10 +658,10 @@ export abstract class CoreApp<
|
|
|
632
658
|
*/
|
|
633
659
|
protected get persistedFields() {
|
|
634
660
|
return [
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
661
|
+
this.fields.deviceId,
|
|
662
|
+
this.fields.devicePassphrase,
|
|
663
|
+
this.fields.serversideDeviceId,
|
|
664
|
+
this.fields.headerToken
|
|
639
665
|
];
|
|
640
666
|
}
|
|
641
667
|
|
|
@@ -660,8 +686,14 @@ export abstract class CoreApp<
|
|
|
660
686
|
this.storage = storage;
|
|
661
687
|
this.name = name;
|
|
662
688
|
|
|
689
|
+
// Fields, attach with the name identifier
|
|
690
|
+
this.fields = appFields.reduce(
|
|
691
|
+
(a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }),
|
|
692
|
+
{} as any
|
|
693
|
+
);
|
|
694
|
+
|
|
663
695
|
// Device id
|
|
664
|
-
this._deviceId = storage.getData(
|
|
696
|
+
this._deviceId = storage.getData(this.fields.deviceId, '');
|
|
665
697
|
|
|
666
698
|
// Restore
|
|
667
699
|
this.restore();
|
|
@@ -684,22 +716,31 @@ export abstract class CoreApp<
|
|
|
684
716
|
private resetKeys() {
|
|
685
717
|
this.storage.clear(
|
|
686
718
|
[
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
719
|
+
this.fields.devicePassphrase,
|
|
720
|
+
this.fields.headerToken,
|
|
721
|
+
this.fields.serversideDeviceId
|
|
690
722
|
],
|
|
691
723
|
false
|
|
692
724
|
);
|
|
693
725
|
this.passphrase = '';
|
|
694
726
|
}
|
|
695
727
|
|
|
728
|
+
/**
|
|
729
|
+
* Add app name as identifier
|
|
730
|
+
* @param field Field
|
|
731
|
+
* @returns Result
|
|
732
|
+
*/
|
|
733
|
+
protected addIdentifier(field: string) {
|
|
734
|
+
return field + '-' + this.name;
|
|
735
|
+
}
|
|
736
|
+
|
|
696
737
|
/**
|
|
697
738
|
* Restore settings from persisted source
|
|
698
739
|
*/
|
|
699
740
|
protected restore() {
|
|
700
741
|
// Devices
|
|
701
742
|
const devices = this.storage.getPersistedData<string[]>(
|
|
702
|
-
|
|
743
|
+
this.fields.devices,
|
|
703
744
|
[]
|
|
704
745
|
);
|
|
705
746
|
|
|
@@ -708,7 +749,7 @@ export abstract class CoreApp<
|
|
|
708
749
|
this.storage.copyFrom(this.persistedFields, false);
|
|
709
750
|
|
|
710
751
|
// Reset device id
|
|
711
|
-
this._deviceId = this.storage.getData(
|
|
752
|
+
this._deviceId = this.storage.getData(this.fields.deviceId, '');
|
|
712
753
|
|
|
713
754
|
// Totally new, no data restored
|
|
714
755
|
if (this._deviceId === '') return false;
|
|
@@ -723,8 +764,9 @@ export abstract class CoreApp<
|
|
|
723
764
|
return false;
|
|
724
765
|
}
|
|
725
766
|
|
|
767
|
+
// this.name to identifier different app's secret
|
|
726
768
|
const passphraseEncrypted = this.storage.getData<string>(
|
|
727
|
-
|
|
769
|
+
this.fields.devicePassphrase
|
|
728
770
|
);
|
|
729
771
|
if (passphraseEncrypted) {
|
|
730
772
|
const passphraseDecrypted = this.decrypt(
|
|
@@ -734,7 +776,7 @@ export abstract class CoreApp<
|
|
|
734
776
|
if (passphraseDecrypted != null) {
|
|
735
777
|
// Add the device to the list
|
|
736
778
|
devices.push(d);
|
|
737
|
-
this.storage.setPersistedData(
|
|
779
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
738
780
|
|
|
739
781
|
this.passphrase = passphraseDecrypted;
|
|
740
782
|
|
|
@@ -754,14 +796,14 @@ export abstract class CoreApp<
|
|
|
754
796
|
persist() {
|
|
755
797
|
// Devices
|
|
756
798
|
const devices = this.storage.getPersistedData<string[]>(
|
|
757
|
-
|
|
799
|
+
this.fields.devices
|
|
758
800
|
);
|
|
759
801
|
if (devices != null) {
|
|
760
802
|
const index = devices.indexOf(this.getDeviceId());
|
|
761
803
|
if (index !== -1) {
|
|
762
804
|
// Remove current device from the list
|
|
763
805
|
devices.splice(index, 1);
|
|
764
|
-
this.storage.setPersistedData(
|
|
806
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
765
807
|
}
|
|
766
808
|
}
|
|
767
809
|
|
|
@@ -829,7 +871,7 @@ export abstract class CoreApp<
|
|
|
829
871
|
|
|
830
872
|
// Serverside encrypted device id
|
|
831
873
|
const identifier = this.storage.getData<string>(
|
|
832
|
-
|
|
874
|
+
this.fields.serversideDeviceId
|
|
833
875
|
);
|
|
834
876
|
|
|
835
877
|
// Timestamp
|
|
@@ -874,7 +916,7 @@ export abstract class CoreApp<
|
|
|
874
916
|
if (callback) callback(false);
|
|
875
917
|
|
|
876
918
|
// Clear device id
|
|
877
|
-
this.storage.setData(
|
|
919
|
+
this.storage.setData(this.fields.deviceId, undefined);
|
|
878
920
|
|
|
879
921
|
return;
|
|
880
922
|
}
|
|
@@ -900,20 +942,20 @@ export abstract class CoreApp<
|
|
|
900
942
|
|
|
901
943
|
// Update device id and cache it
|
|
902
944
|
this._deviceId = data.deviceId;
|
|
903
|
-
this.storage.setData(
|
|
945
|
+
this.storage.setData(this.fields.deviceId, this._deviceId);
|
|
904
946
|
|
|
905
947
|
// Devices
|
|
906
948
|
const devices = this.storage.getPersistedData<string[]>(
|
|
907
|
-
|
|
949
|
+
this.fields.devices,
|
|
908
950
|
[]
|
|
909
951
|
);
|
|
910
952
|
devices.push(this.getDeviceId());
|
|
911
|
-
this.storage.setPersistedData(
|
|
953
|
+
this.storage.setPersistedData(this.fields.devices, devices);
|
|
912
954
|
|
|
913
955
|
// Current passphrase
|
|
914
956
|
this.passphrase = passphrase;
|
|
915
957
|
this.storage.setData(
|
|
916
|
-
|
|
958
|
+
this.fields.devicePassphrase,
|
|
917
959
|
this.encrypt(passphrase, this.name)
|
|
918
960
|
);
|
|
919
961
|
|
|
@@ -959,7 +1001,7 @@ export abstract class CoreApp<
|
|
|
959
1001
|
* @returns Fields
|
|
960
1002
|
*/
|
|
961
1003
|
protected initCallEncryptedUpdateFields(): string[] {
|
|
962
|
-
return [
|
|
1004
|
+
return [this.fields.headerToken];
|
|
963
1005
|
}
|
|
964
1006
|
|
|
965
1007
|
/**
|
|
@@ -987,7 +1029,7 @@ export abstract class CoreApp<
|
|
|
987
1029
|
// Cover the current value
|
|
988
1030
|
if (refreshToken !== '') {
|
|
989
1031
|
if (refreshToken != null) refreshToken = this.encrypt(refreshToken);
|
|
990
|
-
this.storage.setData(
|
|
1032
|
+
this.storage.setData(this.fields.headerToken, refreshToken);
|
|
991
1033
|
}
|
|
992
1034
|
|
|
993
1035
|
// Reset tryLogin state
|
|
@@ -1075,7 +1117,7 @@ export abstract class CoreApp<
|
|
|
1075
1117
|
*/
|
|
1076
1118
|
clearCacheData() {
|
|
1077
1119
|
this.clearCacheToken();
|
|
1078
|
-
this.storage.setData(
|
|
1120
|
+
this.storage.setData(this.fields.devicePassphrase, undefined);
|
|
1079
1121
|
}
|
|
1080
1122
|
|
|
1081
1123
|
/**
|
|
@@ -1083,7 +1125,7 @@ export abstract class CoreApp<
|
|
|
1083
1125
|
*/
|
|
1084
1126
|
clearCacheToken() {
|
|
1085
1127
|
this.cachedRefreshToken = undefined;
|
|
1086
|
-
this.storage.setPersistedData(
|
|
1128
|
+
this.storage.setPersistedData(this.fields.headerToken, undefined);
|
|
1087
1129
|
}
|
|
1088
1130
|
|
|
1089
1131
|
/**
|
|
@@ -1395,7 +1437,7 @@ export abstract class CoreApp<
|
|
|
1395
1437
|
getCacheToken(): string | undefined {
|
|
1396
1438
|
// Temp refresh token
|
|
1397
1439
|
if (this.cachedRefreshToken) return this.cachedRefreshToken;
|
|
1398
|
-
return this.storage.getData<string>(
|
|
1440
|
+
return this.storage.getData<string>(this.fields.headerToken);
|
|
1399
1441
|
}
|
|
1400
1442
|
|
|
1401
1443
|
/**
|
|
@@ -1429,7 +1471,7 @@ export abstract class CoreApp<
|
|
|
1429
1471
|
const response = this.api.transformResponse(rawResponse);
|
|
1430
1472
|
return this.api.getHeaderValue(
|
|
1431
1473
|
response.headers,
|
|
1432
|
-
|
|
1474
|
+
'SmartERPRefreshToken'
|
|
1433
1475
|
);
|
|
1434
1476
|
}
|
|
1435
1477
|
|
|
@@ -1681,7 +1723,7 @@ export abstract class CoreApp<
|
|
|
1681
1723
|
this.userData = user;
|
|
1682
1724
|
|
|
1683
1725
|
// Cache the encrypted serverside device id
|
|
1684
|
-
this.storage.setData(
|
|
1726
|
+
this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
|
|
1685
1727
|
|
|
1686
1728
|
if (keep) {
|
|
1687
1729
|
this.authorize(user.token, refreshToken);
|
|
@@ -1728,30 +1770,3 @@ export abstract class CoreApp<
|
|
|
1728
1770
|
);
|
|
1729
1771
|
}
|
|
1730
1772
|
}
|
|
1731
|
-
|
|
1732
|
-
export namespace CoreApp {
|
|
1733
|
-
/**
|
|
1734
|
-
* Response token header field name
|
|
1735
|
-
*/
|
|
1736
|
-
export const headerTokenField = 'SmartERPRefreshToken';
|
|
1737
|
-
|
|
1738
|
-
/**
|
|
1739
|
-
* Serverside device id encrypted field name
|
|
1740
|
-
*/
|
|
1741
|
-
export const serversideDeviceIdField = 'SmartERPServersideDeviceId';
|
|
1742
|
-
|
|
1743
|
-
/**
|
|
1744
|
-
* Device id field name
|
|
1745
|
-
*/
|
|
1746
|
-
export const deviceIdField = 'SmartERPDeviceId';
|
|
1747
|
-
|
|
1748
|
-
/**
|
|
1749
|
-
* Devices field name
|
|
1750
|
-
*/
|
|
1751
|
-
export const devicesField = 'SmartERPDevices';
|
|
1752
|
-
|
|
1753
|
-
/**
|
|
1754
|
-
* Device passphrase field name
|
|
1755
|
-
*/
|
|
1756
|
-
export const devicePassphraseField = 'SmartERPDevicePassphrase';
|
|
1757
|
-
}
|