deal with errors when enabling key backup

fixes #449
This commit is contained in:
Bruno Windels 2022-01-27 14:19:37 +01:00
parent e80acd4d57
commit bf08c0d850

View File

@ -190,7 +190,8 @@ export class Session {
* @param {string} credential either the passphrase or the recovery key, depending on the type * @param {string} credential either the passphrase or the recovery key, depending on the type
* @return {Promise} resolves or rejects after having tried to enable secret storage * @return {Promise} resolves or rejects after having tried to enable secret storage
*/ */
async enableSecretStorage(type, credential) { enableSecretStorage(type, credential, log = undefined) {
return this._platform.logger.wrapOrRun(log, "enable secret storage", async log => {
if (!this._olm) { if (!this._olm) {
throw new Error("olm required"); throw new Error("olm required");
} }
@ -202,11 +203,13 @@ export class Session {
const readTxn = await this._storage.readTxn([ const readTxn = await this._storage.readTxn([
this._storage.storeNames.accountData, this._storage.storeNames.accountData,
]); ]);
await this._createKeyBackup(key, readTxn); if (await this._createKeyBackup(key, readTxn, log)) {
await this._writeSSSSKey(key); await this._writeSSSSKey(key);
this._hasSecretStorageKey.set(true); this._hasSecretStorageKey.set(true);
return key; return key;
} }
});
}
async _writeSSSSKey(key) { async _writeSSSSKey(key) {
// only after having read a secret, write the key // only after having read a secret, write the key
@ -246,7 +249,9 @@ export class Session {
this._hasSecretStorageKey.set(false); this._hasSecretStorageKey.set(false);
} }
async _createKeyBackup(ssssKey, txn) { _createKeyBackup(ssssKey, txn, log) {
return log.wrap("enable key backup", async log => {
try {
const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform}); const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform});
this._keyBackup = await KeyBackup.fromSecretStorage( this._keyBackup = await KeyBackup.fromSecretStorage(
this._platform, this._platform,
@ -265,6 +270,12 @@ export class Session {
} }
} }
this.needsKeyBackup.set(false); this.needsKeyBackup.set(false);
} catch (err) {
log.catch(err);
return false;
}
return true;
});
} }
get keyBackup() { get keyBackup() {
@ -455,11 +466,12 @@ 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
await this._createKeyBackup(ssssKey, txn); couldReadKeyBackup = await this._createKeyBackup(ssssKey, txn, log);
} }
this._hasSecretStorageKey.set(!!ssssKey); 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([