[gnome-keysign: 1/8] gpgmeh: split importing functions into gpgme and dbus




commit ec4d23901c259496b285fbad6ce1899016d9d084
Author: Tobias Mueller <muelli cryptobitch de>
Date:   Mon Jun 15 09:29:54 2020 +0200

    gpgmeh: split importing functions into gpgme and dbus
    
    We split the functions to make it possible to test them. That is, we
    probably can't test the dbus as easily as the gpgme version.

 keysign/gpgmeh.py | 54 +++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 19 deletions(-)
---
diff --git a/keysign/gpgmeh.py b/keysign/gpgmeh.py
index 5f6a17c..a74d253 100755
--- a/keysign/gpgmeh.py
+++ b/keysign/gpgmeh.py
@@ -546,6 +546,36 @@ def decrypt_and_import_signature(encrypted_sig, homedir=None):
     signature = decrypt_signature(encrypted_sig, homedir=homedir)
     import_signature(signature)
 
+
+
+def import_signature_dbus(signature):
+    "Imports a TPK into the user's keyring by using Seahorse's DBus API"
+    name = "org.gnome.seahorse"
+    path = "/org/gnome/seahorse/keys"
+    bus = dbus.SessionBus()
+    result = []
+
+    proxy = bus.get_object(name, path)
+    iface = "org.gnome.seahorse.KeyService"
+    gpg_iface = dbus.Interface(proxy, iface)
+    payload = base64.b64encode(signature).decode('latin-1')
+    payload = '\n'.join(payload[i:(i + 64)] for i in range(0, len(payload), 64))
+    payload = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n" + payload + "\n-----END PGP PUBLIC KEY BLOCK-----"
+    result = gpg_iface.ImportKeys("openpgp", payload)
+    log.debug("Importing via DBus: %r", result)
+
+    return result
+
+def import_signature_gpgme(signature, homedir=None):
+    "Imports an OpenPGP TPK into a keyring via GPGME"
+    ctx = DirectoryContext(homedir)
+    ctx.op_import(signature)
+    result = ctx.op_import_result()
+    if len(result.imports) < 1:
+        raise GPGMEError
+
+    return result
+
 def import_signature(signature, homedir=None):
     """
     Imports an OpenPGP TPK to the local keyring
@@ -556,29 +586,15 @@ def import_signature(signature, homedir=None):
     This function will try to import the TPK via DBus first and,
     if that failed, resort to using gpgme directly.
     """
-    # Try Seahorse DBus
-    name = "org.gnome.seahorse"
-    path = "/org/gnome/seahorse/keys"
-    bus = dbus.SessionBus()
     result = []
-
     try:
-        proxy = bus.get_object(name, path)
+        # Try Seahorse DBus
+        result = import_signature_dbus(signature)
     except dbus.exceptions.DBusException:
         log.debug("Seahorse DBus is not available")
-    else:
-        iface = "org.gnome.seahorse.KeyService"
-        gpg_iface = dbus.Interface(proxy, iface)
-        payload = base64.b64encode(signature).decode('latin-1')
-        payload = '\n'.join(payload[i:(i + 64)] for i in range(0, len(payload), 64))
-        payload = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n" + payload + "\n-----END PGP PUBLIC KEY 
BLOCK-----"
-        result = gpg_iface.ImportKeys("openpgp", payload)
-        log.debug("Importing via DBus: %r", result)
 
     # If Seahorse failed we try op_import
     if len(result) < 1:
-        ctx = DirectoryContext(homedir)
-        ctx.op_import(signature)
-        result = ctx.op_import_result()
-        if len(result.imports) < 1:
-            raise GPGMEError
+        result = import_signature_gpgme(signature, homedir=homedir)
+
+    return result


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]