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

If a key upload fails, throw an error and emit an event (#1254)

This commit is contained in:
Zoe
2020-03-16 10:24:31 +00:00
committed by GitHub
parent 758e12d6dd
commit 68cebc7ff9
2 changed files with 109 additions and 27 deletions

View File

@@ -55,6 +55,7 @@ import {InRoomChannel, InRoomRequests} from "./verification/request/InRoomChanne
import {ToDeviceChannel, ToDeviceRequests} from "./verification/request/ToDeviceChannel"; import {ToDeviceChannel, ToDeviceRequests} from "./verification/request/ToDeviceChannel";
import * as httpApi from "../http-api"; import * as httpApi from "../http-api";
import {IllegalMethod} from "./verification/IllegalMethod"; import {IllegalMethod} from "./verification/IllegalMethod";
import {KeySignatureUploadError} from "../errors";
const DeviceVerification = DeviceInfo.DeviceVerification; const DeviceVerification = DeviceInfo.DeviceVerification;
@@ -667,15 +668,34 @@ Crypto.prototype._afterCrossSigningLocalKeyChange = async function() {
const device = this._deviceList.getStoredDevice(this._userId, this._deviceId); const device = this._deviceList.getStoredDevice(this._userId, this._deviceId);
const signedDevice = await this._crossSigningInfo.signDevice(this._userId, device); const signedDevice = await this._crossSigningInfo.signDevice(this._userId, device);
logger.info(`Starting background key sig upload for ${this._deviceId}`); logger.info(`Starting background key sig upload for ${this._deviceId}`);
this._baseApis.uploadKeySignatures({
[this._userId]: { const upload = ({ shouldEmit }) => {
[this._deviceId]: signedDevice, return this._baseApis.uploadKeySignatures({
}, [this._userId]: {
}).then(() => { [this._deviceId]: signedDevice,
logger.info(`Finished background key sig upload for ${this._deviceId}`); },
}).catch(e => { }).then((response) => {
logger.error(`Error during background key sig upload for ${this._deviceId}`, e); const { failures } = response || {};
}); if (Object.keys(failures || []).length > 0) {
if (shouldEmit) {
this._baseApis.emit(
"crypto.keySignatureUploadFailure",
failures,
"_afterCrossSigningLocalKeyChange",
upload, // continuation
);
}
throw new KeySignatureUploadError("Key upload failed", { failures });
}
logger.info(`Finished background key sig upload for ${this._deviceId}`);
}).catch(e => {
logger.error(
`Error during background key sig upload for ${this._deviceId}`,
e,
);
});
};
upload({ shouldEmit: true });
const shouldUpgradeCb = ( const shouldUpgradeCb = (
this._baseApis._cryptoCallbacks.shouldUpgradeDeviceVerifications this._baseApis._cryptoCallbacks.shouldUpgradeDeviceVerifications
@@ -959,12 +979,31 @@ Crypto.prototype.checkOwnCrossSigningTrust = async function() {
const keysToUpload = Object.keys(keySignatures); const keysToUpload = Object.keys(keySignatures);
if (keysToUpload.length) { if (keysToUpload.length) {
logger.info(`Starting background key sig upload for ${keysToUpload}`); const upload = ({ shouldEmit }) => {
this._baseApis.uploadKeySignatures({ [this._userId]: keySignatures }).then(() => { logger.info(`Starting background key sig upload for ${keysToUpload}`);
logger.info(`Finished background key sig upload for ${keysToUpload}`); return this._baseApis.uploadKeySignatures({ [this._userId]: keySignatures })
}).catch(e => { .then((response) => {
logger.error(`Error during background key sig upload for ${keysToUpload}`, e); const { failures } = response || {};
}); logger.info(`Finished background key sig upload for ${keysToUpload}`);
if (Object.keys(failures || []).length > 0) {
if (shouldEmit) {
this._baseApis.emit(
"crypto.keySignatureUploadFailure",
failures,
"checkOwnCrossSigningTrust",
upload,
);
}
throw new KeySignatureUploadError("Key upload failed", { failures });
}
}).catch(e => {
logger.error(
`Error during background key sig upload for ${keysToUpload}`,
e,
);
});
};
upload({ shouldEmit: true });
} }
this.emit("userTrustStatusChanged", userId, this.checkUserTrust(userId)); this.emit("userTrustStatusChanged", userId, this.checkUserTrust(userId));
@@ -1605,12 +1644,33 @@ Crypto.prototype.setDeviceVerification = async function(
); );
const device = await this._crossSigningInfo.signUser(xsk); const device = await this._crossSigningInfo.signUser(xsk);
if (device) { if (device) {
logger.info("Uploading signature for " + userId + "..."); const upload = async ({ shouldEmit }) => {
await this._baseApis.uploadKeySignatures({ logger.info("Uploading signature for " + userId + "...");
[userId]: { const response = await this._baseApis.uploadKeySignatures({
[deviceId]: device, [userId]: {
}, [deviceId]: device,
}); },
});
const { failures } = response || {};
if (Object.keys(failures || []).length > 0) {
if (shouldEmit) {
this._baseApis.emit(
"crypto.keySignatureUploadFailure",
failures,
"setDeviceVerification",
upload,
);
}
/* Throwing here causes the process to be cancelled and the other
* user to be notified */
throw new KeySignatureUploadError(
"Key upload failed",
{ failures },
);
}
};
await upload({ shouldEmit: true });
// This will emit events when it comes back down the sync // This will emit events when it comes back down the sync
// (we could do local echo to speed things up) // (we could do local echo to speed things up)
} }
@@ -1659,12 +1719,27 @@ Crypto.prototype.setDeviceVerification = async function(
userId, DeviceInfo.fromStorage(dev, deviceId), userId, DeviceInfo.fromStorage(dev, deviceId),
); );
if (device) { if (device) {
logger.info("Uploading signature for " + deviceId); const upload = async ({shouldEmit}) => {
await this._baseApis.uploadKeySignatures({ logger.info("Uploading signature for " + deviceId);
[userId]: { const response = await this._baseApis.uploadKeySignatures({
[deviceId]: device, [userId]: {
}, [deviceId]: device,
}); },
});
const { failures } = response || {};
if (Object.keys(failures || []).length > 0) {
if (shouldEmit) {
this._baseApis.emit(
"crypto.keySignatureUploadFailure",
failures,
"_afterCrossSigningLocalKeyChange",
upload, // continuation
);
}
throw new KeySignatureUploadError("Key upload failed", { failures });
}
};
await upload({shouldEmit: true});
// XXX: we'll need to wait for the device list to be updated // XXX: we'll need to wait for the device list to be updated
} }
} }

View File

@@ -44,3 +44,10 @@ InvalidCryptoStoreError.prototype = Object.create(Error.prototype, {
}, },
}); });
Reflect.setPrototypeOf(InvalidCryptoStoreError, Error); Reflect.setPrototypeOf(InvalidCryptoStoreError, Error);
export class KeySignatureUploadError extends Error {
constructor(message, value) {
super(message);
this.value = value;
}
}