@etsoo/appscript 1.2.4 → 1.2.9

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.
@@ -445,6 +445,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
445
445
  */
446
446
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
447
447
  private getDeviceId;
448
+ private resetKeys;
448
449
  /**
449
450
  * Restore settings from persisted source
450
451
  */
@@ -118,41 +118,59 @@ 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
  }
125
126
  getDeviceId() {
126
- return this.deviceId.substring(0, 10);
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 = '';
127
136
  }
128
137
  /**
129
138
  * Restore settings from persisted source
130
139
  */
131
140
  restore() {
132
- // Current device id, '' means new, or reload (not included) or duplicate (included)
133
- if (this.deviceId) {
134
- // Devices
135
- const devices = this.storage.getPersistedData(CoreApp.devicesField);
136
- // Exists in the list?
137
- if (!(devices === null || devices === void 0 ? void 0 : devices.includes(this.getDeviceId()))) {
138
- const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
139
- if (passphraseEncrypted) {
140
- const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
141
- if (passphraseDecrypted != null) {
142
- this.passphrase = passphraseDecrypted;
143
- return false;
144
- }
145
- }
146
- }
147
- else {
148
- // Remove passphrase
149
- this.storage.setData(CoreApp.devicePassphraseField, undefined);
150
- this.passphrase = '';
141
+ // Devices
142
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
143
+ if (this.deviceId === '') {
144
+ // First vist, restore and keep the source
145
+ this.storage.copyFrom(this.persistedFields, false);
146
+ // Reset device id
147
+ this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
148
+ // Totally new, no data restored
149
+ if (this._deviceId === '')
150
+ return false;
151
+ }
152
+ // Device exists or not
153
+ const d = this.getDeviceId();
154
+ if (devices.includes(d)) {
155
+ // Duplicate tab, session data copied
156
+ // Remove the token, deviceId, and passphrase
157
+ this.resetKeys();
158
+ return false;
159
+ }
160
+ const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
161
+ if (passphraseEncrypted) {
162
+ const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
163
+ if (passphraseDecrypted != null) {
164
+ // Add the device to the list
165
+ devices.push(d);
166
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
167
+ this.passphrase = passphraseDecrypted;
168
+ return true;
151
169
  }
170
+ // Failed, reset keys
171
+ this.resetKeys();
152
172
  }
153
- // Restore
154
- this.storage.copyFrom(this.persistedFields, true);
155
- return true;
173
+ return false;
156
174
  }
157
175
  /**
158
176
  * Persist settings to source when application exit
@@ -163,6 +181,7 @@ class CoreApp {
163
181
  if (devices != null) {
164
182
  const index = devices.indexOf(this.getDeviceId());
165
183
  if (index !== -1) {
184
+ // Remove current device from the list
166
185
  devices.splice(index, 1);
167
186
  this.storage.setPersistedData(CoreApp.devicesField, devices);
168
187
  }
@@ -445,6 +445,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
445
445
  */
446
446
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
447
447
  private getDeviceId;
448
+ private resetKeys;
448
449
  /**
449
450
  * Restore settings from persisted source
450
451
  */
@@ -115,41 +115,59 @@ 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
  }
122
123
  getDeviceId() {
123
- return this.deviceId.substring(0, 10);
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 = '';
124
133
  }
125
134
  /**
126
135
  * Restore settings from persisted source
127
136
  */
128
137
  restore() {
129
- // Current device id, '' means new, or reload (not included) or duplicate (included)
130
- if (this.deviceId) {
131
- // Devices
132
- const devices = this.storage.getPersistedData(CoreApp.devicesField);
133
- // Exists in the list?
134
- if (!(devices === null || devices === void 0 ? void 0 : devices.includes(this.getDeviceId()))) {
135
- const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
136
- if (passphraseEncrypted) {
137
- const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
138
- if (passphraseDecrypted != null) {
139
- this.passphrase = passphraseDecrypted;
140
- return false;
141
- }
142
- }
143
- }
144
- else {
145
- // Remove passphrase
146
- this.storage.setData(CoreApp.devicePassphraseField, undefined);
147
- this.passphrase = '';
138
+ // Devices
139
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
140
+ if (this.deviceId === '') {
141
+ // First vist, restore and keep the source
142
+ this.storage.copyFrom(this.persistedFields, false);
143
+ // Reset device id
144
+ this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
145
+ // Totally new, no data restored
146
+ if (this._deviceId === '')
147
+ return false;
148
+ }
149
+ // Device exists or not
150
+ const d = this.getDeviceId();
151
+ if (devices.includes(d)) {
152
+ // Duplicate tab, session data copied
153
+ // Remove the token, deviceId, and passphrase
154
+ this.resetKeys();
155
+ return false;
156
+ }
157
+ const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
158
+ if (passphraseEncrypted) {
159
+ const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
160
+ if (passphraseDecrypted != null) {
161
+ // Add the device to the list
162
+ devices.push(d);
163
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
164
+ this.passphrase = passphraseDecrypted;
165
+ return true;
148
166
  }
167
+ // Failed, reset keys
168
+ this.resetKeys();
149
169
  }
150
- // Restore
151
- this.storage.copyFrom(this.persistedFields, true);
152
- return true;
170
+ return false;
153
171
  }
154
172
  /**
155
173
  * Persist settings to source when application exit
@@ -160,6 +178,7 @@ export class CoreApp {
160
178
  if (devices != null) {
161
179
  const index = devices.indexOf(this.getDeviceId());
162
180
  if (index !== -1) {
181
+ // Remove current device from the list
163
182
  devices.splice(index, 1);
164
183
  this.storage.setPersistedData(CoreApp.devicesField, devices);
165
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.2.4",
3
+ "version": "1.2.9",
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
  ];
@@ -650,46 +651,74 @@ export abstract class CoreApp<
650
651
  }
651
652
 
652
653
  private getDeviceId() {
653
- return this.deviceId.substring(0, 10);
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 = '';
654
667
  }
655
668
 
656
669
  /**
657
670
  * Restore settings from persisted source
658
671
  */
659
672
  protected restore() {
660
- // Current device id, '' means new, or reload (not included) or duplicate (included)
661
- if (this.deviceId) {
662
- // Devices
663
- const devices = this.storage.getPersistedData<string[]>(
664
- CoreApp.devicesField
673
+ // Devices
674
+ const devices = this.storage.getPersistedData<string[]>(
675
+ CoreApp.devicesField,
676
+ []
677
+ );
678
+
679
+ if (this.deviceId === '') {
680
+ // First vist, restore and keep the source
681
+ this.storage.copyFrom(this.persistedFields, false);
682
+
683
+ // Reset device id
684
+ this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
685
+
686
+ // Totally new, no data restored
687
+ if (this._deviceId === '') return false;
688
+ }
689
+
690
+ // Device exists or not
691
+ const d = this.getDeviceId();
692
+ if (devices.includes(d)) {
693
+ // Duplicate tab, session data copied
694
+ // Remove the token, deviceId, and passphrase
695
+ this.resetKeys();
696
+ return false;
697
+ }
698
+
699
+ const passphraseEncrypted = this.storage.getData<string>(
700
+ CoreApp.devicePassphraseField
701
+ );
702
+ if (passphraseEncrypted) {
703
+ const passphraseDecrypted = this.decrypt(
704
+ passphraseEncrypted,
705
+ this.name
665
706
  );
707
+ if (passphraseDecrypted != null) {
708
+ // Add the device to the list
709
+ devices.push(d);
710
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
666
711
 
667
- // Exists in the list?
668
- if (!devices?.includes(this.getDeviceId())) {
669
- const passphraseEncrypted = this.storage.getData<string>(
670
- CoreApp.devicePassphraseField
671
- );
672
- if (passphraseEncrypted) {
673
- const passphraseDecrypted = this.decrypt(
674
- passphraseEncrypted,
675
- this.name
676
- );
677
- if (passphraseDecrypted != null) {
678
- this.passphrase = passphraseDecrypted;
679
- return false;
680
- }
681
- }
682
- } else {
683
- // Remove passphrase
684
- this.storage.setData(CoreApp.devicePassphraseField, undefined);
685
- this.passphrase = '';
712
+ this.passphrase = passphraseDecrypted;
713
+
714
+ return true;
686
715
  }
687
- }
688
716
 
689
- // Restore
690
- this.storage.copyFrom(this.persistedFields, true);
717
+ // Failed, reset keys
718
+ this.resetKeys();
719
+ }
691
720
 
692
- return true;
721
+ return false;
693
722
  }
694
723
 
695
724
  /**
@@ -703,6 +732,7 @@ export abstract class CoreApp<
703
732
  if (devices != null) {
704
733
  const index = devices.indexOf(this.getDeviceId());
705
734
  if (index !== -1) {
735
+ // Remove current device from the list
706
736
  devices.splice(index, 1);
707
737
  this.storage.setPersistedData(CoreApp.devicesField, devices);
708
738
  }