Skip to content

Commit 05a20ab

Browse files
committed
Fix up keys even if key backup isn't enabled
Mostly because that's what the test tests, so let's keep that behaviour the same.
1 parent 964aa6d commit 05a20ab

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

src/crypto/index.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -951,38 +951,36 @@ export class Crypto extends EventEmitter {
951951
builder.addSessionBackup(data);
952952
}
953953

954-
if (this.backupManager.getKeyBackupEnabled()) {
955-
// Cache the session backup key
956-
const sessionBackupKey = await secretStorage.get('m.megolm_backup.v1');
957-
if (sessionBackupKey) {
958-
logger.info("Got session backup key from secret storage: caching");
959-
// fix up the backup key if it's in the wrong format, and replace
960-
// in secret storage
961-
const fixedBackupKey = fixBackupKey(sessionBackupKey);
962-
if (fixedBackupKey) {
963-
await secretStorage.store("m.megolm_backup.v1",
964-
fixedBackupKey, [newKeyId || oldKeyId],
965-
);
966-
}
967-
const decodedBackupKey = new Uint8Array(olmlib.decodeBase64(
968-
fixedBackupKey || sessionBackupKey,
969-
));
970-
await builder.addSessionBackupPrivateKeyToCache(decodedBackupKey);
971-
} else {
972-
// key backup is enabled but we don't have a session backup key in SSSS: see if we have one in
973-
// the cache or the user can provide one, and if so, write it to SSSS
974-
const backupKey = await this.getSessionBackupPrivateKey() || await getKeyBackupPassphrase();
975-
if (!backupKey) {
976-
// This will require user intervention to recover from since we don't have the key
977-
// backup key anywhere. The user should probably just set up a new key backup and
978-
// the key for the new backup will be stored. If we hit this scenario in the wild
979-
// with any frequency, we should do more than just log an error.
980-
logger.error("Key backup is enabled but couldn't get key backup key!");
981-
return;
982-
}
983-
logger.info("Got session backup key from cache/user that wasn't in SSSS: saving to SSSS");
984-
await secretStorage.store("m.megolm_backup.v1", olmlib.encodeBase64(backupKey));
954+
// Cache the session backup key
955+
const sessionBackupKey = await secretStorage.get('m.megolm_backup.v1');
956+
if (sessionBackupKey) {
957+
logger.info("Got session backup key from secret storage: caching");
958+
// fix up the backup key if it's in the wrong format, and replace
959+
// in secret storage
960+
const fixedBackupKey = fixBackupKey(sessionBackupKey);
961+
if (fixedBackupKey) {
962+
await secretStorage.store("m.megolm_backup.v1",
963+
fixedBackupKey, [newKeyId || oldKeyId],
964+
);
965+
}
966+
const decodedBackupKey = new Uint8Array(olmlib.decodeBase64(
967+
fixedBackupKey || sessionBackupKey,
968+
));
969+
await builder.addSessionBackupPrivateKeyToCache(decodedBackupKey);
970+
} else if (this.backupManager.getKeyBackupEnabled()) {
971+
// key backup is enabled but we don't have a session backup key in SSSS: see if we have one in
972+
// the cache or the user can provide one, and if so, write it to SSSS
973+
const backupKey = await this.getSessionBackupPrivateKey() || await getKeyBackupPassphrase();
974+
if (!backupKey) {
975+
// This will require user intervention to recover from since we don't have the key
976+
// backup key anywhere. The user should probably just set up a new key backup and
977+
// the key for the new backup will be stored. If we hit this scenario in the wild
978+
// with any frequency, we should do more than just log an error.
979+
logger.error("Key backup is enabled but couldn't get key backup key!");
980+
return;
985981
}
982+
logger.info("Got session backup key from cache/user that wasn't in SSSS: saving to SSSS");
983+
await secretStorage.store("m.megolm_backup.v1", olmlib.encodeBase64(backupKey));
986984
}
987985

988986
const operation = builder.buildOperation();

0 commit comments

Comments
 (0)