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 * Attributes passed to issue request
115 #define GNUNET_IDENTITY_TOKEN_V_ATTR_LIST "requested_verified_attrs"
119 * Token expiration string
121 #define GNUNET_IDENTITY_TOKEN_EXP_STRING "expiration"
126 #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
127 #define GNUNET_REST_ERROR_NO_DATA "No data"
130 * GNUid token lifetime
132 #define GNUNET_GNUID_TOKEN_EXPIRATION_MICROSECONDS 300000000
135 * The configuration handle
137 const struct GNUNET_CONFIGURATION_Handle *cfg;
140 * HTTP methods allows for this plugin
142 static char* allow_methods;
145 * @brief struct returned by the initialization function of the plugin
149 const struct GNUNET_CONFIGURATION_Handle *cfg;
160 struct EgoEntry *next;
165 struct EgoEntry *prev;
180 struct GNUNET_IDENTITY_Ego *ego;
189 struct EgoEntry *ego_head;
194 struct EgoEntry *ego_tail;
199 struct EgoEntry *ego_entry;
202 * Ptr to current ego private key
204 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
207 * Handle to the rest connection
209 struct GNUNET_REST_RequestHandle *conndata_handle;
212 * The processing state
217 * Handle to Identity service.
219 struct GNUNET_IDENTITY_Handle *identity_handle;
224 struct GNUNET_IDENTITY_Operation *op;
229 struct GNUNET_IDENTITY_PROVIDER_Handle *idp;
234 struct GNUNET_IDENTITY_PROVIDER_Operation *idp_op;
237 * Handle to NS service
239 struct GNUNET_NAMESTORE_Handle *ns_handle;
244 struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
249 struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
252 * Desired timeout for the lookup (default is no timeout).
254 struct GNUNET_TIME_Relative timeout;
257 * ID of a task associated with the resolution process.
259 struct GNUNET_SCHEDULER_Task *timeout_task;
262 * The plugin result processor
264 GNUNET_REST_ResultProcessor proc;
267 * The closure of the result processor
277 * Error response message
289 struct GNUNET_JSONAPI_Document *resp_object;
295 * Cleanup lookup handle
296 * @param handle Handle to clean up
299 cleanup_handle (struct RequestHandle *handle)
301 struct EgoEntry *ego_entry;
302 struct EgoEntry *ego_tmp;
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 if (NULL != handle->resp_object)
306 GNUNET_JSONAPI_document_delete (handle->resp_object);
307 if (NULL != handle->timeout_task)
308 GNUNET_SCHEDULER_cancel (handle->timeout_task);
309 if (NULL != handle->identity_handle)
310 GNUNET_IDENTITY_disconnect (handle->identity_handle);
311 if (NULL != handle->idp)
312 GNUNET_IDENTITY_PROVIDER_disconnect (handle->idp);
313 if (NULL != handle->ns_it)
314 GNUNET_NAMESTORE_zone_iteration_stop (handle->ns_it);
315 if (NULL != handle->ns_qe)
316 GNUNET_NAMESTORE_cancel (handle->ns_qe);
317 if (NULL != handle->ns_handle)
318 GNUNET_NAMESTORE_disconnect (handle->ns_handle);
319 if (NULL != handle->url)
320 GNUNET_free (handle->url);
321 if (NULL != handle->emsg)
322 GNUNET_free (handle->emsg);
323 for (ego_entry = handle->ego_head;
327 ego_entry = ego_entry->next;
328 GNUNET_free (ego_tmp->identifier);
329 GNUNET_free (ego_tmp->keystring);
330 GNUNET_free (ego_tmp);
332 GNUNET_free (handle);
337 * Task run on error, sends error message. Cleans up everything.
339 * @param cls the `struct RequestHandle`
344 struct RequestHandle *handle = cls;
345 struct MHD_Response *resp;
348 GNUNET_asprintf (&json_error,
349 "{Error while processing request: %s}",
351 resp = GNUNET_REST_create_response (json_error);
352 handle->proc (handle->proc_cls, resp, handle->response_code);
353 cleanup_handle (handle);
354 GNUNET_free (json_error);
358 * Task run on timeout, sends error message. Cleans up everything.
360 * @param cls the `struct RequestHandle`
363 do_timeout (void *cls)
365 struct RequestHandle *handle = cls;
367 handle->timeout_task = NULL;
373 * Task run on shutdown. Cleans up everything.
378 do_cleanup_handle_delayed (void *cls)
380 struct RequestHandle *handle = cls;
382 cleanup_handle (handle);
387 * Get a ticket for identity
388 * @param cls the handle
389 * @param ticket the ticket returned from the idp
392 token_creat_cont (void *cls,
394 const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
395 const struct GNUNET_IDENTITY_PROVIDER_Token *token)
397 struct GNUNET_JSONAPI_Resource *json_resource;
398 struct RequestHandle *handle = cls;
399 struct MHD_Response *resp;
408 handle->emsg = GNUNET_strdup ("Error in token issue");
409 GNUNET_SCHEDULER_add_now (&do_error, handle);
413 handle->resp_object = GNUNET_JSONAPI_document_new ();
414 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
416 ticket_str = GNUNET_IDENTITY_PROVIDER_ticket_to_string (ticket);
417 token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token);
418 ticket_json = json_string (ticket_str);
419 token_json = json_string (token_str);
420 GNUNET_JSONAPI_resource_add_attr (json_resource,
421 GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
423 GNUNET_JSONAPI_resource_add_attr (json_resource,
424 GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN,
426 GNUNET_free (ticket_str);
427 GNUNET_free (token_str);
428 json_decref (ticket_json);
429 json_decref (token_json);
430 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
432 GNUNET_JSONAPI_document_serialize (handle->resp_object, &result_str);
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
434 resp = GNUNET_REST_create_response (result_str);
435 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
436 GNUNET_free (result_str);
437 GNUNET_SCHEDULER_add_now (&do_cleanup_handle_delayed, handle);
442 * Continueationf for token issue request
444 * @param con the Rest handle
445 * @param url the requested url
446 * @param cls the request handle
449 issue_token_cont (struct GNUNET_REST_RequestHandle *con,
453 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
456 struct RequestHandle *handle = cls;
457 struct EgoEntry *ego_entry;
458 struct GNUNET_HashCode key;
459 struct MHD_Response *resp;
460 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
461 struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
462 struct GNUNET_TIME_Relative etime_rel;
463 struct GNUNET_TIME_Absolute exp_time;
473 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url,
474 GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE))
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "URL invalid: %s\n", handle->url);
477 resp = GNUNET_REST_create_response (NULL);
478 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
479 cleanup_handle (handle);
484 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
485 strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
488 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
492 "Issuer not found\n");
493 GNUNET_SCHEDULER_add_now (&do_error, handle);
496 ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
500 GNUNET_SCHEDULER_add_now (&do_error, handle);
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
506 for (ego_entry = handle->ego_head;
508 ego_entry = ego_entry->next)
510 if (0 != strcmp (ego_val, ego_entry->identifier))
512 egoname = ego_entry->identifier;
515 if ( (NULL == egoname) ||
516 (NULL == ego_entry) )
518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
519 "Ego not found: %s\n",
521 GNUNET_SCHEDULER_add_now (&do_error, handle);
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Ego to issue token for: %s\n",
530 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_ATTR_LIST,
531 strlen (GNUNET_IDENTITY_TOKEN_ATTR_LIST),
536 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
539 handle->emsg = GNUNET_strdup ("Scopes missing!\n");
540 GNUNET_SCHEDULER_add_now (&do_error, handle);
543 scopes = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
547 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_V_ATTR_LIST,
548 strlen (GNUNET_IDENTITY_TOKEN_V_ATTR_LIST),
553 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
556 vattrs = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
563 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST,
564 strlen (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST),
568 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 "Audience missing!\n");
573 GNUNET_SCHEDULER_add_now (&do_error, handle);
576 audience = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Audience to issue token for: %s\n",
582 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
583 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
585 GNUNET_STRINGS_string_to_data (audience,
588 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
592 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE,
593 strlen (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE),
596 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
599 handle->emsg = GNUNET_strdup ("Request nonce missing!\n");
600 GNUNET_SCHEDULER_add_now (&do_error, handle);
603 nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Request nonce: %s\n",
608 GNUNET_assert (1 == sscanf (nonce_str, "%"SCNu64, &nonce));
610 //Get expiration for token from URL parameter
611 GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_EXP_STRING,
612 strlen (GNUNET_IDENTITY_TOKEN_EXP_STRING),
616 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
619 exp_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
622 if (NULL == exp_str) {
623 handle->emsg = GNUNET_strdup ("No expiration given!\n");
624 GNUNET_SCHEDULER_add_now (&do_error, handle);
629 GNUNET_STRINGS_fancy_time_to_relative (exp_str,
632 handle->emsg = GNUNET_strdup ("Expiration invalid!\n");
633 GNUNET_SCHEDULER_add_now (&do_error, handle);
636 time = GNUNET_TIME_absolute_get().abs_value_us;
637 exp_time.abs_value_us = time + etime_rel.rel_value_us;
639 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
640 handle->idp_op = GNUNET_IDENTITY_PROVIDER_issue_token (handle->idp,
654 * Build a GNUid token for identity
656 * @param cls the request handle
659 return_token_list (void *cls)
662 struct RequestHandle *handle = cls;
663 struct MHD_Response *resp;
665 GNUNET_JSONAPI_document_serialize (handle->resp_object, &result_str);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
667 resp = GNUNET_REST_create_response (result_str);
668 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
669 GNUNET_free (result_str);
670 cleanup_handle (handle);
675 token_collect_error_cb (void *cls)
677 struct RequestHandle *handle = cls;
684 * Collect all tokens for an ego
686 * TODO move this into the identity-provider service
690 token_collect (void *cls,
691 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
693 unsigned int rd_count,
694 const struct GNUNET_GNSRECORD_Data *rd);
698 token_collect_finished_cb (void *cls)
700 struct RequestHandle *handle = cls;
701 struct EgoEntry *ego_tmp;
702 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
704 ego_tmp = handle->ego_head;
705 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
708 GNUNET_free (ego_tmp->identifier);
709 GNUNET_free (ego_tmp->keystring);
710 GNUNET_free (ego_tmp);
712 if (NULL == handle->ego_head)
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token END\n");
716 handle->ns_it = NULL;
717 GNUNET_SCHEDULER_add_now (&return_token_list, handle);
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 handle->ego_head->identifier);
724 priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
725 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
727 &token_collect_error_cb,
731 &token_collect_finished_cb,
737 * Collect all tokens for an ego
739 * TODO move this into the identity-provider service
743 token_collect (void *cls,
744 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
746 unsigned int rd_count,
747 const struct GNUNET_GNSRECORD_Data *rd)
749 struct RequestHandle *handle = cls;
752 struct GNUNET_JSONAPI_Resource *json_resource;
756 for (i = 0; i < rd_count; i++)
758 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN)
760 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data);
764 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
766 issuer = json_string (handle->ego_head->identifier);
767 GNUNET_JSONAPI_resource_add_attr (json_resource,
768 GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
770 json_decref (issuer);
771 token = json_string (data);
772 GNUNET_JSONAPI_resource_add_attr (json_resource,
773 GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
777 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
782 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
788 * Respond to OPTIONS request
790 * @param con_handle the connection handle
792 * @param cls the RequestHandle
795 list_token_cont (struct GNUNET_REST_RequestHandle *con_handle,
800 struct GNUNET_HashCode key;
801 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
802 struct RequestHandle *handle = cls;
803 struct EgoEntry *ego_entry;
804 struct EgoEntry *ego_tmp;
806 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
807 strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
811 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No issuer given.\n");
815 GNUNET_SCHEDULER_add_now (&do_error, handle);
818 ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
820 //Remove non-matching egos
821 for (ego_entry = handle->ego_head;
825 ego_entry = ego_entry->next;
826 if (0 != strcmp (ego_val, ego_tmp->identifier))
828 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
831 GNUNET_free (ego_tmp->identifier);
832 GNUNET_free (ego_tmp->keystring);
833 GNUNET_free (ego_tmp);
836 handle->resp_object = GNUNET_JSONAPI_document_new ();
837 if (NULL == handle->ego_head)
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No results.\n");
841 GNUNET_SCHEDULER_add_now (&return_token_list, handle);
844 priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
845 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
846 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
848 &token_collect_error_cb,
852 &token_collect_finished_cb,
858 * Return token to requestor
860 * @param cls request handle
861 * @param token the token
864 exchange_cont (void *cls,
865 const struct GNUNET_IDENTITY_PROVIDER_Token *token,
866 uint64_t ticket_nonce)
869 struct RequestHandle *handle = cls;
870 struct MHD_Response *resp;
871 struct GNUNET_HashCode key;
875 uint64_t expected_nonce;
878 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE,
879 strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE),
883 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
886 handle->emsg = GNUNET_strdup ("No nonce given.");
887 GNUNET_SCHEDULER_add_now (&do_error, handle);
890 nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
892 GNUNET_assert (1 == sscanf (nonce_str, "%"SCNu64, &expected_nonce));
894 if (ticket_nonce != expected_nonce)
896 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
897 "Ticket nonce %"SCNu64" does not match expected nonce %"SCNu64"\n",
898 ticket_nonce, expected_nonce);
899 handle->emsg = GNUNET_strdup ("Ticket nonce does not match expected nonce\n");
900 GNUNET_SCHEDULER_add_now (&do_error, handle);
904 root = json_object ();
905 token_str = GNUNET_IDENTITY_PROVIDER_token_to_string (token);
906 json_object_set_new (root, "token", json_string (token_str));
907 json_object_set_new (root, "token_type", json_string ("jwt"));
908 GNUNET_free (token_str);
910 result = json_dumps (root, JSON_INDENT(1));
911 resp = GNUNET_REST_create_response (result);
912 GNUNET_free (result);
913 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
914 cleanup_handle (handle);
921 * Callback called when identity for token exchange has been found
923 * @param cls request handle
924 * @param ego the identity to use as issuer
925 * @param ctx user context
926 * @param name identity name
930 exchange_token_ticket_cb (void *cls,
931 struct GNUNET_IDENTITY_Ego *ego,
935 struct RequestHandle *handle = cls;
936 struct GNUNET_HashCode key;
937 struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket;
944 handle->emsg = GNUNET_strdup ("No identity found.");
945 GNUNET_SCHEDULER_add_now (&do_error, handle);
950 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
951 strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET),
955 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
958 handle->emsg = GNUNET_strdup ("No ticket given.");
959 GNUNET_SCHEDULER_add_now (&do_error, handle);
962 ticket_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
964 handle->priv_key = GNUNET_IDENTITY_ego_get_private_key (ego);
965 GNUNET_IDENTITY_PROVIDER_string_to_ticket (ticket_str,
968 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
969 handle->idp_op = GNUNET_IDENTITY_PROVIDER_exchange_ticket (handle->idp,
974 GNUNET_IDENTITY_PROVIDER_ticket_destroy (ticket);
981 * Respond to issue request
983 * @param con_handle the connection handle
985 * @param cls the RequestHandle
988 exchange_token_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
992 struct RequestHandle *handle = cls;
995 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
997 &exchange_token_ticket_cb,
1002 * Respond to OPTIONS request
1004 * @param con_handle the connection handle
1005 * @param url the url
1006 * @param cls the RequestHandle
1009 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
1013 struct MHD_Response *resp;
1014 struct RequestHandle *handle = cls;
1016 //For now, independent of path return all options
1017 resp = GNUNET_REST_create_response (NULL);
1018 MHD_add_response_header (resp,
1019 "Access-Control-Allow-Methods",
1021 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1022 cleanup_handle (handle);
1027 * Handle rest request
1029 * @param handle the request handle
1032 init_cont (struct RequestHandle *handle)
1034 struct GNUNET_REST_RequestHandlerError err;
1035 static const struct GNUNET_REST_RequestHandler handlers[] = {
1036 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE, &issue_token_cont},
1037 //{MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK, &check_token_cont},
1038 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &list_token_cont},
1039 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER, &options_cont},
1040 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_OAUTH2_TOKEN, &exchange_token_ticket_cont},
1041 GNUNET_REST_HANDLER_END
1044 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle,
1049 handle->response_code = err.error_code;
1050 GNUNET_SCHEDULER_add_now (&do_error, handle);
1055 * If listing is enabled, prints information about the egos.
1057 * This function is initially called for all egos and then again
1058 * whenever a ego's identifier changes or if it is deleted. At the
1059 * end of the initial pass over all egos, the function is once called
1060 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
1061 * be invoked in the future or that there was an error.
1063 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
1064 * this function is only called ONCE, and 'NULL' being passed in
1065 * 'ego' does indicate an error (i.e. name is taken or no default
1066 * value is known). If 'ego' is non-NULL and if '*ctx'
1067 * is set in those callbacks, the value WILL be passed to a subsequent
1068 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
1069 * that one was not NULL).
1071 * When an identity is renamed, this function is called with the
1072 * (known) ego but the NEW identifier.
1074 * When an identity is deleted, this function is called with the
1075 * (known) ego and "NULL" for the 'identifier'. In this case,
1076 * the 'ego' is henceforth invalid (and the 'ctx' should also be
1079 * @param cls closure
1080 * @param ego ego handle
1081 * @param ctx context for application to store data for this ego
1082 * (during the lifetime of this process, initially NULL)
1083 * @param identifier identifier assigned by the user for this ego,
1084 * NULL if the user just deleted the ego and it
1085 * must thus no longer be used
1088 list_ego (void *cls,
1089 struct GNUNET_IDENTITY_Ego *ego,
1091 const char *identifier)
1093 struct RequestHandle *handle = cls;
1094 struct EgoEntry *ego_entry;
1095 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
1097 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
1099 handle->state = ID_REST_STATE_POST_INIT;
1103 if (ID_REST_STATE_INIT == handle->state) {
1104 ego_entry = GNUNET_new (struct EgoEntry);
1105 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
1106 ego_entry->keystring =
1107 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
1108 ego_entry->ego = ego;
1109 ego_entry->identifier = GNUNET_strdup (identifier);
1110 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
1116 * Function processing the REST call
1118 * @param method HTTP method
1119 * @param url URL of the HTTP request
1120 * @param data body of the HTTP request (optional)
1121 * @param data_size length of the body
1122 * @param proc callback function for the result
1123 * @param proc_cls closure for callback function
1124 * @return GNUNET_OK if request accepted
1127 rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
1128 GNUNET_REST_ResultProcessor proc,
1131 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
1133 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1134 handle->proc_cls = proc_cls;
1135 handle->proc = proc;
1136 handle->state = ID_REST_STATE_INIT;
1137 handle->conndata_handle = conndata_handle;
1140 handle->url = GNUNET_strdup (conndata_handle->url);
1141 if (handle->url[strlen (handle->url)-1] == '/')
1142 handle->url[strlen (handle->url)-1] = '\0';
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1148 handle->timeout_task =
1149 GNUNET_SCHEDULER_add_delayed (handle->timeout,
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 * Entry point for the plugin.
1159 * @param cls Config info
1160 * @return NULL on error, otherwise the plugin context
1163 libgnunet_plugin_rest_identity_provider_init (void *cls)
1165 static struct Plugin plugin;
1166 struct GNUNET_REST_Plugin *api;
1169 if (NULL != plugin.cfg)
1170 return NULL; /* can only initialize once! */
1171 memset (&plugin, 0, sizeof (struct Plugin));
1173 api = GNUNET_new (struct GNUNET_REST_Plugin);
1175 api->name = GNUNET_REST_API_NS_IDENTITY_PROVIDER;
1176 api->process_request = &rest_identity_process_request;
1177 GNUNET_asprintf (&allow_methods,
1178 "%s, %s, %s, %s, %s",
1179 MHD_HTTP_METHOD_GET,
1180 MHD_HTTP_METHOD_POST,
1181 MHD_HTTP_METHOD_PUT,
1182 MHD_HTTP_METHOD_DELETE,
1183 MHD_HTTP_METHOD_OPTIONS);
1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1186 _("Identity Token REST API initialized\n"));
1192 * Exit point from the plugin.
1194 * @param cls the plugin context (as returned by "init")
1195 * @return always NULL
1198 libgnunet_plugin_rest_identity_provider_done (void *cls)
1200 struct GNUNET_REST_Plugin *api = cls;
1201 struct Plugin *plugin = api->cls;
1204 GNUNET_free_non_null (allow_methods);
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Identity Token REST plugin is finished\n");
1211 /* end of plugin_rest_gns.c */