2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
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 "gnunet_jsonapi_lib.h"
35 #include "gnunet_jsonapi_util.h"
36 #include "microhttpd.h"
39 #include "gnunet_signatures.h"
40 #include "gnunet_identity_provider_service.h"
45 #define GNUNET_REST_API_NS_IDENTITY_PROVIDER "/idp"
50 #define GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE "/idp/issue"
53 * Check namespace TODO
55 #define GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK "/idp/check"
60 #define GNUNET_REST_API_NS_IDENTITY_OAUTH2_TOKEN "/idp/token"
63 * The parameter name in which the ticket must be provided
65 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET "ticket"
68 * The parameter name in which the expected nonce must be provided
70 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE "expected_nonce"
73 * The parameter name in which the ticket must be provided
75 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN "token"
78 * The URL parameter name in which the nonce must be provided
80 #define GNUNET_IDENTITY_TOKEN_REQUEST_NONCE "nonce"
83 * State while collecting all egos
85 #define ID_REST_STATE_INIT 0
88 * Done collecting egos
90 #define ID_REST_STATE_POST_INIT 1
95 #define GNUNET_REST_JSONAPI_IDENTITY_TOKEN "token"
98 * URL parameter to create a GNUid token for a specific audience
100 #define GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST "audience"
103 * URL parameter to create a GNUid token for a specific issuer (EGO)
105 #define GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST "issuer"
108 * Attributes passed to issue request
110 #define GNUNET_IDENTITY_TOKEN_ATTR_LIST "requested_attrs"
113 * Token expiration string
115 #define GNUNET_IDENTITY_TOKEN_EXP_STRING "expiration"
120 #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
121 #define GNUNET_REST_ERROR_NO_DATA "No data"
124 * GNUid token lifetime
126 #define GNUNET_GNUID_TOKEN_EXPIRATION_MICROSECONDS 300000000
129 * The configuration handle
131 const struct GNUNET_CONFIGURATION_Handle *cfg;
134 * HTTP methods allows for this plugin
136 static char* allow_methods;
139 * @brief struct returned by the initialization function of the plugin
143 const struct GNUNET_CONFIGURATION_Handle *cfg;
154 struct EgoEntry *next;
159 struct EgoEntry *prev;
174 struct GNUNET_IDENTITY_Ego *ego;
183 struct EgoEntry *ego_head;
188 struct EgoEntry *ego_tail;
193 struct EgoEntry *ego_entry;
196 * Ptr to current ego private key
198 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
201 * Handle to the rest connection
203 struct GNUNET_REST_RequestHandle *conndata_handle;
206 * The processing state
211 * Handle to Identity service.
213 struct GNUNET_IDENTITY_Handle *identity_handle;
218 struct GNUNET_IDENTITY_Operation *op;
223 struct GNUNET_IDENTITY_PROVIDER_Handle *idp;
228 struct GNUNET_IDENTITY_PROVIDER_Operation *idp_op;
231 * Handle to NS service
233 struct GNUNET_NAMESTORE_Handle *ns_handle;
238 struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
243 struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
246 * Desired timeout for the lookup (default is no timeout).
248 struct GNUNET_TIME_Relative timeout;
251 * ID of a task associated with the resolution process.
253 struct GNUNET_SCHEDULER_Task *timeout_task;
256 * The plugin result processor
258 GNUNET_REST_ResultProcessor proc;
261 * The closure of the result processor
271 * Error response message
283 struct GNUNET_JSONAPI_Document *resp_object;
289 * Cleanup lookup handle
290 * @param handle Handle to clean up
293 cleanup_handle (struct RequestHandle *handle)
295 struct EgoEntry *ego_entry;
296 struct EgoEntry *ego_tmp;
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 if (NULL != handle->resp_object)
300 GNUNET_JSONAPI_document_delete (handle->resp_object);
301 if (NULL != handle->timeout_task)
302 GNUNET_SCHEDULER_cancel (handle->timeout_task);
303 if (NULL != handle->identity_handle)
304 GNUNET_IDENTITY_disconnect (handle->identity_handle);
305 if (NULL != handle->idp)
306 GNUNET_IDENTITY_PROVIDER_disconnect (handle->idp);
307 if (NULL != handle->ns_it)
308 GNUNET_NAMESTORE_zone_iteration_stop (handle->ns_it);
309 if (NULL != handle->ns_qe)
310 GNUNET_NAMESTORE_cancel (handle->ns_qe);
311 if (NULL != handle->ns_handle)
312 GNUNET_NAMESTORE_disconnect (handle->ns_handle);
313 if (NULL != handle->url)
314 GNUNET_free (handle->url);
315 if (NULL != handle->emsg)
316 GNUNET_free (handle->emsg);
317 for (ego_entry = handle->ego_head;
321 ego_entry = ego_entry->next;
322 GNUNET_free (ego_tmp->identifier);
323 GNUNET_free (ego_tmp->keystring);
324 GNUNET_free (ego_tmp);
326 GNUNET_free (handle);
331 * Task run on error, sends error message. Cleans up everything.
333 * @param cls the `struct RequestHandle`
338 struct RequestHandle *handle = cls;
339 struct MHD_Response *resp;
342 GNUNET_asprintf (&json_error,
343 "{Error while processing request: %s}",
345 resp = GNUNET_REST_create_json_response (json_error);
346 handle->proc (handle->proc_cls, resp, handle->response_code);
347 cleanup_handle (handle);
348 GNUNET_free (json_error);
352 * Task run on timeout, sends error message. Cleans up everything.
354 * @param cls the `struct RequestHandle`
357 do_timeout (void *cls)
359 struct RequestHandle *handle = cls;
361 handle->timeout_task = NULL;
367 * Task run on shutdown. Cleans up everything.
372 do_cleanup_handle_delayed (void *cls)
374 struct RequestHandle *handle = cls;
376 cleanup_handle (handle);
381 * Get a ticket for identity
382 * @param cls the handle
383 * @param ticket the ticket returned from the idp
386 token_creat_cont (void *cls,
388 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
389 const struct GNUNET_IDENTITY_PROVIDER_Token *token)
391 struct GNUNET_JSONAPI_Resource *json_resource;
392 struct RequestHandle *handle = cls;
393 struct MHD_Response *resp;
402 handle->emsg = GNUNET_strdup ("Error in token issue");
403 GNUNET_SCHEDULER_add_now (&do_error, handle);
407 handle->resp_object = GNUNET_JSONAPI_document_new ();
408 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
410 ticket_str = GNUNET_IDENTITY_PROVIDER_ticket_to_string (ticket);
411 token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token);
412 ticket_json = json_string (ticket_str);
413 token_json = json_string (token_str);
414 GNUNET_JSONAPI_resource_add_attr (json_resource,
415 GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
417 GNUNET_JSONAPI_resource_add_attr (json_resource,
418 GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN,
420 GNUNET_free (ticket_str);
421 GNUNET_free (token_str);
422 json_decref (ticket_json);
423 json_decref (token_json);
424 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
426 GNUNET_JSONAPI_document_serialize (handle->resp_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 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
430 GNUNET_free (result_str);
431 GNUNET_SCHEDULER_add_now (&do_cleanup_handle_delayed, handle);
436 * Continueationf for token issue request
438 * @param con the Rest handle
439 * @param url the requested url
440 * @param cls the request handle
443 issue_token_cont (struct GNUNET_REST_RequestHandle *con,
447 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
450 struct RequestHandle *handle = cls;
451 struct EgoEntry *ego_entry;
452 struct GNUNET_HashCode key;
453 struct MHD_Response *resp;
454 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
455 struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
456 struct GNUNET_TIME_Relative etime_rel;
457 struct GNUNET_TIME_Absolute exp_time;
466 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url,
467 GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE))
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "URL invalid: %s\n", handle->url);
470 resp = GNUNET_REST_create_json_response (NULL);
471 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
472 cleanup_handle (handle);
477 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
478 strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
481 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485 "Issuer not found\n");
486 GNUNET_SCHEDULER_add_now (&do_error, handle);
489 ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
493 GNUNET_SCHEDULER_add_now (&do_error, handle);
494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499 for (ego_entry = handle->ego_head;
501 ego_entry = ego_entry->next)
503 if (0 != strcmp (ego_val, ego_entry->identifier))
505 egoname = ego_entry->identifier;
508 if ( (NULL == egoname) ||
509 (NULL == ego_entry) )
511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512 "Ego not found: %s\n",
514 GNUNET_SCHEDULER_add_now (&do_error, handle);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Ego to issue token for: %s\n",
523 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_ATTR_LIST,
524 strlen (GNUNET_IDENTITY_TOKEN_ATTR_LIST),
529 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
532 handle->emsg = GNUNET_strdup ("Scopes missing!\n");
533 GNUNET_SCHEDULER_add_now (&do_error, handle);
536 scopes = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
541 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST,
542 strlen (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST),
546 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 "Audience missing!\n");
551 GNUNET_SCHEDULER_add_now (&do_error, handle);
554 audience = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Audience to issue token for: %s\n",
560 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
561 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
563 GNUNET_STRINGS_string_to_data (audience,
566 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
570 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE,
571 strlen (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE),
574 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
577 handle->emsg = GNUNET_strdup ("Request nonce missing!\n");
578 GNUNET_SCHEDULER_add_now (&do_error, handle);
581 nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Request nonce: %s\n",
586 sscanf (nonce_str, "%"SCNu64, &nonce);
588 //Get expiration for token from URL parameter
589 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_EXP_STRING,
590 strlen (GNUNET_IDENTITY_TOKEN_EXP_STRING),
594 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
597 exp_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
600 if (NULL == exp_str) {
601 handle->emsg = GNUNET_strdup ("No expiration given!\n");
602 GNUNET_SCHEDULER_add_now (&do_error, handle);
607 GNUNET_STRINGS_fancy_time_to_relative (exp_str,
610 handle->emsg = GNUNET_strdup ("Expiration invalid!\n");
611 GNUNET_SCHEDULER_add_now (&do_error, handle);
614 time = GNUNET_TIME_absolute_get().abs_value_us;
615 exp_time.abs_value_us = time + etime_rel.rel_value_us;
617 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
618 handle->idp_op = GNUNET_IDENTITY_PROVIDER_issue_token (handle->idp,
631 * Build a GNUid token for identity
633 * @param cls the request handle
636 return_token_list (void *cls)
639 struct RequestHandle *handle = cls;
640 struct MHD_Response *resp;
642 GNUNET_JSONAPI_document_serialize (handle->resp_object, &result_str);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
644 resp = GNUNET_REST_create_json_response (result_str);
645 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
646 GNUNET_free (result_str);
647 cleanup_handle (handle);
651 * Collect all tokens for an ego
653 * TODO move this into the identity-provider service
657 token_collect (void *cls,
658 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
660 unsigned int rd_count,
661 const struct GNUNET_GNSRECORD_Data *rd)
665 struct RequestHandle *handle = cls;
666 struct EgoEntry *ego_tmp;
667 struct GNUNET_JSONAPI_Resource *json_resource;
668 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
674 ego_tmp = handle->ego_head;
675 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
678 GNUNET_free (ego_tmp->identifier);
679 GNUNET_free (ego_tmp->keystring);
680 GNUNET_free (ego_tmp);
682 if (NULL == handle->ego_head)
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token END\n");
686 handle->ns_it = NULL;
687 GNUNET_SCHEDULER_add_now (&return_token_list, handle);
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Next ego: %s\n", handle->ego_head->identifier);
692 priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
693 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
700 for (i = 0; i < rd_count; i++)
702 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN)
704 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data);
708 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
710 issuer = json_string (handle->ego_head->identifier);
711 GNUNET_JSONAPI_resource_add_attr (json_resource,
712 GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
714 json_decref (issuer);
715 token = json_string (data);
716 GNUNET_JSONAPI_resource_add_attr (json_resource,
717 GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
721 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
726 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
732 * Respond to OPTIONS request
734 * @param con_handle the connection handle
736 * @param cls the RequestHandle
739 list_token_cont (struct GNUNET_REST_RequestHandle *con_handle,
744 struct GNUNET_HashCode key;
745 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
746 struct RequestHandle *handle = cls;
747 struct EgoEntry *ego_entry;
748 struct EgoEntry *ego_tmp;
750 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
751 strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
755 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No issuer given.\n");
759 GNUNET_SCHEDULER_add_now (&do_error, handle);
762 ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
764 //Remove non-matching egos
765 for (ego_entry = handle->ego_head;
769 ego_entry = ego_entry->next;
770 if (0 != strcmp (ego_val, ego_tmp->identifier))
772 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
775 GNUNET_free (ego_tmp->identifier);
776 GNUNET_free (ego_tmp->keystring);
777 GNUNET_free (ego_tmp);
780 handle->resp_object = GNUNET_JSONAPI_document_new ();
781 if (NULL == handle->ego_head)
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No results.\n");
785 GNUNET_SCHEDULER_add_now (&return_token_list, handle);
788 priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
789 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
790 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
798 * Return token to requestor
800 * @param cls request handle
801 * @param token the token
804 exchange_cont (void *cls,
805 const struct GNUNET_IDENTITY_PROVIDER_Token *token,
806 uint64_t ticket_nonce)
809 struct RequestHandle *handle = cls;
810 struct MHD_Response *resp;
811 struct GNUNET_HashCode key;
815 uint64_t expected_nonce;
818 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE,
819 strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE),
823 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
826 handle->emsg = GNUNET_strdup ("No nonce given.");
827 GNUNET_SCHEDULER_add_now (&do_error, handle);
830 nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
832 GNUNET_assert (1 == sscanf (nonce_str, "%"SCNu64, &expected_nonce));
834 if (ticket_nonce != expected_nonce)
836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837 "Ticket nonce %lu does not match expected nonce %lu\n",
838 ticket_nonce, expected_nonce);
839 handle->emsg = GNUNET_strdup ("Ticket nonce does not match expected nonce\n");
840 GNUNET_SCHEDULER_add_now (&do_error, handle);
844 root = json_object ();
845 token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token);
846 json_object_set_new (root, "token", json_string (token_str));
847 json_object_set_new (root, "token_type", json_string ("jwt"));
848 GNUNET_free (token_str);
850 result = json_dumps (root, JSON_INDENT(1));
851 resp = GNUNET_REST_create_json_response (result);
852 GNUNET_free (result);
853 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
854 cleanup_handle (handle);
861 * Callback called when identity for token exchange has been found
863 * @param cls request handle
864 * @param ego the identity to use as issuer
865 * @param ctx user context
866 * @param name identity name
870 exchange_token_ticket_cb (void *cls,
871 struct GNUNET_IDENTITY_Ego *ego,
875 struct RequestHandle *handle = cls;
876 struct GNUNET_HashCode key;
877 struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket;
884 handle->emsg = GNUNET_strdup ("No identity found.");
885 GNUNET_SCHEDULER_add_now (&do_error, handle);
890 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
891 strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET),
895 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
898 handle->emsg = GNUNET_strdup ("No ticket given.");
899 GNUNET_SCHEDULER_add_now (&do_error, handle);
902 ticket_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
904 handle->priv_key = GNUNET_IDENTITY_ego_get_private_key (ego);
905 GNUNET_IDENTITY_PROVIDER_string_to_ticket (ticket_str,
908 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
909 handle->idp_op = GNUNET_IDENTITY_PROVIDER_exchange_ticket (handle->idp,
914 GNUNET_IDENTITY_PROVIDER_ticket_destroy (ticket);
921 * Respond to issue request
923 * @param con_handle the connection handle
925 * @param cls the RequestHandle
928 exchange_token_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
932 struct RequestHandle *handle = cls;
935 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
937 &exchange_token_ticket_cb,
942 * Respond to OPTIONS request
944 * @param con_handle the connection handle
946 * @param cls the RequestHandle
949 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
953 struct MHD_Response *resp;
954 struct RequestHandle *handle = cls;
956 //For now, independent of path return all options
957 resp = GNUNET_REST_create_json_response (NULL);
958 MHD_add_response_header (resp,
959 "Access-Control-Allow-Methods",
961 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
962 cleanup_handle (handle);
967 * Handle rest request
969 * @param handle the request handle
972 init_cont (struct RequestHandle *handle)
974 struct GNUNET_REST_RequestHandlerError err;
975 static const struct GNUNET_REST_RequestHandler handlers[] = {
976 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE, &issue_token_cont},
977 //{MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK, &check_token_cont},
978 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &list_token_cont},
979 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &options_cont},
980 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_OAUTH2_TOKEN, &exchange_token_ticket_cont},
981 GNUNET_REST_HANDLER_END
984 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle,
989 handle->response_code = err.error_code;
990 GNUNET_SCHEDULER_add_now (&do_error, handle);
995 * If listing is enabled, prints information about the egos.
997 * This function is initially called for all egos and then again
998 * whenever a ego's identifier changes or if it is deleted. At the
999 * end of the initial pass over all egos, the function is once called
1000 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
1001 * be invoked in the future or that there was an error.
1003 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
1004 * this function is only called ONCE, and 'NULL' being passed in
1005 * 'ego' does indicate an error (i.e. name is taken or no default
1006 * value is known). If 'ego' is non-NULL and if '*ctx'
1007 * is set in those callbacks, the value WILL be passed to a subsequent
1008 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
1009 * that one was not NULL).
1011 * When an identity is renamed, this function is called with the
1012 * (known) ego but the NEW identifier.
1014 * When an identity is deleted, this function is called with the
1015 * (known) ego and "NULL" for the 'identifier'. In this case,
1016 * the 'ego' is henceforth invalid (and the 'ctx' should also be
1019 * @param cls closure
1020 * @param ego ego handle
1021 * @param ctx context for application to store data for this ego
1022 * (during the lifetime of this process, initially NULL)
1023 * @param identifier identifier assigned by the user for this ego,
1024 * NULL if the user just deleted the ego and it
1025 * must thus no longer be used
1028 list_ego (void *cls,
1029 struct GNUNET_IDENTITY_Ego *ego,
1031 const char *identifier)
1033 struct RequestHandle *handle = cls;
1034 struct EgoEntry *ego_entry;
1035 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
1037 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
1039 handle->state = ID_REST_STATE_POST_INIT;
1043 if (ID_REST_STATE_INIT == handle->state) {
1044 ego_entry = GNUNET_new (struct EgoEntry);
1045 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
1046 ego_entry->keystring =
1047 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
1048 ego_entry->ego = ego;
1049 ego_entry->identifier = GNUNET_strdup (identifier);
1050 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
1056 * Function processing the REST call
1058 * @param method HTTP method
1059 * @param url URL of the HTTP request
1060 * @param data body of the HTTP request (optional)
1061 * @param data_size length of the body
1062 * @param proc callback function for the result
1063 * @param proc_cls closure for callback function
1064 * @return GNUNET_OK if request accepted
1067 rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
1068 GNUNET_REST_ResultProcessor proc,
1071 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
1073 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1074 handle->proc_cls = proc_cls;
1075 handle->proc = proc;
1076 handle->state = ID_REST_STATE_INIT;
1077 handle->conndata_handle = conndata_handle;
1080 handle->url = GNUNET_strdup (conndata_handle->url);
1081 if (handle->url[strlen (handle->url)-1] == '/')
1082 handle->url[strlen (handle->url)-1] = '\0';
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1088 handle->timeout_task =
1089 GNUNET_SCHEDULER_add_delayed (handle->timeout,
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 * Entry point for the plugin.
1099 * @param cls Config info
1100 * @return NULL on error, otherwise the plugin context
1103 libgnunet_plugin_rest_identity_provider_init (void *cls)
1105 static struct Plugin plugin;
1106 struct GNUNET_REST_Plugin *api;
1109 if (NULL != plugin.cfg)
1110 return NULL; /* can only initialize once! */
1111 memset (&plugin, 0, sizeof (struct Plugin));
1113 api = GNUNET_new (struct GNUNET_REST_Plugin);
1115 api->name = GNUNET_REST_API_NS_IDENTITY_PROVIDER;
1116 api->process_request = &rest_identity_process_request;
1117 GNUNET_asprintf (&allow_methods,
1118 "%s, %s, %s, %s, %s",
1119 MHD_HTTP_METHOD_GET,
1120 MHD_HTTP_METHOD_POST,
1121 MHD_HTTP_METHOD_PUT,
1122 MHD_HTTP_METHOD_DELETE,
1123 MHD_HTTP_METHOD_OPTIONS);
1125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1126 _("Identity Token REST API initialized\n"));
1132 * Exit point from the plugin.
1134 * @param cls the plugin context (as returned by "init")
1135 * @return always NULL
1138 libgnunet_plugin_rest_identity_provider_done (void *cls)
1140 struct GNUNET_REST_Plugin *api = cls;
1141 struct Plugin *plugin = api->cls;
1144 GNUNET_free_non_null (allow_methods);
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Identity Token REST plugin is finished\n");
1151 /* end of plugin_rest_gns.c */