Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0300.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | fee60f17ebac |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT plugin for Hash functions (XEP-0300) | 4 # SAT plugin for Hash functions (XEP-0300) |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
25 from sat.core import exceptions | 25 from sat.core import exceptions |
26 from twisted.words.xish import domish | 26 from twisted.words.xish import domish |
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | 27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
28 from twisted.internet import threads | 28 from twisted.internet import threads |
29 from twisted.internet import defer | 29 from twisted.internet import defer |
30 from zope.interface import implements | 30 from zope.interface import implementer |
31 from wokkel import disco, iwokkel | 31 from wokkel import disco, iwokkel |
32 from collections import OrderedDict | 32 from collections import OrderedDict |
33 import hashlib | 33 import hashlib |
34 import base64 | 34 import base64 |
35 | 35 |
44 C.PI_HANDLER: "yes", | 44 C.PI_HANDLER: "yes", |
45 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""), | 45 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""), |
46 } | 46 } |
47 | 47 |
48 NS_HASHES = "urn:xmpp:hashes:2" | 48 NS_HASHES = "urn:xmpp:hashes:2" |
49 NS_HASHES_FUNCTIONS = u"urn:xmpp:hash-function-text-names:{}" | 49 NS_HASHES_FUNCTIONS = "urn:xmpp:hash-function-text-names:{}" |
50 BUFFER_SIZE = 2 ** 12 | 50 BUFFER_SIZE = 2 ** 12 |
51 ALGO_DEFAULT = "sha-256" | 51 ALGO_DEFAULT = "sha-256" |
52 | 52 |
53 | 53 |
54 class XEP_0300(object): | 54 class XEP_0300(object): |
55 # TODO: add blake after moving to Python 3 | 55 # TODO: add blake after moving to Python 3 |
56 ALGOS = OrderedDict( | 56 ALGOS = OrderedDict( |
57 ( | 57 ( |
58 (u"md5", hashlib.md5), | 58 ("md5", hashlib.md5), |
59 (u"sha-1", hashlib.sha1), | 59 ("sha-1", hashlib.sha1), |
60 (u"sha-256", hashlib.sha256), | 60 ("sha-256", hashlib.sha256), |
61 (u"sha-512", hashlib.sha512), | 61 ("sha-512", hashlib.sha512), |
62 ) | 62 ) |
63 ) | 63 ) |
64 | 64 |
65 def __init__(self, host): | 65 def __init__(self, host): |
66 log.info(_("plugin Hashes initialization")) | 66 log.info(_("plugin Hashes initialization")) |
96 has_feature = yield self.host.hasFeature( | 96 has_feature = yield self.host.hasFeature( |
97 client, NS_HASHES_FUNCTIONS.format(algo), to_jid | 97 client, NS_HASHES_FUNCTIONS.format(algo), to_jid |
98 ) | 98 ) |
99 if has_feature: | 99 if has_feature: |
100 log.debug( | 100 log.debug( |
101 u"Best hashing algorithm found for {jid}: {algo}".format( | 101 "Best hashing algorithm found for {jid}: {algo}".format( |
102 jid=to_jid.full(), algo=algo | 102 jid=to_jid.full(), algo=algo |
103 ) | 103 ) |
104 ) | 104 ) |
105 defer.returnValue(algo) | 105 defer.returnValue(algo) |
106 | 106 |
154 """ | 154 """ |
155 try: | 155 try: |
156 hash_used_elt = next(parent.elements(NS_HASHES, "hash-used")) | 156 hash_used_elt = next(parent.elements(NS_HASHES, "hash-used")) |
157 except StopIteration: | 157 except StopIteration: |
158 raise exceptions.NotFound | 158 raise exceptions.NotFound |
159 algo = hash_used_elt[u"algo"] | 159 algo = hash_used_elt["algo"] |
160 if not algo: | 160 if not algo: |
161 raise exceptions.DataError | 161 raise exceptions.DataError |
162 return algo | 162 return algo |
163 | 163 |
164 def buildHashElt(self, hash_, algo=ALGO_DEFAULT): | 164 def buildHashElt(self, hash_, algo=ALGO_DEFAULT): |
170 """ | 170 """ |
171 assert hash_ | 171 assert hash_ |
172 assert algo | 172 assert algo |
173 hash_elt = domish.Element((NS_HASHES, "hash")) | 173 hash_elt = domish.Element((NS_HASHES, "hash")) |
174 if hash_ is not None: | 174 if hash_ is not None: |
175 hash_elt.addContent(base64.b64encode(hash_)) | 175 b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8') |
176 hash_elt.addContent(b64_hash) | |
176 hash_elt["algo"] = algo | 177 hash_elt["algo"] = algo |
177 return hash_elt | 178 return hash_elt |
178 | 179 |
179 def parseHashElt(self, parent): | 180 def parseHashElt(self, parent): |
180 """Find and parse a hash element | 181 """Find and parse a hash element |
181 | 182 |
182 if multiple elements are found, the strongest managed one is returned | 183 if multiple elements are found, the strongest managed one is returned |
183 @param (domish.Element): parent of <hash/> element | 184 @param (domish.Element): parent of <hash/> element |
184 @return (tuple[unicode, str]): (algo, hash) tuple | 185 @return (tuple[str, bytes]): (algo, hash) tuple |
185 both values can be None if <hash/> is empty | 186 both values can be None if <hash/> is empty |
186 @raise exceptions.NotFound: the element is not present | 187 @raise exceptions.NotFound: the element is not present |
187 @raise exceptions.DataError: the element is invalid | 188 @raise exceptions.DataError: the element is invalid |
188 """ | 189 """ |
189 algos = XEP_0300.ALGOS.keys() | 190 algos = list(XEP_0300.ALGOS.keys()) |
190 hash_elt = None | 191 hash_elt = None |
191 best_algo = None | 192 best_algo = None |
192 best_value = None | 193 best_value = None |
193 for hash_elt in parent.elements(NS_HASHES, "hash"): | 194 for hash_elt in parent.elements(NS_HASHES, "hash"): |
194 algo = hash_elt.getAttribute("algo") | 195 algo = hash_elt.getAttribute("algo") |
195 try: | 196 try: |
196 idx = algos.index(algo) | 197 idx = algos.index(algo) |
197 except ValueError: | 198 except ValueError: |
198 log.warning(u"Proposed {} algorithm is not managed".format(algo)) | 199 log.warning("Proposed {} algorithm is not managed".format(algo)) |
199 algo = None | 200 algo = None |
200 continue | 201 continue |
201 | 202 |
202 if best_algo is None or algos.index(best_algo) < idx: | 203 if best_algo is None or algos.index(best_algo) < idx: |
203 best_algo = algo | 204 best_algo = algo |
204 best_value = base64.b64decode(unicode(hash_elt)) | 205 best_value = base64.b64decode(str(hash_elt)) |
205 | 206 |
206 if not hash_elt: | 207 if not hash_elt: |
207 raise exceptions.NotFound | 208 raise exceptions.NotFound |
208 if not best_algo or not best_value: | 209 if not best_algo or not best_value: |
209 raise exceptions.DataError | 210 raise exceptions.DataError |
210 return best_algo, best_value | 211 return best_algo, best_value |
211 | 212 |
212 | 213 |
214 @implementer(iwokkel.IDisco) | |
213 class XEP_0300_handler(XMPPHandler): | 215 class XEP_0300_handler(XMPPHandler): |
214 implements(iwokkel.IDisco) | |
215 | 216 |
216 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | 217 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
217 hash_functions_names = [ | 218 hash_functions_names = [ |
218 disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo)) | 219 disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo)) |
219 for algo in XEP_0300.ALGOS | 220 for algo in XEP_0300.ALGOS |