Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0300.py @ 4329:73d83cb53673
plugin blog import dotclear: repalce deprecated `cgi` by `html`
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 20 Nov 2024 14:35:09 +0100 |
parents | 0d7bb4df2343 |
children | 111dce64dcb5 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for Hash functions (XEP-0300) |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
3916 | 20 from typing import Tuple |
21 import base64 | |
22 from collections import OrderedDict | |
23 import hashlib | |
24 | |
25 from twisted.internet import threads | |
26 from twisted.internet import defer | |
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | |
28 from twisted.words.xish import domish | |
29 from wokkel import disco, iwokkel | |
30 from zope.interface import implementer | |
31 | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
32 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
33 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 log = getLogger(__name__) |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
41 C.PI_NAME: "Cryptographic Hash Functions", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
42 C.PI_IMPORT_NAME: "XEP-0300", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
43 C.PI_TYPE: "XEP", |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
44 C.PI_MODES: C.PLUG_MODE_BOTH, |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
45 C.PI_PROTOCOLS: ["XEP-0300"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
46 C.PI_MAIN: "XEP_0300", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
47 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""), |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 } |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
51 NS_HASHES = "urn:xmpp:hashes:2" |
3028 | 52 NS_HASHES_FUNCTIONS = "urn:xmpp:hash-function-text-names:{}" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
53 BUFFER_SIZE = 2**12 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 ALGO_DEFAULT = "sha-256" |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
55 |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 class XEP_0300(object): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
58 # TODO: add blake after moving to Python 3 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 ALGOS = OrderedDict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 ( |
3028 | 61 ("md5", hashlib.md5), |
62 ("sha-1", hashlib.sha1), | |
63 ("sha-256", hashlib.sha256), | |
64 ("sha-512", hashlib.sha512), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 ) |
3916 | 67 ALGO_DEFAULT = ALGO_DEFAULT |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 def __init__(self, host): |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 log.info(_("plugin Hashes initialization")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
71 host.register_namespace("hashes", NS_HASHES) |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
73 def get_handler(self, client): |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
74 return XEP_0300_handler() |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
75 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
76 def get_hasher(self, algo=ALGO_DEFAULT): |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
77 """Return hasher instance |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
78 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
79 @param algo(unicode): one of the XEP_300.ALGOS keys |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
80 @return (hash object): same object s in hashlib. |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
81 update method need to be called for each chunh |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
82 diget or hexdigest can be used at the end |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
83 """ |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
84 return self.ALGOS[algo]() |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
85 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
86 def get_default_algo(self): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
87 return ALGO_DEFAULT |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
88 |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
89 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
90 def get_best_peer_algo(self, to_jid, profile): |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
91 """Return the best available hashing algorith of other peer |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
92 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
93 @param to_jid(jid.JID): peer jid |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
94 @parm profile: %(doc_profile)s |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
95 @return (D(unicode, None)): best available algorithm, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
96 or None if hashing is not possible |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
97 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
98 client = self.host.get_client(profile) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
99 for algo in reversed(XEP_0300.ALGOS): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 has_feature = yield self.host.hasFeature( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 client, NS_HASHES_FUNCTIONS.format(algo), to_jid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 ) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
103 if has_feature: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 log.debug( |
3028 | 105 "Best hashing algorithm found for {jid}: {algo}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 jid=to_jid.full(), algo=algo |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
107 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
108 ) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
109 defer.returnValue(algo) |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
110 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
111 def _calculate_hash_blocking(self, file_obj, hasher): |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
112 """Calculate hash in a blocking way |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
113 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
114 /!\\ blocking method, please use calculate_hash instead |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
115 @param file_obj(file): a file-like object |
3286
ddf3ded93b78
plugin XEP-0300; fixed use of calculateHash with getHasher
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
116 @param hasher(hash object): the method to call to initialise hash object |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
117 @return (str): the hex digest of the hash |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
118 """ |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
119 while True: |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
120 buf = file_obj.read(BUFFER_SIZE) |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
121 if not buf: |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
122 break |
3286
ddf3ded93b78
plugin XEP-0300; fixed use of calculateHash with getHasher
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
123 hasher.update(buf) |
ddf3ded93b78
plugin XEP-0300; fixed use of calculateHash with getHasher
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
124 return hasher.hexdigest() |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
125 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
126 def calculate_hash(self, file_obj, hasher): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
127 return threads.deferToThread(self._calculate_hash_blocking, file_obj, hasher) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
128 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
129 def calculate_hash_elt(self, file_obj=None, algo=ALGO_DEFAULT): |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 """Compute hash and build hash element |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
132 @param file_obj(file, None): file-like object to use to calculate the hash |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
133 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
134 @return (D(domish.Element)): hash element |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
135 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
136 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
137 def hash_calculated(hash_): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
138 return self.build_hash_elt(hash_, algo) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
140 hasher = self.get_hasher(algo) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
141 hash_d = self.calculate_hash(file_obj, hasher) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
142 hash_d.addCallback(hash_calculated) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
143 return hash_d |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
144 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
145 def build_hash_used_elt(self, algo=ALGO_DEFAULT): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 hash_used_elt = domish.Element((NS_HASHES, "hash-used")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 hash_used_elt["algo"] = algo |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
148 return hash_used_elt |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
149 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
150 def parse_hash_used_elt(self, parent): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
151 """Find and parse a hash-used element |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
152 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
153 @param (domish.Element): parent of <hash/> element |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
154 @return (unicode): hash algorithm used |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
155 @raise exceptions.NotFound: the element is not present |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
156 @raise exceptions.DataError: the element is invalid |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
157 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
158 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 hash_used_elt = next(parent.elements(NS_HASHES, "hash-used")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
160 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
161 raise exceptions.NotFound |
3028 | 162 algo = hash_used_elt["algo"] |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
163 if not algo: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
164 raise exceptions.DataError |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
165 return algo |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
166 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
167 def build_hash_elt(self, hash_, algo=ALGO_DEFAULT): |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
168 """Compute hash and build hash element |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
169 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
170 @param hash_(str): hash to use |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 @return (domish.Element): computed hash |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 """ |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
174 assert hash_ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
175 assert algo |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
176 hash_elt = domish.Element((NS_HASHES, "hash")) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
177 if hash_ is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
178 b64_hash = base64.b64encode(hash_.encode("utf-8")).decode("utf-8") |
3028 | 179 hash_elt.addContent(b64_hash) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
180 hash_elt["algo"] = algo |
1526
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 return hash_elt |
bb451fd1cea3
plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3916
diff
changeset
|
183 def parse_hash_elt(self, parent: domish.Element) -> Tuple[str, bytes]: |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
184 """Find and parse a hash element |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
185 |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
186 if multiple elements are found, the strongest managed one is returned |
3916 | 187 @param parent: parent of <hash/> element |
188 @return: (algo, hash) tuple | |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
189 both values can be None if <hash/> is empty |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
190 @raise exceptions.NotFound: the element is not present |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
191 @raise exceptions.DataError: the element is invalid |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
192 """ |
3028 | 193 algos = list(XEP_0300.ALGOS.keys()) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
194 hash_elt = None |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
195 best_algo = None |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
196 best_value = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 for hash_elt in parent.elements(NS_HASHES, "hash"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 algo = hash_elt.getAttribute("algo") |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
199 try: |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
200 idx = algos.index(algo) |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
201 except ValueError: |
3040 | 202 log.warning(f"Proposed {algo} algorithm is not managed") |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
203 algo = None |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
204 continue |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
205 |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
206 if best_algo is None or algos.index(best_algo) < idx: |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
207 best_algo = algo |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
208 best_value = base64.b64decode(str(hash_elt)).decode("utf-8") |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
209 |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
210 if not hash_elt: |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
211 raise exceptions.NotFound |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
212 if not best_algo or not best_value: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
213 raise exceptions.DataError |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
214 return best_algo, best_value |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
215 |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
216 |
3028 | 217 @implementer(iwokkel.IDisco) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
218 class XEP_0300_handler(XMPPHandler): |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
219 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
220 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
221 hash_functions_names = [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
222 disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
223 for algo in XEP_0300.ALGOS |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 ] |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
225 return [disco.DiscoFeature(NS_HASHES)] + hash_functions_names |
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
226 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
227 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1526
diff
changeset
|
228 return [] |