2 This file is part of GNUnet
3 Copyright (C) 2013, 2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file reclaim-attribute/plugin_reclaim_attestation_gnuid.c
23 * @brief reclaim-attribute-plugin-gnuid attribute plugin to provide the API for
27 * @author Martin Schanzenbach
30 #include "gnunet_util_lib.h"
31 #include "gnunet_reclaim_plugin.h"
36 * Convert the 'value' of an attestation to a string.
38 * @param cls closure, unused
39 * @param type type of the attestation
40 * @param data value in binary encoding
41 * @param data_size number of bytes in @a data
42 * @return NULL on error, otherwise human-readable representation of the value
45 jwt_value_to_string (void *cls,
52 case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT:
53 return GNUNET_strndup (data, data_size);
62 * Convert human-readable version of a 'value' of an attestation to the binary
65 * @param cls closure, unused
66 * @param type type of the attestation
67 * @param s human-readable string
68 * @param data set to value in binary encoding (will be allocated)
69 * @param data_size set to number of bytes in @a data
70 * @return #GNUNET_OK on success
73 jwt_string_to_value (void *cls,
83 case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT:
84 *data = GNUNET_strdup (s);
85 *data_size = strlen (s);
95 * Mapping of attestation type numbers to human-readable
96 * attestation type names.
102 } jwt_attest_name_map[] = { { "JWT", GNUNET_RECLAIM_ATTESTATION_TYPE_JWT },
103 { NULL, UINT32_MAX } };
106 * Convert a type name to the corresponding number.
108 * @param cls closure, unused
109 * @param jwt_typename name to convert
110 * @return corresponding number, UINT32_MAX on error
113 jwt_typename_to_number (void *cls, const char *jwt_typename)
118 while ((NULL != jwt_attest_name_map[i].name) &&
119 (0 != strcasecmp (jwt_typename, jwt_attest_name_map[i].name)))
121 return jwt_attest_name_map[i].number;
126 * Convert a type number (i.e. 1) to the corresponding type string
128 * @param cls closure, unused
129 * @param type number of a type to convert
130 * @return corresponding typestring, NULL on error
133 jwt_number_to_typename (void *cls, uint32_t type)
138 while ((NULL != jwt_attest_name_map[i].name) && (type !=
139 jwt_attest_name_map[i].
142 return jwt_attest_name_map[i].name;
146 * Parse a JWT and return the respective claim value as Attribute
148 * @param attest the jwt attestation
149 * @param claim the name of the claim in the JWT
151 * @return a GNUNET_RECLAIM_Attribute, containing the new value
153 struct GNUNET_RECLAIM_AttributeList *
154 jwt_parse_attributes (void *cls,
155 const struct GNUNET_RECLAIM_Attestation *attest)
158 struct GNUNET_RECLAIM_AttributeList *attrs;
160 char *val_str = NULL;
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n");
164 json_error_t *json_err = NULL;
166 if (GNUNET_RECLAIM_ATTESTATION_TYPE_JWT != attest->type)
168 attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
170 jwt_string = GNUNET_strdup (attest->data);
171 const char *jwt_body = strtok (jwt_string, delim);
172 jwt_body = strtok (NULL, delim);
173 GNUNET_STRINGS_base64_decode (jwt_body, strlen (jwt_body),
174 (void **) &decoded_jwt);
175 json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err);
178 json_object_foreach (json_val, key, value) {
179 val_str = json_dumps (value, JSON_ENCODE_ANY);
180 GNUNET_RECLAIM_attribute_list_add (attrs,
183 GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,//FIXME
186 GNUNET_free (val_str);
188 GNUNET_free (jwt_string);
197 * Entry point for the plugin.
200 * @return the exported block API
203 libgnunet_plugin_reclaim_attestation_jwt_init (void *cls)
205 struct GNUNET_RECLAIM_AttestationPluginFunctions *api;
207 api = GNUNET_new (struct GNUNET_RECLAIM_AttestationPluginFunctions);
208 api->value_to_string = &jwt_value_to_string;
209 api->string_to_value = &jwt_string_to_value;
210 api->typename_to_number = &jwt_typename_to_number;
211 api->number_to_typename = &jwt_number_to_typename;
212 api->get_attributes = &jwt_parse_attributes;
218 * Exit point from the plugin.
220 * @param cls the return value from #libgnunet_plugin_block_test_init()
224 libgnunet_plugin_reclaim_attestation_jwt_done (void *cls)
226 struct GNUNET_RECLAIM_AttestationPluginFunctions *api = cls;
233 /* end of plugin_reclaim_attestation_type_gnuid.c */