[epiphany/wip/sync: 19/19] sync-service: Register/unregister client id at sign in/out



commit ba491125b2867b2a83a9bdba844b0d38892b3994
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Wed Apr 12 21:15:57 2017 +0300

    sync-service: Register/unregister client id at sign in/out

 data/org.gnome.epiphany.gschema.xml |    5 ++
 lib/ephy-prefs.h                    |    1 +
 src/sync/ephy-sync-service.c        |   83 +++++++++++++++++++++++++++++++++++
 3 files changed, 89 insertions(+), 0 deletions(-)
---
diff --git a/data/org.gnome.epiphany.gschema.xml b/data/org.gnome.epiphany.gschema.xml
index 7d697c5..2264bbd 100644
--- a/data/org.gnome.epiphany.gschema.xml
+++ b/data/org.gnome.epiphany.gschema.xml
@@ -272,6 +272,11 @@
                        <summary>Currently signed in sync user</summary>
                        <description>The email linked to the Firefox Account used to sync data with Mozilla’s 
servers.</description>
                </key>
+               <key type="s" name="sync-client-id">
+                       <default>''</default>
+                       <summary>Sync client ID</summary>
+                       <description>The sync client ID of the current device.</description>
+               </key>
                <key type="u" name="sync-frequency">
                        <default>30</default>
                        <summary>The sync frequency in minutes</summary>
diff --git a/lib/ephy-prefs.h b/lib/ephy-prefs.h
index 7e53dbe..3167d12 100644
--- a/lib/ephy-prefs.h
+++ b/lib/ephy-prefs.h
@@ -151,6 +151,7 @@ static const char * const ephy_prefs_web_schema[] = {
 
 #define EPHY_PREFS_SYNC_SCHEMA            "org.gnome.Epiphany.sync"
 #define EPHY_PREFS_SYNC_USER              "sync-user"
+#define EPHY_PREFS_SYNC_CLIENT_ID         "sync-client-id"
 #define EPHY_PREFS_SYNC_FREQUENCY         "sync-frequency"
 #define EPHY_PREFS_SYNC_WITH_FIREFOX      "sync-with-firefox"
 #define EPHY_PREFS_SYNC_BOOKMARKS_ENABLED "sync-bookmarks-enabled"
diff --git a/src/sync/ephy-sync-service.c b/src/sync/ephy-sync-service.c
index 096346d..08a8448 100644
--- a/src/sync/ephy-sync-service.c
+++ b/src/sync/ephy-sync-service.c
@@ -1175,6 +1175,86 @@ ephy_sync_service_sync (gpointer user_data)
 }
 
 static void
+ephy_sync_service_unregister_client_id (EphySyncService *self)
+{
+  char *client_id;
+  char *endpoint;
+
+  g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
+
+  client_id = g_settings_get_string (EPHY_SETTINGS_SYNC, EPHY_PREFS_SYNC_CLIENT_ID);
+  endpoint = g_strdup_printf ("storage/clients/%s", client_id);
+
+  ephy_sync_service_queue_storage_request (self, endpoint, SOUP_METHOD_DELETE,
+                                           NULL, -1, -1, NULL, NULL);
+  g_settings_set_string (EPHY_SETTINGS_SYNC, EPHY_PREFS_SYNC_CLIENT_ID, "");
+
+  g_free (endpoint);
+  g_free (client_id);
+}
+
+static void
+ephy_sync_service_register_client_id (EphySyncService *self)
+{
+  SyncCryptoKeyBundle *bundle;
+  JsonNode *node;
+  JsonObject *json;
+  JsonObject *bso;
+  JsonArray *array;
+  char *client_id;
+  char *name;
+  char *protocol;
+  char *record;
+  char *payload;
+  char *body;
+  char *endpoint;
+
+  g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
+
+  node = json_node_new (JSON_NODE_OBJECT);
+  json = json_object_new ();
+  client_id = ephy_sync_crypto_get_random_sync_id ();
+  json_object_set_string_member (json, "id", client_id);
+  name = g_strdup_printf ("%s on Epiphany", client_id);
+  json_object_set_string_member (json, "name", name);
+  json_object_set_string_member (json, "type", "desktop");
+  array = json_array_new ();
+  protocol = g_strdup_printf ("1.%d", STORAGE_VERSION);
+  json_array_add_string_element (array, protocol);
+  json_object_set_array_member (json, "protocols", array);
+  json_object_set_string_member (json, "os", "Linux");
+  json_object_set_string_member (json, "application", "Epiphany");
+  json_object_set_string_member (json, "fxaDeviceId",
+                                 ephy_sync_service_get_secret (self, secrets[UID]));
+  json_node_set_object (node, json);
+  record = json_to_string (node, FALSE);
+  bundle = ephy_sync_service_get_key_bundle (self, "clients");
+  payload = ephy_sync_crypto_encrypt_record (record, bundle);
+  bso = json_object_new ();
+  json_object_set_string_member (bso, "id", client_id);
+  json_object_set_string_member (bso, "payload", payload);
+  json_node_set_object (node, bso);
+  body = json_to_string (node, FALSE);
+  endpoint = g_strdup_printf ("storage/clients/%s", client_id);
+
+  ephy_sync_service_queue_storage_request (self, endpoint, SOUP_METHOD_PUT,
+                                           body, -1, -1, NULL, NULL);
+  g_settings_set_string (EPHY_SETTINGS_SYNC, EPHY_PREFS_SYNC_CLIENT_ID, client_id);
+
+  g_free (endpoint);
+  g_free (body);
+  json_object_unref(bso);
+  g_free (payload);
+  ephy_sync_crypto_key_bundle_free (bundle);
+  g_free (record);
+  g_free (protocol);
+  g_free (name);
+  g_free (client_id);
+  json_object_unref(json);
+  json_node_unref (node);
+}
+
+static void
 ephy_sync_service_stop_periodical_sync (EphySyncService *self)
 {
   g_assert (EPHY_IS_SYNC_SERVICE (self));
@@ -1217,6 +1297,8 @@ sync_secrets_store_finished_cb (EphySyncService *self,
     ephy_sync_service_destroy_session (self);
     g_clear_pointer (&self->user_email, g_free);
     g_hash_table_remove_all (self->secrets);
+  } else {
+    ephy_sync_service_register_client_id (self);
   }
 }
 
@@ -1745,6 +1827,7 @@ ephy_sync_service_do_sign_out (EphySyncService *self)
 {
   g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
 
+  ephy_sync_service_unregister_client_id (self);
   ephy_sync_service_stop_periodical_sync (self);
   ephy_sync_service_destroy_session (self);
   ephy_sync_service_clear_storage_credentials (self);


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