comparison sat/memory/encryption.py @ 3911:8289ac1b34f4

plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo: - support for both (modern) OMEMO under the `urn:xmpp:omemo:2` namespace and (legacy) OMEMO under the `eu.siacs.conversations.axolotl` namespace - maintains one identity across both versions of OMEMO - migrates data from the old plugin - includes more features for protocol stability - uses SCE for modern OMEMO - fully type-checked, linted and format-checked - added type hints to various pieces of backend code used by the plugin - added stubs for some Twisted APIs used by the plugin under stubs/ (use `export MYPYPATH=stubs/` before running mypy) - core (xmpp): enabled `send` trigger and made it an asyncPoint fix 375
author Syndace <me@syndace.dev>
date Tue, 23 Aug 2022 21:06:24 +0200
parents be6d91572633
children cc2705225778
comparison
equal deleted inserted replaced
3910:199598223f82 3911:8289ac1b34f4
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import copy 20 import copy
21 from functools import partial 21 from functools import partial
22 from collections import namedtuple 22 from typing import Optional
23 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
24 from twisted.internet import defer 24 from twisted.internet import defer
25 from twisted.python import failure 25 from twisted.python import failure
26 from sat.core.core_types import EncryptionPlugin, EncryptionSession, MessageData
26 from sat.core.i18n import D_, _ 27 from sat.core.i18n import D_, _
27 from sat.core.constants import Const as C 28 from sat.core.constants import Const as C
28 from sat.core import exceptions 29 from sat.core import exceptions
29 from sat.core.log import getLogger 30 from sat.core.log import getLogger
30 from sat.tools.common import data_format 31 from sat.tools.common import data_format
31 from sat.tools import utils 32 from sat.tools import utils
32 from sat.memory import persistent 33 from sat.memory import persistent
33 34
34 35
35 log = getLogger(__name__) 36 log = getLogger(__name__)
36
37 EncryptionPlugin = namedtuple("EncryptionPlugin", ("instance",
38 "name",
39 "namespace",
40 "priority",
41 "directed"))
42 37
43 38
44 class EncryptionHandler: 39 class EncryptionHandler:
45 """Class to handle encryption sessions for a client""" 40 """Class to handle encryption sessions for a client"""
46 plugins = [] # plugin able to encrypt messages 41 plugins = [] # plugin able to encrypt messages
337 "{destinee} server administrators will be able to read them.").format( 332 "{destinee} server administrators will be able to read them.").format(
338 destinee=entity.full()) 333 destinee=entity.full())
339 334
340 self.client.feedback(entity, msg) 335 self.client.feedback(entity, msg)
341 336
342 def getSession(self, entity): 337 def getSession(self, entity: jid.JID) -> Optional[EncryptionSession]:
343 """Get encryption session for this contact 338 """Get encryption session for this contact
344 339
345 @param entity(jid.JID): get the session for this entity 340 @param entity(jid.JID): get the session for this entity
346 must be a bare jid 341 must be a bare jid
347 @return (dict, None): encryption session data 342 @return (dict, None): encryption session data
474 ) 469 )
475 defer.ensureDeferred(self.start(from_bare_jid, namespace)) 470 defer.ensureDeferred(self.start(from_bare_jid, namespace))
476 471
477 return mess_data 472 return mess_data
478 473
479 def isEncryptionRequested(self, mess_data, namespace=None): 474 def isEncryptionRequested(
475 self,
476 mess_data: MessageData,
477 namespace: Optional[str] = None
478 ) -> bool:
480 """Helper method to check if encryption is requested in an outgoind message 479 """Helper method to check if encryption is requested in an outgoind message
481 480
482 @param mess_data(dict): message data for outgoing message 481 @param mess_data: message data for outgoing message
483 @param namespace(str, None): if set, check if encryption is requested for the 482 @param namespace: if set, check if encryption is requested for the algorithm
484 algorithm specified 483 specified
485 @return (bool): True if the encryption flag is present 484 @return: True if the encryption flag is present
486 """ 485 """
487 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) 486 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
488 if encryption is None: 487 if encryption is None:
489 return False 488 return False
490 # we get plugin even if namespace is None to be sure that the key exists 489 # we get plugin even if namespace is None to be sure that the key exists