diff --git a/matrix_keycloak_bot.py b/matrix_keycloak_bot.py index aa2e1a6..c6828bf 100644 --- a/matrix_keycloak_bot.py +++ b/matrix_keycloak_bot.py @@ -188,13 +188,21 @@ async def on_to_device(client: AsyncClient, event): print(f"Failed to confirm SAS: {resp}") elif isinstance(event, KeyVerificationMac): - print(f"Key verification MAC received from {event.sender} ({event.from_device}). Finalizing verification...") + # Extract device_id safely from the raw event payload + content = event.source.get("content", {}) + from_device = ( + content.get("from_device") + or event.source.get("sender_device") + or "*" + ) + + print(f"Key verification MAC received from {event.sender} ({from_device}). Finalizing verification...") - # 1. Send m.key.verification.done to complete the SAS protocol in Element Web + # 1. Send m.key.verification.done to complete the SAS exchange done_msg = ToDeviceMessage( "m.key.verification.done", event.sender, - event.from_device, + from_device, {"transaction_id": event.transaction_id}, ) resp = await client.to_device(done_msg) @@ -203,11 +211,14 @@ async def on_to_device(client: AsyncClient, event): 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!") + # 2. Mark the sender's device as verified in nio's local device store + if from_device != "*": + device = client.device_store.get(event.sender, {}).get(from_device) + if device: + client.verify_device(device) + print(f"✅ Device {from_device} for {event.sender} is now verified and trusted!") + else: + print(f"⚠️ Device {from_device} not found in store, but verification completed.") elif isinstance(event, KeyVerificationCancel): print(f"Key verification cancelled by {event.sender}: {getattr(event, 'reason', 'No reason given')}")