You've already forked matrix-js-sdk
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:
@@ -55,6 +55,7 @@ import {InRoomChannel, InRoomRequests} from "./verification/request/InRoomChanne
|
||||
import {ToDeviceChannel, ToDeviceRequests} from "./verification/request/ToDeviceChannel";
|
||||
import * as httpApi from "../http-api";
|
||||
import {IllegalMethod} from "./verification/IllegalMethod";
|
||||
import {KeySignatureUploadError} from "../errors";
|
||||
|
||||
const DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
|
||||
@@ -667,15 +668,34 @@ Crypto.prototype._afterCrossSigningLocalKeyChange = async function() {
|
||||
const device = this._deviceList.getStoredDevice(this._userId, this._deviceId);
|
||||
const signedDevice = await this._crossSigningInfo.signDevice(this._userId, device);
|
||||
logger.info(`Starting background key sig upload for ${this._deviceId}`);
|
||||
this._baseApis.uploadKeySignatures({
|
||||
|
||||
const upload = ({ shouldEmit }) => {
|
||||
return this._baseApis.uploadKeySignatures({
|
||||
[this._userId]: {
|
||||
[this._deviceId]: signedDevice,
|
||||
},
|
||||
}).then(() => {
|
||||
}).then((response) => {
|
||||
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);
|
||||
logger.error(
|
||||
`Error during background key sig upload for ${this._deviceId}`,
|
||||
e,
|
||||
);
|
||||
});
|
||||
};
|
||||
upload({ shouldEmit: true });
|
||||
|
||||
const shouldUpgradeCb = (
|
||||
this._baseApis._cryptoCallbacks.shouldUpgradeDeviceVerifications
|
||||
@@ -959,12 +979,31 @@ Crypto.prototype.checkOwnCrossSigningTrust = async function() {
|
||||
|
||||
const keysToUpload = Object.keys(keySignatures);
|
||||
if (keysToUpload.length) {
|
||||
const upload = ({ shouldEmit }) => {
|
||||
logger.info(`Starting background key sig upload for ${keysToUpload}`);
|
||||
this._baseApis.uploadKeySignatures({ [this._userId]: keySignatures }).then(() => {
|
||||
return this._baseApis.uploadKeySignatures({ [this._userId]: keySignatures })
|
||||
.then((response) => {
|
||||
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);
|
||||
logger.error(
|
||||
`Error during background key sig upload for ${keysToUpload}`,
|
||||
e,
|
||||
);
|
||||
});
|
||||
};
|
||||
upload({ shouldEmit: true });
|
||||
}
|
||||
|
||||
this.emit("userTrustStatusChanged", userId, this.checkUserTrust(userId));
|
||||
@@ -1605,12 +1644,33 @@ Crypto.prototype.setDeviceVerification = async function(
|
||||
);
|
||||
const device = await this._crossSigningInfo.signUser(xsk);
|
||||
if (device) {
|
||||
const upload = async ({ shouldEmit }) => {
|
||||
logger.info("Uploading signature for " + userId + "...");
|
||||
await this._baseApis.uploadKeySignatures({
|
||||
const response = await this._baseApis.uploadKeySignatures({
|
||||
[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
|
||||
// (we could do local echo to speed things up)
|
||||
}
|
||||
@@ -1659,12 +1719,27 @@ Crypto.prototype.setDeviceVerification = async function(
|
||||
userId, DeviceInfo.fromStorage(dev, deviceId),
|
||||
);
|
||||
if (device) {
|
||||
const upload = async ({shouldEmit}) => {
|
||||
logger.info("Uploading signature for " + deviceId);
|
||||
await this._baseApis.uploadKeySignatures({
|
||||
const response = await this._baseApis.uploadKeySignatures({
|
||||
[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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,3 +44,10 @@ InvalidCryptoStoreError.prototype = Object.create(Error.prototype, {
|
||||
},
|
||||
});
|
||||
Reflect.setPrototypeOf(InvalidCryptoStoreError, Error);
|
||||
|
||||
export class KeySignatureUploadError extends Error {
|
||||
constructor(message, value) {
|
||||
super(message);
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user