[WIP]: SAS support.

This commit is contained in:
Damir Jelić 2019-04-02 15:10:58 +02:00
parent c1cfc4a8e4
commit ed1b16a4e2

View file

@ -66,7 +66,11 @@ from nio import (
LoginError,
JoinedMembersResponse,
JoinedMembersError,
RoomKeyEvent
RoomKeyEvent,
KeyVerificationStart,
KeyVerificationCancel,
KeyVerificationKey,
KeyVerificationMac
)
from . import globals as G
@ -1206,6 +1210,28 @@ class MatrixServer(object):
room_buffer.undecrypted_events.remove(undecrypted_event)
room_buffer.replace_undecrypted_line(event)
def accept_key_verification(self, event):
_, request = self.client.accept_key_verification(event)
self.send_or_queue(request)
def send_key_verification_key(self, sas):
_, request = self.client.to_device(
"m.key.verification.key",
sas.share_key(),
sas.other_user,
sas.other_device,
)
self.send_or_queue(request)
def send_key_verification_mac(self, sas):
_, request = self.client.to_device(
"m.key.verification.mac",
sas.get_mac(),
sas.other_user,
sas.other_device,
)
self.send_or_queue(request)
def _handle_sync(self, response):
# we got the same batch again, nothing to do
if self.next_batch == response.next_batch:
@ -1215,9 +1241,7 @@ class MatrixServer(object):
self._handle_room_info(response)
for event in response.to_device_events:
if not isinstance(event, RoomKeyEvent):
continue
if isinstance(event, RoomKeyEvent):
message = {
"sender": event.sender,
"sender_key": event.sender_key,
@ -1231,6 +1255,37 @@ class MatrixServer(object):
# TODO try to decrypt some cached undecrypted messages with the
# new key
# self.decrypt_printed_messages(event)
elif isinstance(event, KeyVerificationStart):
self.accept_key_verification(event)
# print(event)
elif isinstance(event, KeyVerificationCancel):
print(event)
elif isinstance(event, KeyVerificationMac):
sas = self.client.active_key_verifications.get(
event.transaction_id, None
)
if sas:
self.send_key_verification_mac(sas)
else:
print("NO SAS??")
print(event)
elif isinstance(event, KeyVerificationKey):
sas = self.client.active_key_verifications.get(
event.transaction_id, None
)
if sas:
emoji = sas.get_emoji()
self.info(" ".join(emoji))
self.send_key_verification_key(sas)
else:
print("NO SAS??")
print(event)
# Full sync response handle everything.
if isinstance(response, SyncResponse):