1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Add method to force re-check of key backup

Also detect when the key backup version changes and do the right
thing

https://github.com/vector-im/riot-web/issues/8524
This commit is contained in:
David Baker
2019-02-13 15:40:07 +00:00
parent 4309749979
commit a087fb37a3
2 changed files with 40 additions and 4 deletions

View File

@@ -849,6 +849,19 @@ MatrixClient.prototype.importRoomKeys = function(keys) {
return this._crypto.importRoomKeys(keys); return this._crypto.importRoomKeys(keys);
}; };
/**
* Force a re-check of the local key backup status against
* what's on the server.
*
* @returns {Object} Object with backup info (as returned by
* getKeyBackupVersion) in backupInfo and
* trust information (as returned by isKeyBackupTrusted)
* in trustInfo.
*/
MatrixClient.prototype.checkKeyBackup = function() {
return this._crypto.checkKeyBackup();
};
/** /**
* Get information about the current key backup. * Get information about the current key backup.
* @returns {Promise} Information object from API or null * @returns {Promise} Information object from API or null

View File

@@ -264,7 +264,7 @@ Crypto.prototype._checkAndStartKeyBackup = async function() {
if (this._baseApis.isGuest()) { if (this._baseApis.isGuest()) {
console.log("Skipping key backup check since user is guest"); console.log("Skipping key backup check since user is guest");
this._checkedForBackup = true; this._checkedForBackup = true;
return; return null;
} }
let backupInfo; let backupInfo;
try { try {
@@ -275,21 +275,38 @@ Crypto.prototype._checkAndStartKeyBackup = async function() {
// well that's told us. we won't try again. // well that's told us. we won't try again.
this._checkedForBackup = true; this._checkedForBackup = true;
} }
return; return null;
} }
this._checkedForBackup = true; this._checkedForBackup = true;
const trustInfo = await this.isKeyBackupTrusted(backupInfo); const trustInfo = await this.isKeyBackupTrusted(backupInfo);
if (trustInfo.usable && !this.backupInfo) { if (trustInfo.usable && !this.backupInfo) {
console.log("Found usable key backup: enabling key backups"); console.log(
"Found usable key backup v" + backupInfo.version +
": enabling key backups",
);
this._baseApis.enableKeyBackup(backupInfo); this._baseApis.enableKeyBackup(backupInfo);
} else if (!trustInfo.usable && this.backupInfo) { } else if (!trustInfo.usable && this.backupInfo) {
console.log("No usable key backup: disabling key backup"); console.log("No usable key backup: disabling key backup");
this._baseApis.disableKeyBackup(); this._baseApis.disableKeyBackup();
} else if (!trustInfo.usable && !this.backupInfo) { } else if (!trustInfo.usable && !this.backupInfo) {
console.log("No usable key backup: not enabling key backup"); console.log("No usable key backup: not enabling key backup");
} else if (trustInfo.usable && this.backupInfo) {
// may not be the same version: if not, we should switch
if (backupInfo.version !== this.backupInfo.version) {
console.log(
"On backup version " + this.backupInfo.version + " but found " +
"version " + backupInfo.version + ": switching.",
);
this._baseApis.disableKeyBackup();
this._baseApis.enableKeyBackup(backupInfo);
} else {
console.log("Backup version " + backupInfo.version + " still current");
}
} }
return {backupInfo, trustInfo};
}; };
Crypto.prototype.setTrustedBackupPubKey = async function(trustedPubKey) { Crypto.prototype.setTrustedBackupPubKey = async function(trustedPubKey) {
@@ -302,10 +319,16 @@ Crypto.prototype.setTrustedBackupPubKey = async function(trustedPubKey) {
/** /**
* Forces a re-check of the key backup and enables/disables it * Forces a re-check of the key backup and enables/disables it
* as appropriate. * as appropriate.
*
* @return {Object} Object with backup info (as returned by
* getKeyBackupVersion) in backupInfo and
* trust information (as returned by isKeyBackupTrusted)
* in trustInfo.
*/ */
Crypto.prototype.checkKeyBackup = async function() { Crypto.prototype.checkKeyBackup = async function() {
this._checkedForBackup = false; this._checkedForBackup = false;
await this._checkAndStartKeyBackup(); const returnInfo = await this._checkAndStartKeyBackup();
return returnInfo;
}; };
/** /**