[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-anastasis] branch master updated: dlsym the validation logic
From: |
gnunet |
Subject: |
[taler-anastasis] branch master updated: dlsym the validation logic |
Date: |
Mon, 21 Dec 2020 15:06:33 +0100 |
This is an automated email from the git hooks/post-receive script.
grothoff pushed a commit to branch master
in repository anastasis.
The following commit(s) were added to refs/heads/master by this push:
new 470616c dlsym the validation logic
470616c is described below
commit 470616c8901a0d6e2be6b9e6a975c8dc3cdbe333
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Mon Dec 21 15:06:31 2020 +0100
dlsym the validation logic
---
contrib/redux.ch.json | 3 +-
src/lib/anastasis_api_redux.c | 117 +++++++++++++++++++++---------------------
src/lib/validation_CH_AHV.c | 34 ++++++++++++
3 files changed, 95 insertions(+), 59 deletions(-)
diff --git a/contrib/redux.ch.json b/contrib/redux.ch.json
index c8bd3dc..9cfeaec 100644
--- a/contrib/redux.ch.json
+++ b/contrib/redux.ch.json
@@ -26,7 +26,8 @@
"de_CH":"AHV-Nummer"
},
"widget": "anastasis_gtk_ia_ahv",
- "validation": "^(756).[0-9]{4}.[0-9]{4}.[0-9]{2}|(756)[0-9]{10}$"
+ "validation-regex":
"^(756).[0-9]{4}.[0-9]{4}.[0-9]{2}|(756)[0-9]{10}$",
+ "validation-logic": "CH_AVH_check"
}
]
}
diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index 5444f2e..72809ff 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -480,15 +480,15 @@ ANASTASIS_redux_countries_init_ (void)
/**
- * Function to validate an AHV number.
+ * Function to validate an input by regular expression ("validation-regex").
*
* @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)
+validate_regex (const char *ahv_number,
+ const char *regexp)
{
{
regex_t regex;
@@ -517,31 +517,7 @@ validate_ahv (const char *ahv_number,
}
regfree (®ex);
}
-
- {
- unsigned int checknum;
- unsigned int next_ten;
- const char *pos = &ahv_number[strlen (ahv_number) - 1];
- bool phase = true;
- unsigned int calculation = 0;
-
- checknum = *pos - 48;
- while (pos > ahv_number)
- {
- pos--;
- if ('.' == *pos)
- continue;
- if (phase)
- calculation += ((*pos - 48) * 3);
- else
- calculation += *pos - 48;
- phase = ! phase;
- }
- /* round up to the next ten */
- next_ten = ((calculation + 9) / 10) * 10;
- calculation = next_ten - calculation;
- return (checknum == calculation);
- }
+ return true;
}
@@ -922,6 +898,12 @@ enter_user_attributes (json_t *state,
ANASTASIS_ActionCallback cb,
void *cb_cls)
{
+ size_t index;
+ json_t *attributes;
+ json_t *required_attribute;
+ json_t *required_attributes;
+
+ GNUNET_assert (NULL != state);
if (NULL == arguments)
{
ANASTASIS_redux_fail (cb,
@@ -930,47 +912,66 @@ enter_user_attributes (json_t *state,
"enter_user_attributes");
return NULL;
}
- json_t *attributes = json_object_get (arguments,
- "identity_attributes");
+ 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)
+ *required_attributes = json_object_get (state,
+ "required_attributes");
+ GNUNET_assert (json_is_array (required_attributes));
+ json_array_foreach (required_attributes, index, required_attribute)
+ {
+ const char *name;
+ const char *attribute_value;
+ const char *regexp;
+ const char *reglog;
+ bool (*regfun)(const char *);
+
+ name = json_string_value (json_object_get (required_attribute,
+ "name"));
+ attribute_value = json_string_value (json_object_get (attributes,
+ name));
+ regexp = json_string_value (json_object_get (attribute,
+ "validation-regex"));
+ if ( (NULL != regexp) &&
+ (! validate_regex (value,
+ regexp)) )
{
- 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"));
- }
+ json_t *error = json_pack (
+ "{s:I, s:s}",
+ "code",
+ (json_int_t)
+ // FIXME: create EC for 'regex failed'
+ ANASTASIS_EC_GENERIC_PARAMETER_MALFORMED,
+ "hint", regexp);
+ cb (cb_cls,
+ ANASTASIS_EC_INVALID,
+ error);
+ json_decref (error);
+ return NULL;
}
- if (! validate_ahv (json_string_value (
- json_object_get (attributes, "ahv_number")),
- regexp))
+ reglog = json_string_value (json_object_get (attribute,
+ "validation-logic"));
+ if ( (NULL != reglog) &&
+ (NULL != (regfun = dlsym (RTLD_DEFAULT,
+ reglog))) &&
+ (! regfun (attribute_value)) )
{
- json_t *error = json_pack ("{s:I, s:s}",
- "code",
- (json_int_t)
- ANASTASIS_EC_GENERIC_PARAMETER_MALFORMED,
- "hint",
- "Validation of AHV number failed. Please
check if number is correct.");
+ json_t *error = json_pack (
+ "{s:I, s:s}",
+ "code",
+ (json_int_t)
+ // FIXME: create EC for 'regex failed'
+ ANASTASIS_EC_GENERIC_PARAMETER_MALFORMED,
+ "hint", reglog);
cb (cb_cls,
ANASTASIS_EC_INVALID,
error);
+ json_decref (error);
return NULL;
}
}
+
const char *s_mode = get_state_mode (state);
GNUNET_assert (NULL != s_mode);
json_t *auth_providers = json_object_get (arguments,
diff --git a/src/lib/validation_CH_AHV.c b/src/lib/validation_CH_AHV.c
new file mode 100644
index 0000000..0718a88
--- /dev/null
+++ b/src/lib/validation_CH_AHV.c
@@ -0,0 +1,34 @@
+/**
+ * Function to validate a Swiss AHV number.
+ *
+ * @param avh_number ahv number to validate (input)
+ * @return true if validation passed, else false
+ */
+bool
+CH_AHV_check (const char *ahv_number)
+{
+ {
+ unsigned int checknum;
+ unsigned int next_ten;
+ const char *pos = &ahv_number[strlen (ahv_number) - 1];
+ bool phase = true;
+ unsigned int calculation = 0;
+
+ checknum = *pos - 48;
+ while (pos > ahv_number)
+ {
+ pos--;
+ if ('.' == *pos)
+ continue;
+ if (phase)
+ calculation += ((*pos - 48) * 3);
+ else
+ calculation += *pos - 48;
+ phase = ! phase;
+ }
+ /* round up to the next ten */
+ next_ten = ((calculation + 9) / 10) * 10;
+ calculation = next_ten - calculation;
+ return (checknum == calculation);
+ }
+}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [taler-anastasis] branch master updated: dlsym the validation logic,
gnunet <=