Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0380.py @ 4307:6a0155f410bd
test (unit): add test for plugin XEP-0033:
those replace the legacy XEP-0033 test from libervia/backend/test/test_plugin_xep_0033.py.
rel 450
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 26 Sep 2024 16:12:01 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for Explicit Message Encryption |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
20 from libervia.backend.core.i18n import _, D_ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
21 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
22 from libervia.backend.core.log import getLogger |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.protocols.jabber import jid |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 log = getLogger(__name__) |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 PLUGIN_INFO = { |
3028 | 28 C.PI_NAME: "Explicit Message Encryption", |
29 C.PI_IMPORT_NAME: "XEP-0380", | |
30 C.PI_TYPE: "SEC", | |
31 C.PI_PROTOCOLS: ["XEP-0380"], | |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_DEPENDENCIES: [], |
3028 | 33 C.PI_MAIN: "XEP_0380", |
34 C.PI_HANDLER: "no", | |
35 C.PI_DESCRIPTION: _("""Implementation of Explicit Message Encryption"""), | |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 } |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
3028 | 38 NS_EME = "urn:xmpp:eme:0" |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 KNOWN_NAMESPACES = { |
3028 | 40 "urn:xmpp:otr:0": "OTR", |
41 "jabber:x:encrypted": "Legacy OpenPGP", | |
42 "urn:xmpp:openpgp:0": "OpenPGP for XMPP", | |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 } |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 class XEP_0380(object): |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 def __init__(self, host): |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
50 host.trigger.add("sendMessage", self._send_message_trigger) |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
51 host.trigger.add("message_received", self._message_received_trigger, priority=100) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
52 host.register_namespace("eme", NS_EME) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
54 def _add_eme_element(self, mess_data, namespace, name): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
55 message_elt = mess_data["xml"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
56 encryption_elt = message_elt.addElement((NS_EME, "encryption")) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
57 encryption_elt["namespace"] = namespace |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 if name is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
59 encryption_elt["name"] = name |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 return mess_data |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
62 def _send_message_trigger(self, client, mess_data, __, post_xml_treatments): |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 if encryption is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
65 namespace = encryption["plugin"].namespace |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 if namespace not in KNOWN_NAMESPACES: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
67 name = encryption["plugin"].name |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 else: |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 name = None |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 post_xml_treatments.addCallback( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
71 self._add_eme_element, namespace=namespace, name=name |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
72 ) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 return True |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
75 def _message_received_trigger(self, client, message_elt, post_treat): |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
77 encryption_elt = next(message_elt.elements(NS_EME, "encryption")) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 except StopIteration: |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 return True |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
81 namespace = encryption_elt["namespace"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
82 if namespace in client.encryption.get_namespaces(): |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 # message is encrypted and we can decrypt it |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 return True |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 |
3028 | 86 name = KNOWN_NAMESPACES.get(namespace, encryption_elt.getAttribute("name")) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 # at this point, message is encrypted but we know that we can't decrypt it, |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 # we need to notify the user |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
90 sender_s = message_elt["from"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
91 to_jid = jid.JID(message_elt["from"]) |
3028 | 92 algorithm = "{} [{}]".format(name, namespace) if name else namespace |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 log.warning( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
94 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
95 "Message from {sender} is encrypted with {algorithm} and we can't " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
96 "decrypt it.".format(sender=message_elt["from"], algorithm=algorithm) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
97 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
98 ) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 user_msg = D_( |
3028 | 101 "User {sender} sent you an encrypted message (encrypted with {algorithm}), " |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
102 "and we can't decrypt it." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
103 ).format(sender=sender_s, algorithm=algorithm) |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 |
2754
3bea6b5ae972
plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents:
2750
diff
changeset
|
105 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR} |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 client.feedback(to_jid, user_msg, extra) |
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 return False |