[epiphany/wip/ephy-sync] Functions for Hawk post/get requests



commit 4dfde4a4c0ac941a219b2085dc5814dc5fae8551
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Sun Jun 26 22:36:46 2016 +0300

    Functions for Hawk post/get requests

 src/ephy-sync-service.c |  130 +++++++++++++++++++++++++++++++++++++----------
 src/ephy-sync-utils.c   |   36 +++++++++++++
 src/ephy-sync-utils.h   |    4 ++
 3 files changed, 142 insertions(+), 28 deletions(-)
---
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index a074281..a5651e4 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -28,7 +28,8 @@
 #include <libsoup/soup.h>
 #include <string.h>
 
-#define BASEURL     "https://api.accounts.firefox.com/v1";
+#define FXA_BASEURL   "https://api.accounts.firefox.com/";
+#define FXA_VERSION   "v1/"
 
 struct _EphySyncService {
   GObject parent_instance;
@@ -42,6 +43,100 @@ struct _EphySyncService {
 
 G_DEFINE_TYPE (EphySyncService, ephy_sync_service, G_TYPE_OBJECT);
 
+static SoupMessage *
+synchronous_hawk_get_request (EphySyncService *self,
+                              const gchar     *endpoint,
+                              const gchar     *id,
+                              guint8          *key,
+                              gsize            key_length)
+{
+  EphySyncCryptoHawkHeader *hawk_header;
+  SoupMessage *message;
+  gchar *url;
+
+  url = g_strdup_printf ("%s%s%s", FXA_BASEURL, FXA_VERSION, endpoint);
+  message = soup_message_new (SOUP_METHOD_GET, url);
+  hawk_header = ephy_sync_crypto_compute_hawk_header (url, "GET",
+                                                      id,
+                                                      key, key_length,
+                                                      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);
+
+  g_free (url);
+  ephy_sync_crypto_hawk_header_free (hawk_header);
+
+  return message;
+}
+
+static SoupMessage *
+synchronous_hawk_post_request (EphySyncService *self,
+                               const gchar     *endpoint,
+                               const gchar     *id,
+                               guint8          *key,
+                               gsize            key_length,
+                               gchar           *request_body)
+{
+  EphySyncCryptoHawkHeader *hawk_header;
+  EphySyncCryptoHawkOptions *hawk_options;
+  SoupMessage *message;
+  gchar *url;
+
+  url = g_strdup_printf ("%s%s%s", FXA_BASEURL, FXA_VERSION, endpoint);
+  message = soup_message_new (SOUP_METHOD_POST, url);
+  soup_message_set_request (message,
+                            "application/json",
+                            SOUP_MEMORY_TAKE,
+                            request_body,
+                            strlen (request_body));
+  hawk_options = ephy_sync_crypto_hawk_options_new (NULL, NULL, NULL,
+                                                    g_strdup ("application/json"),
+                                                    NULL, NULL, NULL,
+                                                    g_strdup (request_body),
+                                                    NULL);
+  hawk_header = ephy_sync_crypto_compute_hawk_header (url, "POST",
+                                                      id,
+                                                      key, key_length,
+                                                      hawk_options);
+  soup_message_headers_append (message->request_headers,
+                               "authorization", hawk_header->header);
+  soup_message_headers_append (message->request_headers,
+                               "content-type", "application/json");
+LOG ("[%d] Sending synchronous HAWK POST request to %s endpoint", __LINE__, endpoint);
+  soup_session_send_message (self->soup_session, message);
+
+  g_free (url);
+  ephy_sync_crypto_hawk_options_free (hawk_options);
+  ephy_sync_crypto_hawk_header_free (hawk_header);
+
+  return message;
+}
+
+static SoupMessage *
+synchronous_fxa_post_request (EphySyncService *self,
+                              const gchar     *endpoint,
+                              gchar           *request_body)
+{
+  SoupMessage *message;
+  gchar *url;
+
+  url = g_strdup_printf ("%s%s%s", FXA_BASEURL, FXA_VERSION, endpoint);
+  message = soup_message_new (SOUP_METHOD_POST, url);
+  soup_message_set_request (message,
+                            "application/json",
+                            SOUP_MEMORY_TAKE,
+                            request_body,
+                            strlen (request_body));
+LOG ("[%d] Sending synchronous POST request to %s endpoint", __LINE__, endpoint);
+  soup_session_send_message (self->soup_session, message);
+
+  g_free (url);
+
+  return message;
+}
+
 static void
 ephy_sync_service_finalize (GObject *object)
 {
@@ -82,26 +177,6 @@ ephy_sync_service_init (EphySyncService *self)
 LOG ("[%d] sync service inited", __LINE__);
 }
 
-static SoupMessage *
-synchronous_post_request (EphySyncService *self,
-                          const gchar     *endpoint,
-                          gchar           *request_body)
-{
-  SoupMessage *message;
-
-  message = soup_message_new (SOUP_METHOD_POST,
-                              g_strdup_printf ("%s/%s", BASEURL, endpoint));
-  soup_message_set_request (message,
-                            "application/json",
-                            SOUP_MEMORY_TAKE,
-                            request_body,
-                            strlen (request_body));
-LOG ("[%d] Sending synchronous POST request to %s endpoint", __LINE__, endpoint);
-  soup_session_send_message (self->soup_session, message);
-
-  return message;
-}
-
 EphySyncService *
 ephy_sync_service_new (void)
 {
@@ -194,13 +269,12 @@ ephy_sync_service_login (EphySyncService  *self,
   authPW = ephy_sync_service_get_token (self, EPHY_SYNC_TOKEN_AUTHPW);
   g_return_val_if_fail (authPW, FALSE);
 
-  request_body = g_strconcat ("{\"authPW\": \"", authPW,
-                              "\", \"email\": \"", self->user_email,
-                              "\"}", NULL);
-
-  message = synchronous_post_request (self,
-                                      "account/login?keys=true",
-                                      request_body);
+  request_body = ephy_sync_utils_build_json_string ("authPW", authPW,
+                                                    "email", self->user_email,
+                                                    NULL);
+  message = synchronous_fxa_post_request (self,
+                                          "account/login?keys=true",
+                                          request_body);
 LOG ("[%d] status code: %u", __LINE__, message->status_code);
 
   parser = json_parser_new ();
diff --git a/src/ephy-sync-utils.c b/src/ephy-sync-utils.c
index 68d2f36..c307b0b 100644
--- a/src/ephy-sync-utils.c
+++ b/src/ephy-sync-utils.c
@@ -93,6 +93,42 @@ 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,
+                                   ...)
+{
+  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;
+}
+
 /* FIXME: Only for debugging, remove when no longer needed */
 void
 ephy_sync_utils_display_hex (const gchar *data_name,
diff --git a/src/ephy-sync-utils.h b/src/ephy-sync-utils.h
index d3eeae6..9e33850 100644
--- a/src/ephy-sync-utils.h
+++ b/src/ephy-sync-utils.h
@@ -46,6 +46,10 @@ guint8      *ephy_sync_utils_decode_hex           (const gchar *hex_string);
 
 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;
+
 /* FIXME: Only for debugging, remove when no longer needed */
 void         ephy_sync_utils_display_hex          (const gchar *data_name,
                                                    guint8      *data,


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