Mercurial > libervia-backend
annotate libervia/backend/memory/encryption.py @ 4293:9447796408f6
tests (unit): add test for XEP-0298 plugin + fix XEP-0167:
fix 447
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 29 Jul 2024 03:49:26 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
20 import copy |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
21 from functools import partial |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
22 from typing import Optional |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
23 from twisted.words.protocols.jabber import jid |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
24 from twisted.internet import defer |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
25 from twisted.python import failure |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
26 from libervia.backend.core.core_types import ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
27 EncryptionPlugin, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
28 EncryptionSession, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
29 MessageData, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
30 ) |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
31 from libervia.backend.core.i18n import D_, _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
32 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
|
33 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
34 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
35 from libervia.backend.tools.common import data_format |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
36 from libervia.backend.tools import utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
37 from libervia.backend.memory import persistent |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
38 |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
40 log = getLogger(__name__) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
43 class EncryptionHandler: |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 """Class to handle encryption sessions for a client""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
45 |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 plugins = [] # plugin able to encrypt messages |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
48 def __init__(self, client): |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
49 self.client = client |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 self._sessions = {} # bare_jid ==> encryption_data |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
51 self._stored_session = persistent.PersistentDict( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
52 "core:encryption", profile=client.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
53 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
55 @property |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
56 def host(self): |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
57 return self.client.host_app |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
58 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
59 async def load_sessions(self): |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
60 """Load persistent sessions""" |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
61 await self._stored_session.load() |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
62 start_d_list = [] |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
63 for entity_jid_s, namespace in self._stored_session.items(): |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
64 entity = jid.JID(entity_jid_s) |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
65 start_d_list.append(defer.ensureDeferred(self.start(entity, namespace))) |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
66 |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
67 if start_d_list: |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
68 result = await defer.DeferredList(start_d_list) |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
69 for idx, (success, err) in enumerate(result): |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
70 if not success: |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
71 entity_jid_s, namespace = list(self._stored_session.items())[idx] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
72 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
73 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
74 "Could not restart {namespace!r} encryption with {entity}: {err}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
75 ).format(namespace=namespace, entity=entity_jid_s, err=err) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 ) |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
77 log.info(_("encryption sessions restored")) |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
78 |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
80 def register_plugin(cls, plg_instance, name, namespace, priority=0, directed=False): |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 """Register a plugin handling an encryption algorithm |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 @param plg_instance(object): instance of the plugin |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 it must have the following methods: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
85 - get_trust_ui(entity): return a XMLUI for trust management |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
86 entity(jid.JID): entity to manage |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
87 The returned XMLUI must be a form |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
88 if may have the following methods: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
89 - start_encryption(entity): start encrypted session |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
90 entity(jid.JID): entity to start encrypted session with |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
91 - stop_encryption(entity): start encrypted session |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
92 entity(jid.JID): entity to stop encrypted session with |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
93 if they don't exists, those 2 methods will be ignored. |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
94 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
95 @param name(unicode): human readable name of the encryption algorithm |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 @param namespace(unicode): namespace of the encryption algorithm |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 @param priority(int): priority of this plugin to encrypt an message when not |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 selected manually |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
99 @param directed(bool): True if this plugin is directed (if it works with one |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
100 device only at a time) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 """ |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
102 existing_ns = set() |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
103 existing_names = set() |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
104 for p in cls.plugins: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
105 existing_ns.add(p.namespace.lower()) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
106 existing_names.add(p.name.lower()) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
107 if namespace.lower() in existing_ns: |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 raise exceptions.ConflictError("A plugin with this namespace already exists!") |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
109 if name.lower() in existing_names: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
110 raise exceptions.ConflictError("A plugin with this name already exists!") |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
111 plugin = EncryptionPlugin( |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 instance=plg_instance, |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 name=name, |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 namespace=namespace, |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
115 priority=priority, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
116 directed=directed, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
117 ) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
118 cls.plugins.append(plugin) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 cls.plugins.sort(key=lambda p: p.priority) |
3028 | 120 log.info(_("Encryption plugin registered: {name}").format(name=name)) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
122 @classmethod |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
123 def getPlugins(cls): |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
124 return cls.plugins |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
125 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
126 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
127 def get_plugin(cls, namespace): |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
128 try: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
129 return next(p for p in cls.plugins if p.namespace == namespace) |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
130 except StopIteration: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
131 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
132 _("Can't find requested encryption plugin: {namespace}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
133 namespace=namespace |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
134 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
135 ) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
136 |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
137 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
138 def get_namespaces(cls): |
2749
4b8271399f67
core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents:
2743
diff
changeset
|
139 """Get available plugin namespaces""" |
4b8271399f67
core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents:
2743
diff
changeset
|
140 return {p.namespace for p in cls.getPlugins()} |
4b8271399f67
core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents:
2743
diff
changeset
|
141 |
4b8271399f67
core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents:
2743
diff
changeset
|
142 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
143 def get_ns_from_name(cls, name): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
144 """Retrieve plugin namespace from its name |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
145 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
146 @param name(unicode): name of the plugin (case insensitive) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
147 @return (unicode): namespace of the plugin |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
148 @raise exceptions.NotFound: there is not encryption plugin of this name |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
149 """ |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
150 for p in cls.plugins: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
151 if p.name.lower() == name.lower(): |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
152 return p.namespace |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
153 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
154 _('Can\'t find a plugin with the name "{name}".'.format(name=name)) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
155 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
157 def get_bridge_data(self, session): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
158 """Retrieve session data serialized for bridge. |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
159 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
160 @param session(dict): encryption session |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
161 @return (unicode): serialized data for bridge |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
162 """ |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
163 if session is None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
164 return "" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
165 plugin = session["plugin"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
166 bridge_data = {"name": plugin.name, "namespace": plugin.namespace} |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
167 if "directed_devices" in session: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
168 bridge_data["directed_devices"] = session["directed_devices"] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
169 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
170 return data_format.serialise(bridge_data) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
171 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
172 async def _start_encryption(self, plugin, entity): |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
173 """Start encryption with a plugin |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
174 |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
175 This method must be called just before adding a plugin session. |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
176 StartEncryptionn method of plugin will be called if it exists. |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
177 """ |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
178 if not plugin.directed: |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
179 await self._stored_session.aset(entity.userhost(), plugin.namespace) |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
180 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
181 start_encryption = plugin.instance.start_encryption |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
182 except AttributeError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
183 log.debug(f"No start_encryption method found for {plugin.namespace}") |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
184 else: |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
185 # we copy entity to avoid having the resource changed by stop_encryption |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
186 await utils.as_deferred(start_encryption, self.client, copy.copy(entity)) |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
187 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
188 async def _stop_encryption(self, plugin, entity): |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
189 """Stop encryption with a plugin |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
190 |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
191 This method must be called just before removing a plugin session. |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
192 StopEncryptionn method of plugin will be called if it exists. |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
193 """ |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
194 try: |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
195 await self._stored_session.adel(entity.userhost()) |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
196 except KeyError: |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
197 pass |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
198 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
199 stop_encryption = plugin.instance.stop_encryption |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
200 except AttributeError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
201 log.debug(f"No stop_encryption method found for {plugin.namespace}") |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
202 else: |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
203 # we copy entity to avoid having the resource changed by stop_encryption |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
204 return utils.as_deferred(stop_encryption, self.client, copy.copy(entity)) |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
205 |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
206 async def start(self, entity, namespace=None, replace=False): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
207 """Start an encryption session with an entity |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
208 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
209 @param entity(jid.JID): entity to start an encryption session with |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 must be bare jid is the algorithm encrypt for all devices |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
211 @param namespace(unicode, None): namespace of the encryption algorithm |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
212 to use. |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 None to select automatically an algorithm |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
214 @param replace(bool): if True and an encrypted session already exists, |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
215 it will be replaced by the new one |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 """ |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 if not self.plugins: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
218 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
219 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
220 "No encryption plugin is registered, " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
221 "an encryption session can't be started" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
222 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
223 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 if namespace is None: |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
226 plugin = self.plugins[0] |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
228 plugin = self.get_plugin(namespace) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
230 bare_jid = entity.userhostJID() |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
231 if bare_jid in self._sessions: |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
232 # we have already an encryption session with this contact |
3028 | 233 former_plugin = self._sessions[bare_jid]["plugin"] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
234 if former_plugin.namespace == namespace: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
235 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
236 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
237 "Session with {bare_jid} is already encrypted with {name}. " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
238 "Nothing to do." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
239 ).format(bare_jid=bare_jid, name=former_plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
240 ) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
241 return |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
242 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
243 if replace: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
244 # there is a conflict, but replacement is requested |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
245 # so we stop previous encryption to use new one |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
246 del self._sessions[bare_jid] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
247 await self._stop_encryption(former_plugin, entity) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
248 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
249 msg = _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
250 "Session with {bare_jid} is already encrypted with {name}. " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
251 "Please stop encryption session before changing algorithm." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
252 ).format(bare_jid=bare_jid, name=plugin.name) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
253 log.warning(msg) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
254 raise exceptions.ConflictError(msg) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
255 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
256 data = {"plugin": plugin} |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
257 if plugin.directed: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
258 if not entity.resource: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
259 entity.resource = self.host.memory.main_resource_get(self.client, entity) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
260 if not entity.resource: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
261 raise exceptions.NotFound( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
262 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
263 "No resource found for {destinee}, can't encrypt with {name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
264 ).format(destinee=entity.full(), name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
265 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
266 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
267 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
268 "No resource specified to encrypt with {name}, using " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
269 "{destinee}." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
270 ).format(destinee=entity.full(), name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
271 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 # indicate that we encrypt only for some devices |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
273 directed_devices = data["directed_devices"] = [entity.resource] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
274 elif entity.resource: |
3028 | 275 raise ValueError(_("{name} encryption must be used with bare jids.")) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
277 await self._start_encryption(plugin, entity) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 self._sessions[entity.userhostJID()] = data |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
279 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
280 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
281 "Encryption session has been set for {entity_jid} with " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
282 "{encryption_name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
283 ).format(entity_jid=entity.full(), encryption_name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
284 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
285 self.host.bridge.message_encryption_started( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
286 entity.full(), self.get_bridge_data(data), self.client.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
287 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
288 msg = D_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
289 "Encryption session started: your messages with {destinee} are " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
290 "now end to end encrypted using {name} algorithm." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
291 ).format(destinee=entity.full(), name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
292 directed_devices = data.get("directed_devices") |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
293 if directed_devices: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
294 msg += "\n" + D_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
295 "Message are encrypted only for {nb_devices} device(s): " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
296 "{devices_list}." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
297 ).format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
298 nb_devices=len(directed_devices), devices_list=", ".join(directed_devices) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
299 ) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
300 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
301 self.client.feedback(bare_jid, msg) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
303 async def stop(self, entity, namespace=None): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
304 """Stop an encryption session with an entity |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
305 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
306 @param entity(jid.JID): entity with who the encryption session must be stopped |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
307 must be bare jid if the algorithm encrypt for all devices |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
308 @param namespace(unicode): namespace of the session to stop |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
309 when specified, used to check that we stop the right encryption session |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
310 """ |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
311 session = self.getSession(entity.userhostJID()) |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
312 if not session: |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
313 raise failure.Failure( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
314 exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
315 _("There is no encryption session with this " "entity.") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
316 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
317 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
318 plugin = session["plugin"] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
319 if namespace is not None and plugin.namespace != namespace: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
320 raise exceptions.InternalError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
321 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
322 "The encryption session is not run with the expected plugin: encrypted " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
323 "with {current_name} and was expecting {expected_name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
324 ).format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
325 current_name=session["plugin"].namespace, expected_name=namespace |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
326 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
327 ) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
328 if entity.resource: |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
329 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
330 directed_devices = session["directed_devices"] |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
331 except KeyError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
332 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
333 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
334 "There is a session for the whole entity (i.e. all devices of the " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
335 "entity), not a directed one. Please use bare jid if you want to " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
336 "stop the whole encryption with this entity." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
337 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
338 ) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
339 |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
340 try: |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
341 directed_devices.remove(entity.resource) |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
342 except ValueError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
343 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
344 _("There is no directed session with this " "entity.") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
345 ) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
346 else: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
347 if not directed_devices: |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
348 # if we have no more directed device sessions, |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
349 # we stop the whole session |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
350 # see comment below for deleting session before stopping encryption |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
351 del self._sessions[entity.userhostJID()] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
352 await self._stop_encryption(plugin, entity) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
353 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
354 # plugin's stop_encryption may call stop again (that's the case with OTR) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
355 # so we need to remove plugin from session before calling self._stop_encryption |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
356 del self._sessions[entity.userhostJID()] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
357 await self._stop_encryption(plugin, entity) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
358 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
359 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
360 _("encryption session stopped with entity {entity}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
361 entity=entity.full() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
362 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
363 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
364 self.host.bridge.message_encryption_stopped( |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
365 entity.full(), |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
366 { |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
367 "name": plugin.name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
368 "namespace": plugin.namespace, |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
369 }, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
370 self.client.profile, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
371 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
372 msg = D_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
373 "Encryption session finished: your messages with {destinee} are " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
374 "NOT end to end encrypted anymore.\nYour server administrators or " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
375 "{destinee} server administrators will be able to read them." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
376 ).format(destinee=entity.full()) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
377 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
378 self.client.feedback(entity, msg) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
379 |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
380 def getSession(self, entity: jid.JID) -> Optional[EncryptionSession]: |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
381 """Get encryption session for this contact |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
382 |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
383 @param entity(jid.JID): get the session for this entity |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
384 must be a bare jid |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
385 @return (dict, None): encryption session data |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
386 None if there is not encryption for this session with this jid |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
387 """ |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
388 if entity.resource: |
3028 | 389 raise ValueError("Full jid given when expecting bare jid") |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
390 return self._sessions.get(entity) |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
391 |
3921
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
392 def get_namespace(self, entity: jid.JID) -> Optional[str]: |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
393 """Helper method to get the current encryption namespace used |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
394 |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
395 @param entity: get the namespace for this entity must be a bare jid |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
396 @return: the algorithm namespace currently used in this session, or None if no |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
397 e2ee is currently used. |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
398 """ |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
399 session = self.getSession(entity) |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
400 if session is None: |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
401 return None |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
402 return session["plugin"].namespace |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
403 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
404 def get_trust_ui(self, entity_jid, namespace=None): |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
405 """Retrieve encryption UI |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
406 |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
407 @param entity_jid(jid.JID): get the UI for this entity |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
408 must be a bare jid |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
409 @param namespace(unicode): namespace of the algorithm to manage |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
410 if None use current algorithm |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
411 @return D(xmlui): XMLUI for trust management |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
412 the xmlui is a form |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
413 None if there is not encryption for this session with this jid |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
414 @raise exceptions.NotFound: no algorithm/plugin found |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
415 @raise NotImplementedError: plugin doesn't handle UI management |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
416 """ |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
417 if namespace is None: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
418 session = self.getSession(entity_jid) |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
419 if not session: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
420 raise exceptions.NotFound( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
421 "No encryption session currently active for {entity_jid}".format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
422 entity_jid=entity_jid.full() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
423 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
424 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
425 plugin = session["plugin"] |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
426 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
427 plugin = self.get_plugin(namespace) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
428 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
429 get_trust_ui = plugin.instance.get_trust_ui |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
430 except AttributeError: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
431 raise NotImplementedError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
432 "Encryption plugin doesn't handle trust management UI" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
433 ) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
434 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
435 return utils.as_deferred(get_trust_ui, self.client, entity_jid) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
436 |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
437 ## Menus ## |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
438 |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
439 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
440 def _import_menus(cls, host): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
441 host.import_menu( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
442 (D_("Encryption"), D_("unencrypted (plain text)")), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
443 partial(cls._on_menu_unencrypted, host=host), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
444 security_limit=0, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
445 help_string=D_("End encrypted session"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
446 type_=C.MENU_SINGLE, |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
447 ) |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
448 for plg in cls.getPlugins(): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
449 host.import_menu( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
450 (D_("Encryption"), plg.name), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
451 partial(cls._on_menu_name, host=host, plg=plg), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
452 security_limit=0, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
453 help_string=D_("Start {name} session").format(name=plg.name), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
454 type_=C.MENU_SINGLE, |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
455 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
456 host.import_menu( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
457 (D_("Encryption"), D_("⛨ {name} trust").format(name=plg.name)), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
458 partial(cls._on_menu_trust, host=host, plg=plg), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
459 security_limit=0, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
460 help_string=D_("Manage {name} trust").format(name=plg.name), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
461 type_=C.MENU_SINGLE, |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
462 ) |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
463 |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
464 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
465 def _on_menu_unencrypted(cls, data, host, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
466 client = host.get_client(profile) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
467 peer_jid = jid.JID(data["jid"]).userhostJID() |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
468 d = defer.ensureDeferred(client.encryption.stop(peer_jid)) |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
469 d.addCallback(lambda __: {}) |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
470 return d |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
471 |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
472 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
473 def _on_menu_name(cls, data, host, plg, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
474 client = host.get_client(profile) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
475 peer_jid = jid.JID(data["jid"]) |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
476 if not plg.directed: |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
477 peer_jid = peer_jid.userhostJID() |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
478 d = defer.ensureDeferred( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
479 client.encryption.start(peer_jid, plg.namespace, replace=True) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
480 ) |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
481 d.addCallback(lambda __: {}) |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
482 return d |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
483 |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
484 @classmethod |
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
485 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
486 def _on_menu_trust(cls, data, host, plg, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
487 client = host.get_client(profile) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
488 peer_jid = jid.JID(data["jid"]).userhostJID() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
489 ui = yield client.encryption.get_trust_ui(peer_jid, plg.namespace) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
490 defer.returnValue({"xmlui": ui.toXml()}) |
2810
c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
491 |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 ## Triggers ## |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
493 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
494 def set_encryption_flag(self, mess_data): |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
495 """Set "encryption" key in mess_data if session with destinee is encrypted""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
496 to_jid = mess_data["to"] |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
497 encryption = self._sessions.get(to_jid.userhostJID()) |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
498 if encryption is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
499 plugin = encryption["plugin"] |
3231
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
500 if mess_data["type"] == "groupchat" and plugin.directed: |
3104
118d91c932a7
plugin XEP-0384: OMEMO for MUC implementation:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
501 raise exceptions.InternalError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
502 f"encryption flag must not be set for groupchat if encryption algorithm " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
503 f"({encryption['plugin'].name}) is directed!" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
504 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
505 mess_data[C.MESS_KEY_ENCRYPTION] = encryption |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
506 self.mark_as_encrypted(mess_data, plugin.namespace) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
507 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
508 ## Misc ## |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
509 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
510 def mark_as_encrypted(self, mess_data, namespace): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
511 """Helper method to mark a message as having been e2e encrypted. |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
512 |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
513 This should be used in the post_treat workflow of message_received trigger of |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
514 the plugin |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
515 @param mess_data(dict): message data as used in post treat workflow |
3231
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
516 @param namespace(str): namespace of the algorithm used for encrypting the message |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
517 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
518 mess_data["extra"][C.MESS_KEY_ENCRYPTED] = True |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
519 from_bare_jid = mess_data["from"].userhostJID() |
3231
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
520 if from_bare_jid != self.client.jid.userhostJID(): |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
521 session = self.getSession(from_bare_jid) |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
522 if session is None: |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
523 # if we are currently unencrypted, we start a session automatically |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
524 # to avoid sending unencrypted messages in an encrypted context |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
525 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
526 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
527 "Starting e2e session with {peer_jid} as we receive encrypted " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
528 "messages" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
529 ).format(peer_jid=from_bare_jid) |
3231
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
530 ) |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
531 defer.ensureDeferred(self.start(from_bare_jid, namespace)) |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
532 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
533 return mess_data |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
534 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
535 def is_encryption_requested( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
536 self, mess_data: MessageData, namespace: Optional[str] = None |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
537 ) -> bool: |
3180
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
538 """Helper method to check if encryption is requested in an outgoind message |
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
539 |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
540 @param mess_data: message data for outgoing message |
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
541 @param namespace: if set, check if encryption is requested for the algorithm |
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
542 specified |
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
543 @return: True if the encryption flag is present |
3180
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
544 """ |
3217
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
545 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) |
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
546 if encryption is None: |
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
547 return False |
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
548 # we get plugin even if namespace is None to be sure that the key exists |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
549 plugin = encryption["plugin"] |
3217
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
550 if namespace is None: |
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
551 return True |
0469c53ed5dd
core (memory/encryption): namespace can now be specified to test a specific algorithm in isEncryptionRequested
Goffi <goffi@goffi.org>
parents:
3180
diff
changeset
|
552 return plugin.namespace == namespace |
3180
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
553 |
3171
d073d82d9044
core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
554 def isEncrypted(self, mess_data): |
3228
cc3fea71c365
core (memory/encryption): set encrypted flag also for outgoing messages and put it in extra:
Goffi <goffi@goffi.org>
parents:
3226
diff
changeset
|
555 """Helper method to check if a message has the e2e encrypted flag |
3171
d073d82d9044
core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
556 |
d073d82d9044
core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
557 @param mess_data(dict): message data |
d073d82d9044
core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
558 @return (bool): True if the encrypted flag is present |
d073d82d9044
core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
559 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
560 return mess_data["extra"].get(C.MESS_KEY_ENCRYPTED, False) |
3171
d073d82d9044
core (memory/encryption): new "isEncrypted" method to check if encrypted flag is set in message data
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
561 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
562 def mark_as_trusted(self, mess_data): |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
563 """Helper methor to mark a message as sent from a trusted entity. |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
564 |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
565 This should be used in the post_treat workflow of message_received trigger of |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
566 the plugin |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
567 @param mess_data(dict): message data as used in post treat workflow |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
568 """ |
2752
1fa615faec8b
core (constants): added a constant for "encrypted"
Goffi <goffi@goffi.org>
parents:
2749
diff
changeset
|
569 mess_data[C.MESS_KEY_TRUSTED] = True |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
570 return mess_data |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
571 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
572 def mark_as_untrusted(self, mess_data): |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
573 """Helper methor to mark a message as sent from an untrusted entity. |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
574 |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
575 This should be used in the post_treat workflow of message_received trigger of |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
576 the plugin |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
577 @param mess_data(dict): message data as used in post treat workflow |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
578 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
579 mess_data["trusted"] = False |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
580 return mess_data |