2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @author Martin Schanzenbach
22 * @file identity/plugin_rest_identity.c
23 * @brief GNUnet Namestore REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include "gnunet_identity_service.h"
30 #include "gnunet_rest_lib.h"
31 #include "microhttpd.h"
33 #include "gnunet_signatures.h"
38 #define GNUNET_REST_API_NS_IDENTITY "/identity"
41 * State while collecting all egos
43 #define ID_REST_STATE_INIT 0
46 * Done collecting egos
48 #define ID_REST_STATE_POST_INIT 1
53 #define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego"
58 #define GNUNET_REST_JSONAPI_IDENTITY_NAME "name"
61 * Attribute to rename "name" TODO we changed id to the pubkey
62 * so this can be unified with "name"
64 #define GNUNET_REST_JSONAPI_IDENTITY_NEWNAME "newname"
67 * URL parameter to change the subsytem for ego
69 #define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem"
73 * URL parameter to create a GNUid token for a specific audience
75 #define GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN "create_token_for"
78 * Attribute containing the GNUid token if
79 * GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN was requested
81 #define GNUNET_REST_JSONAPI_IDENTITY_GNUID "gnuid_token"
86 #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
87 #define GNUNET_REST_ERROR_NO_DATA "No data"
90 * GNUid token lifetime
92 #define GNUNET_GNUID_TOKEN_EXPIRATION_SECONDS 360
95 * The configuration handle
97 const struct GNUNET_CONFIGURATION_Handle *cfg;
100 * HTTP methods allows for this plugin
102 static char* allow_methods;
105 * @brief struct returned by the initialization function of the plugin
109 const struct GNUNET_CONFIGURATION_Handle *cfg;
120 struct EgoEntry *next;
125 struct EgoEntry *prev;
140 struct GNUNET_IDENTITY_Ego *ego;
149 struct EgoEntry *ego_head;
154 struct EgoEntry *ego_tail;
157 * Handle to the rest connection
159 struct RestConnectionDataHandle *conndata_handle;
162 * The processing state
167 * Handle to GNS service.
169 struct GNUNET_IDENTITY_Handle *identity_handle;
174 struct GNUNET_IDENTITY_Operation *op;
177 * Desired timeout for the lookup (default is no timeout).
179 struct GNUNET_TIME_Relative timeout;
182 * ID of a task associated with the resolution process.
184 struct GNUNET_SCHEDULER_Task * timeout_task;
187 * The plugin result processor
189 GNUNET_REST_ResultProcessor proc;
192 * The closure of the result processor
197 * The name to look up
202 * The subsystem set from REST
212 * The data from the REST request
217 * the length of the REST data
227 * Error response message
235 * Cleanup lookup handle
236 * @param handle Handle to clean up
239 cleanup_handle (struct RequestHandle *handle)
241 struct EgoEntry *ego_entry;
242 struct EgoEntry *ego_tmp;
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 if (NULL != handle->name)
246 GNUNET_free (handle->name);
247 if (NULL != handle->timeout_task)
248 GNUNET_SCHEDULER_cancel (handle->timeout_task);
249 if (NULL != handle->identity_handle)
250 GNUNET_IDENTITY_disconnect (handle->identity_handle);
251 if (NULL != handle->subsys)
252 GNUNET_free (handle->subsys);
253 if (NULL != handle->url)
254 GNUNET_free (handle->url);
255 if (NULL != handle->emsg)
256 GNUNET_free (handle->emsg);
257 for (ego_entry = handle->ego_head;
261 ego_entry = ego_entry->next;
262 GNUNET_free (ego_tmp->identifier);
263 GNUNET_free (ego_tmp->keystring);
264 GNUNET_free (ego_tmp);
266 GNUNET_free (handle);
271 * Task run on shutdown. Cleans up everything.
274 * @param tc scheduler context
278 const struct GNUNET_SCHEDULER_TaskContext *tc)
280 struct RequestHandle *handle = cls;
281 struct MHD_Response *resp;
284 GNUNET_asprintf (&json_error,
285 "{Error while processing request: %s}",
288 resp = GNUNET_REST_create_json_response (json_error);
289 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
290 cleanup_handle (handle);
291 GNUNET_free (json_error);
295 * Build a GNUid token for identity
296 * @param handle the handle
297 * @param ego_entry the ego to build the token for
298 * @param name name of the ego
299 * @param token_aud token audience
300 * @param token the resulting gnuid token
303 make_gnuid_token (struct RequestHandle *handle,
304 struct EgoEntry *ego_entry,
306 const char *token_aud,
313 char *payload_base64;
317 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
318 struct GNUNET_CRYPTO_EcdsaSignature sig;
319 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
321 time = GNUNET_TIME_absolute_get().abs_value_us;
322 header = json_object ();
323 json_object_set_new (header, "alg", json_string ("ED512"));
324 json_object_set_new (header, "typ", json_string ("JWT"));
326 payload = json_object ();
327 json_object_set_new (payload, "iss", json_string (ego_entry->keystring));
328 json_object_set_new (payload, "sub", json_string (name));
329 json_object_set_new (payload, "nbf", json_integer (time));
330 json_object_set_new (payload, "iat", json_integer (time));
331 json_object_set_new (payload, "exp", json_integer (time+GNUNET_GNUID_TOKEN_EXPIRATION_SECONDS));
332 json_object_set_new (payload, "aud", json_string (token_aud));
333 header_str = json_dumps (header, JSON_COMPACT);
334 GNUNET_STRINGS_base64_encode (header_str,
337 char* padding = strtok(header_base64, "=");
338 while (NULL != padding)
339 padding = strtok(NULL, "=");
341 payload_str = json_dumps (payload, JSON_COMPACT);
342 GNUNET_STRINGS_base64_encode (payload_str,
343 strlen (payload_str),
345 padding = strtok(payload_base64, "=");
346 while (NULL != padding)
347 padding = strtok(NULL, "=");
349 GNUNET_asprintf (token, "%s,%s", header_base64, payload_base64);
350 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
352 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
355 htonl (strlen (*token) + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
356 purpose->purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN);
357 memcpy (&purpose[1], *token, strlen (*token));
358 if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_sign (priv_key,
362 GNUNET_free (*token);
363 sig_str = GNUNET_STRINGS_data_to_string_alloc (&sig,
364 sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
365 GNUNET_asprintf (token, "%s.%s.%s",
366 header_base64, payload_base64, sig_str);
367 GNUNET_free (sig_str);
368 GNUNET_free (header_str);
369 GNUNET_free (header_base64);
370 GNUNET_free (payload_str);
371 GNUNET_free (payload_base64);
372 GNUNET_free (purpose);
373 json_decref (header);
374 json_decref (payload);
378 * Callback for IDENTITY_get()
380 * @param cls the RequestHandle
381 * @param ego the Ego found
382 * @param ctx the context
383 * @param name the id of the ego
386 get_ego_for_subsys (void *cls,
387 struct GNUNET_IDENTITY_Ego *ego,
391 struct RequestHandle *handle = cls;
392 struct JsonApiObject *json_object;
393 struct JsonApiResource *json_resource;
394 struct EgoEntry *ego_entry;
395 struct MHD_Response *resp;
399 json_object = GNUNET_REST_jsonapi_object_new ();
401 for (ego_entry = handle->ego_head;
403 ego_entry = ego_entry->next)
405 if ( (NULL != name) && (0 != strcmp (name, ego_entry->identifier)) )
409 json_resource = GNUNET_REST_jsonapi_resource_new
410 (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring);
411 name_json = json_string (ego_entry->identifier);
412 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
413 GNUNET_REST_JSONAPI_IDENTITY_NAME,
415 json_decref (name_json);
416 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
419 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
421 GNUNET_REST_jsonapi_object_delete (json_object);
422 handle->emsg = GNUNET_strdup("No identity matches results!");
423 GNUNET_SCHEDULER_add_now (&do_error, handle);
426 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
428 resp = GNUNET_REST_create_json_response (result_str);
429 GNUNET_REST_jsonapi_object_delete (json_object);
430 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
431 GNUNET_free (result_str);
432 cleanup_handle (handle);
436 * Create a response with requested ego(s)
438 * @param con the Rest handle
439 * @param url the requested url
440 * @param cls the request handle
443 ego_info_response (struct RestConnectionDataHandle *con,
450 char *create_token_for;
453 struct RequestHandle *handle = cls;
454 struct EgoEntry *ego_entry;
455 struct GNUNET_HashCode key;
456 struct MHD_Response *resp;
457 struct JsonApiObject *json_object;
458 struct JsonApiResource *json_resource;
462 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
464 resp = GNUNET_REST_create_json_response (NULL);
465 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
466 cleanup_handle (handle);
471 if (strlen (GNUNET_REST_API_NS_IDENTITY) < strlen (handle->url))
473 keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY)+1];
475 for (ego_entry = handle->ego_head;
477 ego_entry = ego_entry->next)
479 if ( (NULL != keystring) && (0 != strcmp (keystring, ego_entry->keystring)) )
481 egoname = ego_entry->identifier;
485 if ( NULL == egoname ) {
486 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM,
487 strlen (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM),
490 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
493 subsys_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
495 if (NULL != subsys_val)
497 GNUNET_asprintf (&handle->subsys, "%s", subsys_val);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsys_val);
499 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
508 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN,
509 strlen (GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN),
513 create_token_for = NULL;
515 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
517 create_token_for = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
520 json_object = GNUNET_REST_jsonapi_object_new ();
523 for (ego_entry = handle->ego_head;
525 ego_entry = ego_entry->next)
527 if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
529 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO,
530 ego_entry->keystring);
531 name_str = json_string (ego_entry->identifier);
532 GNUNET_REST_jsonapi_resource_add_attr (
534 GNUNET_REST_JSONAPI_IDENTITY_NAME,
536 json_decref (name_str);
537 if (NULL != create_token_for)
539 make_gnuid_token (handle,
541 ego_entry->identifier,
544 token_str = json_string (token);
546 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
547 GNUNET_REST_JSONAPI_IDENTITY_GNUID,
549 json_decref (token_str);
551 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
553 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
555 GNUNET_REST_jsonapi_object_delete (json_object);
556 handle->emsg = GNUNET_strdup ("No identities found!");
557 GNUNET_SCHEDULER_add_now (&do_error, handle);
560 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
562 resp = GNUNET_REST_create_json_response (result_str);
563 GNUNET_REST_jsonapi_object_delete (json_object);
564 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
565 GNUNET_free (result_str);
566 cleanup_handle (handle);
570 * Processing finished
572 * @param cls request handle
573 * @param emsg error message
576 do_finished (void *cls, const char *emsg)
578 struct RequestHandle *handle = cls;
579 struct MHD_Response *resp;
584 handle->emsg = GNUNET_strdup (emsg);
585 GNUNET_SCHEDULER_add_now (&do_error, handle);
588 resp = GNUNET_REST_create_json_response (NULL);
589 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
590 cleanup_handle (handle);
596 * @param con rest handle
598 * @param cls request handle
601 ego_create_cont (struct RestConnectionDataHandle *con,
605 struct RequestHandle *handle = cls;
606 struct EgoEntry *ego_entry;
607 struct MHD_Response *resp;
608 struct JsonApiObject *json_obj;
609 struct JsonApiResource *json_res;
610 json_t *egoname_json;
612 char term_data[handle->data_size+1];
614 if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url))
616 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
617 GNUNET_SCHEDULER_add_now (&do_error, handle);
620 if (0 >= handle->data_size)
622 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
623 GNUNET_SCHEDULER_add_now (&do_error, handle);
626 term_data[handle->data_size] = '\0';
627 memcpy (term_data, handle->data, handle->data_size);
628 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
629 if (NULL == json_obj)
631 GNUNET_SCHEDULER_add_now (&do_error, handle);
634 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
636 GNUNET_REST_jsonapi_object_delete (json_obj);
637 handle->emsg = GNUNET_strdup ("Provided resource count invalid");
638 GNUNET_SCHEDULER_add_now (&do_error, handle);
641 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
642 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
644 GNUNET_REST_jsonapi_object_delete (json_obj);
645 resp = GNUNET_REST_create_json_response (NULL);
646 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
647 cleanup_handle (handle);
650 egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME);
651 if (!json_is_string (egoname_json))
653 GNUNET_REST_jsonapi_object_delete (json_obj);
654 handle->emsg = GNUNET_strdup ("No name provided");
655 GNUNET_SCHEDULER_add_now (&do_error, handle);
658 egoname = json_string_value (egoname_json);
659 for (ego_entry = handle->ego_head;
661 ego_entry = ego_entry->next)
663 if (0 == strcasecmp (egoname, ego_entry->identifier))
665 GNUNET_REST_jsonapi_object_delete (json_obj);
666 resp = GNUNET_REST_create_json_response (NULL);
667 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
668 cleanup_handle (handle);
672 GNUNET_asprintf (&handle->name, "%s", egoname);
673 GNUNET_REST_jsonapi_object_delete (json_obj);
674 handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
682 * Handle ego edit request
684 * @param con rest connection handle
685 * @param url the url that is requested
686 * @param cls the RequestHandle
689 ego_edit_cont (struct RestConnectionDataHandle *con,
693 struct JsonApiObject *json_obj;
694 struct JsonApiResource *json_res;
695 struct RequestHandle *handle = cls;
696 struct EgoEntry *ego_entry;
697 struct MHD_Response *resp;
700 const char *keystring;
703 char term_data[handle->data_size+1];
704 int ego_exists = GNUNET_NO;
706 if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url))
708 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
709 GNUNET_SCHEDULER_add_now (&do_error, handle);
713 keystring = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
715 for (ego_entry = handle->ego_head;
717 ego_entry = ego_entry->next)
719 if (0 != strcasecmp (keystring, ego_entry->keystring))
721 ego_exists = GNUNET_YES;
725 if (GNUNET_NO == ego_exists)
727 resp = GNUNET_REST_create_json_response (NULL);
728 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
729 cleanup_handle (handle);
733 if (0 >= handle->data_size)
735 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
736 GNUNET_SCHEDULER_add_now (&do_error, handle);
740 term_data[handle->data_size] = '\0';
741 memcpy (term_data, handle->data, handle->data_size);
742 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
744 if (NULL == json_obj)
746 handle->emsg = GNUNET_strdup ("Data invalid");
747 GNUNET_SCHEDULER_add_now (&do_error, handle);
751 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
753 GNUNET_REST_jsonapi_object_delete (json_obj);
754 handle->emsg = GNUNET_strdup ("Resource amount invalid");
755 GNUNET_SCHEDULER_add_now (&do_error, handle);
758 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
760 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
762 GNUNET_REST_jsonapi_object_delete (json_obj);
763 handle->emsg = GNUNET_strdup ("Resource type invalid");
764 GNUNET_SCHEDULER_add_now (&do_error, handle);
769 name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res,
770 GNUNET_REST_JSONAPI_IDENTITY_NEWNAME);
771 if ((NULL != name_json) && json_is_string (name_json))
773 newname = json_string_value (name_json);
774 for (ego_entry = handle->ego_head;
776 ego_entry = ego_entry->next)
778 if (0 == strcasecmp (newname, ego_entry->identifier) &&
779 0 != strcasecmp (keystring, ego_entry->keystring))
781 //Ego with same name not allowed
782 GNUNET_REST_jsonapi_object_delete (json_obj);
783 resp = GNUNET_REST_create_json_response (NULL);
784 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
785 cleanup_handle (handle);
789 handle->op = GNUNET_IDENTITY_rename (handle->identity_handle,
790 ego_entry->identifier,
794 GNUNET_REST_jsonapi_object_delete (json_obj);
799 subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
800 if ( (NULL != subsys_json) && json_is_string (subsys_json))
802 subsys = json_string_value (subsys_json);
803 GNUNET_asprintf (&handle->subsys, "%s", subsys);
804 GNUNET_REST_jsonapi_object_delete (json_obj);
805 handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
812 GNUNET_REST_jsonapi_object_delete (json_obj);
813 handle->emsg = GNUNET_strdup ("Subsystem not provided");
814 GNUNET_SCHEDULER_add_now (&do_error, handle);
818 ego_delete_cont (struct RestConnectionDataHandle *con_handle,
822 const char *keystring;
823 struct EgoEntry *ego_entry;
824 struct MHD_Response *resp;
825 struct RequestHandle *handle = cls;
826 int ego_exists = GNUNET_NO;
828 if (strlen (GNUNET_REST_API_NS_IDENTITY) >= strlen (handle->url))
830 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
831 GNUNET_SCHEDULER_add_now (&do_error, handle);
835 keystring = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
836 for (ego_entry = handle->ego_head;
838 ego_entry = ego_entry->next)
840 if (0 != strcasecmp (keystring, ego_entry->keystring))
842 ego_exists = GNUNET_YES;
845 if (GNUNET_NO == ego_exists)
847 resp = GNUNET_REST_create_json_response (NULL);
848 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
849 cleanup_handle (handle);
852 handle->op = GNUNET_IDENTITY_delete (handle->identity_handle,
853 ego_entry->identifier,
861 * Respond to OPTIONS request
863 * @param con_handle the connection handle
865 * @param cls the RequestHandle
868 options_cont (struct RestConnectionDataHandle *con_handle,
872 struct MHD_Response *resp;
873 struct RequestHandle *handle = cls;
875 //For now, independent of path return all options
876 resp = GNUNET_REST_create_json_response (NULL);
877 MHD_add_response_header (resp,
878 "Access-Control-Allow-Methods",
880 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
881 cleanup_handle (handle);
886 * Handle rest request
888 * @param handle the request handle
891 init_cont (struct RequestHandle *handle)
893 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
894 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_info_response},
895 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create_cont},
896 {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &ego_edit_cont},
897 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete_cont},
898 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY, &options_cont},
899 GNUNET_REST_HANDLER_END
902 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle))
904 handle->emsg = GNUNET_strdup ("Request unsupported");
905 GNUNET_SCHEDULER_add_now (&do_error, handle);
910 * If listing is enabled, prints information about the egos.
912 * This function is initially called for all egos and then again
913 * whenever a ego's identifier changes or if it is deleted. At the
914 * end of the initial pass over all egos, the function is once called
915 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
916 * be invoked in the future or that there was an error.
918 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
919 * this function is only called ONCE, and 'NULL' being passed in
920 * 'ego' does indicate an error (i.e. name is taken or no default
921 * value is known). If 'ego' is non-NULL and if '*ctx'
922 * is set in those callbacks, the value WILL be passed to a subsequent
923 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
924 * that one was not NULL).
926 * When an identity is renamed, this function is called with the
927 * (known) ego but the NEW identifier.
929 * When an identity is deleted, this function is called with the
930 * (known) ego and "NULL" for the 'identifier'. In this case,
931 * the 'ego' is henceforth invalid (and the 'ctx' should also be
935 * @param ego ego handle
936 * @param ctx context for application to store data for this ego
937 * (during the lifetime of this process, initially NULL)
938 * @param identifier identifier assigned by the user for this ego,
939 * NULL if the user just deleted the ego and it
940 * must thus no longer be used
944 struct GNUNET_IDENTITY_Ego *ego,
946 const char *identifier)
948 struct RequestHandle *handle = cls;
949 struct EgoEntry *ego_entry;
950 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
952 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
954 handle->state = ID_REST_STATE_POST_INIT;
958 if (ID_REST_STATE_INIT == handle->state) {
959 ego_entry = GNUNET_new (struct EgoEntry);
960 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
961 ego_entry->keystring =
962 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
963 ego_entry->ego = ego;
964 GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
965 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
971 * Function processing the REST call
973 * @param method HTTP method
974 * @param url URL of the HTTP request
975 * @param data body of the HTTP request (optional)
976 * @param data_size length of the body
977 * @param proc callback function for the result
978 * @param proc_cls closure for callback function
979 * @return GNUNET_OK if request accepted
982 rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle,
983 GNUNET_REST_ResultProcessor proc,
986 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
990 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
992 handle->proc_cls = proc_cls;
994 handle->state = ID_REST_STATE_INIT;
995 handle->conndata_handle = conndata_handle;
996 handle->data = conndata_handle->data;
997 handle->data_size = conndata_handle->data_size;
998 handle->method = conndata_handle->method;
999 GNUNET_asprintf (&handle->url, "%s", conndata_handle->url);
1000 if (handle->url[strlen (handle->url)-1] == '/')
1001 handle->url[strlen (handle->url)-1] = '\0';
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1007 GNUNET_strdup ("Timeout");
1008 handle->timeout_task =
1009 GNUNET_SCHEDULER_add_delayed (handle->timeout,
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 * Entry point for the plugin.
1021 * @param cls Config info
1022 * @return NULL on error, otherwise the plugin context
1025 libgnunet_plugin_rest_identity_init (void *cls)
1027 static struct Plugin plugin;
1028 struct GNUNET_REST_Plugin *api;
1031 if (NULL != plugin.cfg)
1032 return NULL; /* can only initialize once! */
1033 memset (&plugin, 0, sizeof (struct Plugin));
1035 api = GNUNET_new (struct GNUNET_REST_Plugin);
1037 api->name = GNUNET_REST_API_NS_IDENTITY;
1038 api->process_request = &rest_identity_process_request;
1039 GNUNET_asprintf (&allow_methods,
1040 "%s, %s, %s, %s, %s",
1041 MHD_HTTP_METHOD_GET,
1042 MHD_HTTP_METHOD_POST,
1043 MHD_HTTP_METHOD_PUT,
1044 MHD_HTTP_METHOD_DELETE,
1045 MHD_HTTP_METHOD_OPTIONS);
1047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1048 _("Identity REST API initialized\n"));
1054 * Exit point from the plugin.
1056 * @param cls the plugin context (as returned by "init")
1057 * @return always NULL
1060 libgnunet_plugin_rest_identity_done (void *cls)
1062 struct GNUNET_REST_Plugin *api = cls;
1063 struct Plugin *plugin = api->cls;
1066 GNUNET_free_non_null (allow_methods);
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Identity REST plugin is finished\n");
1073 /* end of plugin_rest_gns.c */