gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 12/15: worked on recovery reducer


From: gnunet
Subject: [taler-anastasis] 12/15: worked on recovery reducer
Date: Sun, 31 Jan 2021 17:06:18 +0100

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

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

commit f1a66696ececf2524f9bc1dbbc93b41982d34294
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Wed Jan 27 14:50:54 2021 +0100

    worked on recovery reducer
---
 src/lib/anastasis.c                    |   2 +-
 src/lib/anastasis_api_recovery_redux.c | 239 +++++++++++++++++++++++++++++----
 src/lib/testing_cmd_challenge_start.c  |   5 +
 3 files changed, 218 insertions(+), 28 deletions(-)

diff --git a/src/lib/anastasis.c b/src/lib/anastasis.c
index bf67949..aead0b0 100644
--- a/src/lib/anastasis.c
+++ b/src/lib/anastasis.c
@@ -539,7 +539,7 @@ ANASTASIS_json_to_challenge (json_t *challenge)
 {
   GNUNET_assert (json_is_object (challenge));
   const char *challenge_str;
-  struct ANASTASIS_Challenge *c;
+  struct ANASTASIS_Challenge *c = NULL;
 
   challenge_str =
     json_string_value (
diff --git a/src/lib/anastasis_api_recovery_redux.c 
b/src/lib/anastasis_api_recovery_redux.c
index 1a81d83..d1ab313 100644
--- a/src/lib/anastasis_api_recovery_redux.c
+++ b/src/lib/anastasis_api_recovery_redux.c
@@ -127,6 +127,26 @@ struct ChallengeState
    * Current state
    */
   json_t *state;
+
+  /**
+   * Payment order ID we got back, if any. Otherwise NULL.
+   */
+  char *payment_order_id;
+
+  /**
+   * Payment order ID we are to provide in the request, may be NULL.
+   */
+  struct ANASTASIS_PaymentSecretP payment_order_req;
+
+  /**
+   * True if @e payment_order_req is initialized.
+   */
+  bool payment_order_set;
+
+  /**
+   * code we read in the file generated by the plugin
+   */
+  char *code;
 };
 
 
@@ -142,34 +162,118 @@ set_state (json_t *state,
 
 
 static void
-challenge_answer_cb (void *af_cls,
-                     unsigned int http_status_code,
-                     enum TALER_ErrorCode ec)
+challenge_payment_cb (void *cls,
+                      const char *taler_pay_url,
+                      const char *server_url,
+                      unsigned int http_status)
 {
+  const char *m;
   json_t *challenges;
   json_t *challenge;
-  struct ANASTASIS_ChallengeInformation *ci;
-  struct ChallengeState *cs = af_cls;
+  struct ChallengeState *cs = cls;
 
-  if (http_status_code == MHD_HTTP_PAYMENT_REQUIRED)
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "At %s:%d the taler pay url is %s\n",
+              __FILE__, __LINE__,
+              taler_pay_url);
+  if (http_status != MHD_HTTP_PAYMENT_REQUIRED)
   {
-    // FIXME: Payment for challenge (get code etc.) implemented?
-    set_state (cs->state,
-               ANASTASIS_recovery_state_to_string (
-                 ANASTASIS_RECOVERY_STATE_CHALLENGE_PAYING));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Unexpected response code %u/%d to in %s:%u\n",
+                cs->http_status,
+                http_status,
+                __FILE__,
+                __LINE__);
+    ANASTASIS_redux_fail (cs->cb,
+                          cs->cb_cls,
+                          ANASTASIS_EC_INVALID, // FIXME: Error code
+                          "Unexpected response code!");
+    return;
+  }
+
+  if (0 != strncmp (taler_pay_url,
+                    "taler://pay/http",
+                    strlen ("taler://pay/http")))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Did not find `%s' in `%s'\n",
+                "/-/-/",
+                taler_pay_url);
+    ANASTASIS_redux_fail (cs->cb,
+                          cs->cb_cls,
+                          ANASTASIS_EC_INVALID, // FIXME: Error code
+                          "Invalid pay uri!");
+    return;
   }
-  else if (http_status_code != MHD_HTTP_OK)
+  m = strstr (taler_pay_url, "/-/-/");
+  if (NULL == m)
   {
-    json_t *error
-      = json_pack ("{s:I, s:s}",
-                   "code",
-                   (json_int_t) ec,
-                   "hint",
-                   "Failed challenge answer!");
-    cs->cb (cs->cb_cls,
-            ANASTASIS_EC_NONE,
-            error);
-    cs->cb = NULL;
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Did not find `%s' in `%s'\n",
+                "/-/-/",
+                taler_pay_url);
+    ANASTASIS_redux_fail (cs->cb,
+                          cs->cb_cls,
+                          ANASTASIS_EC_INVALID, // FIXME: Error code
+                          "Invalid pay uri!");
+
+    /* NOTE: The above is a simplifying assumption for the
+        test-logic, hitting this code merely means that
+        the assumptions for the test (i.e. no instance) are
+        not satisfied, it is not inherently the case that
+        the above token must appear in the payment request!
+
+        So if you hit this, you might just want to modify
+        the code here to handle this better! */
+    return;
+  }
+  cs->payment_order_id = GNUNET_strdup (&m[strlen ("/-/-/")]);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "At %s:%d Order ID from Anastasis service is %s\n",
+              __FILE__, __LINE__,
+              cs->payment_order_id);
+
+  set_state (cs->state,
+             ANASTASIS_recovery_state_to_string (
+               ANASTASIS_RECOVERY_STATE_CHALLENGE_PAYING));
+
+  challenges = json_object_get (cs->state,
+                                "challenges");
+  GNUNET_assert (json_is_array (challenges));
+  challenge =
+    json_array_get (challenges,
+                    cs->challenge_index);
+  GNUNET_assert (0 == json_object_set (challenge,
+                                       "pay_uri",
+                                       json_string (taler_pay_url)));
+  GNUNET_assert (0 == json_object_set (challenge,
+                                       "order_id",
+                                       json_string (cs->payment_order_id)));
+
+  cs->cb (cs->cb_cls,
+          ANASTASIS_EC_NONE,
+          cs->state);
+  cs->cb = NULL;
+}
+
+
+static void
+challenge_start_cb (void *cls,
+                    const char *response_string,
+                    unsigned int http_status_code)
+{
+  json_t *challenges;
+  json_t *challenge;
+  struct ANASTASIS_ChallengeInformation *ci;
+  struct ChallengeState *cs = cls;
+
+  if (http_status_code != MHD_HTTP_OK)
+  {
+    ANASTASIS_redux_fail (cs->cb,
+                          cs->cb_cls,
+                          ANASTASIS_EC_INVALID, // FIXME: Error code
+                          "Unexpected response code!");
+
     return;
   }
   else
@@ -188,6 +292,7 @@ challenge_answer_cb (void *af_cls,
   challenge =
     json_array_get (challenges,
                     cs->challenge_index);
+  // overwrite challenge
   challenge = ANASTASIS_challenge_information_to_json (ci);
   GNUNET_assert (0 == json_array_set_new (challenges,
                                           cs->challenge_index,
@@ -304,11 +409,12 @@ select_challenge (json_t *state,
                        "code"));
   }
 
-  cs->cao = ANASTASIS_challenge_answer (ctx,
-                                        (struct ANASTASIS_Challenge *) c,
-                                        cs->answer,
-                                        &challenge_answer_cb,
-                                        cs);
+  ANASTASIS_challenge_start (ctx,
+                             (struct ANASTASIS_Challenge *) c,
+                             &challenge_payment_cb,
+                             cs,
+                             &challenge_start_cb,
+                             cs);
   return NULL;
 }
 
@@ -321,6 +427,15 @@ pay_challenge (json_t *state,
                void *cb_cls)
 {
   // FIXME: implement
+  if (NULL == arguments)
+  {
+    ANASTASIS_redux_fail (cb,
+                          cb_cls,
+                          TALER_EC_ANASTASIS_REDUCER_ACTION_INVALID,
+                          "pay_challenge");
+    return NULL;
+  }
+
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
@@ -328,6 +443,24 @@ pay_challenge (json_t *state,
 }
 
 
+static void
+challenge_answer_cb (void *af_cls,
+                     unsigned int http_status_code,
+                     enum TALER_ErrorCode ec)
+{
+  struct ChallengeState *cs = af_cls;
+  if (http_status_code != MHD_HTTP_OK)
+  {
+    ANASTASIS_redux_fail (cs->cb,
+                          cs->cb_cls,
+                          ec,
+                          "Unexpected response code!");
+    return;
+  }
+
+}
+
+
 static struct ANASTASIS_ReduxAction *
 solve_challenge (json_t *state,
                  const json_t *arguments,
@@ -335,7 +468,59 @@ solve_challenge (json_t *state,
                  ANASTASIS_ActionCallback cb,
                  void *cb_cls)
 {
-  // FIXME: implement
+  if (NULL == arguments)
+  {
+    ANASTASIS_redux_fail (cb,
+                          cb_cls,
+                          TALER_EC_ANASTASIS_REDUCER_ACTION_INVALID,
+                          "solve_challenge");
+    return NULL;
+  }
+
+  json_t *challenge;
+  json_t *challenges;
+  const char *method;
+  const struct ANASTASIS_Challenge *c;
+  struct ChallengeState *cs = GNUNET_new (struct ChallengeState);
+
+  cs->cb = cb;
+  cs->cb_cls = cb_cls;
+  cs->state = state;
+  cs->challenge_index =
+    (unsigned int) json_integer_value (
+      json_object_get (arguments,
+                       "challenge_index"));
+  challenges = json_object_get (state,
+                                "challenges");
+  GNUNET_assert (json_is_array (challenges));
+  challenge =
+    json_array_get (challenges,
+                    cs->challenge_index);
+  c = ANASTASIS_json_to_challenge (challenge);
+
+  method = json_string_value (
+    json_object_get (challenge,
+                     "method"));
+  if (0 == strcmp (method, "question"))
+  {
+    cs->mode = 0;
+    cs->answer = json_string_value (
+      json_object_get (arguments,
+                       "answer"));
+  }
+  else
+  {
+    cs->mode = 1;
+    cs->answer = json_string_value (
+      json_object_get (arguments,
+                       "code"));
+  }
+
+  cs->cao = ANASTASIS_challenge_answer (ctx,
+                                        (struct ANASTASIS_Challenge *) c,
+                                        cs->answer,
+                                        &challenge_answer_cb,
+                                        cs);
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
diff --git a/src/lib/testing_cmd_challenge_start.c 
b/src/lib/testing_cmd_challenge_start.c
index 4bc2826..2831a02 100644
--- a/src/lib/testing_cmd_challenge_start.c
+++ b/src/lib/testing_cmd_challenge_start.c
@@ -65,10 +65,12 @@ struct ChallengeState
    * Index of the challenge we are solving
    */
   unsigned int challenge_index;
+
   /**
    * Reference to the payment
    */
   const char *payment_ref;
+
   /**
    * Payment order ID we got back, if any. Otherwise NULL.
    */
@@ -78,16 +80,19 @@ struct ChallengeState
    * Payment order ID we are to provide in the request, may be NULL.
    */
   struct ANASTASIS_PaymentSecretP payment_order_req;
+
   /**
    * True if @e payment_order_req is initialized.
    */
   bool payment_order_set;
+
   /**
    * code we read in the file generated by the plugin
    */
   char *code;
 };
 
+
 static void
 challenge_start_cb (void *cls,
                     const char *response_string,

-- 
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]