[epiphany/wip/ephy-sync: 27/126] Test method for account/login endpoint



commit 5d6e3af677aa0dd4d4eb4fba2713876fdd9a5ee3
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Sun Jun 5 00:23:05 2016 +0300

    Test method for account/login endpoint

 configure.ac            |    2 +
 src/ephy-sync-service.c |   88 +++++++++++++++++++++++++++++++++++++++++++++--
 src/ephy-sync-service.h |    6 +++
 src/ephy-sync-window.c  |   26 ++++++++++++--
 4 files changed, 116 insertions(+), 6 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 63ab1ff..40c22ab 100644
--- a/configure.ac
+++ b/configure.ac
@@ -83,6 +83,7 @@ GCR_REQUIRED=3.5.5
 AVAHI_REQUIRED=0.6.22
 GVDB_REQUIRED="glib-2.0 >= $GLIB_REQUIRED"
 NETTLE_REQUIRED=3.2
+JSON_GLIB_MIN_VERSION=0.14
 
 # Tests
 
@@ -116,6 +117,7 @@ PKG_CHECK_MODULES([DEPENDENCIES], [
                  avahi-gobject >= $AVAHI_REQUIRED
                  avahi-client >= $AVAHI_REQUIRED
                  nettle >= NETTLE_REQUIRED
+                 json-glib-1.0 >= $JSON_GLIB_MIN_VERSION
                  ])
 
 # Check requirements for gvdb
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index 54afaea..a1123ab 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -2,6 +2,8 @@
 
 #include <string.h>
 #include <glib/gstdio.h>
+#include <json-glib/json-glib.h>
+#include <libsoup/soup.h>
 #include <nettle/hmac.h>
 #include <nettle/pbkdf2.h>
 
@@ -11,6 +13,8 @@ struct _EphySyncService {
 
 G_DEFINE_TYPE (EphySyncService, ephy_sync_service, G_TYPE_OBJECT);
 
+static const gchar hex_digits[] = "0123456789abcdef";
+
 static gchar *
 KW (const gchar *name)
 {
@@ -21,7 +25,8 @@ KW (const gchar *name)
 }
 
 static gchar *
-KWE (const gchar *name, const gchar *emailUTF8)
+KWE (const gchar *name,
+     const gchar *emailUTF8)
 {
   g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
   return g_strconcat ("identity.mozilla.com/picl/v1/",
@@ -31,6 +36,24 @@ KWE (const gchar *name, const gchar *emailUTF8)
                       NULL);
 }
 
+static gchar *
+encode_hex (guint8 *data,
+            gsize   data_len)
+{
+  gchar *retval = g_malloc (data_len * 2 + 1);
+
+  for (gsize i = 0; i < data_len; i++) {
+    guint8 byte = data[i];
+
+    retval[2 * i] = hex_digits[byte >> 4];
+    retval[2 * i + 1] = hex_digits[byte & 0xf];
+  }
+
+  retval[data_len * 2] = 0;
+
+  return retval;
+}
+
 /*
  * Runs 1000 PBKDF2 iterations using sha256 as hash function.
  */
@@ -114,6 +137,21 @@ ephy_sync_service_init (EphySyncService *self)
   g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
 }
 
+static void
+server_response_cb (SoupSession *session,
+                    SoupMessage *message,
+                    gpointer user_data)
+{
+  g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
+
+  if (message->status_code == 200) {
+    g_printf ("response body: %s\n", message->response_body->data);
+    // TODO: parse response data using JsonParser
+  } else {
+    g_printerr ("Error response from server: [%u] %s\n", message->status_code, message->reason_phrase);
+  }
+}
+
 EphySyncService *
 ephy_sync_service_new (void)
 {
@@ -124,6 +162,48 @@ ephy_sync_service_new (void)
 }
 
 void
+ephy_sync_service_try_login (EphySyncService *self,
+                             gboolean login_with_keys,
+                             const gchar *emailUTF8,
+                             guint8 *authPW,
+                             guint8 *sessionToken,
+                             guint8 *keyFetchToken)
+{
+  SoupSession *session;
+  SoupMessage *message;
+  char *request_body;
+  char *authPW_hex;
+
+  g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
+  g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
+
+  session = soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
+                                           "test-json",
+                                           NULL);
+  message = soup_message_new (SOUP_METHOD_POST,
+                              "https://api.accounts.firefox.com/v1/account/login";);
+
+  authPW_hex = encode_hex (authPW, TOKEN_LENGTH);
+  request_body = g_strconcat ("{\"authPW\": \"",
+                              authPW_hex,
+                              "\", \"email\": \"",
+                              emailUTF8,
+                              "\"}",
+                              NULL);
+
+  soup_message_set_request (message,
+                            "application/json",
+                            SOUP_MEMORY_COPY,
+                            request_body,
+                            strlen (request_body));
+
+  soup_session_queue_message (session, message, server_response_cb, NULL);
+
+  // TODO: find a way to safely free authPW_hex, request_body
+  // TODO: find a way to safely destroy session, message
+}
+
+void
 ephy_sync_service_stretch (EphySyncService *self,
                            const gchar *emailUTF8,
                            const gchar *passwordUTF8,
@@ -140,7 +220,7 @@ ephy_sync_service_stretch (EphySyncService *self,
   g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
 
   salt_stretch = KWE ("quickStretch", emailUTF8);
-  quickStretchedPW = g_malloc0 (TOKEN_LENGTH);
+  quickStretchedPW = g_malloc (TOKEN_LENGTH);
   pbkdf2_1k (strlen (passwordUTF8), (guint8 *) passwordUTF8,
              strlen (salt_stretch), (guint8 *) salt_stretch,
              TOKEN_LENGTH, quickStretchedPW);
@@ -166,7 +246,9 @@ ephy_sync_service_stretch (EphySyncService *self,
 }
 
 void
-ephy_sync_service_display_hex (const gchar *name, gsize length, guint8 *data)
+ephy_sync_service_display_hex (const gchar *name,
+                               gsize length,
+                               guint8 *data)
 {
   g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
 
diff --git a/src/ephy-sync-service.h b/src/ephy-sync-service.h
index 5fdb681..3b37926 100644
--- a/src/ephy-sync-service.h
+++ b/src/ephy-sync-service.h
@@ -16,6 +16,12 @@ void             ephy_sync_service_stretch      (EphySyncService *self,
                                                  const gchar *passwordUTF8,
                                                  guint8 *authPW,
                                                  guint8 *unwrapBKey);
+void             ephy_sync_service_try_login    (EphySyncService *self,
+                                                 gboolean login_with_keys,
+                                                 const gchar *emailUTF8,
+                                                 guint8 *authPW,
+                                                 guint8 *sessionToken,
+                                                 guint8 *keyFetchToken);
 void             ephy_sync_service_display_hex  (const gchar *name,
                                                  gsize length,
                                                  guint8 *data);
diff --git a/src/ephy-sync-window.c b/src/ephy-sync-window.c
index b47981b..db50189 100644
--- a/src/ephy-sync-window.c
+++ b/src/ephy-sync-window.c
@@ -2,6 +2,7 @@
 #include "ephy-sync-service.h"
 #include "ephy-gui.h"
 
+#include <string.h>
 #include <glib/gstdio.h>
 #include <gtk/gtk.h>
 
@@ -37,6 +38,8 @@ submit_action (GSimpleAction *action,
   const gchar *passwordUTF8;
   guint8 *authPW;
   guint8 *unwrapBKey;
+  guint8 *sessionToken;
+  guint8 *keyFetchToken;
   EphySyncWindow *self = EPHY_SYNC_WINDOW (user_data);
   g_printf ("[%s:%d, %s]\n", __FILE__, __LINE__, __func__);
 
@@ -45,20 +48,37 @@ submit_action (GSimpleAction *action,
   g_printf ("email: %s\n", emailUTF8);
   g_printf ("password: %s\n", passwordUTF8);
 
-  authPW = g_malloc0 (TOKEN_LENGTH);
-  unwrapBKey = g_malloc0 (TOKEN_LENGTH);
+  /* Only for easy testing */
+  if (!strlen (emailUTF8) && !strlen (passwordUTF8)) {
+    emailUTF8 = g_strdup ("andré@example.org");
+    passwordUTF8 = g_strdup ("pässwörd");
+  }
 
+  authPW = g_malloc (TOKEN_LENGTH);
+  unwrapBKey = g_malloc (TOKEN_LENGTH);
   ephy_sync_service_stretch (self->sync_service,
                              emailUTF8,
                              passwordUTF8,
                              authPW,
                              unwrapBKey);
-
   ephy_sync_service_display_hex ("authPW", TOKEN_LENGTH, authPW);
   ephy_sync_service_display_hex ("unwrapBKey", TOKEN_LENGTH, unwrapBKey);
 
+  sessionToken = g_malloc (TOKEN_LENGTH);
+  keyFetchToken = g_malloc0 (TOKEN_LENGTH);
+  ephy_sync_service_try_login (self->sync_service,
+                               FALSE,
+                               emailUTF8,
+                               authPW,
+                               sessionToken,
+                               keyFetchToken);
+  ephy_sync_service_display_hex ("sessionToken", TOKEN_LENGTH, sessionToken);
+  ephy_sync_service_display_hex ("keyFetchToken", TOKEN_LENGTH, keyFetchToken);
+
   g_free (authPW);
   g_free (unwrapBKey);
+  g_free (sessionToken);
+  g_free (keyFetchToken);
 }
 
 static void


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