gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] branch master updated (b999774 -> d795aef)


From: gnunet
Subject: [taler-anastasis] branch master updated (b999774 -> d795aef)
Date: Mon, 02 Nov 2020 11:44:29 +0100

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

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

    from b999774  Merge branch 'master' of ssh://git.taler.net/anastasis
     new e329f11  add_policy
     new f4986cd  ahv number validation function
     new d795aef  fix ahv validation

The 3 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/redux.ch.json                |  19 +++---
 src/cli/anastasis-cli-redux.c        |   8 ++-
 src/include/anastasis_error_codes.h  |   5 ++
 src/include/anastasis_redux.h        |   1 +
 src/lib/anastasis_api_backup_redux.c |  87 +++++++++++++++++++++++-
 src/lib/anastasis_api_redux.c        | 126 ++++++++++++++++++++++++++++++++++-
 6 files changed, 232 insertions(+), 14 deletions(-)

diff --git a/contrib/redux.ch.json b/contrib/redux.ch.json
index e434037..075ac6a 100644
--- a/contrib/redux.ch.json
+++ b/contrib/redux.ch.json
@@ -3,8 +3,8 @@
        {
            "type": "string",
            "name": "full_name",
-               "label": "Full name",
-               "label_i18n":{
+                 "label": "Full name",
+                 "label_i18n":{
                                                "de_DE":"Vollstaendiger Name",
                                                "de_CH":"Vollstaendiger Name"
                                         },
@@ -13,21 +13,20 @@
        {
            "type": "date",
            "name": "birthdate",
-               "label": "Birthdate",
+                 "label": "Birthdate",
            "label_i18n":{"de_CH":"Geburtsdatum"},
            "widget": "anastasis_gtk_ia_birthdate"
        },
        {
-           "type": "string",
-           "name": "ahv_number",
-               "label": "AHV number",
-               "label_i18n":{
+      "type": "string",
+      "name": "ahv_number",
+                 "label": "AHV number",
+                 "label_i18n":{
                                                "de_DE":"AHV-Nummer",
                                                "de_CH":"AHV-Nummer"
                                         },
-           "widget": "anastasis_gtk_ia_ahv"
+           "widget": "anastasis_gtk_ia_ahv",
+      "validation": "^(756)\\.[0-9]{4}\\.[0-9]{4}\\.[0-9]{2}|(756)[0-9]{10}$"
        }
        ]
 }
-
-
diff --git a/src/cli/anastasis-cli-redux.c b/src/cli/anastasis-cli-redux.c
index 66259cf..8600fca 100644
--- a/src/cli/anastasis-cli-redux.c
+++ b/src/cli/anastasis-cli-redux.c
@@ -27,6 +27,7 @@
 #include "anastasis_redux.h"
 #include <taler/taler_util.h>
 #include <taler/taler_error_codes.h>
+#include <taler/taler_json_lib.h>
 #include "anastasis_util_lib.h"
 
 /**
@@ -136,9 +137,12 @@ action_cb (void *cls,
     persist_new_state (result_state,
                        output_filename);
   if (ANASTASIS_EC_NONE != error_code)
+  {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Redux failed with error %d\n",
-                error_code);
+                "Redux failed with error %d: %s\n",
+                TALER_JSON_get_error_code (result_state),
+                TALER_JSON_get_error_hint (result_state));
+  }
   GNUNET_SCHEDULER_shutdown ();
   global_ret = (ANASTASIS_EC_NONE != error_code) ? 1 : 0;
 }
diff --git a/src/include/anastasis_error_codes.h 
b/src/include/anastasis_error_codes.h
index 114397e..7fda5ab 100644
--- a/src/include/anastasis_error_codes.h
+++ b/src/include/anastasis_error_codes.h
@@ -50,6 +50,11 @@ enum ANASTASIS_ErrorCode
    */
   ANASTASIS_EC_INVALID = 6001,
 
+  /**
+   * Special code to indicate that a parameter in the request was malformed.
+   */
+  ANASTASIS_EC_PARAMETER_MALFORMED = 6002,
+
   /**
    * The specified User was unknown
    */
diff --git a/src/include/anastasis_redux.h b/src/include/anastasis_redux.h
index 27de17d..61d151c 100644
--- a/src/include/anastasis_redux.h
+++ b/src/include/anastasis_redux.h
@@ -27,6 +27,7 @@
 #include <gnunet/gnunet_util_lib.h>
 #include "anastasis_error_codes.h"
 #include "anastasis_service.h"
+#include <regex.h>
 
 #define ANASTASIS_GENERIC_STATES(REDUX_STATE) \
   REDUX_STATE (ERROR) \
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index 2bcec84..a1b9af5 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -509,7 +509,92 @@ add_policy (json_t *state,
             ANASTASIS_ActionCallback cb,
             void *cb_cls)
 {
-  // FIXME: implement
+  size_t index;
+  json_t *method;
+  struct TALER_Amount recovery_cost;
+  json_t *methods = json_array ();
+
+  GNUNET_assert (json_is_array (methods));
+  json_t *policy = json_object ();
+
+  GNUNET_assert (NULL != policy);
+  json_t *policies = json_object_get (state,
+                                      "policies");
+
+  GNUNET_assert (json_is_array (policies));
+  json_t *arg_array = json_object_get (arguments,
+                                       "policy");
+
+  GNUNET_assert (json_is_array (arg_array));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_amount_get_zero (
+                   json_string_value (json_object_get (state,
+                                                       "currency")),
+                   &recovery_cost));
+
+  json_array_foreach (arg_array, index, method)
+  {
+    struct TALER_Amount method_cost;
+    size_t provider_index;
+    json_t *provider;
+    json_t *auth_method_arr = json_object_get (state,
+                                               "authentication_methods");
+
+    GNUNET_assert (json_is_array (auth_method_arr));
+    size_t index = json_integer_value (json_object_get (method,
+                                                        "auth_method_index"));
+
+    const char *method_type = json_string_value (json_array_get (
+                                                   auth_method_arr,
+                                                   index));
+
+    const char *provider_id = json_string_value (json_object_get (method,
+                                                                  "provider"));
+
+    json_t *auth_provider_arr = json_object_get (json_object_get (state,
+                                                                  
"authentication_providers"),
+                                                 method_type);
+
+    GNUNET_assert (json_is_array (auth_provider_arr));
+    json_array_foreach (auth_provider_arr, provider_index, provider)
+    {
+      json_t *provider_data;
+      if (NULL != (provider_data = json_object_get (provider,
+                                                    provider_id)))
+      {
+        GNUNET_assert (GNUNET_OK ==
+                       TALER_string_to_amount (
+                         json_string_value (json_object_get (provider_data,
+                                                             "method_cost")),
+                         &method_cost));
+        if (0 >
+            TALER_amount_add (&recovery_cost,
+                              &recovery_cost,
+                              &method_cost))
+        {
+          GNUNET_break (0);
+          return NULL;
+        }
+        json_decref (provider_data);
+        break;
+      }
+    }
+    json_decref (auth_provider_arr);
+    json_array_append_new (methods,
+                           json_pack ("{s:I, s:s}",
+                                      "authentication_method",
+                                      (json_int_t) index,
+                                      "provider",
+                                      provider_id));
+  }
+  policy = json_pack ("{s:o, s:o}",
+                      "recovery_cost",
+                      TALER_JSON_from_amount (&recovery_cost),
+                      "methods",
+                      methods);
+  json_array_append_new (policies,
+                         policy);
+
   cb (cb_cls,
       ANASTASIS_EC_NONE,
       state);
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index b362a3c..9a392d1 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -408,6 +408,93 @@ ANASTASIS_redux_countries_init_ (void)
 }
 
 
+/**
+ * Function to validate an AHV number.
+ * @param ahv_number ahv number to validate
+ * @param regexp regular expression to validate form of ahv number
+ * @return true if validation passed, else false
+ */
+static bool
+validate_ahv (const char *ahv_number,
+              const char *regexp)
+{
+  unsigned int checknum;
+  char num_to_check[12];
+  regex_t regex;
+  int regex_result;
+
+  // check if ahv has correct form
+  regex_result = regcomp (&regex,
+                          regexp,
+                          REG_EXTENDED);
+  if (0 < regex_result)
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to compile regular expression.");
+    regfree (&regex);
+    return false;
+  }
+  regex_result = regexec (&regex,
+                          ahv_number,
+                          0,
+                          NULL,
+                          0);
+  if (0 != regex_result)
+  {
+    regfree (&regex);
+    return false;
+  }
+  regfree (&regex);
+
+  // check if ahv contains points
+  if (strstr (ahv_number, "."))
+  {
+    for (unsigned int i = 0; i < 3; i++)
+    {
+      num_to_check[i] = ahv_number[i];
+    }
+    for (unsigned int i = 0; i < 4; i++)
+    {
+      num_to_check[3 + i] = ahv_number[4 + i];
+    }
+    for (unsigned int i = 0; i < 4; i++)
+    {
+      num_to_check[7 + i] = ahv_number[9 + i];
+    }
+    num_to_check[11] = ahv_number[15];
+  }
+  else
+  {
+    for (unsigned int i = 0; i < 12; i++)
+    {
+      num_to_check[i] = ahv_number[i];
+    }
+  }
+  int calculation = 0;
+  int next_ten = 0;
+  checknum = (int) ahv_number[strlen (ahv_number) - 1] - 48;
+
+  for (unsigned int i = 0; i < strlen (num_to_check); i++)
+  {
+    int x = (int) num_to_check[strlen (num_to_check) - i - 1] - 48;
+    if (0 == i % 2)
+      calculation = calculation + (x * 3);
+    else
+      calculation = calculation + x;
+  }
+  // round up to the next ten
+  next_ten = (calculation + 10) / 10;
+  next_ten = next_ten * 10;
+  // calculate check number
+  calculation = next_ten - calculation;
+
+  if (checknum == calculation)
+    return true;
+  return false;
+}
+
+
 /**
  * Function to load json containing country specific
  * identity attributes. Returns #GNUNET_OK if operation
@@ -701,9 +788,46 @@ enter_user_attributes (json_t *state,
                        void *cb_cls)
 {
   GNUNET_assert (NULL != arguments);
-  json_t *attributes = json_object_get (arguments, "identity_attributes");
+  json_t *attributes = json_object_get (arguments,
+                                        "identity_attributes");
   GNUNET_assert (NULL != attributes);
   GNUNET_assert (NULL != state);
+  // validate AHV number
+  if (json_object_get (attributes, "ahv_number"))
+  {
+    size_t index;
+    json_t *attribute;
+    const char *regexp = NULL;
+    json_t *required_attributes = json_object_get (state,
+                                                   "required_attributes");
+
+    GNUNET_assert (json_is_array (required_attributes));
+    json_array_foreach (required_attributes, index, attribute)
+    {
+      const char *name = json_string_value (
+        json_object_get (attribute,
+                         "name"));
+      if (0 == strcmp (name, "ahv_number"))
+      {
+        regexp = json_string_value (json_object_get (attribute,
+                                                     "validation"));
+      }
+    }
+    if (! validate_ahv (json_string_value (
+                          json_object_get (attributes, "ahv_number")),
+                        regexp))
+    {
+      json_t *error = json_pack ("{s:I, s:s}",
+                                 "code",
+                                 (json_int_t) ANASTASIS_EC_PARAMETER_MALFORMED,
+                                 "hint",
+                                 "Validation of AHV number failed. Please 
check if number is correct.");
+      cb (cb_cls,
+          ANASTASIS_EC_INVALID,
+          error);
+      return NULL;
+    }
+  }
   const char *s_mode = get_state_mode (state);
   GNUNET_assert (NULL != s_mode);
   json_t *auth_providers = json_object_get (arguments,

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