Debian Codegraph @

examples/btbv_session_manager.py

raw symbols SCIP
1 import abc
2 import enum
3 from typing import FrozenSet, Optional, Set
4
5 from typing_extensions import assert_never
6
7 import omemo
8
9
10 __all__ = [
11 "BTBVSessionManager",
12 "BTBVTrustLevel"
13 ]
14
15
16 @enum.unique
17 class BTBVTrustLevel(enum.Enum):
18 """
19 Trust levels modeling Blind Trust Before Verification (BTBV).
20 """
21
22 TRUSTED = "TRUSTED"
23 BLINDLY_TRUSTED = "BLINDLY_TRUSTED"
24 UNDECIDED = "UNDECIDED"
25 DISTRUSTED = "DISTRUSTED"
26
27
28 # Note that while this is an "example", it is fully functional and can be used as-is.
29 class BTBVSessionManager(omemo.SessionManager):
30 """
31 Partial :class:`omemo.SessionManager` implementation with BTBV as its trust system.
32 """
33
34 async def _evaluate_custom_trust_level(self, device: omemo.DeviceInformation) -> omemo.TrustLevel:
35 try:
36 trust_level = BTBVTrustLevel(device.trust_level_name)
37 except ValueError as e:
38 raise omemo.UnknownTrustLevel(f"Unknown trust level name: {device.trust_level_name}") from e
39
40 if trust_level is BTBVTrustLevel.TRUSTED or trust_level is BTBVTrustLevel.BLINDLY_TRUSTED:
41 return omemo.TrustLevel.TRUSTED
42 if trust_level is BTBVTrustLevel.UNDECIDED:
43 return omemo.TrustLevel.UNDECIDED
44 if trust_level is BTBVTrustLevel.DISTRUSTED:
45 return omemo.TrustLevel.DISTRUSTED
46
47 assert_never(trust_level)
48
49 async def _make_trust_decision(
50 self,
51 undecided: FrozenSet[omemo.DeviceInformation],
52 identifier: Optional[str]
53 ) -> None:
54 # For BTBV, affected JIDs can be separated into two pools: one pool of JIDs for which blind trust is
55 # active, i.e. no manual verification was performed before, and one pool of JIDs to use manual trust
56 # with instead.
57 bare_jids = { device.bare_jid for device in undecided }
58
59 blind_trust_bare_jids: Set[str] = set()
60 manual_trust_bare_jids: Set[str] = set()
61
62 # For each bare JID, decide whether blind trust applies
63 for bare_jid in bare_jids:
64 # Get all known devices belonging to the bare JID
65 devices = await self.get_device_information(bare_jid)
66
67 # If the trust levels of all devices correspond to those used by blind trust, blind trust applies.
68 # Otherwise, fall back to manual trust.
69 if all(BTBVTrustLevel(device.trust_level_name) in {
70 BTBVTrustLevel.UNDECIDED,
71 BTBVTrustLevel.BLINDLY_TRUSTED
72 } for device in devices):
73 blind_trust_bare_jids.add(bare_jid)
74 else:
75 manual_trust_bare_jids.add(bare_jid)
76
77 # With the JIDs sorted into their respective pools, the undecided devices can be categorized too
78 blindly_trusted_devices = { dev for dev in undecided if dev.bare_jid in blind_trust_bare_jids }
79 manually_trusted_devices = { dev for dev in undecided if dev.bare_jid in manual_trust_bare_jids }
80
81 # Blindly trust devices handled by blind trust
82 if len(blindly_trusted_devices) > 0:
83 for device in blindly_trusted_devices:
84 await self.set_trust(
85 device.bare_jid,
86 device.identity_key,
87 BTBVTrustLevel.BLINDLY_TRUSTED.name
88 )
89
90 await self._devices_blindly_trusted(frozenset(blindly_trusted_devices), identifier)
91
92 # Prompt the user for manual trust decisions on the devices handled by manual trust
93 if len(manually_trusted_devices) > 0:
94 await self._prompt_manual_trust(frozenset(manually_trusted_devices), identifier)
95
96 async def _devices_blindly_trusted(
97 self,
98 blindly_trusted: FrozenSet[omemo.DeviceInformation],
99 identifier: Optional[str]
100 ) -> None:
101 """
102 Get notified about newly blindly trusted devices. This method is called automatically by
103 :meth:`_make_trust_decision` whenever at least one device was blindly trusted. You can use this method
104 for example to notify the user about the automated change in trust.
105
106 Does nothing by default.
107
108 Args:
109 blindly_trusted: A set of devices that were blindly trusted.
110 identifier: Forwarded from :meth:`_make_trust_decision`, refer to its documentation for details.
111 """
112
113 @abc.abstractmethod
114 async def _prompt_manual_trust(
115 self,
116 manually_trusted: FrozenSet[omemo.DeviceInformation],
117 identifier: Optional[str]
118 ) -> None:
119 """
120 Prompt manual trust decision on a set of undecided identity keys. The trust decisions are expected to
121 be persisted by calling :meth:`set_trust`.
122
123 Args:
124 manually_trusted: A set of devices whose trust has to be manually decided by the user.
125 identifier: Forwarded from :meth:`_make_trust_decision`, refer to its documentation for details.
126
127 Raises:
128 TrustDecisionFailed: if for any reason the trust decision failed/could not be completed. Feel free
129 to raise a subclass instead.
130
131 Note:
132 This is called when the encryption needs to know whether it is allowed to encrypt for these
133 devices or not. When this method returns, all previously undecided trust levels should have been
134 replaced by calling :meth:`set_trust` with a different trust level. If they are not replaced or
135 still evaluate to the undecided trust level after the call, the encryption will fail with an
136 exception. See :meth:`encrypt` for details.
137 """