From 179db446fe6bc8c001c4a53d7fe551c11e56cfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 25 Aug 2019 01:56:28 +0200 Subject: [PATCH] Add get_trust_for_jid method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- slixmpp_omemo/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/slixmpp_omemo/__init__.py b/slixmpp_omemo/__init__.py index 6352c53..fb4a5d7 100644 --- a/slixmpp_omemo/__init__.py +++ b/slixmpp_omemo/__init__.py @@ -16,6 +16,7 @@ from typing import Any, Dict, List, Optional, Set, Tuple, Union import os import json import base64 +import codecs import asyncio from slixmpp.plugins.xep_0060.stanza import Items, EventItems from slixmpp.plugins.xep_0004 import Form @@ -77,6 +78,10 @@ def _load_device_id(data_dir: str) -> int: return did +def fp_from_ik(ik: bytes) -> str: + """Convert identityKey to a string representation (fingerprint)""" + return codecs.getencoder("hex")(ik)[0].decode("US-ASCII").upper() + def _parse_bundle(backend: Backend, bundle: Bundle) -> ExtendedPublicBundle: identity_key = b64dec(bundle['identityKey']['value'].strip()) @@ -382,6 +387,16 @@ class XEP_0384(BasePlugin): def distrust(self, jid: JID, device_id: int, ik: bytes) -> None: self._omemo.distrust(jid.bare, device_id, ik) + def get_trust_for_jid(self, jid: JID) -> Dict[str, List[Optional[Tuple[bytes, bool, str]]]]: + devices = self._omemo.getTrustForJID(jid.bare) + for trust in devices['active'].values(): + if trust is not None: + trust['fingerprint'] = fp_from_ik(trust['key']) + for trust in devices['inactive'].values(): + if trust is not None: + trust['fingerprint'] = fp_from_ik(trust['key']) + return devices + def is_encrypted(self, msg: Message) -> bool: return msg.xml.find('{%s}encrypted' % OMEMO_BASE_NS) is not None