Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find existing requests when starting a new verification request #1209

Merged
merged 10 commits into from
Feb 14, 2020
14 changes: 14 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,20 @@ MatrixClient.prototype.requestVerificationDM = function(userId, roomId) {
return this._crypto.requestVerificationDM(userId, roomId);
};

/**
* Finds a DM verification request that is already in progress for the given room id
*
* @param {string} roomId the room to use for verification
*
* @returns {module:crypto/verification/request/VerificationRequest?} the VerificationRequest that is in progress, if any
*/
MatrixClient.prototype.findVerificationRequestDMInProgress = function(roomId) {
if (this._crypto === null) {
throw new Error("End-to-end encryption disabled");
}
return this._crypto.findVerificationRequestDMInProgress(roomId);
};

/**
* Request a key verification from another user.
*
Expand Down
14 changes: 14 additions & 0 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,16 @@ Crypto.prototype.setDeviceVerification = async function(
return deviceObj;
};

Crypto.prototype.findVerificationRequestDMInProgress = function(roomId) {
return this._inRoomVerificationRequests.findRequestInProgress(roomId);
};

Crypto.prototype.requestVerificationDM = function(userId, roomId) {
const existingRequest = this._inRoomVerificationRequests.
findRequestInProgress(roomId);
if (existingRequest) {
return Promise.resolve(existingRequest);
}
const channel = new InRoomChannel(this._baseApis, roomId, userId);
return this._requestVerificationWithChannel(
userId,
Expand All @@ -1632,6 +1641,11 @@ Crypto.prototype.requestVerification = function(userId, devices) {
if (!devices) {
devices = Object.keys(this._deviceList.getRawStoredDevicesForUser(userId));
}
const existingRequest = this._toDeviceVerificationRequests
.findRequestInProgress(userId, devices);
if (existingRequest) {
return Promise.resolve(existingRequest);
}
const channel = new ToDeviceChannel(this._baseApis, userId, devices);
return this._requestVerificationWithChannel(
userId,
Expand Down
10 changes: 6 additions & 4 deletions src/crypto/verification/SAS.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,16 @@ export class SAS extends Base {
+ this._channel.transactionId;
const sasBytes = olmSAS.generate_bytes(sasInfo, 6);
const verifySAS = new Promise((resolve, reject) => {
this.emit("show_sas", {
this.sasEvent = {
sas: generateSas(sasBytes, sasMethods),
confirm: () => {
this._sendMAC(olmSAS, macMethod);
resolve();
},
cancel: () => reject(newUserCancelledError()),
mismatch: () => reject(newMismatchedSASError()),
});
};
this.emit("show_sas", this.sasEvent);
});


Expand Down Expand Up @@ -390,15 +391,16 @@ export class SAS extends Base {
+ this._channel.transactionId;
const sasBytes = olmSAS.generate_bytes(sasInfo, 6);
const verifySAS = new Promise((resolve, reject) => {
this.emit("show_sas", {
this.sasEvent = {
sas: generateSas(sasBytes, sasMethods),
confirm: () => {
this._sendMAC(olmSAS, macMethod);
resolve();
},
cancel: () => reject(newUserCancelledError()),
mismatch: () => reject(newMismatchedSASError()),
});
};
this.emit("show_sas", this.sasEvent);
});


Expand Down
12 changes: 11 additions & 1 deletion src/crypto/verification/request/InRoomChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ export class InRoomRequests {
getRequest(event) {
const roomId = event.getRoomId();
const txnId = InRoomChannel.getTransactionId(event);
// console.log(`looking for request in room ${roomId} with txnId ${txnId} for an ${event.getType()} from ${event.getSender()}...`);
return this._getRequestByTxnId(roomId, txnId);
}

Expand Down Expand Up @@ -351,4 +350,15 @@ export class InRoomRequests {
}
}
}

findRequestInProgress(roomId) {
const requestsByTxnId = this._requestsByRoomId.get(roomId);
if (requestsByTxnId) {
for(const request of requestsByTxnId.values()) {
bwindels marked this conversation as resolved.
Show resolved Hide resolved
if (request.pending) {
return request;
}
}
}
}
}
25 changes: 25 additions & 0 deletions src/crypto/verification/request/ToDeviceChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ export class ToDeviceChannel {
this._deviceId = deviceId;
}

isToDevices(devices) {
if (devices.length === this._devices.length) {
for (const device of devices) {
const d = this._devices.find(d => d.deviceId === device.deviceId);
if (!d) {
return false;
}
}
return true;
} else {
return false;
}
}

get deviceId() {
return this._deviceId;
}
Expand Down Expand Up @@ -335,4 +349,15 @@ export class ToDeviceRequests {
}
}
}

findRequestInProgress(userId, devices) {
const requestsByTxnId = this._requestsByUserId.get(userId);
if (requestsByTxnId) {
for(const request of requestsByTxnId.values()) {
bwindels marked this conversation as resolved.
Show resolved Hide resolved
if (request.pending && request.channel.isToDevices(devices)) {
return request;
}
}
}
}
}
5 changes: 3 additions & 2 deletions src/crypto/verification/request/VerificationRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ export class VerificationRequest extends EventEmitter {

/** whether this request has sent it's initial event and needs more events to complete */
get pending() {
return this._phase !== PHASE_DONE
&& this._phase !== PHASE_CANCELLED;
return !this.observeOnly &&
this._phase !== PHASE_DONE &&
this._phase !== PHASE_CANCELLED;
}

/** Whether this request was initiated by the syncing user.
Expand Down