From 6b7d643929df49a32729b480c8982be7a294af57 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Mon, 3 Mar 2014 16:07:36 +0100 Subject: [PATCH] libsecret: Add secret_service_encode_dbus_secret() and opposite Add secret_service_encode_dbus_secret() and secret_service_decode_dbus_secret() functions for encoding and decoding the Secret Service API DBus structs that carry secrets on the wire. These are not added to the stable or scripting APIs. --- .../libsecret/libsecret-sections.txt | 2 + libsecret/secret-paths.c | 59 +++++++++++++++++++ libsecret/secret-paths.h | 6 ++ libsecret/test-paths.c | 29 +++++++++ 4 files changed, 96 insertions(+) diff --git a/docs/reference/libsecret/libsecret-sections.txt b/docs/reference/libsecret/libsecret-sections.txt index d62dd5d..1870dbf 100644 --- a/docs/reference/libsecret/libsecret-sections.txt +++ b/docs/reference/libsecret/libsecret-sections.txt @@ -289,6 +289,8 @@ secret_service_read_alias_dbus_path_sync secret_service_set_alias_to_dbus_path secret_service_set_alias_to_dbus_path_finish secret_service_set_alias_to_dbus_path_sync +secret_service_encode_dbus_secret +secret_service_decode_dbus_secret
diff --git a/libsecret/secret-paths.c b/libsecret/secret-paths.c index 5c034be..3ce3eee 100644 --- a/libsecret/secret-paths.c +++ b/libsecret/secret-paths.c @@ -2581,3 +2581,62 @@ secret_service_prompt_at_dbus_path_finish (SecretService *self, return secret_service_prompt_finish (self, result, error); } + +/** + * secret_service_encode_dbus_secret: + * @service: the service + * @value: the secret value + * + * Encodes a #SecretValue into GVariant for use with the Secret Service + * DBus API. + * + * The resulting GVariant will have a (oayays) signature. + * + * A session must have already been established by the #SecretService. + * + * Returns: (transfer floating): the encoded secret + */ +GVariant * +secret_service_encode_dbus_secret (SecretService *service, + SecretValue *value) +{ + SecretSession *session; + + g_return_val_if_fail (service != NULL, NULL); + g_return_val_if_fail (value != NULL, NULL); + + session = _secret_service_get_session (service); + g_return_val_if_fail (session != NULL, NULL); + + return _secret_session_encode_secret (session, value); +} + +/** + * secret_service_decode_dbus_secret: + * @service: the service + * @value: the encoded secret + * + * Decode a #SecretValue into GVariant received with the Secret Service + * DBus API. + * + * The GVariant should have a (oayays) signature. + * + * A session must have already been established by the #SecretService, and + * the encoded secret must be valid for that session. + * + * Returns: (transfer full): the decoded secret value + */ +SecretValue * +secret_service_decode_dbus_secret (SecretService *service, + GVariant *value) +{ + SecretSession *session; + + g_return_val_if_fail (service != NULL, NULL); + g_return_val_if_fail (value != NULL, NULL); + + session = _secret_service_get_session (service); + g_return_val_if_fail (session != NULL, NULL); + + return _secret_session_decode_secret (session, value); +} diff --git a/libsecret/secret-paths.h b/libsecret/secret-paths.h index 8bda883..0702297 100644 --- a/libsecret/secret-paths.h +++ b/libsecret/secret-paths.h @@ -272,6 +272,12 @@ gboolean secret_service_set_alias_to_dbus_path_sync (SecretSe GCancellable *cancellable, GError **error); +GVariant * secret_service_encode_dbus_secret (SecretService *service, + SecretValue *value); + +SecretValue * secret_service_decode_dbus_secret (SecretService *service, + GVariant *value); + G_END_DECLS #endif /* __SECRET_SERVICE_H___ */ diff --git a/libsecret/test-paths.c b/libsecret/test-paths.c index 9f93eae..631ce13 100644 --- a/libsecret/test-paths.c +++ b/libsecret/test-paths.c @@ -710,6 +710,33 @@ test_set_alias_path (Test *test, g_assert (path == NULL); } +static void +test_encode_decode_secret (Test *test, + gconstpointer unused) +{ + GVariant *variant; + SecretValue *value; + SecretValue *decoded; + GError *error = NULL; + + value = secret_value_new ("zerogjuggs", -1, "text/plain"); + + secret_service_ensure_session_sync (test->service, NULL, &error); + g_assert_no_error (error); + + variant = secret_service_encode_dbus_secret (test->service, value); + g_assert (variant != NULL); + g_assert_cmpstr (g_variant_get_type_string (variant), ==, "(oayays)"); + secret_value_unref (value); + + decoded = secret_service_decode_dbus_secret (test->service, variant); + g_assert (variant != NULL); + g_variant_unref (variant); + + g_assert_cmpstr (secret_value_get_text (decoded), ==, "zerogjuggs"); + secret_value_unref (decoded); +} + int main (int argc, char **argv) { @@ -746,5 +773,7 @@ main (int argc, char **argv) g_test_add ("/service/set-alias-path", Test, "mock-service-normal.py", setup, test_set_alias_path, teardown); + g_test_add ("/service/encode-decode-secret", Test, "mock-service-normal.py", setup, test_encode_decode_secret, teardown); + return egg_tests_run_with_loop (); }