[libsecret/wip/dueno/local-file: 8/8] password: Hook storage backend functions



commit f4fe6ce6af820c38425ac28ed2e36420b8f2e8e7
Author: Daiki Ueno <dueno src gnome org>
Date:   Sun Sep 23 15:00:01 2018 +0200

    password: Hook storage backend functions

 libsecret/Makefile.am       |  1 +
 libsecret/secret-password.c | 66 ++++++++++++++++++++++++++++++++++++++-------
 libsecret/secret-private.h  |  2 ++
 libsecret/secret-storage.c  |  8 ++++++
 libsecret/store2.jwe        |  8 ++++++
 libsecret/test-password.c   | 30 +++++++++++++++++++++
 6 files changed, 105 insertions(+), 10 deletions(-)
---
diff --git a/libsecret/Makefile.am b/libsecret/Makefile.am
index cfef7b5..7dadf7e 100644
--- a/libsecret/Makefile.am
+++ b/libsecret/Makefile.am
@@ -374,4 +374,5 @@ EXTRA_DIST += \
        $(JS_TESTS) \
        $(PY_TESTS) \
        libsecret/store1.jwe \
+       libsecret/store2.jwe \
        $(NULL)
diff --git a/libsecret/secret-password.c b/libsecret/secret-password.c
index 149995d..8df45c5 100644
--- a/libsecret/secret-password.c
+++ b/libsecret/secret-password.c
@@ -17,6 +17,7 @@
 #include "secret-attributes.h"
 #include "secret-password.h"
 #include "secret-private.h"
+#include "secret-storage.h"
 #include "secret-value.h"
 
 #include <egg/egg-secure-memory.h>
@@ -42,6 +43,27 @@
  * Stability: Stable
  */
 
+static gboolean
+_secret_password_use_storage (void)
+{
+       static gboolean flatpak_info_read;
+       static gboolean in_flatpak;
+       const gchar *envvar;
+
+       if (flatpak_info_read)
+               return in_flatpak;
+
+       flatpak_info_read = TRUE;
+       if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS))
+               in_flatpak = TRUE;
+
+       if (in_flatpak)
+               return TRUE;
+
+       envvar = g_getenv ("SECRET_STORAGE_PASSWORD");
+       return envvar && *envvar != '\0';
+}
+
 /**
  * secret_password_store: (skip)
  * @schema: the schema for attributes
@@ -149,8 +171,12 @@ secret_password_storev (const SecretSchema *schema,
 
        value = secret_value_new (password, -1, "text/plain");
 
-       secret_service_store (NULL, schema, attributes, collection,
-                             label, value, cancellable, callback, user_data);
+       if (_secret_password_use_storage ())
+               secret_storage_store (NULL, schema, attributes, collection,
+                                     label, value, cancellable, callback, user_data);
+       else
+               secret_service_store (NULL, schema, attributes, collection,
+                                     label, value, cancellable, callback, user_data);
 
        secret_value_unref (value);
 }
@@ -169,7 +195,10 @@ secret_password_store_finish (GAsyncResult *result,
                               GError **error)
 {
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-       return secret_service_store_finish (NULL, result, error);
+       if (_secret_password_use_storage ())
+               return secret_storage_store_finish (NULL, result, error);
+       else
+               return secret_service_store_finish (NULL, result, error);
 }
 
 /**
@@ -377,8 +406,12 @@ secret_password_lookupv (const SecretSchema *schema,
        if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
                return;
 
-       secret_service_lookup (NULL, schema, attributes,
-                              cancellable, callback, user_data);
+       if (_secret_password_use_storage ())
+               secret_storage_lookup (NULL, schema, attributes,
+                                      cancellable, callback, user_data);
+       else
+               secret_service_lookup (NULL, schema, attributes,
+                                      cancellable, callback, user_data);
 }
 
 /**
@@ -399,7 +432,10 @@ secret_password_lookup_nonpageable_finish (GAsyncResult *result,
 
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-       value = secret_service_lookup_finish (NULL, result, error);
+       if (_secret_password_use_storage ())
+               value = secret_storage_lookup_finish (NULL, result, error);
+       else
+               value = secret_service_lookup_finish (NULL, result, error);
        if (value == NULL)
                return NULL;
 
@@ -424,7 +460,10 @@ secret_password_lookup_finish (GAsyncResult *result,
 
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-       value = secret_service_lookup_finish (NULL, result, error);
+       if (_secret_password_use_storage ())
+               value = secret_storage_lookup_finish (NULL, result, error);
+       else
+               value = secret_service_lookup_finish (NULL, result, error);
        if (value == NULL)
                return NULL;
 
@@ -719,8 +758,12 @@ secret_password_clearv (const SecretSchema *schema,
        if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
                return;
 
-       secret_service_clear (NULL, schema, attributes,
-                             cancellable, callback, user_data);
+       if (_secret_password_use_storage ())
+               secret_storage_clear (NULL, schema, attributes,
+                                     cancellable, callback, user_data);
+       else
+               secret_service_clear (NULL, schema, attributes,
+                                     cancellable, callback, user_data);
 }
 
 /**
@@ -738,7 +781,10 @@ secret_password_clear_finish (GAsyncResult *result,
                               GError **error)
 {
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-       return secret_service_clear_finish (NULL, result, error);
+       if (_secret_password_use_storage ())
+               return secret_storage_clear_finish (NULL, result, error);
+       else
+               return secret_service_clear_finish (NULL, result, error);
 }
 
 /**
diff --git a/libsecret/secret-private.h b/libsecret/secret-private.h
index e563887..c50065b 100644
--- a/libsecret/secret-private.h
+++ b/libsecret/secret-private.h
@@ -210,6 +210,8 @@ const SecretSchema * _secret_schema_ref_if_nonstatic          (const SecretSchem
 
 void                 _secret_schema_unref_if_nonstatic        (const SecretSchema *schema);
 
+void                 _secret_storage_reset_default            (void);
+
 G_END_DECLS
 
 #endif /* __SECRET_PRIVATE_H___ */
diff --git a/libsecret/secret-storage.c b/libsecret/secret-storage.c
index 9670fd0..6528c8f 100644
--- a/libsecret/secret-storage.c
+++ b/libsecret/secret-storage.c
@@ -371,6 +371,14 @@ secret_storage_get_default_finish (GAsyncResult  *result,
        return g_task_propagate_pointer (G_TASK (result), error);
 }
 
+void
+_secret_storage_reset_default (void)
+{
+       G_LOCK (storage_instance);
+       g_clear_object (&storage_instance);
+       G_UNLOCK (storage_instance);
+}
+
 static void
 on_replace_contents (GObject *source_object,
                     GAsyncResult *result,
diff --git a/libsecret/store2.jwe b/libsecret/store2.jwe
new file mode 100644
index 0000000..d88fd47
--- /dev/null
+++ b/libsecret/store2.jwe
@@ -0,0 +1,8 @@
+{
+    
"ciphertext":"sUehOAP0CosBbqUIh2mHorVEz6ETa_EEi4wt9bmKL9geI1S3g2yAsB6zbFaOgzf2JRaN0xC8ctJLFueb1qPboxziZlhLzbXvWP_yWpVkmWqlAXbfZDu8b2dkRIcFYL7TNCeUB4IObzRjgGpwJ7R2xDrIqNqklvN7A2ouzDPZN-dhQcgG02EPRp1NXRujQASl4-qfsRafRSTP8qF3",
+    "encrypted_key":"",
+    "iv":"xoQz-88KTYZWmvRh",
+    "tag":"H1YXD6dQVUnj7WCdjks7Ag",
+    "protected":"eyJlbmMiOiJBMTI4R0NNIn0",
+    "header":{"alg":"dir"}
+}
diff --git a/libsecret/test-password.c b/libsecret/test-password.c
index b745427..5b14f8b 100644
--- a/libsecret/test-password.c
+++ b/libsecret/test-password.c
@@ -48,6 +48,7 @@ static const SecretSchema NO_NAME_SCHEMA = {
 
 typedef struct {
        GPid pid;
+       gchar *directory;
 } Test;
 
 static void
@@ -69,6 +70,31 @@ teardown (Test *test,
        mock_service_stop ();
 }
 
+static void
+setup_storage (Test *test,
+              gconstpointer data)
+{
+       gchar *path;
+
+       _secret_storage_reset_default ();
+
+       test->directory = egg_tests_create_scratch_directory (SRCDIR "/libsecret/store2.jwe", NULL);
+       path = g_build_filename (test->directory, "store2.jwe", NULL);
+       g_setenv ("SECRET_STORAGE_PATH", path, TRUE);
+       g_free (path);
+       g_setenv ("SECRET_STORAGE_PASSWORD", "12345", TRUE);
+}
+
+static void
+teardown_storage (Test *test,
+                 gconstpointer unused)
+{
+       egg_tests_remove_scratch_directory (test->directory);
+       g_free (test->directory);
+       g_unsetenv ("SECRET_STORAGE_PATH");
+       g_unsetenv ("SECRET_STORAGE_PASSWORD");
+}
+
 static void
 on_complete_get_result (GObject *source,
                         GAsyncResult *result,
@@ -381,6 +407,10 @@ main (int argc, char **argv)
        g_test_add ("/password/delete-async", Test, "mock-service-delete.py", setup, test_delete_async, 
teardown);
        g_test_add ("/password/clear-no-name", Test, "mock-service-delete.py", setup, test_clear_no_name, 
teardown);
 
+       g_test_add ("/password/storage/lookup-sync", Test, NULL, setup_storage, test_lookup_sync, 
teardown_storage);
+       g_test_add ("/password/storage/store-sync", Test, NULL, setup_storage, test_store_sync, 
teardown_storage);
+       g_test_add ("/password/storage/delete-sync", Test, NULL, setup_storage, test_delete_sync, 
teardown_storage);
+
        g_test_add_func ("/password/free-null", test_password_free_null);
 
        return egg_tests_run_with_loop ();


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