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,22 +190,25 @@ 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) {
if (!this._olm) { return this._platform.logger.wrapOrRun(log, "enable secret storage", async log => {
throw new Error("olm required"); if (!this._olm) {
} throw new Error("olm required");
if (this._keyBackup) { }
return false; if (this._keyBackup) {
} return false;
const key = await ssssKeyFromCredential(type, credential, this._storage, this._platform, this._olm); }
// and create key backup, which needs to read from accountData const key = await ssssKeyFromCredential(type, credential, this._storage, this._platform, this._olm);
const readTxn = await this._storage.readTxn([ // and create key backup, which needs to read from accountData
this._storage.storeNames.accountData, const readTxn = await this._storage.readTxn([
]); this._storage.storeNames.accountData,
await this._createKeyBackup(key, readTxn); ]);
await this._writeSSSSKey(key); if (await this._createKeyBackup(key, readTxn, log)) {
this._hasSecretStorageKey.set(true); await this._writeSSSSKey(key);
return key; this._hasSecretStorageKey.set(true);
return key;
}
});
} }
async _writeSSSSKey(key) { async _writeSSSSKey(key) {
@ -246,25 +249,33 @@ export class Session {
this._hasSecretStorageKey.set(false); this._hasSecretStorageKey.set(false);
} }
async _createKeyBackup(ssssKey, txn) { _createKeyBackup(ssssKey, txn, log) {
const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform}); return log.wrap("enable key backup", async log => {
this._keyBackup = await KeyBackup.fromSecretStorage( try {
this._platform, const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform});
this._olm, this._keyBackup = await KeyBackup.fromSecretStorage(
secretStorage, this._platform,
this._hsApi, this._olm,
this._keyLoader, secretStorage,
this._storage, this._hsApi,
txn this._keyLoader,
); this._storage,
if (this._keyBackup) { txn
for (const room of this._rooms.values()) { );
if (room.isEncrypted) { if (this._keyBackup) {
room.enableKeyBackup(this._keyBackup); for (const room of this._rooms.values()) {
if (room.isEncrypted) {
room.enableKeyBackup(this._keyBackup);
}
}
} }
this.needsKeyBackup.set(false);
} catch (err) {
log.catch(err);
return false;
} }
} return true;
this.needsKeyBackup.set(false); });
} }
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([