[epiphany/wip/ephy-sync: 38/52] Don't keep six tokens in a GHashTable



commit 6f2b3831f73c5b151f2a2720179c4312a14a22a2
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Fri Jul 22 18:28:07 2016 +0300

    Don't keep six tokens in a GHashTable

 src/ephy-sync-secret.c  |   40 ++++++------
 src/ephy-sync-secret.h  |    6 +-
 src/ephy-sync-service.c |  150 ++++++++++++++++++++++++++++++++--------------
 src/ephy-sync-service.h |   29 ++++++---
 src/ephy-sync-utils.c   |   21 -------
 src/ephy-sync-utils.h   |   11 ----
 src/prefs-dialog.c      |    6 +-
 7 files changed, 149 insertions(+), 114 deletions(-)
---
diff --git a/src/ephy-sync-secret.c b/src/ephy-sync-secret.c
index 6aa6707..e52babc 100644
--- a/src/ephy-sync-secret.c
+++ b/src/ephy-sync-secret.c
@@ -91,10 +91,10 @@ ephy_sync_secret_load_tokens (EphySyncService *sync_service)
   GError *error = NULL;
   GList *matches;
   GList *tmp;
-  EphySyncTokenType token_type;
+  EphySyncServiceTokenType type;
   const gchar *emailUTF8;
-  const gchar *token_value;
-  const gchar *token_name;
+  const gchar *value;
+  const gchar *name;
   gchar *user_email;
 
   user_email = ephy_sync_service_get_user_email (sync_service);
@@ -113,18 +113,18 @@ ephy_sync_secret_load_tokens (EphySyncService *sync_service)
 
     attributes = secret_item_get_attributes (secret_item);
     emailUTF8 = g_hash_table_lookup (attributes, EMAIL_KEY);
-    token_type = g_ascii_strtoull (g_hash_table_lookup (attributes, TOKEN_TYPE_KEY),
-                                   NULL, 10);
-    token_name = g_hash_table_lookup (attributes, TOKEN_NAME_KEY);
+    type = g_ascii_strtoull (g_hash_table_lookup (attributes, TOKEN_TYPE_KEY),
+                             NULL, 10);
+    name = g_hash_table_lookup (attributes, TOKEN_NAME_KEY);
     secret_value = secret_item_get_secret (secret_item);
-    token_value = secret_value_get_text (secret_value);
+    value = secret_value_get_text (secret_value);
 
     /* Sanity check */
     if (g_strcmp0 (emailUTF8, user_email))
       continue;
 
-    ephy_sync_service_set_token (sync_service, g_strdup (token_value), token_type);
-LOG ("[%d] Loaded token %s with value %s for email %s", __LINE__, token_name, token_value, emailUTF8);
+    ephy_sync_service_set_token (sync_service, g_strdup (value), type);
+LOG ("[%d] Loaded token %s with value %s for email %s", __LINE__, name, value, emailUTF8);
 
     g_hash_table_unref (attributes);
   }
@@ -133,31 +133,31 @@ LOG ("[%d] Loaded token %s with value %s for email %s", __LINE__, token_name, to
 }
 
 void
-ephy_sync_secret_store_token (const gchar       *emailUTF8,
-                              gchar             *token_value,
-                              EphySyncTokenType  token_type)
+ephy_sync_secret_store_token (const gchar              *emailUTF8,
+                              gchar                    *value,
+                              EphySyncServiceTokenType  type)
 {
   SecretValue *secret_value;
   GHashTable *attributes;
-  const gchar *token_name;
+  const gchar *name;
   gchar *label;
 
   g_return_if_fail (emailUTF8);
-  g_return_if_fail (token_value);
+  g_return_if_fail (value);
 
-LOG ("[%d] Storing token %s with value %s for email %s ", __LINE__, ephy_sync_utils_token_name_from_type 
(token_type), token_value, emailUTF8);
+LOG ("[%d] Storing token %s with value %s for email %s ", __LINE__, ephy_sync_service_token_name_from_type 
(type), value, emailUTF8);
 
-  token_name = ephy_sync_utils_token_name_from_type (token_type);
-  secret_value = secret_value_new (token_value, -1, "text/plain");
+  name = ephy_sync_service_token_name_from_type (type);
+  secret_value = secret_value_new (value, -1, "text/plain");
   attributes = secret_attributes_build (EPHY_SYNC_TOKEN_SCHEMA,
                                         EMAIL_KEY, emailUTF8,
-                                        TOKEN_TYPE_KEY, token_type,
-                                        TOKEN_NAME_KEY, token_name,
+                                        TOKEN_TYPE_KEY, type,
+                                        TOKEN_NAME_KEY, name,
                                         NULL);
   /* Translators: The %s is the name of the token whose value is stored.
    * Example: quickStretchedPW or authPW
    */
-  label = g_strdup_printf (_("Token value for %s token"), token_name);
+  label = g_strdup_printf (_("Token value for %s token"), name);
 
   secret_service_store (NULL,
                         EPHY_SYNC_TOKEN_SCHEMA,
diff --git a/src/ephy-sync-secret.h b/src/ephy-sync-secret.h
index b5826b5..398afd1 100644
--- a/src/ephy-sync-secret.h
+++ b/src/ephy-sync-secret.h
@@ -38,9 +38,9 @@ void ephy_sync_secret_forget_all_tokens (void);
 
 void ephy_sync_secret_load_tokens       (EphySyncService *sync_service);
 
-void ephy_sync_secret_store_token       (const gchar       *emailUTF8,
-                                         gchar             *token_value,
-                                         EphySyncTokenType  token_type);
+void ephy_sync_secret_store_token       (const gchar              *emailUTF8,
+                                         gchar                    *value,
+                                         EphySyncServiceTokenType  type);
 
 G_END_DECLS
 
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index f36e164..caf6492 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -39,7 +39,12 @@ struct _EphySyncService {
 
   SoupSession *soup_session;
 
-  GHashTable *tokens;
+  gchar *uid;
+  gchar *sessionToken;
+  gchar *keyFetchToken;
+  gchar *unwrapBKey;
+  gchar *kA;
+  gchar *kB;
 
   gchar *user_email;
   gint64 last_auth_at;
@@ -271,7 +276,7 @@ check_certificate (EphySyncService *self,
   principal = json_object_get_object_member (object, "principal");
   email = json_object_get_string_member (principal, "email");
   uid_email = g_strdup_printf ("%s@%s",
-                               ephy_sync_service_get_token (self, EPHY_SYNC_TOKEN_UID),
+                               self->uid,
                                soup_uri_get_host (soup_uri_new (FXA_BASEURL)));
 
   if (g_str_equal (uid_email, email) == FALSE) {
@@ -308,7 +313,7 @@ query_token_server (EphySyncService *self,
 
   g_return_val_if_fail (assertion != NULL, FALSE);
 
-  kB = ephy_sync_crypto_decode_hex (ephy_sync_service_get_token (self, EPHY_SYNC_TOKEN_KB));
+  kB = ephy_sync_crypto_decode_hex (self->kB);
   hashed_kB = g_compute_checksum_for_data (G_CHECKSUM_SHA256, kB, EPHY_SYNC_TOKEN_LENGTH);
   client_state = g_strndup (hashed_kB, EPHY_SYNC_TOKEN_LENGTH);
   authorization = g_strdup_printf ("BrowserID %s", assertion);
@@ -383,12 +388,17 @@ ephy_sync_service_finalize (GObject *object)
 {
   EphySyncService *self = EPHY_SYNC_SERVICE (object);
 
+  g_free (self->uid);
+  g_free (self->sessionToken);
+  g_free (self->keyFetchToken);
+  g_free (self->unwrapBKey);
+  g_free (self->kA);
+  g_free (self->kB);
   g_free (self->user_email);
   g_free (self->certificate);
   g_free (self->storage_endpoint);
   g_free (self->token_server_id);
   g_free (self->token_server_key);
-  g_hash_table_destroy (self->tokens);
   g_clear_object (&self->soup_session);
   ephy_sync_crypto_rsa_key_pair_free (self->keypair);
 
@@ -408,8 +418,6 @@ ephy_sync_service_init (EphySyncService *self)
 {
   gchar *sync_user = NULL;
 
-  self->tokens = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                        NULL, g_free);
   self->soup_session = soup_session_new ();
 
   sync_user = g_settings_get_string (EPHY_SETTINGS_MAIN,
@@ -430,6 +438,27 @@ ephy_sync_service_new (void)
                                           NULL));
 }
 
+const gchar *
+ephy_sync_service_token_name_from_type (EphySyncServiceTokenType token_type)
+{
+  switch (token_type) {
+    case TOKEN_UID:
+      return "uid";
+    case TOKEN_SESSIONTOKEN:
+      return "sessionToken";
+    case TOKEN_KEYFETCHTOKEN:
+      return "keyFetchToken";
+    case TOKEN_UNWRAPBKEY:
+      return "unwrapBKey";
+    case TOKEN_KA:
+      return "kA";
+    case TOKEN_KB:
+      return "kB";
+  default:
+    g_assert_not_reached ();
+  }
+}
+
 gchar *
 ephy_sync_service_get_user_email (EphySyncService *self)
 {
@@ -445,52 +474,78 @@ ephy_sync_service_set_user_email (EphySyncService *self,
 }
 
 gchar *
-ephy_sync_service_get_token (EphySyncService   *self,
-                             EphySyncTokenType  token_type)
+ephy_sync_service_get_token (EphySyncService          *self,
+                             EphySyncServiceTokenType  type)
 {
-  const gchar *token_name;
-  gchar *token_value;
-
-  token_name = ephy_sync_utils_token_name_from_type (token_type);
-  token_value = (gchar *) g_hash_table_lookup (self->tokens, token_name);
-
-LOG ("[%d] Returning token %s with value %s", __LINE__, token_name, token_value);
-
-  return token_value;
+  switch (type) {
+    case TOKEN_UID:
+      return self->uid;
+    case TOKEN_SESSIONTOKEN:
+      return self->sessionToken;
+    case TOKEN_KEYFETCHTOKEN:
+      return self->keyFetchToken;
+    case TOKEN_UNWRAPBKEY:
+      return self->unwrapBKey;
+    case TOKEN_KA:
+      return self->kA;
+    case TOKEN_KB:
+      return self->kB;
+    default:
+      g_assert_not_reached ();
+  }
 }
 
 void
-ephy_sync_service_set_token (EphySyncService   *self,
-                             gchar             *token_value,
-                             EphySyncTokenType  token_type)
+ephy_sync_service_set_token (EphySyncService          *self,
+                             gchar                    *value,
+                             EphySyncServiceTokenType  type)
 {
-  const gchar *token_name;
-
-LOG ("[%d] token_type: %d", __LINE__, token_type);
-  token_name = ephy_sync_utils_token_name_from_type (token_type);
-  g_hash_table_insert (self->tokens,
-                       (gpointer) token_name,
-                       (gpointer) token_value);
-
-LOG ("[%d] Saved token %s with value %s", __LINE__, token_name, token_value);
+  switch (type) {
+    case TOKEN_UID:
+      g_free (self->uid);
+      self->uid = value;
+      break;
+    case TOKEN_SESSIONTOKEN:
+      g_free (self->sessionToken);
+      self->sessionToken = value;
+      break;
+    case TOKEN_KEYFETCHTOKEN:
+      g_free (self->keyFetchToken);
+      self->keyFetchToken = value;
+      break;
+    case TOKEN_UNWRAPBKEY:
+      g_free (self->unwrapBKey);
+      self->unwrapBKey = value;
+      break;
+    case TOKEN_KA:
+      g_free (self->kA);
+      self->kA = value;
+      break;
+    case TOKEN_KB:
+      g_free (self->kB);
+      self->kB = value;
+      break;
+    default:
+      g_assert_not_reached ();
+  }
 }
 
 void
-ephy_sync_service_set_and_store_tokens (EphySyncService   *self,
-                                        gchar             *token_value,
-                                        EphySyncTokenType  token_type,
+ephy_sync_service_set_and_store_tokens (EphySyncService          *self,
+                                        gchar                    *first_value,
+                                        EphySyncServiceTokenType  first_type,
                                         ...)
 {
-  EphySyncTokenType type;
+  EphySyncServiceTokenType type;
   gchar *value;
   va_list args;
 
-  ephy_sync_service_set_token (self, token_value, token_type);
-  ephy_sync_secret_store_token (self->user_email, token_value, token_type);
+  ephy_sync_service_set_token (self, first_value, first_type);
+  ephy_sync_secret_store_token (self->user_email, first_value, first_type);
 
-  va_start (args, token_type);
+  va_start (args, first_type);
   while ((value = va_arg (args, gchar *)) != NULL) {
-    type = va_arg (args, EphySyncTokenType);
+    type = va_arg (args, EphySyncServiceTokenType);
     ephy_sync_service_set_token (self, value, type);
     ephy_sync_secret_store_token (self->user_email, value, type);
   }
@@ -500,7 +555,12 @@ ephy_sync_service_set_and_store_tokens (EphySyncService   *self,
 void
 ephy_sync_service_delete_all_tokens (EphySyncService *self)
 {
-  g_hash_table_remove_all (self->tokens);
+  g_clear_pointer (&self->uid, g_free);
+  g_clear_pointer (&self->sessionToken, g_free);
+  g_clear_pointer (&self->keyFetchToken, g_free);
+  g_clear_pointer (&self->unwrapBKey, g_free);
+  g_clear_pointer (&self->kA, g_free);
+  g_clear_pointer (&self->kB, g_free);
 
 LOG ("[%d] Deleted all tokens", __LINE__);
 }
@@ -597,10 +657,10 @@ ephy_sync_service_fetch_sync_keys (EphySyncService *self,
   /* Everything is okay, save the tokens. */
   ephy_sync_service_set_user_email (self, email);
   ephy_sync_service_set_and_store_tokens (self,
-                                          g_strdup (keyFetchToken), EPHY_SYNC_TOKEN_KEYFETCHTOKEN,
-                                          g_strdup (unwrapBKey), EPHY_SYNC_TOKEN_UNWRAPBKEY,
-                                          ephy_sync_crypto_encode_hex (sync_keys->kA, 0), EPHY_SYNC_TOKEN_KA,
-                                          ephy_sync_crypto_encode_hex (sync_keys->kB, 0), EPHY_SYNC_TOKEN_KB,
+                                          g_strdup (keyFetchToken), TOKEN_KEYFETCHTOKEN,
+                                          g_strdup (unwrapBKey), TOKEN_UNWRAPBKEY,
+                                          ephy_sync_crypto_encode_hex (sync_keys->kA, 0), TOKEN_KA,
+                                          ephy_sync_crypto_encode_hex (sync_keys->kB, 0), TOKEN_KB,
                                           NULL);
   retval = TRUE;
 
@@ -624,7 +684,6 @@ ephy_sync_service_sign_certificate (EphySyncService *self)
   EphySyncCryptoRSAKeyPair *keypair;
   JsonNode *node;
   JsonObject *json;
-  gchar *sessionToken;
   gchar *tokenID;
   gchar *public_key_json;
   gchar *request_body;
@@ -634,13 +693,12 @@ ephy_sync_service_sign_certificate (EphySyncService *self)
   guint status_code;
   gboolean retval = FALSE;
 
-  sessionToken = ephy_sync_service_get_token (self, EPHY_SYNC_TOKEN_SESSIONTOKEN);
-  g_return_val_if_fail (sessionToken != NULL, FALSE);
+  g_return_val_if_fail (self->sessionToken != NULL, FALSE);
 
   keypair = ephy_sync_crypto_generate_rsa_key_pair ();
   g_return_val_if_fail (keypair != NULL, FALSE);
 
-  processed_st = ephy_sync_crypto_process_session_token (sessionToken);
+  processed_st = ephy_sync_crypto_process_session_token (self->sessionToken);
   tokenID = ephy_sync_crypto_encode_hex (processed_st->tokenID, 0);
 
   n_str = mpz_get_str (NULL, 10, keypair->public.n);
diff --git a/src/ephy-sync-service.h b/src/ephy-sync-service.h
index 680a3b5..94206e4 100644
--- a/src/ephy-sync-service.h
+++ b/src/ephy-sync-service.h
@@ -19,8 +19,6 @@
 #ifndef EPHY_SYNC_SERVICE_H
 #define EPHY_SYNC_SERVICE_H
 
-#include "ephy-sync-utils.h"
-
 #include <glib-object.h>
 
 G_BEGIN_DECLS
@@ -29,23 +27,34 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (EphySyncService, ephy_sync_service, EPHY, SYNC_SERVICE, GObject)
 
+typedef enum {
+  TOKEN_UID,
+  TOKEN_SESSIONTOKEN,
+  TOKEN_KEYFETCHTOKEN,
+  TOKEN_UNWRAPBKEY,
+  TOKEN_KA,
+  TOKEN_KB
+} EphySyncServiceTokenType;
+
 EphySyncService *ephy_sync_service_new                  (void);
 
+const gchar     *ephy_sync_service_token_name_from_type (EphySyncServiceTokenType token_type);
+
 gchar           *ephy_sync_service_get_user_email       (EphySyncService *self);
 
 void             ephy_sync_service_set_user_email       (EphySyncService *self,
                                                          const gchar     *email);
 
-gchar           *ephy_sync_service_get_token            (EphySyncService   *self,
-                                                         EphySyncTokenType  token_type);
+gchar           *ephy_sync_service_get_token            (EphySyncService          *self,
+                                                         EphySyncServiceTokenType  token_type);
 
-void             ephy_sync_service_set_token            (EphySyncService   *self,
-                                                         gchar             *token_value,
-                                                         EphySyncTokenType  token_type);
+void             ephy_sync_service_set_token            (EphySyncService          *self,
+                                                         gchar                    *token_value,
+                                                         EphySyncServiceTokenType  token_type);
 
-void             ephy_sync_service_set_and_store_tokens (EphySyncService   *self,
-                                                         gchar             *token_value,
-                                                         EphySyncTokenType  token_type,
+void             ephy_sync_service_set_and_store_tokens (EphySyncService          *self,
+                                                         gchar                    *token_value,
+                                                         EphySyncServiceTokenType  token_type,
                                                          ...) G_GNUC_NULL_TERMINATED;
 
 void             ephy_sync_service_delete_all_tokens    (EphySyncService *self);
diff --git a/src/ephy-sync-utils.c b/src/ephy-sync-utils.c
index 11e96c2..d235ec7 100644
--- a/src/ephy-sync-utils.c
+++ b/src/ephy-sync-utils.c
@@ -18,27 +18,6 @@
 
 #include "ephy-sync-utils.h"
 
-const gchar *
-ephy_sync_utils_token_name_from_type (EphySyncTokenType token_type)
-{
-  switch (token_type) {
-  case EPHY_SYNC_TOKEN_UID:
-    return "uid";
-  case EPHY_SYNC_TOKEN_SESSIONTOKEN:
-    return "sessionToken";
-  case EPHY_SYNC_TOKEN_KEYFETCHTOKEN:
-    return "keyFetchToken";
-  case EPHY_SYNC_TOKEN_UNWRAPBKEY:
-    return "unwrapBKey";
-  case EPHY_SYNC_TOKEN_KA:
-    return "kA";
-  case EPHY_SYNC_TOKEN_KB:
-    return "kB";
-  default:
-    g_assert_not_reached ();
-  }
-}
-
 gchar *
 ephy_sync_utils_build_json_string (const gchar *first_key,
                                    const gchar *first_value,
diff --git a/src/ephy-sync-utils.h b/src/ephy-sync-utils.h
index 3b8cf51..a7d057f 100644
--- a/src/ephy-sync-utils.h
+++ b/src/ephy-sync-utils.h
@@ -25,17 +25,6 @@ G_BEGIN_DECLS
 
 #define EPHY_SYNC_TOKEN_LENGTH 32
 
-typedef enum {
-  EPHY_SYNC_TOKEN_UID,
-  EPHY_SYNC_TOKEN_SESSIONTOKEN,
-  EPHY_SYNC_TOKEN_KEYFETCHTOKEN,
-  EPHY_SYNC_TOKEN_UNWRAPBKEY,
-  EPHY_SYNC_TOKEN_KA,
-  EPHY_SYNC_TOKEN_KB,
-} EphySyncTokenType;
-
-const gchar *ephy_sync_utils_token_name_from_type (EphySyncTokenType token_type);
-
 gchar       *ephy_sync_utils_build_json_string    (const gchar *first_key,
                                                    const gchar *first_value,
                                                    ...) G_GNUC_NULL_TERMINATED;
diff --git a/src/prefs-dialog.c b/src/prefs-dialog.c
index bdebc44..dbb988f 100644
--- a/src/prefs-dialog.c
+++ b/src/prefs-dialog.c
@@ -291,8 +291,8 @@ server_message_received_cb (WebKitUserContentManager *manager,
     /* Everything is okay, save the tokens. */
     g_settings_set_string (EPHY_SETTINGS_MAIN, EPHY_PREFS_SYNC_USER, email);
     ephy_sync_service_set_and_store_tokens (service,
-                                            g_strdup (uid), EPHY_SYNC_TOKEN_UID,
-                                            g_strdup (sessionToken), EPHY_SYNC_TOKEN_SESSIONTOKEN,
+                                            g_strdup (uid), TOKEN_UID,
+                                            g_strdup (sessionToken), TOKEN_SESSIONTOKEN,
                                             NULL);
 
     /* Translators: the %s refers to the email of the currently logged in user. */
@@ -350,7 +350,7 @@ on_sync_sign_out_button_clicked (GtkWidget   *button,
   gchar *sessionToken;
 
   service = ephy_shell_get_global_sync_service ();
-  sessionToken = ephy_sync_service_get_token (service, EPHY_SYNC_TOKEN_SESSIONTOKEN);
+  sessionToken = ephy_sync_service_get_token (service, TOKEN_SESSIONTOKEN);
 
   /* Destroy session and delete tokens. */
   ephy_sync_service_destroy_session (service, sessionToken);


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