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')}") # ------------------------------------------------------------------------------