@etsoo/appscript 1.2.3 → 1.2.8

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.
@@ -444,6 +444,8 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
444
444
  * @param name Application name
445
445
  */
446
446
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
447
+ private getDeviceId;
448
+ private resetKeys;
447
449
  /**
448
450
  * Restore settings from persisted source
449
451
  */
@@ -743,6 +745,10 @@ export declare namespace CoreApp {
743
745
  * Device id field name
744
746
  */
745
747
  const deviceIdField = "SmartERPDeviceId";
748
+ /**
749
+ * Devices field name
750
+ */
751
+ const devicesField = "SmartERPDevices";
746
752
  /**
747
753
  * Device passphrase field name
748
754
  */
@@ -44,10 +44,10 @@ class CoreApp {
44
44
  this.notifier = notifier;
45
45
  this.storage = storage;
46
46
  this.name = name;
47
- // Restore
48
- this.restore();
49
47
  // Device id
50
48
  this._deviceId = storage.getData(CoreApp.deviceIdField, '');
49
+ // Restore
50
+ this.restore();
51
51
  this.setApi(api);
52
52
  const { currentCulture, currentRegion } = settings;
53
53
  this.changeCulture(currentCulture);
@@ -118,35 +118,74 @@ class CoreApp {
118
118
  get persistedFields() {
119
119
  return [
120
120
  CoreApp.deviceIdField,
121
+ CoreApp.devicePassphraseField,
121
122
  CoreApp.serversideDeviceIdField,
122
123
  CoreApp.headerTokenField
123
124
  ];
124
125
  }
126
+ getDeviceId() {
127
+ return this.deviceId.substring(0, 15);
128
+ }
129
+ resetKeys() {
130
+ this.storage.clear([
131
+ CoreApp.devicePassphraseField,
132
+ CoreApp.headerTokenField,
133
+ CoreApp.serversideDeviceIdField
134
+ ], false);
135
+ this.passphrase = '';
136
+ }
125
137
  /**
126
138
  * Restore settings from persisted source
127
139
  */
128
140
  restore() {
141
+ // Devices
142
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
143
+ // Current device id, '' means new, or reload (not included) or duplicate (included)
144
+ if (this.deviceId === '') {
145
+ // First vist, restore
146
+ this.storage.copyFrom(this.persistedFields, true);
147
+ // Reset device id
148
+ this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
149
+ }
150
+ else {
151
+ const d = this.getDeviceId();
152
+ if (devices.includes(d)) {
153
+ // Duplicate tab, session data copied
154
+ // Remove the token, deviceId, and passphrase
155
+ this.resetKeys();
156
+ return false;
157
+ }
158
+ }
129
159
  const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
130
160
  if (passphraseEncrypted) {
131
161
  const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
132
162
  if (passphraseDecrypted != null) {
133
163
  this.passphrase = passphraseDecrypted;
134
- // Same device
135
- if (this.deviceId ===
136
- this.storage.getPersistedData(CoreApp.deviceIdField)) {
137
- this.storage.clear(this.persistedFields, true);
164
+ const d = this.getDeviceId();
165
+ if (!devices.includes(d)) {
166
+ devices.push(d);
167
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
138
168
  }
139
- return false;
169
+ return true;
140
170
  }
171
+ // Failed, reset keys
172
+ this.resetKeys();
141
173
  }
142
- // Restore
143
- this.storage.copyFrom(this.persistedFields, true);
144
- return true;
174
+ return false;
145
175
  }
146
176
  /**
147
177
  * Persist settings to source when application exit
148
178
  */
149
179
  persist() {
180
+ // Devices
181
+ const devices = this.storage.getPersistedData(CoreApp.devicesField);
182
+ if (devices != null) {
183
+ const index = devices.indexOf(this.getDeviceId());
184
+ if (index !== -1) {
185
+ devices.splice(index, 1);
186
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
187
+ }
188
+ }
150
189
  if (!this.authorized)
151
190
  return;
152
191
  this.storage.copyTo(this.persistedFields);
@@ -267,6 +306,10 @@ class CoreApp {
267
306
  // Update device id and cache it
268
307
  this._deviceId = data.deviceId;
269
308
  this.storage.setData(CoreApp.deviceIdField, this._deviceId);
309
+ // Devices
310
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
311
+ devices.push(this.getDeviceId());
312
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
270
313
  // Current passphrase
271
314
  this.passphrase = passphrase;
272
315
  this.storage.setData(CoreApp.devicePassphraseField, this.encrypt(passphrase, this.name));
@@ -922,6 +965,10 @@ exports.CoreApp = CoreApp;
922
965
  * Device id field name
923
966
  */
924
967
  CoreApp.deviceIdField = 'SmartERPDeviceId';
968
+ /**
969
+ * Devices field name
970
+ */
971
+ CoreApp.devicesField = 'SmartERPDevices';
925
972
  /**
926
973
  * Device passphrase field name
927
974
  */
@@ -444,6 +444,8 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
444
444
  * @param name Application name
445
445
  */
446
446
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
447
+ private getDeviceId;
448
+ private resetKeys;
447
449
  /**
448
450
  * Restore settings from persisted source
449
451
  */
@@ -743,6 +745,10 @@ export declare namespace CoreApp {
743
745
  * Device id field name
744
746
  */
745
747
  const deviceIdField = "SmartERPDeviceId";
748
+ /**
749
+ * Devices field name
750
+ */
751
+ const devicesField = "SmartERPDevices";
746
752
  /**
747
753
  * Device passphrase field name
748
754
  */
@@ -41,10 +41,10 @@ export class CoreApp {
41
41
  this.notifier = notifier;
42
42
  this.storage = storage;
43
43
  this.name = name;
44
- // Restore
45
- this.restore();
46
44
  // Device id
47
45
  this._deviceId = storage.getData(CoreApp.deviceIdField, '');
46
+ // Restore
47
+ this.restore();
48
48
  this.setApi(api);
49
49
  const { currentCulture, currentRegion } = settings;
50
50
  this.changeCulture(currentCulture);
@@ -115,35 +115,74 @@ export class CoreApp {
115
115
  get persistedFields() {
116
116
  return [
117
117
  CoreApp.deviceIdField,
118
+ CoreApp.devicePassphraseField,
118
119
  CoreApp.serversideDeviceIdField,
119
120
  CoreApp.headerTokenField
120
121
  ];
121
122
  }
123
+ getDeviceId() {
124
+ return this.deviceId.substring(0, 15);
125
+ }
126
+ resetKeys() {
127
+ this.storage.clear([
128
+ CoreApp.devicePassphraseField,
129
+ CoreApp.headerTokenField,
130
+ CoreApp.serversideDeviceIdField
131
+ ], false);
132
+ this.passphrase = '';
133
+ }
122
134
  /**
123
135
  * Restore settings from persisted source
124
136
  */
125
137
  restore() {
138
+ // Devices
139
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
140
+ // Current device id, '' means new, or reload (not included) or duplicate (included)
141
+ if (this.deviceId === '') {
142
+ // First vist, restore
143
+ this.storage.copyFrom(this.persistedFields, true);
144
+ // Reset device id
145
+ this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
146
+ }
147
+ else {
148
+ const d = this.getDeviceId();
149
+ if (devices.includes(d)) {
150
+ // Duplicate tab, session data copied
151
+ // Remove the token, deviceId, and passphrase
152
+ this.resetKeys();
153
+ return false;
154
+ }
155
+ }
126
156
  const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
127
157
  if (passphraseEncrypted) {
128
158
  const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
129
159
  if (passphraseDecrypted != null) {
130
160
  this.passphrase = passphraseDecrypted;
131
- // Same device
132
- if (this.deviceId ===
133
- this.storage.getPersistedData(CoreApp.deviceIdField)) {
134
- this.storage.clear(this.persistedFields, true);
161
+ const d = this.getDeviceId();
162
+ if (!devices.includes(d)) {
163
+ devices.push(d);
164
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
135
165
  }
136
- return false;
166
+ return true;
137
167
  }
168
+ // Failed, reset keys
169
+ this.resetKeys();
138
170
  }
139
- // Restore
140
- this.storage.copyFrom(this.persistedFields, true);
141
- return true;
171
+ return false;
142
172
  }
143
173
  /**
144
174
  * Persist settings to source when application exit
145
175
  */
146
176
  persist() {
177
+ // Devices
178
+ const devices = this.storage.getPersistedData(CoreApp.devicesField);
179
+ if (devices != null) {
180
+ const index = devices.indexOf(this.getDeviceId());
181
+ if (index !== -1) {
182
+ devices.splice(index, 1);
183
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
184
+ }
185
+ }
147
186
  if (!this.authorized)
148
187
  return;
149
188
  this.storage.copyTo(this.persistedFields);
@@ -264,6 +303,10 @@ export class CoreApp {
264
303
  // Update device id and cache it
265
304
  this._deviceId = data.deviceId;
266
305
  this.storage.setData(CoreApp.deviceIdField, this._deviceId);
306
+ // Devices
307
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
308
+ devices.push(this.getDeviceId());
309
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
267
310
  // Current passphrase
268
311
  this.passphrase = passphrase;
269
312
  this.storage.setData(CoreApp.devicePassphraseField, this.encrypt(passphrase, this.name));
@@ -918,6 +961,10 @@ export class CoreApp {
918
961
  * Device id field name
919
962
  */
920
963
  CoreApp.deviceIdField = 'SmartERPDeviceId';
964
+ /**
965
+ * Devices field name
966
+ */
967
+ CoreApp.devicesField = 'SmartERPDevices';
921
968
  /**
922
969
  * Device passphrase field name
923
970
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.2.3",
3
+ "version": "1.2.8",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -606,6 +606,7 @@ export abstract class CoreApp<
606
606
  protected get persistedFields() {
607
607
  return [
608
608
  CoreApp.deviceIdField,
609
+ CoreApp.devicePassphraseField,
609
610
  CoreApp.serversideDeviceIdField,
610
611
  CoreApp.headerTokenField
611
612
  ];
@@ -632,12 +633,12 @@ export abstract class CoreApp<
632
633
  this.storage = storage;
633
634
  this.name = name;
634
635
 
635
- // Restore
636
- this.restore();
637
-
638
636
  // Device id
639
637
  this._deviceId = storage.getData(CoreApp.deviceIdField, '');
640
638
 
639
+ // Restore
640
+ this.restore();
641
+
641
642
  this.setApi(api);
642
643
 
643
644
  const { currentCulture, currentRegion } = settings;
@@ -649,10 +650,50 @@ export abstract class CoreApp<
649
650
  this.setup();
650
651
  }
651
652
 
653
+ private getDeviceId() {
654
+ return this.deviceId.substring(0, 15);
655
+ }
656
+
657
+ private resetKeys() {
658
+ this.storage.clear(
659
+ [
660
+ CoreApp.devicePassphraseField,
661
+ CoreApp.headerTokenField,
662
+ CoreApp.serversideDeviceIdField
663
+ ],
664
+ false
665
+ );
666
+ this.passphrase = '';
667
+ }
668
+
652
669
  /**
653
670
  * Restore settings from persisted source
654
671
  */
655
672
  protected restore() {
673
+ // Devices
674
+ const devices = this.storage.getPersistedData<string[]>(
675
+ CoreApp.devicesField,
676
+ []
677
+ );
678
+
679
+ // Current device id, '' means new, or reload (not included) or duplicate (included)
680
+ if (this.deviceId === '') {
681
+ // First vist, restore
682
+ this.storage.copyFrom(this.persistedFields, true);
683
+
684
+ // Reset device id
685
+ this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
686
+ } else {
687
+ const d = this.getDeviceId();
688
+
689
+ if (devices.includes(d)) {
690
+ // Duplicate tab, session data copied
691
+ // Remove the token, deviceId, and passphrase
692
+ this.resetKeys();
693
+ return false;
694
+ }
695
+ }
696
+
656
697
  const passphraseEncrypted = this.storage.getData<string>(
657
698
  CoreApp.devicePassphraseField
658
699
  );
@@ -664,28 +705,41 @@ export abstract class CoreApp<
664
705
  if (passphraseDecrypted != null) {
665
706
  this.passphrase = passphraseDecrypted;
666
707
 
667
- // Same device
668
- if (
669
- this.deviceId ===
670
- this.storage.getPersistedData<string>(CoreApp.deviceIdField)
671
- ) {
672
- this.storage.clear(this.persistedFields, true);
708
+ const d = this.getDeviceId();
709
+ if (!devices.includes(d)) {
710
+ devices.push(d);
711
+ this.storage.setPersistedData(
712
+ CoreApp.devicesField,
713
+ devices
714
+ );
673
715
  }
674
716
 
675
- return false;
717
+ return true;
676
718
  }
677
- }
678
719
 
679
- // Restore
680
- this.storage.copyFrom(this.persistedFields, true);
720
+ // Failed, reset keys
721
+ this.resetKeys();
722
+ }
681
723
 
682
- return true;
724
+ return false;
683
725
  }
684
726
 
685
727
  /**
686
728
  * Persist settings to source when application exit
687
729
  */
688
730
  persist() {
731
+ // Devices
732
+ const devices = this.storage.getPersistedData<string[]>(
733
+ CoreApp.devicesField
734
+ );
735
+ if (devices != null) {
736
+ const index = devices.indexOf(this.getDeviceId());
737
+ if (index !== -1) {
738
+ devices.splice(index, 1);
739
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
740
+ }
741
+ }
742
+
689
743
  if (!this.authorized) return;
690
744
  this.storage.copyTo(this.persistedFields);
691
745
  }
@@ -823,6 +877,14 @@ export abstract class CoreApp<
823
877
  this._deviceId = data.deviceId;
824
878
  this.storage.setData(CoreApp.deviceIdField, this._deviceId);
825
879
 
880
+ // Devices
881
+ const devices = this.storage.getPersistedData<string[]>(
882
+ CoreApp.devicesField,
883
+ []
884
+ );
885
+ devices.push(this.getDeviceId());
886
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
887
+
826
888
  // Current passphrase
827
889
  this.passphrase = passphrase;
828
890
  this.storage.setData(
@@ -1617,6 +1679,11 @@ export namespace CoreApp {
1617
1679
  */
1618
1680
  export const deviceIdField = 'SmartERPDeviceId';
1619
1681
 
1682
+ /**
1683
+ * Devices field name
1684
+ */
1685
+ export const devicesField = 'SmartERPDevices';
1686
+
1620
1687
  /**
1621
1688
  * Device passphrase field name
1622
1689
  */