gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 08/08: rework add_authentication


From: gnunet
Subject: [taler-anastasis] 08/08: rework add_authentication
Date: Fri, 09 Oct 2020 12:03:49 +0200

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

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

commit 1586ccd258e53951780c07f12a4b632fffc214e5
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Fri Oct 9 12:02:44 2020 +0200

    rework add_authentication
---
 src/include/anastasis_redux.h          | 29 +++++++++++++--
 src/lib/anastasis_api_backup_redux.c   | 29 +++++++--------
 src/lib/anastasis_api_recovery_redux.c |  2 +-
 src/lib/anastasis_api_redux.c          | 66 +++++++++++++++++++++++++++++++---
 4 files changed, 102 insertions(+), 24 deletions(-)

diff --git a/src/include/anastasis_redux.h b/src/include/anastasis_redux.h
index d87e7b6..7da95ae 100644
--- a/src/include/anastasis_redux.h
+++ b/src/include/anastasis_redux.h
@@ -28,13 +28,13 @@
 #include "anastasis_error_codes.h"
 
 #define FOREACH_STATE(REDUX_STATE) \
-  REDUX_STATE (NONE) \
   REDUX_STATE (ContinentSelectionState)   \
   REDUX_STATE (CountrySelectionState)  \
   REDUX_STATE (UserAttributesCollectionState)   \
   REDUX_STATE (AuthenticationsEditingState)  \
   REDUX_STATE (PoliciesReviewingState)   \
   REDUX_STATE (BackupPayingState)  \
+  REDUX_STATE (NoState) \
 
 #define GENERATE_ENUM(ENUM) ENUM,
 #define GENERATE_STRING(STRING) #STRING,
@@ -63,6 +63,16 @@ extern const json_t *provider_list;
  */
 extern const char *currency;
 
+
+/**
+ * Returns the integer value to a string value of a state.
+ *
+ * @param state_string
+ * @return int
+ */
+int get_enum_from_string (const char *state_string);
+
+
 /**
  * Returns an initial ANASTASIS backup state.
  *
@@ -165,7 +175,7 @@ ANASTASIS_recovery_action (json_t *state,
  * by a callback function.
  * This function can do network access to talk to anastasis service providers.
  *
- * @param state input state
+ * @param[in,out] state input/output state (to be modified)
  * @param action what action to perform
  * @param arguments data for the @a action
  * @param cb function to call with the result
@@ -180,4 +190,19 @@ ANASTASIS_redux_action (const json_t *state,
                         void *cb_cls);
 
 
+/**
+ * Function which is called when we need to go a state backwards.
+ * Sets state to previous state.
+ *
+ * @param[in,out] state input/output state (to be modified)
+ * @param arguments normally unused
+ * @param cb callback
+ * @param cb_cls closure
+ */
+void
+state_back (json_t *state,
+            const json_t *arguments,
+            ANASTASIS_ActionCallback cb,
+            void *cb_cls);
+
 #endif  /* _ANASTASIS_REDUX_H */
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index 3909357..d6b1369 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -153,25 +153,20 @@ add_authentication (json_t *state,
                     ANASTASIS_ActionCallback cb,
                     void *cb_cls)
 {
-  json_t *new_state;
-  json_t *methods = json_object_get (arguments, "authentication_methods");
-
-  GNUNET_assert (NULL != methods);
-  new_state = json_deep_copy (state);
-  GNUNET_assert (NULL != new_state);
-  json_object_set (new_state,
-                   "authentication_methods",
-                   methods);
-
-  {
-
-  }
+  json_t *method = json_object_get (arguments, "authentication_method");
+  GNUNET_assert (NULL != method);
+  json_t *auth_method_arr = json_object_get (state, "authentication_methods");
+  if (NULL == auth_method_arr)
+    auth_method_arr = json_array ();
+  GNUNET_assert (NULL != auth_method_arr);
+  GNUNET_assert (0 == json_array_append_new (auth_method_arr, method));
+  GNUNET_assert (0 == json_object_set (state,
+                                       "authentication_methods",
+                                       auth_method_arr));
 
   cb (cb_cls,
       ANASTASIS_EC_NONE,
-      new_state);
-  json_decref (methods);
-  json_decref (new_state);
+      state);
 }
 
 
@@ -446,7 +441,7 @@ ANASTASIS_backup_action (json_t *state,
       "next",
       &done_authentication
     },
-    { NONE, NULL, NULL }
+    { NoState, NULL, NULL }
   };
   const char *s = json_string_value (json_object_get (state,
                                                       "backup_state"));
diff --git a/src/lib/anastasis_api_recovery_redux.c 
b/src/lib/anastasis_api_recovery_redux.c
index 205b800..6dfe14a 100644
--- a/src/lib/anastasis_api_recovery_redux.c
+++ b/src/lib/anastasis_api_recovery_redux.c
@@ -125,7 +125,7 @@ ANASTASIS_recovery_action (json_t *state,
     const char *recovery_action;
     DispatchHandler fun;
   } dispatchers[] = {
-    { NONE, NULL, NULL }
+    { NoState, NULL, NULL }
   };
   const char *s = json_string_value (json_object_get (state,
                                                       "recovery_state"));
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index ed8bd6d..09e28a0 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -58,6 +58,24 @@ typedef void
                    void *cb_cls);
 
 
+/**
+ * Returns the integer value to a string value of a state.
+ * -1 if state unknown.
+ *
+ * @param state_string
+ * @return int
+ */
+int get_enum_from_string (const char *state_string)
+{
+  for (unsigned int i = ContinentSelectionState; i != NoState; i++)
+  {
+    if (0 == strcmp (state_string, STATE_STRING[i]))
+      return i;
+  }
+  return -1;
+}
+
+
 /**
  * Extract the mode of a state from json
  *
@@ -359,7 +377,16 @@ enter_user_attributes (json_t *state,
 }
 
 
-static void
+/**
+ * Function which is called when we need to go a state backwards.
+ * Sets state to previous state.
+ *
+ * @param[in,out] state input/output state (to be modified)
+ * @param arguments normally unused
+ * @param cb callback
+ * @param cb_cls closure
+ */
+void
 state_back (json_t *state,
             const json_t *arguments,
             ANASTASIS_ActionCallback cb,
@@ -367,8 +394,26 @@ state_back (json_t *state,
 {
   GNUNET_assert (NULL != state);
   const char *s_mode = get_state_mode (state);
+  GNUNET_assert (NULL != s_mode);
+  const char*state_string = json_string_value (json_object_get (state,
+                                                                s_mode));
+  GNUNET_assert (NULL != state_string);
+  int state_index = get_enum_from_string (state_string);
+  GNUNET_assert (GNUNET_SYSERR != state_index);
 
-  /** FIXME: state backwards logic */
+  if (state_index == BackupPayingState + 1) // offset for recovery states
+  {
+    state_index = UserAttributesCollectionState;
+  }
+  else
+    state_index = state_index - 1;
+  GNUNET_assert (0 == json_object_set_new (state,
+                                           s_mode,
+                                           json_string (
+                                             STATE_STRING[state_index])));
+  cb (cb_cls,
+      ANASTASIS_EC_NONE,
+      state);
 }
 
 
@@ -453,9 +498,9 @@ ANASTASIS_redux_action (const json_t *state,
     {
       AuthenticationsEditingState,
       "back",
-      &enter_user_attributes
+      &state_back
     },
-    { NONE, NULL, NULL }
+    { NoState, NULL, NULL }
   };
   GNUNET_assert (NULL != state);
   json_t *new_state;
@@ -488,16 +533,29 @@ ANASTASIS_redux_action (const json_t *state,
     }
   }
   if (0 == strcmp (s_mode, "backup_state"))
+  {
     ANASTASIS_backup_action (new_state,
                              action,
                              arguments,
                              cb,
                              NULL);
+    json_decref (new_state);
+    return;
+  }
+
   if (0 == strcmp (s_mode, "recovery_state"))
+  {
     ANASTASIS_recovery_action (new_state,
                                action,
                                arguments,
                                cb,
                                NULL);
+    json_decref (new_state);
+    return;
+  }
   json_decref (new_state);
+  GNUNET_break (0);
+  cb (cb_cls,
+      ANASTASIS_EC_INVALID, // FIXME: Define correct error code
+      NULL);
 }

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