Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0300.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
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 sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 | |
23 log = getLogger(__name__) | 24 log = getLogger(__name__) |
24 from sat.core import exceptions | 25 from sat.core import exceptions |
25 from twisted.words.xish import domish | 26 from twisted.words.xish import domish |
26 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | 27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
27 from twisted.internet import threads | 28 from twisted.internet import threads |
39 C.PI_TYPE: "XEP", | 40 C.PI_TYPE: "XEP", |
40 C.PI_MODES: C.PLUG_MODE_BOTH, | 41 C.PI_MODES: C.PLUG_MODE_BOTH, |
41 C.PI_PROTOCOLS: ["XEP-0300"], | 42 C.PI_PROTOCOLS: ["XEP-0300"], |
42 C.PI_MAIN: "XEP_0300", | 43 C.PI_MAIN: "XEP_0300", |
43 C.PI_HANDLER: "yes", | 44 C.PI_HANDLER: "yes", |
44 C.PI_DESCRIPTION: _("""Management of cryptographic hashes""") | 45 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""), |
45 } | 46 } |
46 | 47 |
47 NS_HASHES = "urn:xmpp:hashes:2" | 48 NS_HASHES = "urn:xmpp:hashes:2" |
48 NS_HASHES_FUNCTIONS = u"urn:xmpp:hash-function-text-names:{}" | 49 NS_HASHES_FUNCTIONS = u"urn:xmpp:hash-function-text-names:{}" |
49 BUFFER_SIZE = 2**12 | 50 BUFFER_SIZE = 2 ** 12 |
50 ALGO_DEFAULT = 'sha-256' | 51 ALGO_DEFAULT = "sha-256" |
51 | 52 |
52 | 53 |
53 class XEP_0300(object): | 54 class XEP_0300(object): |
54 # TODO: add blake after moving to Python 3 | 55 # TODO: add blake after moving to Python 3 |
55 ALGOS = OrderedDict(( | 56 ALGOS = OrderedDict( |
56 (u'md5', hashlib.md5), | 57 ( |
57 (u'sha-1', hashlib.sha1), | 58 (u"md5", hashlib.md5), |
58 (u'sha-256', hashlib.sha256), | 59 (u"sha-1", hashlib.sha1), |
59 (u'sha-512', hashlib.sha512), | 60 (u"sha-256", hashlib.sha256), |
60 )) | 61 (u"sha-512", hashlib.sha512), |
62 ) | |
63 ) | |
61 | 64 |
62 def __init__(self, host): | 65 def __init__(self, host): |
63 log.info(_("plugin Hashes initialization")) | 66 log.info(_("plugin Hashes initialization")) |
64 host.registerNamespace('hashes', NS_HASHES) | 67 host.registerNamespace("hashes", NS_HASHES) |
65 | 68 |
66 def getHandler(self, client): | 69 def getHandler(self, client): |
67 return XEP_0300_handler() | 70 return XEP_0300_handler() |
68 | 71 |
69 def getHasher(self, algo=ALGO_DEFAULT): | 72 def getHasher(self, algo=ALGO_DEFAULT): |
88 @return (D(unicode, None)): best available algorithm, | 91 @return (D(unicode, None)): best available algorithm, |
89 or None if hashing is not possible | 92 or None if hashing is not possible |
90 """ | 93 """ |
91 client = self.host.getClient(profile) | 94 client = self.host.getClient(profile) |
92 for algo in reversed(XEP_0300.ALGOS): | 95 for algo in reversed(XEP_0300.ALGOS): |
93 has_feature = yield self.host.hasFeature(client, NS_HASHES_FUNCTIONS.format(algo), to_jid) | 96 has_feature = yield self.host.hasFeature( |
97 client, NS_HASHES_FUNCTIONS.format(algo), to_jid | |
98 ) | |
94 if has_feature: | 99 if has_feature: |
95 log.debug(u"Best hashing algorithm found for {jid}: {algo}".format( | 100 log.debug( |
96 jid=to_jid.full(), | 101 u"Best hashing algorithm found for {jid}: {algo}".format( |
97 algo=algo)) | 102 jid=to_jid.full(), algo=algo |
103 ) | |
104 ) | |
98 defer.returnValue(algo) | 105 defer.returnValue(algo) |
99 | 106 |
100 def _calculateHashBlocking(self, file_obj, hasher): | 107 def _calculateHashBlocking(self, file_obj, hasher): |
101 """Calculate hash in a blocking way | 108 """Calculate hash in a blocking way |
102 | 109 |
121 | 128 |
122 @param file_obj(file, None): file-like object to use to calculate the hash | 129 @param file_obj(file, None): file-like object to use to calculate the hash |
123 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS | 130 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS |
124 @return (D(domish.Element)): hash element | 131 @return (D(domish.Element)): hash element |
125 """ | 132 """ |
133 | |
126 def hashCalculated(hash_): | 134 def hashCalculated(hash_): |
127 return self.buildHashElt(hash_, algo) | 135 return self.buildHashElt(hash_, algo) |
136 | |
128 hasher = self.ALGOS[algo] | 137 hasher = self.ALGOS[algo] |
129 hash_d = self.calculateHash(file_obj, hasher) | 138 hash_d = self.calculateHash(file_obj, hasher) |
130 hash_d.addCallback(hashCalculated) | 139 hash_d.addCallback(hashCalculated) |
131 return hash_d | 140 return hash_d |
132 | 141 |
133 def buildHashUsedElt(self, algo=ALGO_DEFAULT): | 142 def buildHashUsedElt(self, algo=ALGO_DEFAULT): |
134 hash_used_elt = domish.Element((NS_HASHES, 'hash-used')) | 143 hash_used_elt = domish.Element((NS_HASHES, "hash-used")) |
135 hash_used_elt['algo'] = algo | 144 hash_used_elt["algo"] = algo |
136 return hash_used_elt | 145 return hash_used_elt |
137 | 146 |
138 def parseHashUsedElt(self, parent): | 147 def parseHashUsedElt(self, parent): |
139 """Find and parse a hash-used element | 148 """Find and parse a hash-used element |
140 | 149 |
142 @return (unicode): hash algorithm used | 151 @return (unicode): hash algorithm used |
143 @raise exceptions.NotFound: the element is not present | 152 @raise exceptions.NotFound: the element is not present |
144 @raise exceptions.DataError: the element is invalid | 153 @raise exceptions.DataError: the element is invalid |
145 """ | 154 """ |
146 try: | 155 try: |
147 hash_used_elt = next(parent.elements(NS_HASHES, 'hash-used')) | 156 hash_used_elt = next(parent.elements(NS_HASHES, "hash-used")) |
148 except StopIteration: | 157 except StopIteration: |
149 raise exceptions.NotFound | 158 raise exceptions.NotFound |
150 algo = hash_used_elt[u'algo'] | 159 algo = hash_used_elt[u"algo"] |
151 if not algo: | 160 if not algo: |
152 raise exceptions.DataError | 161 raise exceptions.DataError |
153 return algo | 162 return algo |
154 | 163 |
155 def buildHashElt(self, hash_, algo=ALGO_DEFAULT): | 164 def buildHashElt(self, hash_, algo=ALGO_DEFAULT): |
159 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS | 168 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS |
160 @return (domish.Element): computed hash | 169 @return (domish.Element): computed hash |
161 """ | 170 """ |
162 assert hash_ | 171 assert hash_ |
163 assert algo | 172 assert algo |
164 hash_elt = domish.Element((NS_HASHES, 'hash')) | 173 hash_elt = domish.Element((NS_HASHES, "hash")) |
165 if hash_ is not None: | 174 if hash_ is not None: |
166 hash_elt.addContent(base64.b64encode(hash_)) | 175 hash_elt.addContent(base64.b64encode(hash_)) |
167 hash_elt['algo'] = algo | 176 hash_elt["algo"] = algo |
168 return hash_elt | 177 return hash_elt |
169 | 178 |
170 def parseHashElt(self, parent): | 179 def parseHashElt(self, parent): |
171 """Find and parse a hash element | 180 """Find and parse a hash element |
172 | 181 |
179 """ | 188 """ |
180 algos = XEP_0300.ALGOS.keys() | 189 algos = XEP_0300.ALGOS.keys() |
181 hash_elt = None | 190 hash_elt = None |
182 best_algo = None | 191 best_algo = None |
183 best_value = None | 192 best_value = None |
184 for hash_elt in parent.elements(NS_HASHES, 'hash'): | 193 for hash_elt in parent.elements(NS_HASHES, "hash"): |
185 algo = hash_elt.getAttribute('algo') | 194 algo = hash_elt.getAttribute("algo") |
186 try: | 195 try: |
187 idx = algos.index(algo) | 196 idx = algos.index(algo) |
188 except ValueError: | 197 except ValueError: |
189 log.warning(u"Proposed {} algorithm is not managed".format(algo)) | 198 log.warning(u"Proposed {} algorithm is not managed".format(algo)) |
190 algo = None | 199 algo = None |
202 | 211 |
203 | 212 |
204 class XEP_0300_handler(XMPPHandler): | 213 class XEP_0300_handler(XMPPHandler): |
205 implements(iwokkel.IDisco) | 214 implements(iwokkel.IDisco) |
206 | 215 |
207 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | 216 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
208 hash_functions_names = [disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo)) for algo in XEP_0300.ALGOS] | 217 hash_functions_names = [ |
218 disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo)) | |
219 for algo in XEP_0300.ALGOS | |
220 ] | |
209 return [disco.DiscoFeature(NS_HASHES)] + hash_functions_names | 221 return [disco.DiscoFeature(NS_HASHES)] + hash_functions_names |
210 | 222 |
211 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | 223 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
212 return [] | 224 return [] |