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_gns_service.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_rest_lib.h"
34 #include "microhttpd.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_identity_provider_service.h"
42 #define GNUNET_REST_API_NS_IDENTITY_PROVIDER "/idp"
47 #define GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE "/idp/issue"
50 * Check namespace TODO
52 #define GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK "/idp/check"
57 #define GNUNET_REST_API_NS_IDENTITY_OAUTH2_TOKEN "/idp/token"
60 * The URL parameter name in which the ticket must be provided
62 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET "ticket"
65 * The URL parameter name in which the nonce must be provided
67 #define GNUNET_IDENTITY_TOKEN_REQUEST_NONCE "nonce"
70 * State while collecting all egos
72 #define ID_REST_STATE_INIT 0
75 * Done collecting egos
77 #define ID_REST_STATE_POST_INIT 1
82 #define GNUNET_REST_JSONAPI_IDENTITY_TOKEN "token"
85 * URL parameter to create a GNUid token for a specific audience
87 #define GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST "audience"
90 * URL parameter to create a GNUid token for a specific issuer (EGO)
92 #define GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST "issuer"
95 * Attributes passed to issue request
97 #define GNUNET_IDENTITY_TOKEN_ATTR_LIST "requested_attrs"
100 * Token expiration string
102 #define GNUNET_IDENTITY_TOKEN_EXP_STRING "expiration"
107 #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
108 #define GNUNET_REST_ERROR_NO_DATA "No data"
111 * GNUid token lifetime
113 #define GNUNET_GNUID_TOKEN_EXPIRATION_MICROSECONDS 300000000
116 * The configuration handle
118 const struct GNUNET_CONFIGURATION_Handle *cfg;
121 * HTTP methods allows for this plugin
123 static char* allow_methods;
126 * @brief struct returned by the initialization function of the plugin
130 const struct GNUNET_CONFIGURATION_Handle *cfg;
141 struct EgoEntry *next;
146 struct EgoEntry *prev;
161 struct GNUNET_IDENTITY_Ego *ego;
170 struct EgoEntry *ego_head;
175 struct EgoEntry *ego_tail;
180 struct EgoEntry *ego_entry;
183 * Ptr to current ego private key
185 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
188 * Handle to the rest connection
190 struct RestConnectionDataHandle *conndata_handle;
193 * The processing state
198 * Handle to Identity service.
200 struct GNUNET_IDENTITY_Handle *identity_handle;
205 struct GNUNET_IDENTITY_Operation *op;
210 struct GNUNET_IDENTITY_PROVIDER_Handle *idp;
215 struct GNUNET_IDENTITY_PROVIDER_Operation *idp_op;
218 * Handle to NS service
220 struct GNUNET_NAMESTORE_Handle *ns_handle;
225 struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
230 struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
233 * Desired timeout for the lookup (default is no timeout).
235 struct GNUNET_TIME_Relative timeout;
238 * ID of a task associated with the resolution process.
240 struct GNUNET_SCHEDULER_Task * timeout_task;
243 * The plugin result processor
245 GNUNET_REST_ResultProcessor proc;
248 * The closure of the result processor
258 * Error response message
265 struct JsonApiObject *resp_object;
271 * Cleanup lookup handle
272 * @param handle Handle to clean up
275 cleanup_handle (struct RequestHandle *handle)
277 struct EgoEntry *ego_entry;
278 struct EgoEntry *ego_tmp;
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 if (NULL != handle->resp_object)
282 GNUNET_REST_jsonapi_object_delete (handle->resp_object);
283 if (NULL != handle->timeout_task)
284 GNUNET_SCHEDULER_cancel (handle->timeout_task);
285 if (NULL != handle->identity_handle)
286 GNUNET_IDENTITY_disconnect (handle->identity_handle);
287 if (NULL != handle->idp)
288 GNUNET_IDENTITY_PROVIDER_disconnect (handle->idp);
289 if (NULL != handle->ns_it)
290 GNUNET_NAMESTORE_zone_iteration_stop (handle->ns_it);
291 if (NULL != handle->ns_qe)
292 GNUNET_NAMESTORE_cancel (handle->ns_qe);
293 if (NULL != handle->ns_handle)
294 GNUNET_NAMESTORE_disconnect (handle->ns_handle);
295 if (NULL != handle->url)
296 GNUNET_free (handle->url);
297 if (NULL != handle->emsg)
298 GNUNET_free (handle->emsg);
299 for (ego_entry = handle->ego_head;
303 ego_entry = ego_entry->next;
304 GNUNET_free (ego_tmp->identifier);
305 GNUNET_free (ego_tmp->keystring);
306 GNUNET_free (ego_tmp);
308 GNUNET_free (handle);
313 * Task run on shutdown. Cleans up everything.
316 * @param tc scheduler context
320 const struct GNUNET_SCHEDULER_TaskContext *tc)
322 struct RequestHandle *handle = cls;
323 struct MHD_Response *resp;
326 GNUNET_asprintf (&json_error,
327 "{Error while processing request: %s}",
329 resp = GNUNET_REST_create_json_response (json_error);
330 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
331 cleanup_handle (handle);
332 GNUNET_free (json_error);
336 * Task run on shutdown. Cleans up everything.
339 * @param tc scheduler context
342 do_cleanup_handle_delayed (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 struct RequestHandle *handle = cls;
346 cleanup_handle(handle);
351 * Get a ticket for identity
352 * @param cls the handle
353 * @param ticket the ticket returned from the idp
356 token_creat_cont (void *cls,
357 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
359 struct JsonApiResource *json_resource;
360 struct RequestHandle *handle = cls;
361 struct MHD_Response *resp;
362 json_t *token_ticket_json;
368 handle->emsg = GNUNET_strdup ("Error in token issue");
369 GNUNET_SCHEDULER_add_now (&do_error, handle);
373 handle->resp_object = GNUNET_REST_jsonapi_object_new ();
374 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
376 ticket_str = GNUNET_IDENTITY_PROVIDER_ticket_to_string (ticket);
377 token_ticket_json = json_string (ticket_str);
378 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
379 GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
381 GNUNET_free (ticket_str);
382 json_decref (token_ticket_json);
383 GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource);
384 GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result_str);
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
386 resp = GNUNET_REST_create_json_response (result_str);
387 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
388 GNUNET_free (result_str);
389 GNUNET_SCHEDULER_add_now (&do_cleanup_handle_delayed, handle);
395 * Continueationf for token issue request
397 * @param con the Rest handle
398 * @param url the requested url
399 * @param cls the request handle
402 issue_token_cont (struct RestConnectionDataHandle *con,
406 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
409 struct RequestHandle *handle = cls;
410 struct EgoEntry *ego_entry;
411 struct GNUNET_HashCode key;
412 struct MHD_Response *resp;
413 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
414 struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
415 struct GNUNET_TIME_Relative etime_rel;
416 struct GNUNET_TIME_Absolute exp_time;
425 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url,
426 GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE))
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "URL invalid: %s\n", handle->url);
429 resp = GNUNET_REST_create_json_response (NULL);
430 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
431 cleanup_handle (handle);
436 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
437 strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
440 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Issuer not found\n");
444 GNUNET_SCHEDULER_add_now (&do_error, handle);
447 ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
451 GNUNET_SCHEDULER_add_now (&do_error, handle);
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Ego invalid: %s\n", ego_val);
455 for (ego_entry = handle->ego_head;
457 ego_entry = ego_entry->next)
459 if (0 != strcmp (ego_val, ego_entry->identifier))
461 egoname = ego_entry->identifier;
464 if (NULL == egoname || NULL == ego_entry)
466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Ego not found: %s\n", ego_val);
467 GNUNET_SCHEDULER_add_now (&do_error, handle);
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ego to issue token for: %s\n", egoname);
474 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_ATTR_LIST,
475 strlen (GNUNET_IDENTITY_TOKEN_ATTR_LIST),
480 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
483 handle->emsg = GNUNET_strdup ("Scopes missing!\n");
484 GNUNET_SCHEDULER_add_now (&do_error, handle);
487 scopes = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
492 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST,
493 strlen (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST),
497 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Audience missing!\n");
501 GNUNET_SCHEDULER_add_now (&do_error, handle);
504 audience = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience to issue token for: %s\n", audience);
508 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
509 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
511 GNUNET_STRINGS_string_to_data (audience,
514 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
518 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE,
519 strlen (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE),
522 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
525 handle->emsg = GNUNET_strdup ("Request nonce missing!\n");
526 GNUNET_SCHEDULER_add_now (&do_error, handle);
529 nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request nonce: %s\n", nonce_str);
532 sscanf (nonce_str, "%lu", &nonce);
534 //Get expiration for token from URL parameter
535 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_EXP_STRING,
536 strlen (GNUNET_IDENTITY_TOKEN_EXP_STRING),
540 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
543 exp_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
546 if (NULL == exp_str) {
547 handle->emsg = GNUNET_strdup ("No expiration given!\n");
548 GNUNET_SCHEDULER_add_now (&do_error, handle);
553 GNUNET_STRINGS_fancy_time_to_relative (exp_str,
556 handle->emsg = GNUNET_strdup ("Expiration invalid!\n");
557 GNUNET_SCHEDULER_add_now (&do_error, handle);
560 time = GNUNET_TIME_absolute_get().abs_value_us;
561 exp_time.abs_value_us = time + etime_rel.rel_value_us;
563 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
564 handle->idp_op = GNUNET_IDENTITY_PROVIDER_issue_token (handle->idp,
577 * Build a GNUid token for identity
579 * @param cls the request handle
580 * @param tc task context
583 return_token_list (void *cls,
584 const struct GNUNET_SCHEDULER_TaskContext *tc)
587 struct RequestHandle *handle = cls;
588 struct MHD_Response *resp;
590 GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result_str);
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
592 resp = GNUNET_REST_create_json_response (result_str);
593 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
594 GNUNET_free (result_str);
595 cleanup_handle (handle);
599 * Collect all tokens for an ego
601 * TODO move this into the identity-provider service
605 token_collect (void *cls,
606 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
608 unsigned int rd_count,
609 const struct GNUNET_GNSRECORD_Data *rd)
613 struct RequestHandle *handle = cls;
614 struct EgoEntry *ego_tmp;
615 struct JsonApiResource *json_resource;
616 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
622 ego_tmp = handle->ego_head;
623 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
626 GNUNET_free (ego_tmp->identifier);
627 GNUNET_free (ego_tmp->keystring);
628 GNUNET_free (ego_tmp);
630 if (NULL == handle->ego_head)
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token END\n");
634 handle->ns_it = NULL;
635 GNUNET_SCHEDULER_add_now (&return_token_list, handle);
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Next ego: %s\n", handle->ego_head->identifier);
640 priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
641 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
648 for (i = 0; i < rd_count; i++)
650 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN)
652 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data);
656 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
658 issuer = json_string (handle->ego_head->identifier);
659 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
660 GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
662 json_decref (issuer);
663 token = json_string (data);
664 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
665 GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
669 GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource);
674 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
680 * Respond to OPTIONS request
682 * @param con_handle the connection handle
684 * @param cls the RequestHandle
687 list_token_cont (struct RestConnectionDataHandle *con_handle,
692 struct GNUNET_HashCode key;
693 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
694 struct RequestHandle *handle = cls;
695 struct EgoEntry *ego_entry;
696 struct EgoEntry *ego_tmp;
698 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
699 strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
703 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No issuer given.\n");
707 GNUNET_SCHEDULER_add_now (&do_error, handle);
710 ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
712 //Remove non-matching egos
713 for (ego_entry = handle->ego_head;
717 ego_entry = ego_entry->next;
718 if (0 != strcmp (ego_val, ego_tmp->identifier))
720 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
723 GNUNET_free (ego_tmp->identifier);
724 GNUNET_free (ego_tmp->keystring);
725 GNUNET_free (ego_tmp);
728 handle->resp_object = GNUNET_REST_jsonapi_object_new ();
729 if (NULL == handle->ego_head)
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No results.\n");
733 GNUNET_SCHEDULER_add_now (&return_token_list, handle);
736 priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
737 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
738 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
746 * Return token to requestor
748 * @param cls request handle
749 * @param token the token
752 exchange_cont (void *cls,
753 const struct GNUNET_IDENTITY_PROVIDER_Token *token)
756 struct RequestHandle *handle = cls;
757 struct MHD_Response *resp;
761 root = json_object ();
762 token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token);
763 json_object_set_new (root, "identity_token", json_string (token_str));
764 json_object_set_new (root, "token_type", json_string ("jwt"));
765 GNUNET_free (token_str);
767 result = json_dumps (root, JSON_INDENT(1));
768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", result);
769 resp = GNUNET_REST_create_json_response (result);
770 GNUNET_free (result);
771 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
772 cleanup_handle (handle);
779 * Callback called when identity for token exchange has been found
781 * @param cls request handle
782 * @param ego the identity to use as issuer
783 * @param ctx user context
784 * @param name identity name
788 exchange_token_ticket_cb (void *cls,
789 struct GNUNET_IDENTITY_Ego *ego,
793 struct RequestHandle *handle = cls;
794 struct GNUNET_HashCode key;
795 struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket;
802 handle->emsg = GNUNET_strdup ("No identity found.");
803 GNUNET_SCHEDULER_add_now (&do_error, handle);
807 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
808 strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET),
812 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
815 handle->emsg = GNUNET_strdup ("No ticket given.");
816 GNUNET_SCHEDULER_add_now (&do_error, handle);
819 ticket_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
822 handle->priv_key = GNUNET_IDENTITY_ego_get_private_key (ego);
823 GNUNET_IDENTITY_PROVIDER_string_to_ticket (ticket_str,
826 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
827 handle->idp_op = GNUNET_IDENTITY_PROVIDER_exchange_ticket (handle->idp,
832 GNUNET_IDENTITY_PROVIDER_ticket_destroy (ticket);
839 * Respond to issue request
841 * @param con_handle the connection handle
843 * @param cls the RequestHandle
846 exchange_token_ticket_cont (struct RestConnectionDataHandle *con_handle,
850 struct RequestHandle *handle = cls;
853 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
855 &exchange_token_ticket_cb,
860 * Respond to OPTIONS request
862 * @param con_handle the connection handle
864 * @param cls the RequestHandle
867 options_cont (struct RestConnectionDataHandle *con_handle,
871 struct MHD_Response *resp;
872 struct RequestHandle *handle = cls;
874 //For now, independent of path return all options
875 resp = GNUNET_REST_create_json_response (NULL);
876 MHD_add_response_header (resp,
877 "Access-Control-Allow-Methods",
879 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
880 cleanup_handle (handle);
885 * Handle rest request
887 * @param handle the request handle
890 init_cont (struct RequestHandle *handle)
892 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
893 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE, &issue_token_cont},
894 //{MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK, &check_token_cont},
895 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &list_token_cont},
896 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &options_cont},
897 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_OAUTH2_TOKEN, &exchange_token_ticket_cont},
898 GNUNET_REST_HANDLER_END
901 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle))
903 handle->emsg = GNUNET_strdup ("Request unsupported");
904 GNUNET_SCHEDULER_add_now (&do_error, handle);
909 * If listing is enabled, prints information about the egos.
911 * This function is initially called for all egos and then again
912 * whenever a ego's identifier changes or if it is deleted. At the
913 * end of the initial pass over all egos, the function is once called
914 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
915 * be invoked in the future or that there was an error.
917 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
918 * this function is only called ONCE, and 'NULL' being passed in
919 * 'ego' does indicate an error (i.e. name is taken or no default
920 * value is known). If 'ego' is non-NULL and if '*ctx'
921 * is set in those callbacks, the value WILL be passed to a subsequent
922 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
923 * that one was not NULL).
925 * When an identity is renamed, this function is called with the
926 * (known) ego but the NEW identifier.
928 * When an identity is deleted, this function is called with the
929 * (known) ego and "NULL" for the 'identifier'. In this case,
930 * the 'ego' is henceforth invalid (and the 'ctx' should also be
934 * @param ego ego handle
935 * @param ctx context for application to store data for this ego
936 * (during the lifetime of this process, initially NULL)
937 * @param identifier identifier assigned by the user for this ego,
938 * NULL if the user just deleted the ego and it
939 * must thus no longer be used
943 struct GNUNET_IDENTITY_Ego *ego,
945 const char *identifier)
947 struct RequestHandle *handle = cls;
948 struct EgoEntry *ego_entry;
949 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
951 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
953 handle->state = ID_REST_STATE_POST_INIT;
957 if (ID_REST_STATE_INIT == handle->state) {
958 ego_entry = GNUNET_new (struct EgoEntry);
959 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
960 ego_entry->keystring =
961 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
962 ego_entry->ego = ego;
963 GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
964 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
970 * Function processing the REST call
972 * @param method HTTP method
973 * @param url URL of the HTTP request
974 * @param data body of the HTTP request (optional)
975 * @param data_size length of the body
976 * @param proc callback function for the result
977 * @param proc_cls closure for callback function
978 * @return GNUNET_OK if request accepted
981 rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle,
982 GNUNET_REST_ResultProcessor proc,
985 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
987 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
988 handle->proc_cls = proc_cls;
990 handle->state = ID_REST_STATE_INIT;
991 handle->conndata_handle = conndata_handle;
994 GNUNET_asprintf (&handle->url, "%s", conndata_handle->url);
995 if (handle->url[strlen (handle->url)-1] == '/')
996 handle->url[strlen (handle->url)-1] = '\0';
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1002 handle->timeout_task =
1003 GNUNET_SCHEDULER_add_delayed (handle->timeout,
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 * Entry point for the plugin.
1015 * @param cls Config info
1016 * @return NULL on error, otherwise the plugin context
1019 libgnunet_plugin_rest_identity_token_init (void *cls)
1021 static struct Plugin plugin;
1022 struct GNUNET_REST_Plugin *api;
1025 if (NULL != plugin.cfg)
1026 return NULL; /* can only initialize once! */
1027 memset (&plugin, 0, sizeof (struct Plugin));
1029 api = GNUNET_new (struct GNUNET_REST_Plugin);
1031 api->name = GNUNET_REST_API_NS_IDENTITY_PROVIDER;
1032 api->process_request = &rest_identity_process_request;
1033 GNUNET_asprintf (&allow_methods,
1034 "%s, %s, %s, %s, %s",
1035 MHD_HTTP_METHOD_GET,
1036 MHD_HTTP_METHOD_POST,
1037 MHD_HTTP_METHOD_PUT,
1038 MHD_HTTP_METHOD_DELETE,
1039 MHD_HTTP_METHOD_OPTIONS);
1041 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1042 _("Identity Token REST API initialized\n"));
1048 * Exit point from the plugin.
1050 * @param cls the plugin context (as returned by "init")
1051 * @return always NULL
1054 libgnunet_plugin_rest_identity_token_done (void *cls)
1056 struct GNUNET_REST_Plugin *api = cls;
1057 struct Plugin *plugin = api->cls;
1060 GNUNET_free_non_null (allow_methods);
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063 "Identity Token REST plugin is finished\n");
1067 /* end of plugin_rest_gns.c */