gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 03/03: fix ahv validation


From: gnunet
Subject: [taler-anastasis] 03/03: fix ahv validation
Date: Mon, 02 Nov 2020 11:44:32 +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 d795aefe984b7c851a41eccf83c4b11af2028b58
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Mon Nov 2 11:44:21 2020 +0100

    fix ahv validation
---
 contrib/redux.ch.json               |  2 +-
 src/cli/anastasis-cli-redux.c       |  8 +++--
 src/include/anastasis_error_codes.h |  5 +++
 src/include/anastasis_redux.h       |  1 +
 src/lib/anastasis_api_redux.c       | 69 +++++++++++++++++++++++++++++++++++--
 5 files changed, 80 insertions(+), 5 deletions(-)

diff --git a/contrib/redux.ch.json b/contrib/redux.ch.json
index 677b800..075ac6a 100644
--- a/contrib/redux.ch.json
+++ b/contrib/redux.ch.json
@@ -26,7 +26,7 @@
                                                "de_CH":"AHV-Nummer"
                                         },
            "widget": "anastasis_gtk_ia_ahv",
-      "validation": "/(756.\d{4}.\d{4}.\d{2}|756\d{10})/g"
+      "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_redux.c b/src/lib/anastasis_api_redux.c
index 8091a58..9a392d1 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -411,13 +411,41 @@ 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)
+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, "."))
@@ -760,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]