From 5dc3fe402339f19cb1ad2619d4a0e3ff7c650b5b Mon Sep 17 00:00:00 2001 From: Astra Logical Date: Thu, 23 Jul 2026 11:01:46 -0400 Subject: [PATCH] Respond to final mac message and verify device in keystore --- matrix_keycloak_bot.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/matrix_keycloak_bot.py b/matrix_keycloak_bot.py index f98ea7f..aa2e1a6 100644 --- a/matrix_keycloak_bot.py +++ b/matrix_keycloak_bot.py @@ -187,8 +187,30 @@ async def on_to_device(client: AsyncClient, event): if isinstance(resp, ToDeviceError): print(f"Failed to confirm SAS: {resp}") - elif isinstance(event, (KeyVerificationMac, KeyVerificationCancel)): - print(f"Key verification event ({event.__class__.__name__}) received from {event.sender}.") + elif isinstance(event, KeyVerificationMac): + print(f"Key verification MAC received from {event.sender} ({event.from_device}). Finalizing verification...") + + # 1. Send m.key.verification.done to complete the SAS protocol in Element Web + done_msg = ToDeviceMessage( + "m.key.verification.done", + event.sender, + event.from_device, + {"transaction_id": event.transaction_id}, + ) + resp = await client.to_device(done_msg) + if isinstance(resp, ToDeviceError): + print(f"Failed to send verification done: {resp}") + else: + print(f"Sent m.key.verification.done for {event.transaction_id}.") + + # 2. Mark the device as verified in matrix-nio's device store + if event.sender in client.device_store and event.from_device in client.device_store[event.sender]: + device = client.device_store[event.sender][event.from_device] + client.verify_device(device) + print(f"✅ Device {event.from_device} for {event.sender} is now verified and trusted!") + + elif isinstance(event, KeyVerificationCancel): + print(f"Key verification cancelled by {event.sender}: {getattr(event, 'reason', 'No reason given')}") # ------------------------------------------------------------------------------