2 This file is part of GNUnet.
3 Copyright (C) 2012-2016 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 gns/plugin_rest_credential.c
23 * @brief GNUnet CREDENTIAL REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include <gnunet_identity_service.h>
30 #include <gnunet_gnsrecord_lib.h>
31 #include <gnunet_namestore_service.h>
32 #include <gnunet_credential_service.h>
33 #include <gnunet_rest_lib.h>
34 #include <gnunet_jsonapi_lib.h>
35 #include <gnunet_jsonapi_util.h>
38 #define GNUNET_REST_API_NS_CREDENTIAL "/credential"
40 #define GNUNET_REST_API_NS_CREDENTIAL_ISSUE "/credential/issue"
42 #define GNUNET_REST_API_NS_CREDENTIAL_VERIFY "/credential/verify"
44 #define GNUNET_REST_JSONAPI_CREDENTIAL_EXPIRATION "expiration"
46 #define GNUNET_REST_JSONAPI_CREDENTIAL_SUBJECT_KEY "subject_key"
48 #define GNUNET_REST_JSONAPI_CREDENTIAL "credential"
50 #define GNUNET_REST_JSONAPI_CREDENTIAL_TYPEINFO "credential"
52 #define GNUNET_REST_JSONAPI_DELEGATIONS "delegations"
54 #define GNUNET_REST_JSONAPI_CREDENTIAL_ISSUER_ATTR "attribute"
56 #define GNUNET_REST_JSONAPI_CREDENTIAL_SUBJECT_ATTR "credential"
59 * @brief struct returned by the initialization function of the plugin
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 const struct GNUNET_CONFIGURATION_Handle *cfg;
71 * Handle to Credential service.
73 struct GNUNET_CREDENTIAL_Handle *credential;
76 * Handle to lookup request
78 struct GNUNET_CREDENTIAL_Request *verify_request;
81 * Handle to issue request
83 struct GNUNET_CREDENTIAL_Request *issue_request;
88 struct GNUNET_IDENTITY_Handle *identity;
91 * Handle to identity operation
93 struct GNUNET_IDENTITY_Operation *id_op;
96 * Handle to rest request
98 struct GNUNET_REST_RequestHandle *rest_handle;
101 * ID of a task associated with the resolution process.
103 struct GNUNET_SCHEDULER_Task * timeout_task;
106 * The root of the received JSON or NULL
111 * The plugin result processor
113 GNUNET_REST_ResultProcessor proc;
116 * The closure of the result processor
121 * The issuer attribute to verify
126 * The subject attribute
131 * The public key of the issuer
133 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
136 * The public key of the subject
138 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
148 struct GNUNET_TIME_Relative timeout;
154 * Cleanup lookup handle.
156 * @param handle Handle to clean up
159 cleanup_handle (struct RequestHandle *handle)
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 if (NULL != handle->json_root)
164 json_decref (handle->json_root);
166 if (NULL != handle->issuer_attr)
167 GNUNET_free (handle->issuer_attr);
168 if (NULL != handle->subject_attr)
169 GNUNET_free (handle->subject_attr);
170 if (NULL != handle->verify_request)
171 GNUNET_CREDENTIAL_verify_cancel (handle->verify_request);
172 if (NULL != handle->credential)
173 GNUNET_CREDENTIAL_disconnect (handle->credential);
174 if (NULL != handle->id_op)
175 GNUNET_IDENTITY_cancel (handle->id_op);
176 if (NULL != handle->identity)
177 GNUNET_IDENTITY_disconnect (handle->identity);
178 if (NULL != handle->timeout_task)
180 GNUNET_SCHEDULER_cancel (handle->timeout_task);
182 GNUNET_free (handle);
187 * Task run on shutdown. Cleans up everything.
190 * @param tc scheduler context
195 struct RequestHandle *handle = cls;
196 struct MHD_Response *resp;
198 resp = GNUNET_REST_create_response (NULL);
199 handle->proc (handle->proc_cls, resp, handle->response_code);
200 cleanup_handle (handle);
204 * Attribute delegation to JSON
205 * @param attr the attribute
206 * @return JSON, NULL if failed
209 attribute_delegation_to_json (struct GNUNET_CREDENTIAL_Delegation *delegation_chain_entry)
215 issuer = GNUNET_CRYPTO_ecdsa_public_key_to_string (&delegation_chain_entry->issuer_key);
218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219 "Issuer in delegation malformed\n");
222 subject = GNUNET_CRYPTO_ecdsa_public_key_to_string (&delegation_chain_entry->subject_key);
225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
226 "Subject in credential malformed\n");
227 GNUNET_free (issuer);
230 attr_obj = json_object ();
232 json_object_set_new (attr_obj, "issuer", json_string (issuer));
233 json_object_set_new (attr_obj, "issuer_attribute",
234 json_string (delegation_chain_entry->issuer_attribute));
236 json_object_set_new (attr_obj, "subject", json_string (subject));
237 if (0 < delegation_chain_entry->subject_attribute_len)
239 json_object_set_new (attr_obj, "subject_attribute",
240 json_string (delegation_chain_entry->subject_attribute));
242 GNUNET_free (issuer);
243 GNUNET_free (subject);
248 * JSONAPI resource to Credential
249 * @param res the JSONAPI resource
250 * @return the resulting credential, NULL if failed
252 static struct GNUNET_CREDENTIAL_Credential*
253 json_to_credential (json_t *res)
255 struct GNUNET_CREDENTIAL_Credential *cred;
257 const char *attribute;
258 const char *signature;
261 tmp = json_object_get (res, "attribute");
262 if (0 == json_is_string (tmp))
266 attribute = json_string_value (tmp);
267 cred = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Credential)
268 + strlen (attribute));
269 cred->issuer_attribute = attribute;
270 cred->issuer_attribute_len = strlen (attribute);
271 tmp = json_object_get (res, "issuer");
272 if (0 == json_is_string (tmp))
278 GNUNET_CRYPTO_ecdsa_public_key_from_string (json_string_value(tmp),
279 strlen (json_string_value(tmp)),
281 tmp = json_object_get (res, "subject");
282 if (0 == json_is_string (tmp))
287 GNUNET_CRYPTO_ecdsa_public_key_from_string (json_string_value(tmp),
288 strlen (json_string_value(tmp)),
291 tmp = json_object_get (res, "signature");
292 if (0 == json_is_string (tmp))
297 signature = json_string_value (tmp);
298 GNUNET_STRINGS_base64_decode (signature,
301 GNUNET_memcpy (&cred->signature,
303 sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
306 tmp = json_object_get (res, "expiration");
307 if (0 == json_is_integer (tmp))
312 cred->expiration.abs_value_us = json_integer_value (tmp);
319 * @param cred the credential
320 * @return the resulting json, NULL if failed
323 credential_to_json (struct GNUNET_CREDENTIAL_Credential *cred)
328 char attribute[cred->issuer_attribute_len + 1];
331 issuer = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred->issuer_key);
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 "Issuer in credential malformed\n");
338 subject = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred->subject_key);
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
342 "Subject in credential malformed\n");
343 GNUNET_free (issuer);
346 GNUNET_STRINGS_base64_encode ((char*)&cred->signature,
347 sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
350 cred->issuer_attribute,
351 cred->issuer_attribute_len);
352 attribute[cred->issuer_attribute_len] = '\0';
353 cred_obj = json_object ();
354 json_object_set_new (cred_obj, "issuer", json_string (issuer));
355 json_object_set_new (cred_obj, "subject", json_string (subject));
356 json_object_set_new (cred_obj, "attribute", json_string (attribute));
357 json_object_set_new (cred_obj, "signature", json_string (signature));
358 json_object_set_new (cred_obj, "expiration", json_integer (cred->expiration.abs_value_us));
359 GNUNET_free (issuer);
360 GNUNET_free (subject);
361 GNUNET_free (signature);
366 * Function called with the result of a Credential lookup.
368 * @param cls the 'const char *' name that was resolved
369 * @param cd_count number of records returned
370 * @param cd array of @a cd_count records with the results
373 handle_verify_response (void *cls,
374 unsigned int d_count,
375 struct GNUNET_CREDENTIAL_Delegation *delegation_chain,
376 unsigned int c_count,
377 struct GNUNET_CREDENTIAL_Credential *cred)
380 struct RequestHandle *handle = cls;
381 struct MHD_Response *resp;
382 struct GNUNET_JSONAPI_Document *json_document;
383 struct GNUNET_JSONAPI_Resource *json_resource;
393 handle->verify_request = NULL;
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397 handle->response_code = MHD_HTTP_NOT_FOUND;
398 GNUNET_SCHEDULER_add_now (&do_error, handle);
401 issuer = GNUNET_CRYPTO_ecdsa_public_key_to_string (&handle->issuer_key);
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 "Issuer in delegation malformed\n");
408 GNUNET_asprintf (&id,
411 handle->issuer_attr);
412 GNUNET_free (issuer);
413 json_document = GNUNET_JSONAPI_document_new ();
414 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_CREDENTIAL_TYPEINFO,
417 attr_array = json_array ();
418 for (i = 0; i < d_count; i++)
420 attr_obj = attribute_delegation_to_json (&delegation_chain[i]);
421 json_array_append_new (attr_array, attr_obj);
423 cred_array = json_array ();
424 for (i=0;i<c_count;i++)
426 cred_obj = credential_to_json (&cred[i]);
427 json_array_append_new (cred_array, cred_obj);
429 GNUNET_JSONAPI_resource_add_attr (json_resource,
430 GNUNET_REST_JSONAPI_CREDENTIAL,
432 GNUNET_JSONAPI_resource_add_attr (json_resource,
433 GNUNET_REST_JSONAPI_DELEGATIONS,
435 GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
436 GNUNET_JSONAPI_document_serialize (json_document, &result);
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 json_decref (attr_array);
441 json_decref (cred_array);
442 GNUNET_JSONAPI_document_delete (json_document);
443 resp = GNUNET_REST_create_response (result);
444 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
445 GNUNET_free (result);
446 cleanup_handle (handle);
451 verify_cred_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
455 struct RequestHandle *handle = cls;
456 struct GNUNET_HashCode key;
457 struct GNUNET_JSONAPI_Document *json_obj;
458 struct GNUNET_JSONAPI_Resource *res;
459 struct GNUNET_CREDENTIAL_Credential *cred;
463 uint32_t credential_count;
464 uint32_t resource_count;
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 handle->credential = GNUNET_CREDENTIAL_connect (cfg);
472 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 if (NULL == handle->credential)
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
479 "Connecting to CREDENTIAL failed\n");
480 GNUNET_SCHEDULER_add_now (&do_error, handle);
483 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_CREDENTIAL_ISSUER_ATTR,
484 strlen (GNUNET_REST_JSONAPI_CREDENTIAL_ISSUER_ATTR),
487 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491 "Missing issuer attribute\n");
492 GNUNET_SCHEDULER_add_now (&do_error, handle);
495 tmp = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
497 entity_attr = GNUNET_strdup (tmp);
498 tmp = strtok(entity_attr, ".");
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 "Malformed issuer or attribute\n");
503 GNUNET_free (entity_attr);
504 GNUNET_SCHEDULER_add_now (&do_error, handle);
508 GNUNET_CRYPTO_ecdsa_public_key_from_string (tmp,
510 &handle->issuer_key))
512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513 "Malformed issuer key\n");
514 GNUNET_free (entity_attr);
515 GNUNET_SCHEDULER_add_now (&do_error, handle);
518 tmp = strtok (NULL, "."); //Issuer attribute
521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
522 "Malformed attribute\n");
523 GNUNET_free (entity_attr);
524 GNUNET_SCHEDULER_add_now (&do_error, handle);
527 handle->issuer_attr = GNUNET_strdup (tmp);
528 GNUNET_free (entity_attr);
530 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_CREDENTIAL_SUBJECT_ATTR,
531 strlen (GNUNET_REST_JSONAPI_CREDENTIAL_SUBJECT_ATTR),
534 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
538 "Missing subject or attribute\n");
539 GNUNET_free (entity_attr);
540 GNUNET_SCHEDULER_add_now (&do_error, handle);
543 tmp = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
548 "Malformed subject\n");
549 GNUNET_free (entity_attr);
550 GNUNET_SCHEDULER_add_now (&do_error, handle);
554 GNUNET_CRYPTO_ecdsa_public_key_from_string (tmp,
556 &handle->subject_key)) {
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 "Malformed subject key\n");
559 GNUNET_free (entity_attr);
560 GNUNET_SCHEDULER_add_now (&do_error, handle);
563 GNUNET_free (entity_attr);
565 if (0 >= handle->rest_handle->data_size)
567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
568 "Missing credentials\n");
569 GNUNET_SCHEDULER_add_now (&do_error, handle);
573 struct GNUNET_JSON_Specification docspec[] = {
574 GNUNET_JSON_spec_jsonapi_document (&json_obj),
575 GNUNET_JSON_spec_end()
577 char term_data[handle->rest_handle->data_size+1];
578 term_data[handle->rest_handle->data_size] = '\0';
579 credential_count = 0;
580 GNUNET_memcpy (term_data,
581 handle->rest_handle->data,
582 handle->rest_handle->data_size);
583 data_js = json_loads (term_data,
586 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (data_js, docspec,
588 json_decref (data_js);
589 if (NULL == json_obj)
591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
592 "Unable to parse JSONAPI Object from %s\n",
594 GNUNET_SCHEDULER_add_now (&do_error, handle);
598 resource_count = GNUNET_JSONAPI_document_resource_count(json_obj);
599 struct GNUNET_CREDENTIAL_Credential credentials[credential_count];
600 for (i=0;i<resource_count;i++)
602 res = (GNUNET_JSONAPI_document_get_resource(json_obj, i));
603 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type(res,
604 GNUNET_REST_JSONAPI_CREDENTIAL_TYPEINFO))
606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
607 "Resource not a credential!\n");
611 cred_json = GNUNET_JSONAPI_resource_read_attr (res,
612 GNUNET_REST_JSONAPI_CREDENTIAL);
613 cred = json_to_credential (cred_json);
614 GNUNET_memcpy (&credentials[i],
616 sizeof (struct GNUNET_CREDENTIAL_Credential));
617 credentials[i].issuer_attribute = GNUNET_strdup (cred->issuer_attribute);
621 handle->verify_request = GNUNET_CREDENTIAL_verify (handle->credential,
624 &handle->subject_key,
627 &handle_verify_response,
629 for (i=0;i<credential_count;i++)
630 GNUNET_free ((char*)credentials[i].issuer_attribute);
635 send_cred_response (struct RequestHandle *handle,
636 struct GNUNET_CREDENTIAL_Credential *cred)
638 struct MHD_Response *resp;
639 struct GNUNET_JSONAPI_Document *json_document;
640 struct GNUNET_JSONAPI_Resource *json_resource;
648 GNUNET_assert (NULL != cred);
649 issuer = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred->issuer_key);
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "Subject malformed\n");
656 GNUNET_asprintf (&id,
660 subject = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred->subject_key);
663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
664 "Subject malformed\n");
667 GNUNET_STRINGS_base64_encode ((char*)&cred->signature,
668 sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
670 json_document = GNUNET_JSONAPI_document_new ();
671 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_CREDENTIAL_TYPEINFO,
674 cred_obj = json_object();
675 json_object_set_new (cred_obj, "issuer", json_string (issuer));
676 json_object_set_new (cred_obj, "subject", json_string (subject));
677 json_object_set_new (cred_obj, "expiration", json_integer( cred->expiration.abs_value_us));
678 json_object_set_new (cred_obj, "signature", json_string (signature));
679 GNUNET_JSONAPI_resource_add_attr (json_resource,
680 GNUNET_REST_JSONAPI_CREDENTIAL,
682 GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
683 GNUNET_JSONAPI_document_serialize (json_document, &result);
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 json_decref (cred_obj);
688 GNUNET_JSONAPI_document_delete (json_document);
689 resp = GNUNET_REST_create_response (result);
690 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
691 GNUNET_free (result);
692 GNUNET_free (signature);
693 GNUNET_free (issuer);
694 GNUNET_free (subject);
695 cleanup_handle (handle);
699 get_cred_issuer_cb (void *cls,
700 struct GNUNET_IDENTITY_Ego *ego,
704 struct RequestHandle *handle = cls;
705 struct GNUNET_TIME_Absolute etime_abs;
706 struct GNUNET_TIME_Relative etime_rel;
707 const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer_key;
708 struct GNUNET_HashCode key;
709 struct GNUNET_CREDENTIAL_Credential *cred;
710 char* expiration_str;
713 handle->id_op = NULL;
717 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
718 "Issuer not configured!\n");
719 GNUNET_SCHEDULER_add_now (&do_error, handle);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Connecting to credential service...\n");
725 handle->credential = GNUNET_CREDENTIAL_connect (cfg);
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 if (NULL == handle->credential)
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 "Connecting to CREDENTIAL failed\n");
732 GNUNET_SCHEDULER_add_now (&do_error, handle);
735 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_CREDENTIAL_EXPIRATION,
736 strlen (GNUNET_REST_JSONAPI_CREDENTIAL_EXPIRATION),
739 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
743 "Missing expiration\n");
744 GNUNET_SCHEDULER_add_now (&do_error, handle);
747 expiration_str = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
749 if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration_str,
752 etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
753 } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration_str,
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 "Malformed expiration: %s\n", expiration_str);
758 GNUNET_SCHEDULER_add_now (&do_error, handle);
761 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_CREDENTIAL_ISSUER_ATTR,
762 strlen (GNUNET_REST_JSONAPI_CREDENTIAL_ISSUER_ATTR),
765 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
769 "Missing issuer attribute\n");
770 GNUNET_SCHEDULER_add_now (&do_error, handle);
773 handle->issuer_attr = GNUNET_strdup(GNUNET_CONTAINER_multihashmap_get
774 (handle->rest_handle->url_param_map,
776 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_CREDENTIAL_SUBJECT_KEY,
777 strlen (GNUNET_REST_JSONAPI_CREDENTIAL_SUBJECT_KEY),
780 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
784 "Missing subject\n");
785 GNUNET_SCHEDULER_add_now (&do_error, handle);
788 tmp = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
793 "Malformed subject\n");
794 GNUNET_SCHEDULER_add_now (&do_error, handle);
798 GNUNET_CRYPTO_ecdsa_public_key_from_string (tmp,
800 &handle->subject_key)) {
801 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
802 "Malformed subject key\n");
803 GNUNET_SCHEDULER_add_now (&do_error, handle);
806 issuer_key = GNUNET_IDENTITY_ego_get_private_key (ego);
807 cred = GNUNET_CREDENTIAL_credential_issue (issuer_key,
808 &handle->subject_key,
813 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
814 "Failed to create credential\n");
815 GNUNET_SCHEDULER_add_now (&do_error, handle);
818 send_cred_response (handle, cred);
823 issue_cred_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
827 struct RequestHandle *handle = cls;
829 handle->identity = GNUNET_IDENTITY_connect (cfg,
832 handle->id_op = GNUNET_IDENTITY_get(handle->identity,
836 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
842 * Handle rest request
844 * @param handle the lookup handle
847 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
851 struct MHD_Response *resp;
852 struct RequestHandle *handle = cls;
854 //For GNS, independent of path return all options
855 resp = GNUNET_REST_create_response (NULL);
856 MHD_add_response_header (resp,
857 "Access-Control-Allow-Methods",
858 MHD_HTTP_METHOD_GET);
859 handle->proc (handle->proc_cls,
862 cleanup_handle (handle);
867 * Function processing the REST call
869 * @param method HTTP method
870 * @param url URL of the HTTP request
871 * @param data body of the HTTP request (optional)
872 * @param data_size length of the body
873 * @param proc callback function for the result
874 * @param proc_cls closure for callback function
875 * @return GNUNET_OK if request accepted
878 rest_credential_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
879 GNUNET_REST_ResultProcessor proc,
882 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
883 struct GNUNET_REST_RequestHandlerError err;
885 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
886 handle->proc_cls = proc_cls;
888 handle->rest_handle = conndata_handle;
890 static const struct GNUNET_REST_RequestHandler handlers[] = {
891 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CREDENTIAL_VERIFY, &verify_cred_cont},
892 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CREDENTIAL_ISSUE, &issue_cred_cont},
893 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CREDENTIAL, &options_cont},
894 GNUNET_REST_HANDLER_END
897 if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle,
902 handle->response_code = err.error_code;
903 GNUNET_SCHEDULER_add_now (&do_error, handle);
909 * Entry point for the plugin.
911 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
912 * @return NULL on error, otherwise the plugin context
915 libgnunet_plugin_rest_credential_init (void *cls)
917 static struct Plugin plugin;
919 struct GNUNET_REST_Plugin *api;
921 if (NULL != plugin.cfg)
922 return NULL; /* can only initialize once! */
923 memset (&plugin, 0, sizeof (struct Plugin));
925 api = GNUNET_new (struct GNUNET_REST_Plugin);
927 api->name = GNUNET_REST_API_NS_CREDENTIAL;
928 api->process_request = &rest_credential_process_request;
929 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
930 _("GNS REST API initialized\n"));
936 * Exit point from the plugin.
938 * @param cls the plugin context (as returned by "init")
939 * @return always NULL
942 libgnunet_plugin_rest_credential_done (void *cls)
944 struct GNUNET_REST_Plugin *api = cls;
945 struct Plugin *plugin = api->cls;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "GNS REST plugin is finished\n");
954 /* end of plugin_rest_gns.c */