jp: jp asyncio port:

/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\

This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).

During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
This commit is contained in:
Goffi 2019-09-25 08:56:41 +02:00
commit 76aadbb3fb
4 changed files with 3299 additions and 3300 deletions

View file

@ -45,7 +45,7 @@ class BlockCipher(object):
@param key (unicode): the encryption key
@param text (unicode): the text to encrypt
@param leave_empty (bool): if True, empty text will be returned "as is"
@return: Deferred: base-64 encoded str
@return (D(str)): base-64 encoded encrypted message
"""
if leave_empty and text == "":
return succeed(text)
@ -59,6 +59,7 @@ class BlockCipher(object):
cipher = AES.new(key, AES.MODE_CFB, iv)
d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode("utf-8")))
d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
d.addCallback(lambda bytes_cypher: bytes_cypher.decode('utf-8'))
return d
@classmethod
@ -137,7 +138,7 @@ class PasswordHasher(object):
@return: Deferred: base-64 encoded str
"""
if leave_empty and password == "":
return succeed(b"")
return succeed("")
salt = (
b64decode(salt)[: PasswordHasher.SALT_LEN]
if salt
@ -145,11 +146,12 @@ class PasswordHasher(object):
)
d = deferToThread(PBKDF2, password, salt)
d.addCallback(lambda hashed: b64encode(salt + hashed))
d.addCallback(lambda hashed_bytes: hashed_bytes.decode('utf-8'))
return d
@classmethod
def compare_hash(cls, hashed_attempt, hashed):
assert isinstance(hashed, bytes)
assert isinstance(hashed, str)
return hashed_attempt == hashed
@classmethod
@ -160,7 +162,9 @@ class PasswordHasher(object):
@param hashed (str): the hash of the password
@return: Deferred: boolean
"""
assert isinstance(attempt, str)
assert isinstance(hashed, str)
leave_empty = hashed == ""
d = PasswordHasher.hash(attempt, hashed, leave_empty)
d.addCallback(cls.compare_hash, hashed=hashed.encode('utf-8'))
d.addCallback(cls.compare_hash, hashed=hashed)
return d

View file

@ -434,7 +434,7 @@ class Discovery(object):
extensions[form_type or ""] = fields
defer.returnValue((
disco_infos.features,
[str(f) for f in disco_infos.features],
[(cat, type_, name or "")
for (cat, type_), name in list(disco_infos.identities.items())],
extensions))

View file

@ -560,9 +560,11 @@ class Memory(object):
def initPersonalKey(__):
# be sure to call this after checking that the profile doesn't exist yet
# generated once for all and saved in a PersistentDict
personal_key = BlockCipher.getRandomKey(
base64=True
) # generated once for all and saved in a PersistentDict
).decode('utf-8')
self.auth_sessions.newSession(
{C.MEMORY_CRYPTO_KEY: personal_key}, profile=name
) # will be encrypted by setParam
@ -1260,8 +1262,8 @@ class Memory(object):
parent = current["parent"]
if not parent:
break
files_data = yield self.getFile(
self, client, peer_jid=None, file_id=parent, perms_to_check=None
files_data = yield self.getFiles(
client, peer_jid=None, file_id=parent, perms_to_check=None
)
try:
current = files_data[0]

View file

@ -529,17 +529,18 @@ class Params(object):
return d.addCallback(gotPlainPassword)
def __type_to_string(self, result):
""" convert result to string, according to its type """
def _type_to_str(self, result):
"""Convert result to string, according to its type """
if isinstance(result, bool):
return "true" if result else "false"
elif isinstance(result, int):
return C.boolConst(result)
elif isinstance(result, (list, set, tuple)):
return ', '.join(self._type_to_str(r) for r in result)
else:
return str(result)
return result
def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
""" Same as getParamA but for bridge: convert non string value to string """
return self.__type_to_string(
return self._type_to_str(
self.getParamA(name, category, attr, profile_key=profile_key)
)
@ -599,15 +600,10 @@ class Params(object):
return self._getAttr(node[1], attr, value)
def asyncGetStringParamA(
self,
name,
category,
attr="value",
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE):
d = self.asyncGetParamA(name, category, attr, security_limit, profile_key)
d.addCallback(self.__type_to_string)
d.addCallback(self._type_to_str)
return d
def asyncGetParamA(
@ -957,14 +953,8 @@ class Params(object):
categories.append(cat.getAttribute("name"))
return categories
def setParam(
self,
name,
value,
category,
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
def setParam(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE):
"""Set a parameter, return None if the parameter is not in param xml.
Parameter of type 'password' that are not the SàT profile password are
@ -994,13 +984,11 @@ class Params(object):
return defer.succeed(None)
if not self.checkSecurityLimit(node[1], security_limit):
log.warning(
_(
"Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
% {"param": name, "cat": category}
)
)
return defer.succeed(None)
msg = _(
f"{profile!r} is trying to set parameter {name!r} in category "
f"{category!r} without authorization!!!")
log.warning(msg)
raise exceptions.PermissionError(msg)
type_ = node[1].getAttribute("type")
if type_ == "int":
@ -1010,12 +998,10 @@ class Params(object):
try:
int(value)
except ValueError:
log.debug(
_(
"Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
% {"param": name, "cat": category}
)
)
log.warning(_(
f"Trying to set parameter {name!r} in category {category!r} with"
f"an non-integer value"
))
return defer.succeed(None)
if node[1].hasAttribute("constraint"):
constraint = node[1].getAttribute("constraint")