Handle the rest of the key verification events

This commit is contained in:
Astra Logical 2026-07-23 10:28:41 -04:00
parent bda6076240
commit 4b09b7ff43

View file

@ -12,9 +12,12 @@ from nio import (
RoomSendResponse,
InviteMemberEvent,
OlmUnverifiedDeviceError,
KeyVerificationRequest,
KeyVerificationStart,
KeyVerificationKey,
KeyVerificationMac,
KeyVerificationCancel,
KeyVerificationDone,
ToDeviceError,
)
@ -140,21 +143,28 @@ def get_requestable_groups(username: str) -> tuple[bool, str]:
# ------------------------------------------------------------------------------
async def on_to_device(client: AsyncClient, event):
"""Handles incoming interactive SAS key verification events automatically."""
if isinstance(event, KeyVerificationStart):
print(f"Received key verification request from {event.sender} ({event.from_device}). Accepting...")
if isinstance(event, KeyVerificationRequest):
print(f"Received verification request from {event.sender} ({event.from_device}). Accepting request...")
resp = await client.accept_key_verification(event.transaction_id)
if isinstance(resp, ToDeviceError):
print(f"Failed to accept verification: {resp}")
print(f"Failed to accept verification request: {resp}")
elif isinstance(event, KeyVerificationStart):
print(f"Received key verification start from {event.sender} ({event.from_device}). Accepting start...")
resp = await client.accept_key_verification(event.transaction_id)
if isinstance(resp, ToDeviceError):
print(f"Failed to accept verification start: {resp}")
elif isinstance(event, KeyVerificationKey):
print(f"Received SAS key from {event.sender}. Auto-confirming...")
print(f"Received SAS key from {event.sender}. Auto-confirming SAS...")
resp = await client.confirm_short_auth_string(event.transaction_id)
if isinstance(resp, ToDeviceError):
print(f"Failed to confirm SAS: {resp}")
elif isinstance(event, KeyVerificationMac):
elif isinstance(event, (KeyVerificationMac, KeyVerificationDone)):
print(f"✅ Key verification successfully completed with {event.sender} ({event.from_device})!")
# ------------------------------------------------------------------------------
# Matrix Message Sending Wrappers
# ------------------------------------------------------------------------------
@ -380,7 +390,14 @@ async def main():
)
client.add_to_device_callback(
lambda event: on_to_device(client, event),
(KeyVerificationStart, KeyVerificationKey, KeyVerificationMac),
(
KeyVerificationRequest,
KeyVerificationStart,
KeyVerificationKey,
KeyVerificationMac,
KeyVerificationCancel,
KeyVerificationDone,
),
)
print(f"Connecting to Matrix homeserver {MATRIX_HOMESERVER}...")