aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2017-10-09 11:00:40 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2017-10-09 11:00:40 +0200
commit0e7c1fb8feff37774d66ca46b3eb09492ecf93af (patch)
tree4d751a17bb9f2e9c5f7408dee4dcc1ece8d3d85a /src
parentdc7f9d7e2bf4e9c607738500051adab58b1bd2f3 (diff)
downloadgnunet-0e7c1fb8feff37774d66ca46b3eb09492ecf93af.tar.gz
gnunet-0e7c1fb8feff37774d66ca46b3eb09492ecf93af.zip
-add attribute store API, fixes
Diffstat (limited to 'src')
-rw-r--r--src/identity-provider/plugin_rest_identity_provider.c157
-rw-r--r--src/include/gnunet_jsonapi_lib.h2
-rw-r--r--src/jsonapi/jsonapi_resource.c2
3 files changed, 140 insertions, 21 deletions
diff --git a/src/identity-provider/plugin_rest_identity_provider.c b/src/identity-provider/plugin_rest_identity_provider.c
index bb9f210ef..0ba0666d0 100644
--- a/src/identity-provider/plugin_rest_identity_provider.c
+++ b/src/identity-provider/plugin_rest_identity_provider.c
@@ -338,6 +338,25 @@ collect_error_cb (void *cls)
338 do_error (handle); 338 do_error (handle);
339} 339}
340 340
341static void
342finished_cont (void *cls,
343 int32_t success,
344 const char *emsg)
345{
346 struct RequestHandle *handle = cls;
347 struct MHD_Response *resp;
348
349 resp = GNUNET_REST_create_response (emsg);
350 if (GNUNET_OK != success)
351 {
352 GNUNET_SCHEDULER_add_now (&do_error, handle);
353 return;
354 }
355 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
356 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
357}
358
359
341/** 360/**
342 * Return attributes for identity 361 * Return attributes for identity
343 * 362 *
@@ -475,6 +494,123 @@ list_tickets_cont (struct GNUNET_REST_RequestHandle *con_handle,
475} 494}
476 495
477 496
497static void
498add_attribute_cont (struct GNUNET_REST_RequestHandle *con_handle,
499 const char* url,
500 void *cls)
501{
502 const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity_priv;
503 const char* identity;
504 const char* name_str;
505 const char* value_str;
506
507 struct RequestHandle *handle = cls;
508 struct EgoEntry *ego_entry;
509 struct MHD_Response *resp;
510 struct GNUNET_IDENTITY_PROVIDER_Attribute *attribute;
511 struct GNUNET_JSONAPI_Document *json_obj;
512 struct GNUNET_JSONAPI_Resource *json_res;
513 char term_data[handle->rest_handle->data_size+1];
514 json_t *value_json;
515 json_t *data_json;
516 json_error_t err;
517 struct GNUNET_JSON_Specification docspec[] = {
518 GNUNET_JSON_spec_jsonapi_document (&json_obj),
519 GNUNET_JSON_spec_end()
520 };
521
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding an attribute for %s.\n",
523 handle->url);
524 if ( strlen (GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES) >=
525 strlen (handle->url))
526 {
527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No identity given.\n");
528 GNUNET_SCHEDULER_add_now (&do_error, handle);
529 return;
530 }
531 identity = handle->url + strlen (GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES) + 1;
532
533 for (ego_entry = handle->ego_head;
534 NULL != ego_entry;
535 ego_entry = ego_entry->next)
536 if (0 == strcmp (identity, ego_entry->identifier))
537 break;
538
539 if (NULL == ego_entry)
540 {
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 "Identity unknown (%s)\n", identity);
543 GNUNET_JSONAPI_document_delete (json_obj);
544 return;
545 }
546 identity_priv = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
547
548 if (0 >= handle->rest_handle->data_size)
549 {
550 GNUNET_SCHEDULER_add_now (&do_error, handle);
551 return;
552 }
553
554 term_data[handle->rest_handle->data_size] = '\0';
555 GNUNET_memcpy (term_data,
556 handle->rest_handle->data,
557 handle->rest_handle->data_size);
558 data_json = json_loads (term_data,
559 JSON_DECODE_ANY,
560 &err);
561 GNUNET_assert (GNUNET_OK ==
562 GNUNET_JSON_parse (data_json, docspec,
563 NULL, NULL));
564 json_decref (data_json);
565 if (NULL == json_obj)
566 {
567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
568 "Unable to parse JSONAPI Object from %s\n",
569 term_data);
570 GNUNET_SCHEDULER_add_now (&do_error, handle);
571 return;
572 }
573 if (1 != GNUNET_JSONAPI_document_resource_count (json_obj))
574 {
575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
576 "Cannot create more than 1 resource! (Got %d)\n",
577 GNUNET_JSONAPI_document_resource_count (json_obj));
578 GNUNET_JSONAPI_document_delete (json_obj);
579 GNUNET_SCHEDULER_add_now (&do_error, handle);
580 return;
581 }
582 json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0);
583 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res,
584 GNUNET_REST_JSONAPI_IDENTITY_ATTRIBUTE))
585 {
586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587 "Unsupported JSON data type\n");
588 GNUNET_JSONAPI_document_delete (json_obj);
589 resp = GNUNET_REST_create_response (NULL);
590 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
591 cleanup_handle (handle);
592 return;
593 }
594 name_str = GNUNET_JSONAPI_resource_get_id (json_res);
595 value_json = GNUNET_JSONAPI_resource_read_attr (json_res,
596 "value");
597 value_str = json_string_value (value_json);
598 attribute = GNUNET_IDENTITY_PROVIDER_attribute_new (name_str,
599 GNUNET_IDENTITY_PROVIDER_AT_STRING,
600 value_str,
601 strlen (value_str));
602 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
603 handle->idp_op = GNUNET_IDENTITY_PROVIDER_attribute_store (handle->idp,
604 identity_priv,
605 attribute,
606 &finished_cont,
607 handle);
608 GNUNET_free (attribute);
609 GNUNET_JSONAPI_document_delete (json_obj);
610}
611
612
613
478/** 614/**
479 * Collect all attributes for an ego 615 * Collect all attributes for an ego
480 * 616 *
@@ -560,24 +696,6 @@ list_attribute_cont (struct GNUNET_REST_RequestHandle *con_handle,
560 696
561 697
562static void 698static void
563revoke_finished_cont (void *cls,
564 int32_t success,
565 const char *emsg)
566{
567 struct RequestHandle *handle = cls;
568 struct MHD_Response *resp;
569
570 resp = GNUNET_REST_create_response (emsg);
571 if (GNUNET_OK != success)
572 {
573 GNUNET_SCHEDULER_add_now (&do_error, handle);
574 return;
575 }
576 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
577 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
578}
579
580static void
581revoke_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle, 699revoke_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
582 const char* url, 700 const char* url,
583 void *cls) 701 void *cls)
@@ -698,7 +816,7 @@ revoke_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
698 handle->idp_op = GNUNET_IDENTITY_PROVIDER_ticket_revoke (handle->idp, 816 handle->idp_op = GNUNET_IDENTITY_PROVIDER_ticket_revoke (handle->idp,
699 identity_priv, 817 identity_priv,
700 &ticket, 818 &ticket,
701 &revoke_finished_cont, 819 &finished_cont,
702 handle); 820 handle);
703 GNUNET_JSONAPI_document_delete (json_obj); 821 GNUNET_JSONAPI_document_delete (json_obj);
704} 822}
@@ -740,6 +858,7 @@ init_cont (struct RequestHandle *handle)
740 struct GNUNET_REST_RequestHandlerError err; 858 struct GNUNET_REST_RequestHandlerError err;
741 static const struct GNUNET_REST_RequestHandler handlers[] = { 859 static const struct GNUNET_REST_RequestHandler handlers[] = {
742 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES, &list_attribute_cont}, 860 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES, &list_attribute_cont},
861 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES, &add_attribute_cont},
743 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TICKETS, &list_tickets_cont}, 862 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TICKETS, &list_tickets_cont},
744 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_REVOKE, &revoke_ticket_cont}, 863 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_REVOKE, &revoke_ticket_cont},
745 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER, 864 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER,
diff --git a/src/include/gnunet_jsonapi_lib.h b/src/include/gnunet_jsonapi_lib.h
index f95bff836..2f6b810f0 100644
--- a/src/include/gnunet_jsonapi_lib.h
+++ b/src/include/gnunet_jsonapi_lib.h
@@ -248,7 +248,7 @@ GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource
248 * @param res the JSON resource 248 * @param res the JSON resource
249 * @return the resource id 249 * @return the resource id
250 */ 250 */
251char* 251const char*
252GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource); 252GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
253 253
254 254
diff --git a/src/jsonapi/jsonapi_resource.c b/src/jsonapi/jsonapi_resource.c
index 4a166f58a..be28ad5df 100644
--- a/src/jsonapi/jsonapi_resource.c
+++ b/src/jsonapi/jsonapi_resource.c
@@ -245,7 +245,7 @@ GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource
245 * @param res the JSON resource 245 * @param res the JSON resource
246 * @return the resource id 246 * @return the resource id
247 */ 247 */
248char* 248const char*
249GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource) 249GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource)
250{ 250{
251 return resource->id; 251 return resource->id;