annotate sat/memory/encryption.py @ 2651:ebcff5423465

core (memory/encryption): start improvments, stop and getSession: - "start" won't do anything if session is already encrypted with requested algorithm, and will raise a ConflictError if it's already encrypted but with an other algorithm - implemented "stop", with an optional namespace to check we are stopping the expected algorithm - "getSession" retrieve the current encryption session of a jid, if any
author Goffi <goffi@goffi.org>
date Sat, 11 Aug 2018 18:24:55 +0200
parents 712cb4ff3e13
children 4e130cc9bfc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
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
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from collections import namedtuple
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.log import getLogger
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 EncryptionPlugin = namedtuple("EncryptionPlugin", ("instance",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 "name",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 "namespace",
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 "priority"))
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 class EncryptionHandler(object):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 """Class to handle encryption sessions for a client"""
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 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
37
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def __init__(self, host):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 self._sessions = {} # bare_jid ==> encryption_data
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 @classmethod
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 def registerPlugin(cls, plg_instance, name, namespace, priority=0):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 """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
44
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 @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
46 it must have the following methods:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 - startEncryption(jid.JID): start an encryption session with a bare jid
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 - stopEncryption(jid.JID): stop an encryption session with a bare jid
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param name(unicode): human readable name of the encryption alrgorithm
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @param namespace(unicode): namespace of the encryption algorithm
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 @param priority(int): priority of this plugin to encrypt an message when not
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 selected manually
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 existing_ns = [p.namespace for p in cls.plugins]
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 if namespace in existing_ns:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 raise exceptions.ConflictError("A plugin with this namespace already exists!")
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 plg = EncryptionPlugin(
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 instance=plg_instance,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 name=name,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 namespace=namespace,
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 priority=priority)
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 cls.plugins.append(plg)
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 cls.plugins.sort(key=lambda p: p.priority)
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
64 log.info(_(u"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
65
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def start(self, entity, namespace=None):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 """Start an encrypted session with an entity
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 @param entity(jid.JID): entity to start an encrypted session with
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 must be bare jid is the algorithm encrypt for all devices
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 @param namespace(unicode, None): namespace of the encryption algorithm to use
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 None to select automatically an algorithm
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 if not self.plugins:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 raise exceptions.NotFound(_(u"No encryption plugin is registered, "
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 u"an encryption session can't be started"))
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if namespace is None:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 plg = self.plugins[0]
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 else:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 try:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 plg = next(p for p in self.plugins if p.namespace == namespace)
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 except StopIteration:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 raise exceptions.NotFound(_(
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 u"Can't find requested encryption plugin: {namespace}").format(
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 namespace=namespace))
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
88 bare_jid = entity.userhostJID()
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
89 if bare_jid in self._sessions:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
90 plg = self._sessions[bare_jid]['plugin']
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
91 if plg.namespace == namespace:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
92 log.info(_(u"Session with {bare_jid} is already encrypted with {name}."
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
93 u"Nothing to do.")
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
94 .format(bare_jid=bare_jid, name=plg.name))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
95 return
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
96
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
97 msg = (_(u"Session with {bare_jid} is already encrypted with {name}. "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
98 u"Please stop encryption session before changing algorithm.")
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
99 .format(bare_jid=bare_jid, name=plg.name))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
100 log.warning(msg)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
101 raise exceptions.ConflictError(msg)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
102
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 data = {"plugin": plg}
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if entity.resource:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 # indicate that we encrypt only for some devices
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 data['directed_devices'] = [entity.resource]
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self._sessions[entity.userhostJID()] = data
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
109 log.info(_(u"Encryption session has been set for {bare_jid} with "
2646
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 u"{encryption_name}").format(
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 bare_jid=bare_jid.userhost(), encryption_name=plg.name))
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
2651
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
113 def stop(self, entity, namespace=None):
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
114 """Stop an encrypted session with an entity
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
115
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
116 @param entity(jid.JID): entity with who the encrypted session must be stopped
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
117 must be bare jid is the algorithm encrypt for all devices
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
118 @param namespace(unicode): namespace of the session to stop
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
119 when specified, used to check we stop the right encryption session
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
120 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
121 session = self.getSession(entity.userhostJID())
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
122 if not session:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
123 raise exceptions.NotFound(_(u"There is no encrypted session with this "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
124 u"entity."))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
125 if namespace is not None and session[u'plugin'].namespace != namespace:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
126 raise exceptions.InternalError(_(
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
127 u"The encrypted session is not run with the expected plugin: encrypted "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
128 u"with {current_name} and was expecting {expected_name}").format(
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
129 current_name=session[u'plugin'].namespace,
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
130 expected_name=namespace))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
131 if entity.resource:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
132 try:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
133 directed_devices = session[u'directed_devices']
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
134 except KeyError:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
135 raise exceptions.NotFound(_(
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
136 u"There is a session for the whole entity (i.e. all devices of the "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
137 u"entity), not a directed one. Please use bare jid if you want to "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
138 u"stop the whole encryption with this entity."))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
139
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
140 try:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
141 directed_devices.remove(entity.resource)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
142 except ValueError:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
143 raise exceptions.NotFound(_(u"There is no directed session with this "
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
144 u"entity."))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
145 else:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
146 del self._sessions[entity]
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
147
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
148 log.info(_(u"Encrypted session stopped with entity {entity}").format(
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
149 entity=entity.full()))
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
150
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
151 def getSession(self, entity):
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
152 """Get encryption session for this contact
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
153
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
154 @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
155 must be a bare jid
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
156 @return (dict, None): encrypted session data
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
157 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
158 """
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
159 if entity.resource:
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
160 raise exceptions.InternalError(u"Full jid given when expecting bare jid")
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
diff changeset
161 return self._sessions.get(entity)
ebcff5423465 core (memory/encryption): start improvments, stop and getSession:
Goffi <goffi@goffi.org>
parents: 2646
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 ## Triggers ##
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 def setEncryptionFlag(self, mess_data):
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """Set "encryption" key in mess_data if session with destinee is encrypted"""
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
167
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if mess_data["type"] == "groupchat":
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 # FIXME: to change when group chat encryption will be handled
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 return
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 to_jid = mess_data['to']
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 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
174 if encryption is not None:
712cb4ff3e13 core: new EncryptionHandler class which manage encrypted session as a core feature:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 mess_data[C.MESS_KEY_ENCRYPTION] = encryption