Mercurial > libervia-backend
comparison sat/memory/encryption.py @ 2810:c161a294fffd
core: added a base menu allowing to set encryption session or show the trust management UI.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 24 Feb 2019 14:11:08 +0100 |
parents | 003b8b4b56a7 |
children | ab2696e34d29 |
comparison
equal
deleted
inserted
replaced
2809:00d905e1b0ef | 2810:c161a294fffd |
---|---|
15 # GNU Affero General Public License for more details. | 15 # GNU Affero General Public License for more details. |
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from functools import partial | |
20 from sat.core.i18n import D_, _ | 21 from sat.core.i18n import D_, _ |
21 from sat.core.constants import Const as C | 22 from sat.core.constants import Const as C |
22 from sat.core import exceptions | 23 from sat.core import exceptions |
23 from collections import namedtuple | 24 from collections import namedtuple |
24 from sat.core.log import getLogger | 25 from sat.core.log import getLogger |
25 from sat.tools.common import data_format | 26 from sat.tools.common import data_format |
27 from twisted.words.protocols.jabber import jid | |
26 from twisted.internet import defer | 28 from twisted.internet import defer |
27 from twisted.python import failure | 29 from twisted.python import failure |
28 import copy | 30 import copy |
29 log = getLogger(__name__) | 31 log = getLogger(__name__) |
30 | 32 |
255 @defer.inlineCallbacks | 257 @defer.inlineCallbacks |
256 def stop(self, entity, namespace=None): | 258 def stop(self, entity, namespace=None): |
257 """Stop an encryption session with an entity | 259 """Stop an encryption session with an entity |
258 | 260 |
259 @param entity(jid.JID): entity with who the encryption session must be stopped | 261 @param entity(jid.JID): entity with who the encryption session must be stopped |
260 must be bare jid is the algorithm encrypt for all devices | 262 must be bare jid if the algorithm encrypt for all devices |
261 @param namespace(unicode): namespace of the session to stop | 263 @param namespace(unicode): namespace of the session to stop |
262 when specified, used to check we stop the right encryption session | 264 when specified, used to check we stop the right encryption session |
263 """ | 265 """ |
264 session = self.getSession(entity.userhostJID()) | 266 session = self.getSession(entity.userhostJID()) |
265 if not session: | 267 if not session: |
355 raise NotImplementedError( | 357 raise NotImplementedError( |
356 u"Encryption plugin doesn't handle trust management UI") | 358 u"Encryption plugin doesn't handle trust management UI") |
357 else: | 359 else: |
358 return defer.maybeDeferred(get_trust_ui, self.client, entity_jid) | 360 return defer.maybeDeferred(get_trust_ui, self.client, entity_jid) |
359 | 361 |
362 ## Menus ## | |
363 | |
364 @classmethod | |
365 def _importMenus(cls, host): | |
366 host.importMenu( | |
367 (D_(u"Encryption"), D_(u"unencrypted (plain text)")), | |
368 partial(cls._onMenuUnencrypted, host=host), | |
369 security_limit=0, | |
370 help_string=D_(u"End encrypted session"), | |
371 type_=C.MENU_SINGLE, | |
372 ) | |
373 for plg in cls.getPlugins(): | |
374 host.importMenu( | |
375 (D_(u"Encryption"), plg.name), | |
376 partial(cls._onMenuName, host=host, plg=plg), | |
377 security_limit=0, | |
378 help_string=D_(u"Start {name} session").format(name=plg.name), | |
379 type_=C.MENU_SINGLE, | |
380 ) | |
381 host.importMenu( | |
382 (D_(u"Encryption"), D_(u"⛨ {name} trust").format(name=plg.name)), | |
383 partial(cls._onMenuTrust, host=host, plg=plg), | |
384 security_limit=0, | |
385 help_string=D_(u"Manage {name} trust").format(name=plg.name), | |
386 type_=C.MENU_SINGLE, | |
387 ) | |
388 | |
389 @classmethod | |
390 def _onMenuUnencrypted(cls, data, host, profile): | |
391 client = host.getClient(profile) | |
392 peer_jid = jid.JID(data[u'jid']).userhostJID() | |
393 d = client.encryption.stop(peer_jid) | |
394 d.addCallback(lambda __: {}) | |
395 return d | |
396 | |
397 @classmethod | |
398 def _onMenuName(cls, data, host, plg, profile): | |
399 client = host.getClient(profile) | |
400 peer_jid = jid.JID(data[u'jid']) | |
401 if not plg.directed: | |
402 peer_jid = peer_jid.userhostJID() | |
403 d = client.encryption.start(peer_jid, plg.namespace, replace=True) | |
404 d.addCallback(lambda __: {}) | |
405 return d | |
406 | |
407 @classmethod | |
408 @defer.inlineCallbacks | |
409 def _onMenuTrust(cls, data, host, plg, profile): | |
410 client = host.getClient(profile) | |
411 peer_jid = jid.JID(data[u'jid']).userhostJID() | |
412 ui = yield client.encryption.getTrustUI(peer_jid, plg.namespace) | |
413 defer.returnValue({u'xmlui': ui.toXml()}) | |
414 | |
360 ## Triggers ## | 415 ## Triggers ## |
361 | 416 |
362 def setEncryptionFlag(self, mess_data): | 417 def setEncryptionFlag(self, mess_data): |
363 """Set "encryption" key in mess_data if session with destinee is encrypted""" | 418 """Set "encryption" key in mess_data if session with destinee is encrypted""" |
364 | 419 |