[libsecret/wip/dueno/local-file] secret-backend: Check if portal is available



commit 83f32f0f70b4243aa67f44e4fadb29b8eaf4651b
Author: Daiki Ueno <dueno src gnome org>
Date:   Mon Oct 7 17:20:21 2019 +0200

    secret-backend: Check if portal is available
    
    Before decising to use the file backend, check if the necessary portal
    interface is available on the D-Bus.
    
    Suggested by Patrick Griffis.

 libsecret/secret-backend.c      |  3 ++-
 libsecret/secret-file-backend.c | 49 +++++++++++++++++++++++++++++++++++++++++
 libsecret/secret-file-backend.h |  2 ++
 3 files changed, 53 insertions(+), 1 deletion(-)
---
diff --git a/libsecret/secret-backend.c b/libsecret/secret-backend.c
index 30e3abb..6ce2645 100644
--- a/libsecret/secret-backend.c
+++ b/libsecret/secret-backend.c
@@ -155,7 +155,8 @@ backend_get_impl_type (void)
 #endif
 
 #ifdef WITH_GCRYPT
-       if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS))
+       if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS) &&
+           _secret_file_backend_check_portal_version ())
                extension_name = "file";
        else
 #endif
diff --git a/libsecret/secret-file-backend.c b/libsecret/secret-file-backend.c
index 6bfbc41..cb159c0 100644
--- a/libsecret/secret-file-backend.c
+++ b/libsecret/secret-file-backend.c
@@ -33,6 +33,7 @@ EGG_SECURE_DECLARE (secret_file_backend);
 #define PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"
 #define PORTAL_REQUEST_INTERFACE "org.freedesktop.portal.Request"
 #define PORTAL_SECRET_INTERFACE "org.freedesktop.portal.Secret"
+#define PORTAL_SECRET_VERSION 1
 
 static void secret_file_backend_async_initable_iface (GAsyncInitableIface *iface);
 static void secret_file_backend_backend_iface (SecretBackendInterface *iface);
@@ -767,3 +768,51 @@ secret_file_backend_backend_iface (SecretBackendInterface *iface)
        iface->search = secret_file_backend_real_search;
        iface->search_finish = secret_file_backend_real_search_finish;
 }
+
+gboolean
+_secret_file_backend_check_portal_version (void)
+{
+       GDBusConnection *connection;
+       GVariant *ret;
+       GVariant *value;
+       guint32 version;
+       GError *error = NULL;
+
+       connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+       if (connection == NULL) {
+               g_warning ("couldn't get session bus: %s", error->message);
+               g_error_free (error);
+               g_object_unref (connection);
+               return FALSE;
+       }
+
+       ret = g_dbus_connection_call_sync (connection,
+                                          PORTAL_BUS_NAME,
+                                          PORTAL_OBJECT_PATH,
+                                          "org.freedesktop.DBus.Properties",
+                                          "Get",
+                                          g_variant_new ("(ss)",
+                                                         PORTAL_SECRET_INTERFACE,
+                                                         "version"),
+                                          G_VARIANT_TYPE ("(v)"),
+                                          0, -1, NULL, &error);
+       g_object_unref (connection);
+       if (!ret) {
+               g_message ("secret portal %s is not available: %s",
+                          error->message);
+               g_error_free (error);
+               return FALSE;
+       }
+
+       g_variant_get (ret, "(v)", &value);
+       g_variant_unref (ret);
+       version = g_variant_get_uint32 (value);
+       g_variant_unref (value);
+       if (version != PORTAL_SECRET_VERSION) {
+               g_message ("secret portal version mismatch: %u != %u",
+                          version, PORTAL_SECRET_VERSION);
+               return FALSE;
+       }
+
+       return TRUE;
+}
diff --git a/libsecret/secret-file-backend.h b/libsecret/secret-file-backend.h
index 27d896c..655bed3 100644
--- a/libsecret/secret-file-backend.h
+++ b/libsecret/secret-file-backend.h
@@ -26,6 +26,8 @@ G_BEGIN_DECLS
 #define SECRET_TYPE_FILE_BACKEND (secret_file_backend_get_type ())
 G_DECLARE_FINAL_TYPE (SecretFileBackend, secret_file_backend, SECRET, FILE_BACKEND, GObject)
 
+gboolean _secret_file_backend_check_portal_version (void);
+
 G_END_DECLS
 
 #endif /* __SECRET_FILE_BACKEND_H__ */


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