-
-
Notifications
You must be signed in to change notification settings - Fork 594
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
verification in DMs #1050
Merged
Merged
verification in DMs #1050
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -756,6 +756,132 @@ Crypto.prototype.setDeviceVerification = async function( | |
}; | ||
|
||
|
||
function verificationEventHandler(target, userId, roomId, eventId) { | ||
return function(event) { | ||
// listen for events related to this verification | ||
if (event.getRoomId() !== roomId | ||
|| event.getSender() !== userId) { | ||
return; | ||
} | ||
const content = event.getContent(); | ||
if (!content["m.relates_to"]) { | ||
return; | ||
} | ||
const relatesTo | ||
= content["m.relationship"] || content["m.relates_to"]; | ||
if (!relatesTo.rel_type | ||
|| relatesTo.rel_type !== "m.reference" | ||
|| !relatesTo.event_id | ||
|| relatesTo.event_id !== eventId) { | ||
return; | ||
} | ||
|
||
// the event seems to be related to this verification, so pass it on to | ||
// the verification handler | ||
target.handleEvent(event); | ||
}; | ||
} | ||
|
||
Crypto.prototype.requestVerificationDM = async function(userId, roomId, methods) { | ||
let methodMap; | ||
if (methods) { | ||
methodMap = new Map(); | ||
for (const method of methods) { | ||
if (typeof method === "string") { | ||
methodMap.set(method, defaultVerificationMethods[method]); | ||
} else if (method.NAME) { | ||
methodMap.set(method.NAME, method); | ||
} | ||
} | ||
} else { | ||
methodMap = this._baseApis._crypto._verificationMethods; | ||
} | ||
|
||
let eventId = undefined; | ||
const listenPromise = new Promise(async (_resolve, _reject) => { | ||
const listener = (event) => { | ||
// listen for events related to this verification | ||
if (event.getRoomId() !== roomId | ||
|| event.getSender() !== userId) { | ||
return; | ||
} | ||
const relatesTo = event.getRelation(); | ||
if (!relatesTo || !relatesTo.rel_type | ||
|| relatesTo.rel_type !== "m.reference" | ||
|| !relatesTo.event_id | ||
|| relatesTo.event_id !== eventId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would throw if an event comes in before the verification event gets sent (and eventId is defined).
and it throws with You could put a |
||
return; | ||
} | ||
|
||
const content = event.getContent(); | ||
// the event seems to be related to this verification | ||
switch (event.getType()) { | ||
case "m.key.verification.start": { | ||
const verifier = new (methodMap.get(content.method))( | ||
this._baseApis, userId, content.from_device, eventId, | ||
roomId, event, | ||
); | ||
verifier.handler = verificationEventHandler( | ||
verifier, userId, roomId, eventId, | ||
); | ||
// this handler gets removed when the verification finishes | ||
// (see the verify method of crypto/verification/Base.js) | ||
this._baseApis.on("event", verifier.handler); | ||
resolve(verifier); | ||
break; | ||
} | ||
case "m.key.verification.cancel": { | ||
reject(event); | ||
break; | ||
} | ||
} | ||
}; | ||
this._baseApis.on("event", listener); | ||
|
||
const resolve = (...args) => { | ||
this._baseApis.off("event", listener); | ||
_resolve(...args); | ||
}; | ||
const reject = (...args) => { | ||
this._baseApis.off("event", listener); | ||
_reject(...args); | ||
}; | ||
}); | ||
|
||
const res = await this._baseApis.sendEvent( | ||
roomId, "m.room.message", | ||
{ | ||
body: this._baseApis.getUserId() + " is requesting to verify " + | ||
"your key, but your client does not support in-chat key " + | ||
"verification. You will need to use legacy key " + | ||
"verification to verify keys.", | ||
msgtype: "m.key.verification.request", | ||
to: userId, | ||
from_device: this._baseApis.getDeviceId(), | ||
methods: [...methodMap.keys()], | ||
}, | ||
); | ||
eventId = res.event_id; | ||
|
||
return listenPromise; | ||
}; | ||
|
||
Crypto.prototype.acceptVerificationDM = function(event, Method) { | ||
if (typeof(Method) === "string") { | ||
Method = defaultVerificationMethods[Method]; | ||
} | ||
const content = event.getContent(); | ||
const verifier = new Method( | ||
this._baseApis, event.getSender(), content.from_device, event.getId(), | ||
event.getRoomId(), | ||
); | ||
verifier.handler = verificationEventHandler( | ||
verifier, event.getSender(), event.getRoomId(), event.getId(), | ||
); | ||
this._baseApis.on("event", verifier.handler); | ||
bwindels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return verifier; | ||
}; | ||
|
||
Crypto.prototype.requestVerification = function(userId, methods, devices) { | ||
if (!methods) { | ||
// .keys() returns an iterator, so we need to explicitly turn it into an array | ||
|
@@ -803,20 +929,7 @@ Crypto.prototype.beginKeyVerification = function( | |
this._verificationTransactions.set(userId, new Map()); | ||
} | ||
transactionId = transactionId || randomString(32); | ||
if (method instanceof Array) { | ||
if (method.length !== 2 | ||
|| !this._verificationMethods.has(method[0]) | ||
|| !this._verificationMethods.has(method[1])) { | ||
throw newUnknownMethodError(); | ||
} | ||
/* | ||
return new TwoPartVerification( | ||
this._verificationMethods[method[0]], | ||
this._verificationMethods[method[1]], | ||
userId, deviceId, transactionId, | ||
); | ||
*/ | ||
} else if (this._verificationMethods.has(method)) { | ||
if (this._verificationMethods.has(method)) { | ||
const verifier = new (this._verificationMethods.get(method))( | ||
this._baseApis, userId, deviceId, transactionId, | ||
); | ||
|
@@ -1817,22 +1930,6 @@ Crypto.prototype._onKeyVerificationStart = function(event) { | |
transaction_id: content.transactionId, | ||
})); | ||
return; | ||
} else if (content.next_method) { | ||
if (!this._verificationMethods.has(content.next_method)) { | ||
cancel(newUnknownMethodError({ | ||
transaction_id: content.transactionId, | ||
})); | ||
return; | ||
} else { | ||
/* TODO: | ||
const verification = new TwoPartVerification( | ||
this._verificationMethods[content.method], | ||
this._verificationMethods[content.next_method], | ||
userId, deviceId, | ||
); | ||
this.emit(verification.event_type, verification); | ||
this.emit(verification.first.event_type, verification);*/ | ||
} | ||
} else { | ||
const verifier = new (this._verificationMethods.get(content.method))( | ||
this._baseApis, sender, deviceId, content.transaction_id, | ||
|
@@ -1887,8 +1984,6 @@ Crypto.prototype._onKeyVerificationStart = function(event) { | |
|
||
handler.request.resolve(verifier); | ||
} | ||
} else { | ||
// FIXME: make sure we're in a two-part verification, and the start matches the second part | ||
} | ||
} | ||
this._baseApis.emit("crypto.verification.start", verifier); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this doesn't need to be async anymore i think?