make keyBackup an observable and don't have separate needs-key flag

This commit is contained in:
Bruno Windels 2022-01-28 15:13:58 +01:00
parent eb134a6c47
commit 504f420293

View file

@ -75,8 +75,7 @@ export class Session {
this._megolmDecryption = null; this._megolmDecryption = null;
this._getSyncToken = () => this.syncToken; this._getSyncToken = () => this.syncToken;
this._olmWorker = olmWorker; this._olmWorker = olmWorker;
this._keyBackup = null; this._keyBackup = new ObservableValue(undefined);
this._hasSecretStorageKey = new ObservableValue(null);
this._observedRoomStatus = new Map(); this._observedRoomStatus = new Map();
if (olm) { if (olm) {
@ -171,10 +170,10 @@ export class Session {
megolmEncryption: this._megolmEncryption, megolmEncryption: this._megolmEncryption,
megolmDecryption: this._megolmDecryption, megolmDecryption: this._megolmDecryption,
storage: this._storage, storage: this._storage,
keyBackup: this._keyBackup, keyBackup: this._keyBackup?.get(),
encryptionParams, encryptionParams,
notifyMissingMegolmSession: () => { notifyMissingMegolmSession: () => {
if (!this._keyBackup) { if (!this._keyBackup.get()) {
this.needsKeyBackup.set(true) this.needsKeyBackup.set(true)
} }
}, },
@ -195,7 +194,7 @@ export class Session {
if (!this._olm) { if (!this._olm) {
throw new Error("olm required"); throw new Error("olm required");
} }
if (this._keyBackup) { if (this._keyBackup.get()) {
return false; return false;
} }
const key = await ssssKeyFromCredential(type, credential, this._storage, this._platform, this._olm); const key = await ssssKeyFromCredential(type, credential, this._storage, this._platform, this._olm);
@ -205,7 +204,6 @@ export class Session {
]); ]);
if (await this._createKeyBackup(key, readTxn, log)) { if (await this._createKeyBackup(key, readTxn, log)) {
await this._writeSSSSKey(key); await this._writeSSSSKey(key);
this._hasSecretStorageKey.set(true);
return key; return key;
} }
}); });
@ -237,23 +235,22 @@ export class Session {
throw err; throw err;
} }
await writeTxn.complete(); await writeTxn.complete();
if (this._keyBackup) { if (this._keyBackup.get()) {
for (const room of this._rooms.values()) { for (const room of this._rooms.values()) {
if (room.isEncrypted) { if (room.isEncrypted) {
room.enableKeyBackup(undefined); room.enableKeyBackup(undefined);
} }
} }
this._keyBackup?.dispose(); this._keyBackup.get().dispose();
this._keyBackup = undefined; this._keyBackup.set(undefined);
} }
this._hasSecretStorageKey.set(false);
} }
_createKeyBackup(ssssKey, txn, log) { _createKeyBackup(ssssKey, txn, log) {
return log.wrap("enable key backup", async log => { return log.wrap("enable key backup", async log => {
try { try {
const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform}); const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform});
this._keyBackup = await KeyBackup.fromSecretStorage( const keyBackup = await KeyBackup.fromSecretStorage(
this._platform, this._platform,
this._olm, this._olm,
secretStorage, secretStorage,
@ -262,19 +259,18 @@ export class Session {
this._storage, this._storage,
txn txn
); );
if (this._keyBackup) { if (keyBackup) {
for (const room of this._rooms.values()) { for (const room of this._rooms.values()) {
if (room.isEncrypted) { if (room.isEncrypted) {
room.enableKeyBackup(this._keyBackup); room.enableKeyBackup(keyBackup);
} }
} }
this._keyBackup.set(keyBackup);
return true;
} }
this.needsKeyBackup.set(false);
} catch (err) { } catch (err) {
log.catch(err); log.catch(err);
return false;
} }
return true;
}); });
} }
@ -417,8 +413,8 @@ export class Session {
dispose() { dispose() {
this._olmWorker?.dispose(); this._olmWorker?.dispose();
this._olmWorker = undefined; this._olmWorker = undefined;
this._keyBackup?.dispose(); this._keyBackup.get()?.dispose();
this._keyBackup = undefined; this._keyBackup.set(undefined);
this._megolmDecryption?.dispose(); this._megolmDecryption?.dispose();
this._megolmDecryption = undefined; this._megolmDecryption = undefined;
this._e2eeAccount?.dispose(); this._e2eeAccount?.dispose();
@ -446,7 +442,7 @@ export class Session {
await txn.complete(); await txn.complete();
} }
// enable session backup, this requests the latest backup version // enable session backup, this requests the latest backup version
if (!this._keyBackup) { if (!this._keyBackup.get()) {
if (dehydratedDevice) { if (dehydratedDevice) {
await log.wrap("SSSSKeyFromDehydratedDeviceKey", async log => { await log.wrap("SSSSKeyFromDehydratedDeviceKey", async log => {
const ssssKey = await createSSSSKeyFromDehydratedDeviceKey(dehydratedDevice.key, this._storage, this._platform); const ssssKey = await createSSSSKeyFromDehydratedDeviceKey(dehydratedDevice.key, this._storage, this._platform);
@ -462,12 +458,10 @@ export class Session {
]); ]);
// try set up session backup if we stored the ssss key // try set up session backup if we stored the ssss key
const ssssKey = await ssssReadKey(txn); const ssssKey = await ssssReadKey(txn);
let couldReadKeyBackup = false;
if (ssssKey) { if (ssssKey) {
// txn will end here as this does a network request // txn will end here as this does a network request
couldReadKeyBackup = await this._createKeyBackup(ssssKey, txn, log); await this._createKeyBackup(ssssKey, txn, log);
} }
this._hasSecretStorageKey.set(couldReadKeyBackup);
} }
// restore unfinished operations, like sending out room keys // restore unfinished operations, like sending out room keys
const opsTxn = await this._storage.readWriteTxn([ const opsTxn = await this._storage.readWriteTxn([
@ -632,7 +626,7 @@ export class Session {
} }
} }
if (changes.hasNewRoomKeys) { if (changes.hasNewRoomKeys) {
this._keyBackup?.flush(log); this._keyBackup.get()?.flush(log);
} }
} }