gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] branch master updated (1586ccd -> 94b4bd9)


From: gnunet
Subject: [taler-anastasis] branch master updated (1586ccd -> 94b4bd9)
Date: Fri, 09 Oct 2020 20:43:52 +0200

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a change to branch master
in repository anastasis.

    from 1586ccd  rework add_authentication
     new 829c1ab  move config request to reducer
     new 94b4bd9  fix config request

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 contrib/provider-list.json    |  26 ++--
 src/include/anastasis_redux.h |  10 +-
 src/lib/Makefile.am           |   1 +
 src/lib/anastasis_api_redux.c | 338 +++++++++++++++++++++++++++++++++++++-----
 4 files changed, 316 insertions(+), 59 deletions(-)

diff --git a/contrib/provider-list.json b/contrib/provider-list.json
index 8e5cc82..8a28b07 100644
--- a/contrib/provider-list.json
+++ b/contrib/provider-list.json
@@ -1,24 +1,24 @@
 {
-    "anastasis-provider": [
+    "anastasis_provider": [
        {
-        "provider-id" : "anastasis_01",
-        "provider-url" : "localhost:8086/",
-        "provider-name" : "Anastasis 1"
+        "provider_id" : "anastasis_01",
+        "provider_url" : "localhost:8086/",
+        "provider_name" : "Anastasis 1"
        },
        {
-           "provider-id" : "anastasis_02",
-        "provider-url" : "localhost:8087/",
-        "provider-name" : "Anastasis 2"
+           "provider_id" : "anastasis_02",
+        "provider_url" : "localhost:8087/",
+        "provider_name" : "Anastasis 2"
        },
        {
-           "provider-id" : "anastasis_03",
-        "provider-url" : "localhost:8088/",
-        "provider-name" : "Anastasis 3"
+           "provider_id" : "anastasis_03",
+        "provider_url" : "localhost:8088/",
+        "provider_name" : "Anastasis 3"
        },
        {
-           "provider-id" : "anastasis_04",
-        "provider-url" : "localhost:8089/",
-        "provider-name" : "Anastasis 4"
+           "provider_id" : "anastasis_04",
+        "provider_url" : "localhost:8089/",
+        "provider_name" : "Anastasis 4"
        }
        ]
 }
\ No newline at end of file
diff --git a/src/include/anastasis_redux.h b/src/include/anastasis_redux.h
index 7da95ae..0955c72 100644
--- a/src/include/anastasis_redux.h
+++ b/src/include/anastasis_redux.h
@@ -26,6 +26,7 @@
 #include <jansson.h>
 #include <gnunet/gnunet_util_lib.h>
 #include "anastasis_error_codes.h"
+#include "anastasis_service.h"
 
 #define FOREACH_STATE(REDUX_STATE) \
   REDUX_STATE (ContinentSelectionState)   \
@@ -53,11 +54,6 @@ static const char *STATE_STRING[] = {
  */
 extern const json_t *redux_id_attr;
 
-/**
- * JSON containing anastasis providers.
- */
-extern const json_t *provider_list;
-
 /**
  * Currency of chosen country
  */
@@ -175,10 +171,11 @@ ANASTASIS_recovery_action (json_t *state,
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
- * @param[in,out] state input/output state (to be modified)
+ * @param state input state
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
+ * @param ctx Curl context
  * @param cb_cls closure for @a cb
  * @return failure state or new state
  */
@@ -187,6 +184,7 @@ ANASTASIS_redux_action (const json_t *state,
                         const char *action,
                         const json_t *arguments,
                         ANASTASIS_ActionCallback cb,
+                        struct GNUNET_CURL_Context *ctx,
                         void *cb_cls);
 
 
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 04b092b..cb319a8 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -110,6 +110,7 @@ libanastasisredux_la_LIBADD = \
   -lgnunetutil \
   -ltalerutil \
   -ltalerjson \
+  -lanastasisrest \
   -ljansson \
   $(XLIB)
 
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index 09e28a0..17169db 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -25,17 +25,105 @@
 #include <jansson.h>
 #include "anastasis_redux.h"
 #include "anastasis_error_codes.h"
+#include <taler/taler_json_lib.h>
 
 
 /**
- * JSON containing country specific identity attributes to ask the user for.
+ * Server information
  */
-const json_t *redux_id_attr;
+struct ServerInfo
+{
+  /**
+   * URL of the anastasis backend.
+   */
+  char *backend_url;
+
+  /**
+   * id of the anastasis backend.
+   */
+  char *backend_id;
+
+  /**
+   * label of the anastasis backend.
+   */
+  char *backend_name;
+
+  /**
+   * currrency used by the anastasis backend.
+   */
+  char *backend_currency;
+
+  /**
+   * supported methods of the anastasis backend.
+   */
+  json_t *backend_methods;
+
+  /**
+   * insurance of the anastasis backend.
+   */
+  struct TALER_Amount backend_insurance;
+
+  /**
+   * cost of using the anastasis backend.
+   */
+  struct TALER_Amount backend_cost;
+};
+
+/**
+ * State for a "get config" CMD.
+ */
+struct ConfigState
+{
+  /**
+   * Expected status code.
+   */
+  unsigned int http_status;
+
+  /**
+   * The /config GET operation handle.
+   */
+  struct ANASTASIS_ConfigOperation *co;
+
+  /**
+   * Reference to a ServerInfo.
+   */
+  struct ServerInfo *server;
+
+  /**
+   * Current state
+   */
+  json_t *state;
+
+  /**
+   * Supported methods.
+   */
+  json_t *methods;
+
+  /**
+   * Function to call if config request was successful.
+   */
+  ANASTASIS_ActionCallback cb;
+};
+
+/**
+ * Curl context for communication with taler backend
+ */
+struct GNUNET_CURL_Context *curl_ctx;
+
+/**
+ * Information about the servers.
+ */
+static struct ServerInfo *servers;
+
+/**
+ * Amount of servers.
+ */
+static unsigned int servers_length = 0;
 
 /**
- * JSON containing anastasis providers.
+ * JSON containing country specific identity attributes to ask the user for.
  */
-const json_t *provider_list;
+const json_t *redux_id_attr;
 
 /**
  * Currency of chosen country
@@ -58,6 +146,125 @@ typedef void
                    void *cb_cls);
 
 
+/**
+ * Function called with the results of a #ANASTASIS_get_config().
+ *
+ * @param cls closure
+ * @param http_status HTTP status of the request
+ * @param methods supported methods by this provider
+ * @param annual_fee Annual fee of this service
+ */
+static void
+config_cb (void *cls,
+           unsigned int http_status,
+           const json_t *methods,
+           const char *conf_currency,
+           const struct TALER_Amount *annual_fee)
+{
+  struct ConfigState *cs = cls;
+
+  cs->co = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "At %s:%d url is %s\n", __FILE__, __LINE__,
+              cs->server->backend_url);
+  if (http_status != cs->http_status)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Unexpected response code %u in %s:%u\n",
+                http_status,
+                __FILE__,
+                __LINE__);
+    return;
+  }
+
+  cs->server->backend_methods = methods;
+  cs->server->backend_cost = *annual_fee;
+  cs->server->backend_currency = GNUNET_malloc (strlen (conf_currency));
+  GNUNET_strlcpy (cs->server->backend_currency,
+                  conf_currency,
+                  strlen (conf_currency));
+
+  if (0 == strcmp (cs->server->backend_currency,
+                   json_string_value (json_object_get (cs->state,
+                                                       "currency"))))
+  {
+    json_t *method;
+    size_t index;
+    json_t *config = json_object ();
+    GNUNET_assert (NULL != config);
+    json_t *provider_list = json_array ();
+    GNUNET_assert (NULL != provider_list);
+    json_t *method_arr = json_object_get (methods, "methods");
+    GNUNET_assert (NULL != method_arr);
+    GNUNET_assert (0 == json_object_set_new (config,
+                                             "provider",
+                                             json_string_value (
+                                               cs->server->backend_id)));
+
+    json_array_foreach (method_arr, index, method)
+    {
+      json_t *method_type = json_object_get (method, "method");
+      GNUNET_assert (NULL != method_type);
+      json_t *provider_method = json_object ();
+      GNUNET_assert (NULL != provider_method);
+      json_t *provider_id = json_object ();
+      GNUNET_assert (NULL != provider_id);
+      json_t *method_cost = json_object ();
+      GNUNET_assert (NULL != method_cost);
+      json_t *annual_cost = json_object ();
+      GNUNET_assert (NULL != annual_cost);
+
+      GNUNET_assert (0 == json_object_set_new (method_cost,
+                                               "method_cost",
+                                               json_object_get (method,
+                                                                "cost")));
+      GNUNET_assert (0 == json_object_set_new (provider_id,
+                                               cs->server->backend_id,
+                                               method_cost));
+      GNUNET_assert (0 == json_object_set_new (annual_cost,
+                                               "annual_cost",
+                                               TALER_JSON_from_amount (
+                                                 &cs->server->backend_cost)));
+      GNUNET_assert (0 == json_object_set_new (provider_id,
+                                               cs->server->backend_id,
+                                               annual_cost));
+
+      GNUNET_assert (0 == json_object_set_new (provider_method,
+                                               json_string_value (method_type),
+                                               provider_id));
+
+      GNUNET_assert (0 == json_object_set_new (config,
+                                               json_string_value ("config"),
+                                               provider_method));
+    }
+    for (unsigned int i = 0; i < servers_length; i++)
+    {
+      if (0 == strcmp (servers[i].backend_currency,
+                       json_string_value (json_object_get (cs->state,
+                                                           "currency"))))
+      {
+        json_t *provider = json_object ();
+        GNUNET_assert (NULL != provider);
+        GNUNET_assert (0 == json_object_set_new (provider,
+                                                 "provider_id",
+                                                 servers[i].backend_id));
+        GNUNET_assert (0 == json_object_set_new (provider,
+                                                 "provider_url",
+                                                 servers[i].backend_url));
+        GNUNET_assert (0 == json_array_append_new (provider_list,
+                                                   provider));
+      }
+    }
+    GNUNET_assert (0 == json_object_set_new (config,
+                                             "provider_list",
+                                             provider_list));
+    cs->cb (NULL,
+            ANASTASIS_EC_NONE,
+            config);
+  }
+}
+
+
 /**
  * Returns the integer value to a string value of a state.
  * -1 if state unknown.
@@ -240,7 +447,90 @@ select_country (json_t *state,
                 void *cb_cls)
 {
   json_t *root;
+  const json_t *provider_list;
   json_t *country = json_object_get (arguments, "country_code");
+  char *dn;
+  json_error_t error;
+
+  // initialize provider list
+  {
+    char *path;
+
+    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
+    if (NULL == path)
+    {
+      GNUNET_break (0);
+      return;
+    }
+    GNUNET_asprintf (&dn,
+                     "%s/share/anastasis/provider-list.json",
+                     path);
+    GNUNET_free (path);
+  }
+  provider_list = json_load_file (dn, JSON_COMPACT, &error);
+  if (NULL == provider_list)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to parse `%s': %s at %d:%d (%d)\n",
+                dn,
+                error.text,
+                error.line,
+                error.column,
+                error.position);
+    GNUNET_free (dn);
+    return;
+  }
+
+  // get configs from providers
+  {
+    size_t index;
+    json_t *provider;
+    json_t *provider_list = json_object_get (provider_list,
+                                             "anastasis_provider");
+
+    json_array_foreach (provider_list, index, provider)
+    {
+      struct ServerInfo *server = GNUNET_new (struct ServerInfo);
+      const char *url = json_string_value (json_object_get (provider,
+                                                            "provider_url"));
+      const char *provider_name = json_string_value (json_object_get (provider,
+                                                                      
"provider_name"));
+      const char *provider_id = json_string_value (json_object_get (provider,
+                                                                    
"provider_id"));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Url is: %s\n", url);
+      server->backend_url = GNUNET_malloc (strlen (url) + 1);
+      GNUNET_strlcpy (server->backend_url,
+                      url,
+                      strlen (url) + 1);
+      server->backend_name = GNUNET_malloc (strlen (provider_name) + 1);
+      GNUNET_strlcpy (server->backend_name,
+                      provider_name,
+                      strlen (provider_name) + 1);
+      server->backend_id = GNUNET_malloc (strlen (provider_id) + 1);
+      GNUNET_strlcpy (server->backend_id,
+                      provider_id,
+                      strlen (provider_id) + 1);
+      GNUNET_array_append (servers, servers_length, *server);
+    }
+
+    for (unsigned int i = 0; i < servers_length; i++)
+    {
+      struct ConfigState *cs = GNUNET_new (struct ConfigState);
+      cs->server = &servers[i];
+      cs->http_status = MHD_HTTP_OK;
+      cs->state = state;
+      cs->cb = cb;
+      cs->co = ANASTASIS_get_config (curl_ctx,
+                                     servers[i].backend_url,
+                                     config_cb,
+                                     cs);
+      if (NULL == cs->co)
+      {
+        /** FIXME: Error handling */
+      }
+    }
+  }
 
   GNUNET_assert (NULL != country);
   GNUNET_assert (NULL != state);
@@ -322,44 +612,12 @@ enter_user_attributes (json_t *state,
                        ANASTASIS_ActionCallback cb,
                        void *cb_cls)
 {
-  json_t *root;
-  char *dn;
-  json_error_t error;
   json_t *attributes = json_object_get (arguments, "identity_attributes");
 
   GNUNET_assert (NULL != attributes);
   GNUNET_assert (NULL != state);
   const char *s_mode = get_state_mode (state);
   GNUNET_assert (NULL != s_mode);
-  {
-    char *path;
-
-    path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
-    if (NULL == path)
-    {
-      GNUNET_break (0);
-      return;
-    }
-    GNUNET_asprintf (&dn,
-                     "%s/share/anastasis/provider-list.json",
-                     path);
-    GNUNET_free (path);
-  }
-  provider_list = json_load_file (dn, JSON_COMPACT, &error);
-  if (NULL == provider_list)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to parse `%s': %s at %d:%d (%d)\n",
-                dn,
-                error.text,
-                error.line,
-                error.column,
-                error.position);
-    GNUNET_free (dn);
-    return;
-  }
-  root = json_object_get (provider_list, "anastasis-provider");
-  GNUNET_assert (NULL != root);
   json_object_set_new (state,
                        s_mode,
                        json_string 
(STATE_STRING[AuthenticationsEditingState]));
@@ -367,10 +625,6 @@ enter_user_attributes (json_t *state,
   json_object_set_new (state,
                        "identity_attributes",
                        attributes);
-
-  json_object_set_new (state,
-                       "provider-list",
-                       root);
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -429,6 +683,7 @@ state_back (json_t *state,
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
+ * @param ctx Curl context
  * @param cb_cls closure for @a cb
  * @return failure state or new state
  */
@@ -437,6 +692,7 @@ ANASTASIS_redux_action (const json_t *state,
                         const char *action,
                         const json_t *arguments,
                         ANASTASIS_ActionCallback cb,
+                        struct GNUNET_CURL_Context *ctx,
                         void *cb_cls)
 {
   struct Dispatcher
@@ -520,6 +776,8 @@ ANASTASIS_redux_action (const json_t *state,
           NULL);
     }
   }
+  if (NULL != ctx)
+    curl_ctx = ctx;
   new_state = json_deep_copy (state);
   GNUNET_assert (NULL != new_state);
   for (unsigned int i = 0; NULL != dispatchers[i].fun; i++)

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]