matrixlib.megolm module

Megolm-related functionality

class matrixlib.megolm.InboundMegolmSession

Manages a Megolm session for decrypting

Do not use initializer function. Use the from_* methods instead

property authenticated: bool

Whether we know that the Megolm session comes from the associated sender_key

client: Client
decrypt(event_content: dict) dict

Decrypt an m.room.encrypted event encrypted with Megolm

Arguments:

event_content:

the content of the m.room.encrypted event

Returns the decrypted event, which will be a dict that should have type (the decrypted event type), content (the event content), and room_id (the ID of the room the event was sent to) properties.

classmethod from_outbound_session(c: Client, outbound: OutboundMegolmSession, key: bytes | None = None) InboundMegolmSession

Create an InboundMegolmSession from an OutboundMegolmSession

Arguments:

c:

the client object

outbound:

the OutboundMegolmSession to use

key:

a 32-byte binary used to encrypt the objects in storage. If not specified, uses the same key as used by outbound

classmethod from_room_key(c: Client, user_id: str, sender_key: str, room_key_content: dict, key: bytes) InboundMegolmSession

Create an InboundMegolmSession from an m.room_key event

Arguments:

c:

the client object

user_id:

the user ID of the sender of the m.room_key event

sender_key:

the identity key of the sender of the m.room_key event

room_key_content:

the content of the m.room_key event

key:

a 32-byte binary used to encrypt the objects in storage

classmethod from_storage(c: Client, room_id: str, user_id: str, session_id: str, key: bytes) InboundMegolmSession | None

Load a session from storage

Arguments:

c:

the client object

room_id:

the ID of the room that the session belongs to

user_id:

the ID of the user that the session belongs to

session ID:

the ID of the Megolm session

key:

a 32-byte binary used to encrypt the objects in storage

key: bytes
room_id: str
property sender_key: str

The identity key of the Megolm session’s sender

session: InboundGroupSession
session_data: dict[str, Any]
user_id: str
class matrixlib.megolm.OlmMegolmEncryptionManager(c: Client, key: bytes, device_keys_manager: DeviceKeysManager | None, device_tracker: DeviceTracker | None)

Manages encryption and decryption of room events

Arguments:

c:

the client object

key:

a 32-byte binary used to encrypt the objects in storage

device_keys_manager:

a devices.DeviceKeysManager object to use. If none is provided, a new one will be created.

device_tracker:

a devices.DeviceTracker object to use. If none is provided, a new one will be created.

async decrypt_room_event(encrypted: RoomEvent) RoomEvent

Decrypt an event

Returns the room event that would have been received if the event was not encrypted.

Arguments:

encrypted:

the encrypted room event

class matrixlib.megolm.OutboundMegolmSession(c: Client, room_id: str, room_state_tracker: RoomStateTracker, device_tracker: DeviceTracker, device_keys_manager: DeviceKeysManager, key: bytes | None = None, session_id: str | None = None)

Manages a Megolm session for decrypting

Arguments:

c:

the client object

room_id:

the ID of the room that the session belongs to

room_state_tracker:

a RoomStateTracker object

device_tracker:

a DeviceTracker object

device_keys_manager:

a DeviceKeysManager object

key:

a 32-byte binary used to encrypt the objects in storage

session_id:

if given, will load a session from storage. If omitted a new session will be created

encrypt(event_type: str, content: dict) dict

Encrypt an event

Arguments:

event_type:

the type of the event (e.g. m.room.message)

content:

the event content

Returns the content of a m.room.encrypted event

async get_session_key_for_sending() list[Tuple[dict, dict]]

Get the devices that we need to send the Megolm session, along with the data to send.

Returns a list of tuples. The first item in the tuple is the device key to send to, and the second item in the tuple is the contents of an m.room_key event to send to the device. The event should be sent encrypted.

mark_as_sent(devices: list[Tuple[str, str]]) None

Indicate that the session has been sent to the given devices.

Arguments:

devices:

a list of (user_id, device_id) tuples

property session_id: str

The ID of the Megolm session

exception matrixlib.megolm.SessionExpiredException

Indicates that the session has expired