From: Schanzenbach, Martin Date: Tue, 4 Feb 2020 22:34:10 +0000 (+0100) Subject: towards better API X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=5fb277e8d012d687f4d2d032571cd4b57946bbfb;p=oweals%2Fgnunet.git towards better API --- diff --git a/src/include/gnunet_reclaim_attribute_lib.h b/src/include/gnunet_reclaim_attribute_lib.h index 937a4d8f4..6d3503950 100644 --- a/src/include/gnunet_reclaim_attribute_lib.h +++ b/src/include/gnunet_reclaim_attribute_lib.h @@ -586,6 +586,15 @@ GNUNET_RECLAIM_attestation_number_to_typename (uint32_t type); uint32_t GNUNET_RECLAIM_attestation_typename_to_number (const char *typename); +/** + * Convert an attestation type name to the corresponding number + * + * @param typename name to convert + * @return corresponding number, UINT32_MAX on error + */ +struct GNUNET_RECLAIM_AttributeList* +GNUNET_RECLAIM_attestation_get_attributes (const struct GNUNET_RECLAIM_Attestation *attest); + #if 0 /* keep Emacsens' auto-indent happy */ { diff --git a/src/include/gnunet_reclaim_plugin.h b/src/include/gnunet_reclaim_plugin.h index 4dd5252d2..ed62adf6c 100644 --- a/src/include/gnunet_reclaim_plugin.h +++ b/src/include/gnunet_reclaim_plugin.h @@ -165,6 +165,18 @@ typedef const char *(*GNUNET_RECLAIM_AttestationNumberToTypenameFunction) ( void *cls, uint32_t type); +/** + * Function called to convert a type number (i.e. 1) to the + * corresponding type string + * + * @param cls closure + * @param type number of a type to convert + * @return corresponding typestring, NULL on error + */ +typedef struct GNUNET_RECLAIM_AttributeList *(*GNUNET_RECLAIM_AttestationGetAttributesFunction) ( + void *cls, + const struct GNUNET_RECLAIM_Attestation *attest); + /** @@ -231,6 +243,12 @@ struct GNUNET_RECLAIM_AttestationPluginFunctions */ GNUNET_RECLAIM_AttestationNumberToTypenameFunction number_to_typename; + /** + * Attesation attributes. + */ + GNUNET_RECLAIM_AttestationGetAttributesFunction get_attributes; + + }; diff --git a/src/include/gnunet_reclaim_service.h b/src/include/gnunet_reclaim_service.h index c9c4fa5d1..ef9899dde 100644 --- a/src/include/gnunet_reclaim_service.h +++ b/src/include/gnunet_reclaim_service.h @@ -138,11 +138,13 @@ typedef void (*GNUNET_RECLAIM_AttributeTicketResult) ( * * @param cls The callback closure * @param identity The identity authoritative over the attributes - * @param attr The attribute + * @param attestation The attestation + * @param attributes the parsed attributes */ typedef void (*GNUNET_RECLAIM_AttestationResult) ( void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity, - const struct GNUNET_RECLAIM_Attestation *attestation); + const struct GNUNET_RECLAIM_Attestation *attestation, + const struct GNUNET_RECLAIM_AttributeList *attributes); /** diff --git a/src/reclaim-attribute/Makefile.am b/src/reclaim-attribute/Makefile.am index a1c220340..9617672ee 100644 --- a/src/reclaim-attribute/Makefile.am +++ b/src/reclaim-attribute/Makefile.am @@ -44,6 +44,8 @@ libgnunet_plugin_reclaim_attestation_jwt_la_SOURCES = \ plugin_reclaim_attestation_jwt.c libgnunet_plugin_reclaim_attestation_jwt_la_LIBADD = \ $(top_builddir)/src/util/libgnunetutil.la \ + libgnunetreclaimattribute.la \ + -ljansson\ $(LTLIBINTL) libgnunet_plugin_reclaim_attestation_jwt_la_LDFLAGS = \ $(GN_PLUGIN_LDFLAGS) diff --git a/src/reclaim-attribute/plugin_reclaim_attestation_jwt.c b/src/reclaim-attribute/plugin_reclaim_attestation_jwt.c index eb6043a66..8a67b18cd 100644 --- a/src/reclaim-attribute/plugin_reclaim_attestation_jwt.c +++ b/src/reclaim-attribute/plugin_reclaim_attestation_jwt.c @@ -30,7 +30,7 @@ #include "gnunet_util_lib.h" #include "gnunet_reclaim_plugin.h" #include - +#include /** * Convert the 'value' of an attestation to a string. @@ -142,6 +142,56 @@ jwt_number_to_typename (void *cls, uint32_t type) return jwt_attest_name_map[i].name; } +/** + * Parse a JWT and return the respective claim value as Attribute + * + * @param attest the jwt attestation + * @param claim the name of the claim in the JWT + * + * @return a GNUNET_RECLAIM_Attribute, containing the new value + */ +struct GNUNET_RECLAIM_AttributeList * +jwt_parse_attributes (void *cls, + const struct GNUNET_RECLAIM_Attestation *attest) +{ + char *jwt_string; + struct GNUNET_RECLAIM_AttributeList *attrs; + char delim[] = "."; + char *val_str = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n"); + char *decoded_jwt; + json_t *json_val; + json_error_t *json_err = NULL; + + if (GNUNET_RECLAIM_ATTESTATION_TYPE_JWT != attest->type) + return NULL; + attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); + + jwt_string = GNUNET_strdup (attest->data); + const char *jwt_body = strtok (jwt_string, delim); + jwt_body = strtok (NULL, delim); + GNUNET_STRINGS_base64_decode (jwt_body, strlen (jwt_body), + (void **) &decoded_jwt); + json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err); + const char *key; + json_t *value; + json_object_foreach (json_val, key, value) { + val_str = json_dumps (value, JSON_ENCODE_ANY); + GNUNET_RECLAIM_attribute_list_add (attrs, + key, + NULL, + GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,//FIXME + val_str, + strlen (val_str)); + GNUNET_free (val_str); + } + GNUNET_free (jwt_string); + //FIXME needed?? + return attrs; +} + + + /** * Entry point for the plugin. @@ -159,6 +209,7 @@ libgnunet_plugin_reclaim_attestation_jwt_init (void *cls) api->string_to_value = &jwt_string_to_value; api->typename_to_number = &jwt_typename_to_number; api->number_to_typename = &jwt_number_to_typename; + api->get_attributes = &jwt_parse_attributes; return api; } diff --git a/src/reclaim-attribute/reclaim_attestation.c b/src/reclaim-attribute/reclaim_attestation.c index 1a7776719..fd08b9b12 100644 --- a/src/reclaim-attribute/reclaim_attestation.c +++ b/src/reclaim-attribute/reclaim_attestation.c @@ -500,3 +500,21 @@ GNUNET_RECLAIM_attestation_deserialize (const char *data, size_t data_size) attestation->data = write_ptr; return attestation; } + +struct GNUNET_RECLAIM_AttributeList* +GNUNET_RECLAIM_attestation_get_attributes (const struct GNUNET_RECLAIM_Attestation *attest) +{ + unsigned int i; + struct Plugin *plugin; + struct GNUNET_RECLAIM_AttributeList *ret; + init (); + for (i = 0; i < num_plugins; i++) + { + plugin = attest_plugins[i]; + if (NULL != + (ret = plugin->api->get_attributes (plugin->api->cls, + attest))) + return ret; + } + return NULL; +} diff --git a/src/reclaim/gnunet-reclaim.c b/src/reclaim/gnunet-reclaim.c index 3e31fef4c..0170ceff2 100644 --- a/src/reclaim/gnunet-reclaim.c +++ b/src/reclaim/gnunet-reclaim.c @@ -553,13 +553,27 @@ iter_cb (void *cls, attr->data_size); attr_type = GNUNET_RECLAIM_attribute_number_to_typename (attr->type); id = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof(attr->id)); - fprintf (stdout, - "Name: %s; Value: %s (%s); Flag %u; ID: %s\n", - attr->name, - attr_str, - attr_type, - attr->flag, - id); + if (GNUNET_YES == GNUNET_RECLAIM_id_is_zero (&attr->attestation)) + { + fprintf (stdout, + "Name: %s; Value: %s (%s); Flag %u; ID: %s\n", + attr->name, + attr_str, + attr_type, + attr->flag, + id); + } + else + { + fprintf (stdout, + "Name: %s; Value: %s (%s); Flag %u; ID: %s\n", + attr->name, + attr_str, + attr_type, + attr->flag, + id); + + } GNUNET_free (id); } GNUNET_RECLAIM_get_attributes_next (attr_iterator); @@ -609,7 +623,8 @@ attest_iter_finished (void *cls) static void attest_iter_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity, - const struct GNUNET_RECLAIM_Attestation *attest) + const struct GNUNET_RECLAIM_Attestation *attest, + const struct GNUNET_RECLAIM_AttributeList *attrs) { char *attest_str; char *id; diff --git a/src/reclaim/gnunet-service-reclaim.c b/src/reclaim/gnunet-service-reclaim.c index 61d029665..99831172b 100644 --- a/src/reclaim/gnunet-service-reclaim.c +++ b/src/reclaim/gnunet-service-reclaim.c @@ -1900,7 +1900,10 @@ attest_iter_cb (void *cls, { struct Iterator *ai = cls; struct GNUNET_MQ_Envelope *env; + struct GNUNET_RECLAIM_AttributeList *attrs; + struct GNUNET_RECLAIM_Attestation *att; char *data_tmp; + char *attrs_size; if ((rd_count != 1) || (GNUNET_GNSRECORD_TYPE_RECLAIM_ATTESTATION != rd->record_type)) @@ -1908,6 +1911,10 @@ attest_iter_cb (void *cls, GNUNET_NAMESTORE_zone_iterator_next (ai->ns_it, 1); return; } + att = GNUNET_RECLAIM_attestation_deserialize (rd->data, + rd->data_size); + attrs = GNUNET_RECLAIM_attestation_get_attributes (att); + attrs_size = GNUNET_RECLAIM_attribute_list_serialize_get_size (attrs); struct AttestationResultMessage *arm; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found attestation under: %s\n", @@ -1915,13 +1922,17 @@ attest_iter_cb (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending ATTESTATION_RESULT message\n"); env = GNUNET_MQ_msg_extra (arm, - rd->data_size, + rd->data_size + attrs_size, GNUNET_MESSAGE_TYPE_RECLAIM_ATTESTATION_RESULT); arm->id = htonl (ai->request_id); arm->attestation_len = htons (rd->data_size); GNUNET_CRYPTO_ecdsa_key_get_public (zone, &arm->identity); data_tmp = (char *) &arm[1]; GNUNET_memcpy (data_tmp, rd->data, rd->data_size); + data_tmp += rd->data_size; + GNUNET_RECLAIM_attribute_list_serialize (attrs, + data_tmp); + GNUNET_MQ_send (ai->client->mq, env); } diff --git a/src/reclaim/plugin_rest_openid_connect.c b/src/reclaim/plugin_rest_openid_connect.c index 345dbeed6..b296f6d15 100644 --- a/src/reclaim/plugin_rest_openid_connect.c +++ b/src/reclaim/plugin_rest_openid_connect.c @@ -988,7 +988,8 @@ oidc_attest_collect_finished_cb (void *cls) static void oidc_attest_collect (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity, - const struct GNUNET_RECLAIM_Attestation *attest) + const struct GNUNET_RECLAIM_Attestation *attest, + const struct GNUNET_RECLAIM_AttributeList *attrs) { struct RequestHandle *handle = cls; struct GNUNET_RECLAIM_AttributeListEntry *le; diff --git a/src/reclaim/plugin_rest_reclaim.c b/src/reclaim/plugin_rest_reclaim.c index 6f7a5987b..cddee9b54 100644 --- a/src/reclaim/plugin_rest_reclaim.c +++ b/src/reclaim/plugin_rest_reclaim.c @@ -619,7 +619,8 @@ add_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle, static void attest_collect (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity, - const struct GNUNET_RECLAIM_Attestation *attest) + const struct GNUNET_RECLAIM_Attestation *attest, + const struct GNUNET_RECLAIM_AttributeList *attrs) { struct RequestHandle *handle = cls; json_t *attr_obj; diff --git a/src/reclaim/reclaim.h b/src/reclaim/reclaim.h index 7b5d7ab19..2cd07e861 100644 --- a/src/reclaim/reclaim.h +++ b/src/reclaim/reclaim.h @@ -178,6 +178,11 @@ struct AttestationResultMessage */ uint16_t attestation_len GNUNET_PACKED; + /** + * Length of serialized attribute data + */ + uint16_t attributes_len GNUNET_PACKED; + /** * always zero (for alignment) */ diff --git a/src/reclaim/reclaim_api.c b/src/reclaim/reclaim_api.c index 3820550c9..afab33320 100644 --- a/src/reclaim/reclaim_api.c +++ b/src/reclaim/reclaim_api.c @@ -801,11 +801,14 @@ handle_attestation_result (void *cls, const struct static struct GNUNET_CRYPTO_EcdsaPrivateKey identity_dummy; struct GNUNET_RECLAIM_Handle *h = cls; struct GNUNET_RECLAIM_AttestationIterator *it; + struct GNUNET_RECLAIM_AttributeList *attrs; struct GNUNET_RECLAIM_Operation *op; size_t att_len; + size_t attrs_len; uint32_t r_id = ntohl (msg->id); att_len = ntohs (msg->attestation_len); + attrs_len = ntohs (msg->attributes_len); LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing attestation result.\n"); @@ -836,7 +839,7 @@ handle_attestation_result (void *cls, const struct if (NULL != op) { if (NULL != op->at_cb) - op->at_cb (op->cls, NULL, NULL); + op->at_cb (op->cls, NULL, NULL, NULL); GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, op); free_op (op); } @@ -846,17 +849,20 @@ handle_attestation_result (void *cls, const struct { struct GNUNET_RECLAIM_Attestation *att; att = GNUNET_RECLAIM_attestation_deserialize ((char *) &msg[1], att_len); + char *read_ptr = ((char *) &msg[1]) + att_len; + attrs = GNUNET_RECLAIM_attribute_list_deserialize (read_ptr, attrs_len); if (NULL != it) { if (NULL != it->proc) - it->proc (it->proc_cls, &msg->identity, att); + it->proc (it->proc_cls, &msg->identity, att, attrs); } else if (NULL != op) { if (NULL != op->at_cb) - op->at_cb (op->cls, &msg->identity, att); + op->at_cb (op->cls, &msg->identity, att, attrs); } GNUNET_free (att); + GNUNET_RECLAIM_attribute_list_destroy (attrs); return; } GNUNET_assert (0);