annotate libervia/backend/plugins/plugin_xep_0300.py @ 4340:ea72364131d5 default tip @

doc (components): Update Email Gateway documentation: A section has been added to explain how attachments are handled. fix 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:53:18 +0100
parents 111dce64dcb5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
3 # Libervia plugin for Hash functions (XEP-0300)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # 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
7 # 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
8 # 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
9 # (at your option) any later version.
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # 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
12 # 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
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # 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
17 # 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
18
3916
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
19 import base64
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
20 from collections import OrderedDict
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
21 import hashlib
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
22 from typing import BinaryIO, Callable, Self, TYPE_CHECKING
3916
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
23
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
24 from _hashlib import HASH
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
25 from pydantic import BaseModel, Field
3916
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
26 from twisted.internet import threads
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
27 from twisted.internet import defer
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
28 from twisted.words.protocols.jabber import jid
3916
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
29 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
30 from twisted.words.xish import domish
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
31 from wokkel import disco, iwokkel
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
32 from zope.interface import implementer
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
33
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
34 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
35 from libervia.backend.core.constants import Const as C
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
36 from libervia.backend.core.core_types import SatXMPPEntity
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
37 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
38 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
40 if TYPE_CHECKING:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
41 from libervia.backend.core.main import LiberviaBackend
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
42
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 log = getLogger(__name__)
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 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
48 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
49 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
50 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
51 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
52 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
53 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""),
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 }
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
57 NS_HASHES = "urn:xmpp:hashes:2"
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 NS_HASHES_FUNCTIONS = "urn:xmpp:hash-function-text-names:{}"
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
59 BUFFER_SIZE = 2**12
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 ALGO_DEFAULT = "sha-256"
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
61
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
63 class Hash(BaseModel):
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
64 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
65 Model for hash data.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
66 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
67
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
68 algo: str = Field(description="The algorithm used for hashing.")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
69 hash_: str = Field(min_length=16, description="The base64-encoded hash value.")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
70
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
71 @classmethod
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
72 def from_element(cls, hash_elt: domish.Element) -> Self:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
73 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
74 Create a HashModel instance from a <hash> element.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
75
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
76 @param hash_elt: The <hash> element.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
77 @return: HashModel instance.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
78 @raise exceptions.NotFound: If the <hash> element is not found.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
79 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
80 if hash_elt.uri != NS_HASHES or hash_elt.name != "hash":
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
81 raise exceptions.NotFound("<hash> element not found")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
82 algo = hash_elt.getAttribute("algo")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
83 hash_value = str(hash_elt)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
84 return cls(algo=algo, hash_=hash_value)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
85
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
86 @classmethod
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
87 def from_parent(cls, parent_elt: domish.Element) -> list[Self]:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
88 """Find and return child <hash> element in given parent.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
89
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
90 @param parent_elt: Element which may content child <hash> elements.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
91 @return: list of Hash corresponding to found elements
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
92 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
93 return [
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
94 cls.from_element(hash_elt)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
95 for hash_elt in parent_elt.elements(NS_HASHES, "hash")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
96 ]
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
97
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
98 def to_element(self) -> domish.Element:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
99 """Build the <hash> element from this instance's data.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
100
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
101 @return: <hash> element.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
102 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
103 hash_elt = domish.Element((NS_HASHES, "hash"))
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
104 hash_elt["algo"] = self.algo
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
105 hash_elt.addContent(self.hash_)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
106 return hash_elt
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
107
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
108
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
109 class HashUsed(BaseModel):
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
110 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
111 Model for hash-used data.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
112 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
113
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
114 algo: str = Field(description="The algorithm used for hashing.")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
115
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
116 @classmethod
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
117 def from_element(cls, hash_used_elt: domish.Element) -> Self:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
118 """Create a HashUsedModel instance from a <hash-used> element.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
119
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
120 @param hash_used_elt: The <hash-used> element.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
121 @return: HashUsedModel instance.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
122 @raise exceptions.NotFound: If the <hash-used> element is not found.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
123 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
124 if hash_used_elt.uri != NS_HASHES or hash_used_elt.name != "hash-used":
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
125 child_hash_used_elt = next(
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
126 hash_used_elt.elements(NS_HASHES, "hash-used"), None
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
127 )
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
128 if child_hash_used_elt is None:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
129 raise exceptions.NotFound("<hash-used> element not found")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
130 else:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
131 hash_used_elt = child_hash_used_elt
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
132 algo = hash_used_elt.getAttribute("algo")
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
133 return cls(algo=algo)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
134
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
135 def to_element(self) -> domish.Element:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
136 """Build the <hash-used> element from this instance's data.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
137
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
138 @return: <hash-used> element.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
139 """
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
140 hash_used_elt = domish.Element((NS_HASHES, "hash-used"))
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
141 hash_used_elt["algo"] = self.algo
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
142 return hash_used_elt
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
143
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
144
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
145 class XEP_0300:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
146 # TODO: add blake after moving to Python 3
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
147 ALGOS: OrderedDict[str, Callable] = OrderedDict(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
149 ("md5", hashlib.md5),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
150 ("sha-1", hashlib.sha1),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
151 ("sha-256", hashlib.sha256),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
152 ("sha-512", hashlib.sha512),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 )
3916
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
155 ALGO_DEFAULT = ALGO_DEFAULT
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
157 def __init__(self, host: "LiberviaBackend"):
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 log.info(_("plugin Hashes initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3916
diff changeset
159 host.register_namespace("hashes", NS_HASHES)
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
160 self.host = host
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
162 def get_handler(self, client: SatXMPPEntity) -> XMPPHandler:
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
163 return XEP_0300_handler()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
164
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
165 def get_hasher(self, algo: str = ALGO_DEFAULT) -> Callable:
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
166 """Return hasher instance
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
167
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
168 @param algo: one of the XEP_300.ALGOS keys
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
169 @return: same object s in hashlib.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
170 update method need to be called for each chunk
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
171 digest or hexdigest can be used at the end
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
172 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
173 return self.ALGOS[algo]()
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
174
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
175 def get_default_algo(self) -> str:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
176 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
177
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
178 async def get_best_peer_algo(self, to_jid: jid.JID, profile: str) -> str | None:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
179 """Return the best available hashing algorithm of other peer
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
180
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
181 @param to_jid: peer jid
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
182 @param profile: %(doc_profile)s
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
183 @return: best available algorithm,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
184 or None if hashing is not possible
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
185 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3916
diff changeset
186 client = self.host.get_client(profile)
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
187 for algo in reversed(XEP_0300.ALGOS):
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
188 has_feature = await self.host.hasFeature(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
189 client, NS_HASHES_FUNCTIONS.format(algo), to_jid
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
190 )
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
191 if has_feature:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
192 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
193 "Best hashing algorithm found for {jid}: {algo}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 jid=to_jid.full(), algo=algo
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 )
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
197 return algo
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
198
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
199 def _calculate_hash_blocking(self, file_obj: BinaryIO, hasher: HASH) -> str:
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
200 """Calculate hash in a blocking way
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
201
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3916
diff changeset
202 /!\\ blocking method, please use calculate_hash instead
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
203 @param file_obj: a file-like object
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
204 @param hasher: the method to call to initialise hash object
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
205 @return: the hex digest of the hash
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
206 """
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
207 while True:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
208 buf = file_obj.read(BUFFER_SIZE)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
209 if not buf:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
210 break
3286
ddf3ded93b78 plugin XEP-0300; fixed use of calculateHash with getHasher
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
211 hasher.update(buf)
ddf3ded93b78 plugin XEP-0300; fixed use of calculateHash with getHasher
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
212 return hasher.hexdigest()
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
213
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
214 def calculate_hash(self, file_obj: BinaryIO, hasher: HASH) -> defer.Deferred[str]:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3916
diff changeset
215 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
216
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
217 async def calculate_hash_elt(
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
218 self, file_obj: BinaryIO, algo: str = ALGO_DEFAULT
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
219 ) -> domish.Element:
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 """Compute hash and build hash element
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
222 @param file_obj: file-like object to use to calculate the hash
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
223 @param algo: algorithm to use, must be a key of XEP_0300.ALGOS
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
224 @return: hash element
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
225 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3916
diff changeset
226 hasher = self.get_hasher(algo)
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
227 hash_ = await self.calculate_hash(file_obj, hasher)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
228 return self.build_hash_elt(hash_, algo)
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
229
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
230 def build_hash_used_elt(self, algo: str = ALGO_DEFAULT) -> domish.Element:
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
231 hash_used_model = HashUsed(algo=algo)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
232 return hash_used_model.to_element()
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
233
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
234 def parse_hash_used_elt(self, parent_elt: domish.Element) -> str:
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
235 """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
236
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
237 @param parent: parent of <hash-used/> element
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
238 @return: hash algorithm used
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
239 @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
240 @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
241 """
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
242 hash_used_model = HashUsed.from_element(parent_elt)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
243 return hash_used_model.algo
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
244
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
245 def build_hash_elt(self, hash_hex: str, algo: str = ALGO_DEFAULT) -> domish.Element:
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
246 """Compute hash and build hash element
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
247
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
248 @param hash_: Hexadecimal representation of hash to use.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
249 @param algo: Algorithm to use, must be a key of XEP_0300.ALGOS.
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
250 @return: <hash> element
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251 """
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
252 b64_hash = base64.b64encode(hash_hex.encode()).decode()
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
253 hash_model = Hash(algo=algo, hash_=b64_hash)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
254 return hash_model.to_element()
1526
bb451fd1cea3 plugin XEP-0300: hashes management first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
256 def parse_hash_elt(self, parent: domish.Element) -> tuple[str, str]:
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
257 """Find and parse a hash element
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
258
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
259 if multiple elements are found, the strongest managed one is returned
3916
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
260 @param parent: parent of <hash/> element
40d47cc29ea4 plugin XEP-0300: type hints
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
261 @return: (algo, hash) tuple
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
262 both values can be None if <hash/> is empty
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
263 @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
264 @raise exceptions.DataError: the element is invalid
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
265 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
266 algos = list(XEP_0300.ALGOS.keys())
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
267 hash_elt = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
268 best_algo = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
269 best_value = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
270 for hash_elt in parent.elements(NS_HASHES, "hash"):
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
271 hash_model = Hash.from_element(hash_elt)
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
272 algo = hash_model.algo
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
273 try:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
274 idx = algos.index(algo)
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
275 except ValueError:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
276 log.warning(f"Proposed {algo} algorithm is not managed")
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
277 algo = None
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
278 continue
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
279
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
280 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
281 best_algo = algo
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
282 best_value = base64.b64decode(hash_model.hash_).decode()
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
283
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
284 if not hash_elt:
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
285 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
286 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
287 raise exceptions.DataError
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
288 return best_algo, best_value
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
289
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
290
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
291 @implementer(iwokkel.IDisco)
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
292 class XEP_0300_handler(XMPPHandler):
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
293
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
294 def getDiscoInfo(
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
295 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
296 ) -> list[disco.DiscoFeature]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
297 hash_functions_names = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
298 disco.DiscoFeature(NS_HASHES_FUNCTIONS.format(algo))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 for algo in XEP_0300.ALGOS
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
300 ]
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
301 return [disco.DiscoFeature(NS_HASHES)] + hash_functions_names
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
302
4334
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
303 def getDiscoItems(
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
304 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
111dce64dcb5 plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
305 ) -> list[disco.DiscoItem]:
1618
0de5f210fe56 plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents: 1526
diff changeset
306 return []