[epiphany/wip/ephy-sync: 18/48] ephy-sync: Do some things in a different way
- From: Gabriel - Cristian Ivascu <gabrielivascu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/wip/ephy-sync: 18/48] ephy-sync: Do some things in a different way
- Date: Sat, 20 Aug 2016 17:04:52 +0000 (UTC)
commit bccc9976af5c4754b11e63c9d6294d37d7758a0f
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date: Fri Jul 22 13:04:30 2016 +0300
ephy-sync: Do some things in a different way
src/Makefile.am | 2 -
src/ephy-sync-crypto.c | 66 ++++++++--
src/ephy-sync-crypto.h | 7 +
src/ephy-sync-secret.c | 50 +++-----
src/ephy-sync-secret.h | 6 +-
src/ephy-sync-service.c | 326 ++++++++++++++++++++++++++++++----------------
src/ephy-sync-service.h | 29 +++--
src/ephy-sync-utils.c | 139 --------------------
src/ephy-sync-utils.h | 57 --------
src/prefs-dialog.c | 6 +-
10 files changed, 321 insertions(+), 367 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index c42111e..2f92f9f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -75,8 +75,6 @@ libephymain_la_SOURCES = \
ephy-sync-secret.h \
ephy-sync-service.c \
ephy-sync-service.h \
- ephy-sync-utils.c \
- ephy-sync-utils.h \
ephy-title-box.c \
ephy-title-box.h \
ephy-toolbar.c \
diff --git a/src/ephy-sync-crypto.c b/src/ephy-sync-crypto.c
index 83bb6bb..ce9d87c 100644
--- a/src/ephy-sync-crypto.c
+++ b/src/ephy-sync-crypto.c
@@ -17,8 +17,8 @@
*/
#include "ephy-sync-crypto.h"
-#include "ephy-sync-utils.h"
+#include <glib/gstdio.h>
#include <libsoup/soup.h>
#include <nettle/hmac.h>
#include <nettle/sha2.h>
@@ -26,6 +26,8 @@
#define HAWK_VERSION 1
+static const gchar hex_digits[] = "0123456789abcdef";
+
EphySyncCryptoHawkOptions *
ephy_sync_crypto_hawk_options_new (gchar *app,
gchar *dlg,
@@ -252,6 +254,12 @@ ephy_sync_crypto_rsa_key_pair_free (EphySyncCryptoRSAKeyPair *keypair)
}
static gchar *
+kw (const gchar *name)
+{
+ return g_strconcat ("identity.mozilla.com/picl/v1/", name, NULL);
+}
+
+static gchar *
base64_urlsafe_strip (guint8 *data,
gsize data_length)
{
@@ -304,8 +312,8 @@ are_equal (guint8 *a,
gchar *b_hex;
gboolean retval;
- a_hex = ephy_sync_utils_encode_hex (a, 0);
- b_hex = ephy_sync_utils_encode_hex (b, 0);
+ a_hex = ephy_sync_crypto_encode_hex (a, 0);
+ b_hex = ephy_sync_crypto_encode_hex (b, 0);
retval = g_str_equal (a_hex, b_hex);
g_free (a_hex);
@@ -602,9 +610,9 @@ ephy_sync_crypto_process_key_fetch_token (const gchar *keyFetchToken)
gchar *info_kft;
gchar *info_keys;
- kft = ephy_sync_utils_decode_hex (keyFetchToken);
- info_kft = ephy_sync_utils_kw ("keyFetchToken");
- info_keys = ephy_sync_utils_kw ("account/keys");
+ kft = ephy_sync_crypto_decode_hex (keyFetchToken);
+ info_kft = kw ("keyFetchToken");
+ info_keys = kw ("account/keys");
out1 = g_malloc (3 * EPHY_SYNC_TOKEN_LENGTH);
out2 = g_malloc (3 * EPHY_SYNC_TOKEN_LENGTH);
@@ -653,8 +661,8 @@ ephy_sync_crypto_process_session_token (const gchar *sessionToken)
guint8 *requestKey;
gchar *info;
- st = ephy_sync_utils_decode_hex (sessionToken);
- info = ephy_sync_utils_kw ("sessionToken");
+ st = ephy_sync_crypto_decode_hex (sessionToken);
+ info = kw ("sessionToken");
out = g_malloc (3 * EPHY_SYNC_TOKEN_LENGTH);
hkdf (st, EPHY_SYNC_TOKEN_LENGTH,
@@ -694,7 +702,7 @@ ephy_sync_crypto_retrieve_sync_keys (const gchar *bundle,
guint8 *kB;
EphySyncCryptoSyncKeys *retval = NULL;
- bdl = ephy_sync_utils_decode_hex (bundle);
+ bdl = ephy_sync_crypto_decode_hex (bundle);
ciphertext = g_malloc (2 * EPHY_SYNC_TOKEN_LENGTH);
respMAC = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
wrapKB = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
@@ -918,3 +926,43 @@ out:
return assertion;
}
+
+gchar *
+ephy_sync_crypto_encode_hex (guint8 *data,
+ gsize data_length)
+{
+ gchar *retval;
+ gsize length;
+
+ length = data_length == 0 ? EPHY_SYNC_TOKEN_LENGTH : data_length;
+ retval = g_malloc (length * 2 + 1);
+
+ for (gsize i = 0; i < length; i++) {
+ guint8 byte = data[i];
+
+ retval[2 * i] = hex_digits[byte >> 4];
+ retval[2 * i + 1] = hex_digits[byte & 0xf];
+ }
+
+ retval[length * 2] = 0;
+
+ return retval;
+}
+
+guint8 *
+ephy_sync_crypto_decode_hex (const gchar *hex_string)
+{
+ guint8 *retval;
+ gsize hex_length;
+
+ hex_length = strlen (hex_string);
+ g_return_val_if_fail (hex_length % 2 == 0, NULL);
+
+ retval = g_malloc (hex_length / 2);
+
+ for (gsize i = 0, j = 0; i < hex_length; i += 2, j++) {
+ sscanf(hex_string + i, "%2hhx", retval + j);
+ }
+
+ return retval;
+}
\ No newline at end of file
diff --git a/src/ephy-sync-crypto.h b/src/ephy-sync-crypto.h
index a498415..f9a6580 100644
--- a/src/ephy-sync-crypto.h
+++ b/src/ephy-sync-crypto.h
@@ -24,6 +24,8 @@
G_BEGIN_DECLS
+#define EPHY_SYNC_TOKEN_LENGTH 32
+
typedef struct {
gchar *app;
gchar *dlg;
@@ -123,6 +125,11 @@ gchar *ephy_sync_crypto_create_assertion (const gcha
guint64 duration,
EphySyncCryptoRSAKeyPair *keypair);
+gchar *ephy_sync_crypto_encode_hex (guint8 *data,
+ gsize data_length);
+
+guint8 *ephy_sync_crypto_decode_hex (const gchar *hex_string);
+
G_END_DECLS
#endif
diff --git a/src/ephy-sync-secret.c b/src/ephy-sync-secret.c
index 6aa6707..4fe6829 100644
--- a/src/ephy-sync-secret.c
+++ b/src/ephy-sync-secret.c
@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "ephy-debug.h"
#include "ephy-sync-secret.h"
#include <glib/gi18n.h>
@@ -45,10 +44,8 @@ forget_all_tokens_cb (SecretService *service,
secret_service_clear_finish (service, result, &error);
- if (error)
-LOG ("[%d] Error clearing token secret schema: %s", __LINE__, error->message);
- else
-LOG ("[%d] Successfully cleared token secret schema", __LINE__);
+ if (error != NULL)
+ g_warning ("Failed to clear token secret schema: %s", error->message);
}
static void
@@ -60,10 +57,8 @@ store_token_cb (SecretService *service,
secret_service_store_finish (service, result, &error);
- if (error)
-LOG ("[%d] Error storing token in secret schema: %s", __LINE__, error->message);
- else
-LOG ("[%d] Successfully stored token in secret schema", __LINE__);
+ if (error != NULL)
+ g_warning ("Failed to store token in secret schema: %s", error->message);
}
void
@@ -91,10 +86,9 @@ 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;
gchar *user_email;
user_email = ephy_sync_service_get_user_email (sync_service);
@@ -113,18 +107,16 @@ 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);
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);
g_hash_table_unref (attributes);
}
@@ -133,31 +125,29 @@ 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);
-
- 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 eaab204..e3523b8 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -22,7 +22,6 @@
#include "ephy-sync-crypto.h"
#include "ephy-sync-secret.h"
#include "ephy-sync-service.h"
-#include "ephy-sync-utils.h"
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>
@@ -38,9 +37,13 @@ struct _EphySyncService {
GObject parent_instance;
SoupSession *soup_session;
- JsonParser *parser;
- GHashTable *tokens;
+ gchar *uid;
+ gchar *sessionToken;
+ gchar *keyFetchToken;
+ gchar *unwrapBKey;
+ gchar *kA;
+ gchar *kB;
gchar *user_email;
gint64 last_auth_at;
@@ -55,6 +58,11 @@ struct _EphySyncService {
G_DEFINE_TYPE (EphySyncService, ephy_sync_service, G_TYPE_OBJECT);
+static gchar *
+build_json_string (const gchar *first_key,
+ const gchar *first_value,
+ ...) G_GNUC_NULL_TERMINATED;
+
static guint
synchronous_hawk_post_request (EphySyncService *self,
const gchar *endpoint,
@@ -62,12 +70,12 @@ synchronous_hawk_post_request (EphySyncService *self,
guint8 *key,
gsize key_length,
gchar *request_body,
- JsonObject **jobject)
+ JsonNode **node)
{
EphySyncCryptoHawkOptions *hawk_options;
EphySyncCryptoHawkHeader *hawk_header;
SoupMessage *message;
- JsonNode *root;
+ JsonParser *parser;
gchar *url;
const gchar *content_type = "application/json";
@@ -92,13 +100,13 @@ synchronous_hawk_post_request (EphySyncService *self,
"content-type", content_type);
soup_session_send_message (self->soup_session, message);
- if (jobject != NULL) {
- json_parser_load_from_data (self->parser,
+ if (node != NULL) {
+ parser = json_parser_new ();
+ json_parser_load_from_data (parser,
message->response_body->data,
-1, NULL);
- root = json_parser_get_root (self->parser);
- g_assert (JSON_NODE_HOLDS_OBJECT (root));
- *jobject = json_node_get_object (root);
+ *node = json_node_copy (json_parser_get_root (parser));
+ g_object_unref (parser);
}
g_free (url);
@@ -114,11 +122,11 @@ synchronous_hawk_get_request (EphySyncService *self,
const gchar *id,
guint8 *key,
gsize key_length,
- JsonObject **jobject)
+ JsonNode **node)
{
EphySyncCryptoHawkHeader *hawk_header;
SoupMessage *message;
- JsonNode *root;
+ JsonParser *parser;
gchar *url;
url = g_strdup_printf ("%s%s%s", FXA_BASEURL, FXA_VERSION, endpoint);
@@ -129,17 +137,15 @@ synchronous_hawk_get_request (EphySyncService *self,
NULL);
soup_message_headers_append (message->request_headers,
"authorization", hawk_header->header);
-LOG ("[%d] Sending synchronous HAWK GET request to %s endpoint", __LINE__, endpoint);
soup_session_send_message (self->soup_session, message);
-LOG ("[%d] Got response from server: %u", __LINE__, message->status_code);
- if (jobject != NULL) {
- json_parser_load_from_data (self->parser,
+ if (node != NULL) {
+ parser = json_parser_new ();
+ json_parser_load_from_data (parser,
message->response_body->data,
-1, NULL);
- root = json_parser_get_root (self->parser);
- g_assert (JSON_NODE_HOLDS_OBJECT (root));
- *jobject = json_node_get_object (root);
+ *node = json_node_copy (json_parser_get_root (parser));
+ g_object_unref (parser);
}
g_free (url);
@@ -155,7 +161,7 @@ session_destroyed_cb (SoupSession *session,
{
JsonParser *parser;
JsonNode *root;
- JsonObject *object;
+ JsonObject *json;
if (message->status_code == STATUS_OK) {
LOG ("Session destroyed");
@@ -165,16 +171,52 @@ session_destroyed_cb (SoupSession *session,
parser = json_parser_new ();
json_parser_load_from_data (parser, message->response_body->data, -1, NULL);
root = json_parser_get_root (parser);
- object = json_node_get_object (root);
+ json = json_node_get_object (root);
g_warning ("Failed to destroy session: errno: %ld, errmsg: %s",
- json_object_get_int_member (object, "errno"),
- json_object_get_string_member (object, "message"));
+ json_object_get_int_member (json, "errno"),
+ json_object_get_string_member (json, "message"));
g_object_unref (parser);
}
static gchar *
+build_json_string (const gchar *first_key,
+ const gchar *first_value,
+ ...)
+{
+ va_list args;
+ gchar *json;
+ gchar *key;
+ gchar *value;
+ gchar *tmp;
+
+ json = g_strconcat ("{\"",
+ first_key, "\": \"",
+ first_value, "\"",
+ NULL);
+
+ va_start (args, first_value);
+ while ((key = va_arg (args, gchar *)) != NULL) {
+ value = va_arg (args, gchar *);
+
+ tmp = json;
+ json = g_strconcat (json, ", \"",
+ key, "\": \"",
+ value, "\"",
+ NULL);
+ g_free (tmp);
+ }
+ va_end (args);
+
+ tmp = json;
+ json = g_strconcat (json, "}", NULL);
+ g_free (tmp);
+
+ return json;
+}
+
+static gchar *
get_audience_for_url (const gchar *url)
{
SoupURI *uri;
@@ -237,7 +279,7 @@ check_certificate (EphySyncService *self,
{
JsonParser *parser;
JsonNode *root;
- JsonObject *object;
+ JsonObject *json;
JsonObject *principal;
gchar **pieces;
gchar *header;
@@ -258,8 +300,8 @@ check_certificate (EphySyncService *self,
parser = json_parser_new ();
json_parser_load_from_data (parser, header, -1, NULL);
root = json_parser_get_root (parser);
- object = json_node_get_object (root);
- algorithm = json_object_get_string_member (object, "alg");
+ json = json_node_get_object (root);
+ algorithm = json_object_get_string_member (json, "alg");
if (g_str_equal (algorithm, "RS256") == FALSE) {
g_warning ("Expected algorithm RS256, found %s. Giving up.", algorithm);
@@ -268,11 +310,11 @@ check_certificate (EphySyncService *self,
json_parser_load_from_data (parser, payload, -1, NULL);
root = json_parser_get_root (parser);
- object = json_node_get_object (root);
- principal = json_object_get_object_member (object, "principal");
+ json = json_node_get_object (root);
+ principal = json_object_get_object_member (json, "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) {
@@ -280,7 +322,7 @@ check_certificate (EphySyncService *self,
goto out;
}
- self->last_auth_at = json_object_get_int_member (object, "fxa-lastAuthAt");
+ self->last_auth_at = json_object_get_int_member (json, "fxa-lastAuthAt");
retval = TRUE;
out:
@@ -300,7 +342,7 @@ query_token_server (EphySyncService *self,
SoupMessage *message;
JsonParser *parser;
JsonNode *root;
- JsonObject *object;
+ JsonObject *json;
guint8 *kB;
gchar *hashed_kB;
gchar *client_state;
@@ -309,7 +351,7 @@ query_token_server (EphySyncService *self,
g_return_val_if_fail (assertion != NULL, FALSE);
- kB = ephy_sync_utils_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);
@@ -328,12 +370,12 @@ query_token_server (EphySyncService *self,
message->response_body->data,
-1, NULL);
root = json_parser_get_root (parser);
- object = json_node_get_object (root);
+ json = json_node_get_object (root);
if (message->status_code == STATUS_OK) {
- self->storage_endpoint = g_strdup (json_object_get_string_member (object, "api_endpoint"));
- self->token_server_id = g_strdup (json_object_get_string_member (object, "id"));
- self->token_server_key = g_strdup (json_object_get_string_member (object, "key"));
+ self->storage_endpoint = g_strdup (json_object_get_string_member (json, "api_endpoint"));
+ self->token_server_id = g_strdup (json_object_get_string_member (json, "id"));
+ self->token_server_key = g_strdup (json_object_get_string_member (json, "key"));
} else if (message->status_code == 400) {
g_warning ("Failed to talk to the Token Server: malformed request");
goto out;
@@ -344,8 +386,8 @@ query_token_server (EphySyncService *self,
const gchar *status;
const gchar *description;
- status = json_object_get_string_member (object, "status");
- array = json_object_get_array_member (object, "errors");
+ status = json_object_get_string_member (json, "status");
+ array = json_object_get_array_member (json, "errors");
node = json_array_get_element (array, 0);
errors = json_node_get_object (node);
description = json_object_get_string_member (errors, "description");
@@ -384,25 +426,34 @@ ephy_sync_service_finalize (GObject *object)
{
EphySyncService *self = EPHY_SYNC_SERVICE (object);
- 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);
- g_clear_object (&self->parser);
ephy_sync_crypto_rsa_key_pair_free (self->keypair);
G_OBJECT_CLASS (ephy_sync_service_parent_class)->finalize (object);
}
static void
+ephy_sync_service_dispose (GObject *object)
+{
+ EphySyncService *self = EPHY_SYNC_SERVICE (object);
+
+ g_clear_object (&self->soup_session);
+ g_clear_pointer (&self->user_email, g_free);
+ g_clear_pointer (&self->certificate, g_free);
+ g_clear_pointer (&self->storage_endpoint, g_free);
+ g_clear_pointer (&self->token_server_id, g_free);
+ g_clear_pointer (&self->token_server_key, g_free);
+ ephy_sync_service_delete_all_tokens (self);
+
+ G_OBJECT_CLASS (ephy_sync_service_parent_class)->dispose (object);
+}
+
+static void
ephy_sync_service_class_init (EphySyncServiceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ephy_sync_service_finalize;
+ object_class->dispose = ephy_sync_service_dispose;
}
static void
@@ -410,10 +461,7 @@ 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 ();
- self->parser = json_parser_new ();
sync_user = g_settings_get_string (EPHY_SETTINGS_MAIN,
EPHY_PREFS_SYNC_USER);
@@ -422,8 +470,6 @@ ephy_sync_service_init (EphySyncService *self)
ephy_sync_service_set_user_email (self, sync_user);
ephy_sync_secret_load_tokens (self);
}
-
-LOG ("[%d] sync service inited", __LINE__);
}
EphySyncService *
@@ -433,6 +479,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)
{
@@ -448,52 +515,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);
}
@@ -503,9 +596,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);
-
-LOG ("[%d] Deleted all tokens", __LINE__);
+ 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);
}
void
@@ -526,7 +622,7 @@ ephy_sync_service_destroy_session (EphySyncService *self,
url = g_strdup_printf ("%s%s%s", FXA_BASEURL, FXA_VERSION, endpoint);
processed_st = ephy_sync_crypto_process_session_token (sessionToken);
- tokenID = ephy_sync_utils_encode_hex (processed_st->tokenID, 0);
+ tokenID = ephy_sync_crypto_encode_hex (processed_st->tokenID, 0);
message = soup_message_new (SOUP_METHOD_POST, url);
soup_message_set_request (message, content_type,
@@ -564,29 +660,32 @@ ephy_sync_service_fetch_sync_keys (EphySyncService *self,
{
EphySyncCryptoProcessedKFT *processed_kft = NULL;
EphySyncCryptoSyncKeys *sync_keys = NULL;
- JsonObject *jobject;
+ JsonNode *node;
+ JsonObject *json;
guint8 *unwrapKB;
gchar *tokenID;
guint status_code;
gboolean retval = FALSE;
- unwrapKB = ephy_sync_utils_decode_hex (unwrapBKey);
+ unwrapKB = ephy_sync_crypto_decode_hex (unwrapBKey);
processed_kft = ephy_sync_crypto_process_key_fetch_token (keyFetchToken);
- tokenID = ephy_sync_utils_encode_hex (processed_kft->tokenID, 0);
+ tokenID = ephy_sync_crypto_encode_hex (processed_kft->tokenID, 0);
status_code = synchronous_hawk_get_request (self,
"account/keys",
tokenID,
processed_kft->reqHMACkey,
EPHY_SYNC_TOKEN_LENGTH,
- &jobject);
+ &node);
+ json = json_node_get_object (node);
+
if (status_code != STATUS_OK) {
g_warning ("FxA server errno: %ld, errmsg: %s",
- json_object_get_int_member (jobject, "errno"),
- json_object_get_string_member (jobject, "message"));
+ json_object_get_int_member (json, "errno"),
+ json_object_get_string_member (json, "message"));
goto out;
}
- sync_keys = ephy_sync_crypto_retrieve_sync_keys (json_object_get_string_member (jobject, "bundle"),
+ sync_keys = ephy_sync_crypto_retrieve_sync_keys (json_object_get_string_member (json, "bundle"),
processed_kft->respHMACkey,
processed_kft->respXORkey,
unwrapKB);
@@ -597,19 +696,17 @@ 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_utils_encode_hex (sync_keys->kA, 0), EPHY_SYNC_TOKEN_KA,
- ephy_sync_utils_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;
-LOG ("kA: %s", ephy_sync_utils_encode_hex (sync_keys->kA, 0));
-LOG ("kB: %s", ephy_sync_utils_encode_hex (sync_keys->kB, 0));
-
out:
ephy_sync_crypto_processed_kft_free (processed_kft);
ephy_sync_crypto_sync_keys_free (sync_keys);
+ json_node_free (node);
g_free (tokenID);
g_free (unwrapKB);
@@ -621,8 +718,8 @@ ephy_sync_service_sign_certificate (EphySyncService *self)
{
EphySyncCryptoProcessedST *processed_st;
EphySyncCryptoRSAKeyPair *keypair;
- JsonObject *jobject;
- gchar *sessionToken;
+ JsonNode *node;
+ JsonObject *json;
gchar *tokenID;
gchar *public_key_json;
gchar *request_body;
@@ -632,21 +729,20 @@ 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);
- tokenID = ephy_sync_utils_encode_hex (processed_st->tokenID, 0);
+ 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);
e_str = mpz_get_str (NULL, 10, keypair->public.e);
- public_key_json = ephy_sync_utils_build_json_string ("algorithm", "RS",
- "n", n_str,
- "e", e_str,
- NULL);
+ public_key_json = build_json_string ("algorithm", "RS",
+ "n", n_str,
+ "e", e_str,
+ NULL);
/* The server allows a maximum certificate lifespan of 24 hours == 86400000 ms */
request_body = g_strdup_printf ("{\"publicKey\": %s, \"duration\": 86400000}",
public_key_json);
@@ -656,17 +752,18 @@ ephy_sync_service_sign_certificate (EphySyncService *self)
processed_st->reqHMACkey,
EPHY_SYNC_TOKEN_LENGTH,
request_body,
- &jobject);
+ &node);
+ json = json_node_get_object (node);
if (status_code != STATUS_OK) {
g_warning ("FxA server errno: %ld, errmsg: %s",
- json_object_get_int_member (jobject, "errno"),
- json_object_get_string_member (jobject, "message"));
+ json_object_get_int_member (json, "errno"),
+ json_object_get_string_member (json, "message"));
goto out;
}
/* Check if the certificate is valid */
- certificate = json_object_get_string_member (jobject, "cert");
+ certificate = json_object_get_string_member (json, "cert");
if (check_certificate (self, certificate) == FALSE) {
ephy_sync_crypto_rsa_key_pair_free (keypair);
goto out;
@@ -678,6 +775,7 @@ ephy_sync_service_sign_certificate (EphySyncService *self)
out:
ephy_sync_crypto_processed_st_free (processed_st);
+ json_node_free (node);
g_free (tokenID);
g_free (public_key_json);
g_free (request_body);
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/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]