Mercurial > libervia-backend
annotate libervia/backend/memory/encryption.py @ 4342:17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 13 Jan 2025 01:23:10 +0100 |
parents | e9971a4b0627 |
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 |
4342
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
80 def register_plugin( |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
81 cls, |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
82 plg_instance, |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
83 name: str, |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
84 namespace: str, |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
85 priority: int=0, |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
86 directed: bool=False |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
87 ) -> None: |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 """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
|
89 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 @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
|
91 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
|
92 - 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
|
93 entity(jid.JID): entity to manage |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
94 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
|
95 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
|
96 - 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
|
97 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
|
98 - 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
|
99 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
|
100 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
|
101 |
4342
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
102 @param name: human readable name of the encryption algorithm |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
103 @param namespace: namespace of the encryption algorithm |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
104 @param priority: priority of this plugin to encrypt an message when not |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 selected manually |
4342
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
106 @param directed: True if this plugin is directed (if it works with one |
17fa953c8cd7
core (types): improve `SatXMPPEntity` core type and type hints.
Goffi <goffi@goffi.org>
parents:
4341
diff
changeset
|
107 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
|
108 """ |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
109 existing_ns = set() |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
110 existing_names = set() |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
111 for p in cls.plugins: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
112 existing_ns.add(p.namespace.lower()) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
113 existing_names.add(p.name.lower()) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
114 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
|
115 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
|
116 if name.lower() in existing_names: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
117 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
|
118 plugin = EncryptionPlugin( |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 instance=plg_instance, |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 name=name, |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 namespace=namespace, |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
122 priority=priority, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
123 directed=directed, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
124 ) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
125 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
|
126 cls.plugins.sort(key=lambda p: p.priority) |
3028 | 127 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
|
128 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
129 @classmethod |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
130 def getPlugins(cls): |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
131 return cls.plugins |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
132 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
133 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
134 def get_plugin(cls, namespace): |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
135 try: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
136 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
|
137 except StopIteration: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
138 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
139 _("Can't find requested encryption plugin: {namespace}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
140 namespace=namespace |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
141 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
142 ) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
143 |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
144 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
145 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
|
146 """Get available plugin namespaces""" |
4b8271399f67
core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents:
2743
diff
changeset
|
147 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
|
148 |
4b8271399f67
core (memory/encryption): added getNamespaces method to retrieve all plugins namespaces
Goffi <goffi@goffi.org>
parents:
2743
diff
changeset
|
149 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
150 def get_ns_from_name(cls, name): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
151 """Retrieve plugin namespace from its name |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
152 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
153 @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
|
154 @return (unicode): namespace of the plugin |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
155 @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
|
156 """ |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
157 for p in cls.plugins: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
158 if p.name.lower() == name.lower(): |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
159 return p.namespace |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
160 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
161 _('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
|
162 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
164 def get_bridge_data(self, session): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
165 """Retrieve session data serialized for bridge. |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
166 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
167 @param session(dict): encryption session |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
168 @return (unicode): serialized data for bridge |
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 if session is None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
171 return "" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
172 plugin = session["plugin"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
173 bridge_data = {"name": plugin.name, "namespace": plugin.namespace} |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
174 if "directed_devices" in session: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
175 bridge_data["directed_devices"] = session["directed_devices"] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
176 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
177 return data_format.serialise(bridge_data) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
178 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
179 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
|
180 """Start encryption with a plugin |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
181 |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
182 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
|
183 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
|
184 """ |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
185 if not plugin.directed: |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
186 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
|
187 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
188 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
|
189 except AttributeError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
190 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
|
191 else: |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
192 # 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
|
193 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
|
194 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
195 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
|
196 """Stop encryption with a plugin |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
197 |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
198 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
|
199 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
|
200 """ |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
201 try: |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
202 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
|
203 except KeyError: |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
204 pass |
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
205 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
206 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
|
207 except AttributeError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
208 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
|
209 else: |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
210 # 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
|
211 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
|
212 |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
213 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
|
214 """Start an encryption session with an entity |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
215 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
216 @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
|
217 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
|
218 @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
|
219 to use. |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 None to select automatically an algorithm |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
221 @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
|
222 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
|
223 """ |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 if not self.plugins: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
225 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
226 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
227 "No encryption plugin is registered, " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
228 "an encryption session can't be started" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
229 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
230 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 if namespace is None: |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
233 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
|
234 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
235 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
|
236 |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
237 bare_jid = entity.userhostJID() |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
238 if bare_jid in self._sessions: |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
239 # we have already an encryption session with this contact |
3028 | 240 former_plugin = self._sessions[bare_jid]["plugin"] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
241 if former_plugin.namespace == namespace: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
242 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
243 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
244 "Session with {bare_jid} is already encrypted with {name}. " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
245 "Nothing to do." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
246 ).format(bare_jid=bare_jid, name=former_plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
247 ) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
248 return |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
249 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
250 if replace: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
251 # there is a conflict, but replacement is requested |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
252 # 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
|
253 del self._sessions[bare_jid] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
254 await self._stop_encryption(former_plugin, entity) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
255 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
256 msg = _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
257 "Session with {bare_jid} is already encrypted with {name}. " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
258 "Please stop encryption session before changing algorithm." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
259 ).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
|
260 log.warning(msg) |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
261 raise exceptions.ConflictError(msg) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
262 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
263 data = {"plugin": plugin} |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
264 if plugin.directed: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
265 if not entity.resource: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
266 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
|
267 if not entity.resource: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
268 raise exceptions.NotFound( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
269 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
270 "No resource found for {destinee}, can't encrypt with {name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
271 ).format(destinee=entity.full(), name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
272 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
273 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
274 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
275 "No resource specified to encrypt with {name}, using " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
276 "{destinee}." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
277 ).format(destinee=entity.full(), name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
278 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 # indicate that we encrypt only for some devices |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
280 directed_devices = data["directed_devices"] = [entity.resource] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
281 elif entity.resource: |
3028 | 282 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
|
283 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
284 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
|
285 self._sessions[entity.userhostJID()] = data |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
286 log.info( |
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 "Encryption session has been set for {entity_jid} with " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
289 "{encryption_name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
290 ).format(entity_jid=entity.full(), encryption_name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
291 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
292 self.host.bridge.message_encryption_started( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
293 entity.full(), self.get_bridge_data(data), self.client.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
294 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
295 msg = D_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
296 "Encryption session started: your messages with {destinee} are " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
297 "now end to end encrypted using {name} algorithm." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
298 ).format(destinee=entity.full(), name=plugin.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
299 directed_devices = data.get("directed_devices") |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
300 if directed_devices: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
301 msg += "\n" + D_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
302 "Message are encrypted only for {nb_devices} device(s): " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
303 "{devices_list}." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
304 ).format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
305 nb_devices=len(directed_devices), devices_list=", ".join(directed_devices) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
306 ) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
307 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
308 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
|
309 |
3226
2f406b762788
core (memory/encryption): encryption session are now restored on client connection
Goffi <goffi@goffi.org>
parents:
3217
diff
changeset
|
310 async def stop(self, entity, namespace=None): |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
311 """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
|
312 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
313 @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
|
314 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
|
315 @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
|
316 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
|
317 """ |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
318 session = self.getSession(entity.userhostJID()) |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
319 if not session: |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
320 raise failure.Failure( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
321 exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
322 _("There is no encryption session with this " "entity.") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
323 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
324 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
325 plugin = session["plugin"] |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
326 if namespace is not None and plugin.namespace != namespace: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
327 raise exceptions.InternalError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
328 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
329 "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
|
330 "with {current_name} and was expecting {expected_name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
331 ).format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
332 current_name=session["plugin"].namespace, expected_name=namespace |
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 ) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
335 if entity.resource: |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
336 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
337 directed_devices = session["directed_devices"] |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
338 except KeyError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
339 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
340 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
341 "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
|
342 "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
|
343 "stop the whole encryption with this entity." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
344 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
345 ) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
346 |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
347 try: |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
348 directed_devices.remove(entity.resource) |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
349 except ValueError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
350 raise exceptions.NotFound( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
351 _("There is no directed session with this " "entity.") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
352 ) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
353 else: |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
354 if not directed_devices: |
2743
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
355 # 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
|
356 # we stop the whole session |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
357 # 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
|
358 del self._sessions[entity.userhostJID()] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
359 await self._stop_encryption(plugin, entity) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
360 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
361 # 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
|
362 # 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
|
363 del self._sessions[entity.userhostJID()] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
364 await self._stop_encryption(plugin, entity) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
365 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
366 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
367 _("encryption session stopped with entity {entity}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
368 entity=entity.full() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
369 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
370 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
371 self.host.bridge.message_encryption_stopped( |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
372 entity.full(), |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
373 { |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
374 "name": plugin.name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
375 "namespace": plugin.namespace, |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
376 }, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
377 self.client.profile, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
378 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
379 msg = D_( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
380 "Encryption session finished: your messages with {destinee} are " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
381 "NOT end to end encrypted anymore.\nYour server administrators or " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
382 "{destinee} server administrators will be able to read them." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
383 ).format(destinee=entity.full()) |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
384 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
385 self.client.feedback(entity, msg) |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
386 |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
387 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
|
388 """Get encryption session for this contact |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
389 |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
390 @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
|
391 must be a bare jid |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
392 @return (dict, None): encryption session data |
2651
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
393 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
|
394 """ |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
395 if entity.resource: |
3028 | 396 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
|
397 return self._sessions.get(entity) |
ebcff5423465
core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents:
2646
diff
changeset
|
398 |
3921
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
399 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
|
400 """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
|
401 |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
402 @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
|
403 @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
|
404 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
|
405 """ |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
406 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
|
407 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
|
408 return None |
cc2705225778
core (memory/encryption): helper method to get currently utilised e2ee algorithm's namespace:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
409 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
|
410 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
411 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
|
412 """Retrieve encryption UI |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
413 |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
414 @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
|
415 must be a bare jid |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
416 @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
|
417 if None use current algorithm |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
418 @return D(xmlui): XMLUI for trust management |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
419 the xmlui is a form |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
420 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
|
421 @raise exceptions.NotFound: no algorithm/plugin found |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
422 @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
|
423 """ |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
424 if namespace is None: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
425 session = self.getSession(entity_jid) |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
426 if not session: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
427 raise exceptions.NotFound( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
428 "No encryption session currently active for {entity_jid}".format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
429 entity_jid=entity_jid.full() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
430 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
431 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
432 plugin = session["plugin"] |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
433 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
434 plugin = self.get_plugin(namespace) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
435 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
436 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
|
437 except AttributeError: |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
438 raise NotImplementedError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
439 "Encryption plugin doesn't handle trust management UI" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
440 ) |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
441 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
442 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
|
443 |
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
|
444 ## 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
|
445 |
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
|
446 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
447 def _import_menus(cls, host): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
448 host.import_menu( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
449 (D_("Encryption"), D_("unencrypted (plain text)")), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
450 partial(cls._on_menu_unencrypted, host=host), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
451 security_limit=0, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
452 help_string=D_("End encrypted session"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
453 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
|
454 ) |
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 for plg in cls.getPlugins(): |
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"), plg.name), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
458 partial(cls._on_menu_name, 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_("Start {name} session").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 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
463 host.import_menu( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
464 (D_("Encryption"), D_("⛨ {name} trust").format(name=plg.name)), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
465 partial(cls._on_menu_trust, host=host, plg=plg), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
466 security_limit=0, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
467 help_string=D_("Manage {name} trust").format(name=plg.name), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
468 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
|
469 ) |
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 |
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 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
472 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
|
473 client = host.get_client(profile) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
474 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
|
475 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
|
476 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
|
477 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
|
478 |
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
|
479 @classmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
480 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
|
481 client = host.get_client(profile) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
482 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
|
483 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
|
484 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
|
485 d = defer.ensureDeferred( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
486 client.encryption.start(peer_jid, plg.namespace, replace=True) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
487 ) |
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
|
488 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
|
489 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
|
490 |
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 @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
|
492 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
493 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
|
494 client = host.get_client(profile) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
495 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
|
496 ui = yield client.encryption.get_trust_ui(peer_jid, plg.namespace) |
4341
e9971a4b0627
remove uses of twisted.internet.defer.returnValue
Povilas Kanapickas <povilas@radix.lt>
parents:
4270
diff
changeset
|
497 return {"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
|
498 |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
499 ## Triggers ## |
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
500 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
501 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
|
502 """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
|
503 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
|
504 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
|
505 if encryption is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
506 plugin = encryption["plugin"] |
3231
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
507 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
|
508 raise exceptions.InternalError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
509 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
|
510 f"({encryption['plugin'].name}) is directed!" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
511 ) |
2646
712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
512 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
|
513 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
|
514 |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
515 ## Misc ## |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
516 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
517 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
|
518 """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
|
519 |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
520 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
|
521 the plugin |
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
522 @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
|
523 @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
|
524 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
525 mess_data["extra"][C.MESS_KEY_ENCRYPTED] = True |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
526 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
|
527 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
|
528 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
|
529 if session is None: |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
530 # 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
|
531 # to avoid sending unencrypted messages in an encrypted context |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
532 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
533 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
534 "Starting e2e session with {peer_jid} as we receive encrypted " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
535 "messages" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
536 ).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
|
537 ) |
e756e0eb1be4
core (memory/encryption): automatic start encryption if peer send encrypted message:
Goffi <goffi@goffi.org>
parents:
3228
diff
changeset
|
538 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
|
539 |
2658
4e130cc9bfc0
core (memore/encryption): new methods and checks:
Goffi <goffi@goffi.org>
parents:
2651
diff
changeset
|
540 return mess_data |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
541 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
542 def is_encryption_requested( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
543 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
|
544 ) -> bool: |
3180
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
545 """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
|
546 |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
547 @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
|
548 @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
|
549 specified |
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3479
diff
changeset
|
550 @return: True if the encryption flag is present |
3180
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
551 """ |
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
|
552 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
|
553 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
|
554 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
|
555 # 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
|
556 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
|
557 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
|
558 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
|
559 return plugin.namespace == namespace |
3180
826bca181226
core (memory/encryption): "isEncryptionRequested" helper method:
Goffi <goffi@goffi.org>
parents:
3171
diff
changeset
|
560 |
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 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
|
562 """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
|
563 |
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
|
564 @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
|
565 @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
|
566 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
567 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
|
568 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
569 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
|
570 """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
|
571 |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
572 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
|
573 the plugin |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
574 @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
|
575 """ |
2752
1fa615faec8b
core (constants): added a constant for "encrypted"
Goffi <goffi@goffi.org>
parents:
2749
diff
changeset
|
576 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
|
577 return mess_data |
da59ff099b32
core (memory/encryption), plugin OTR: finished OTR integration in encryption:
Goffi <goffi@goffi.org>
parents:
2733
diff
changeset
|
578 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3921
diff
changeset
|
579 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
|
580 """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
|
581 |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
582 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
|
583 the plugin |
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
584 @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
|
585 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
586 mess_data["trusted"] = False |
2733
e347e32aa07f
core (memory/encryption): new encryptionNamespaceGet and encryptionTrustUIGet methods:
Goffi <goffi@goffi.org>
parents:
2658
diff
changeset
|
587 return mess_data |