aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2017-10-12 11:25:04 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2017-10-12 11:25:04 +0200
commit2d5d51340c15a78b3acb79314781758fdef61ab0 (patch)
tree52a96ca7268af57945312a4fe7a2f0c374459bb4 /src
parent0af3570c12b847c2f38334c1f27ad198011d718b (diff)
downloadgnunet-2d5d51340c15a78b3acb79314781758fdef61ab0.tar.gz
gnunet-2d5d51340c15a78b3acb79314781758fdef61ab0.zip
-add consume rest api
Diffstat (limited to 'src')
-rw-r--r--src/identity-provider/plugin_rest_identity_provider.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/identity-provider/plugin_rest_identity_provider.c b/src/identity-provider/plugin_rest_identity_provider.c
index f136901a0..b6a8f3d36 100644
--- a/src/identity-provider/plugin_rest_identity_provider.c
+++ b/src/identity-provider/plugin_rest_identity_provider.c
@@ -60,6 +60,11 @@
60#define GNUNET_REST_API_NS_IDENTITY_REVOKE "/idp/revoke" 60#define GNUNET_REST_API_NS_IDENTITY_REVOKE "/idp/revoke"
61 61
62/** 62/**
63 * Revoke namespace
64 */
65#define GNUNET_REST_API_NS_IDENTITY_CONSUME "/idp/consume"
66
67/**
63 * Attribute key 68 * Attribute key
64 */ 69 */
65#define GNUNET_REST_JSONAPI_IDENTITY_ATTRIBUTE "attribute" 70#define GNUNET_REST_JSONAPI_IDENTITY_ATTRIBUTE "attribute"
@@ -819,6 +824,161 @@ revoke_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
819 GNUNET_JSONAPI_document_delete (json_obj); 824 GNUNET_JSONAPI_document_delete (json_obj);
820} 825}
821 826
827static void
828consume_cont (void *cls,
829 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
830 const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
831{
832 struct RequestHandle *handle = cls;
833 struct GNUNET_JSONAPI_Resource *json_resource;
834 json_t *value;
835
836 if (NULL == identity)
837 {
838 GNUNET_SCHEDULER_add_now (&return_response, handle);
839 return;
840 }
841
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute: %s\n",
843 attr->name);
844 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_ATTRIBUTE,
845 attr->name);
846 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
847
848 value = json_string (attr->data);
849 GNUNET_JSONAPI_resource_add_attr (json_resource,
850 "value",
851 value);
852 json_decref (value);
853}
854
855static void
856consume_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
857 const char* url,
858 void *cls)
859{
860 const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity_priv;
861 const char* identity_str;
862 const char* audience_str;
863 const char* rnd_str;
864
865 struct RequestHandle *handle = cls;
866 struct EgoEntry *ego_entry;
867 struct MHD_Response *resp;
868 struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
869 struct GNUNET_JSONAPI_Document *json_obj;
870 struct GNUNET_JSONAPI_Resource *json_res;
871 struct GNUNET_CRYPTO_EcdsaPublicKey tmp_pk;
872 char term_data[handle->rest_handle->data_size+1];
873 json_t *rnd_json;
874 json_t *identity_json;
875 json_t *audience_json;
876 json_t *data_json;
877 json_error_t err;
878 struct GNUNET_JSON_Specification docspec[] = {
879 GNUNET_JSON_spec_jsonapi_document (&json_obj),
880 GNUNET_JSON_spec_end()
881 };
882
883 if (0 >= handle->rest_handle->data_size)
884 {
885 GNUNET_SCHEDULER_add_now (&do_error, handle);
886 return;
887 }
888
889 term_data[handle->rest_handle->data_size] = '\0';
890 GNUNET_memcpy (term_data,
891 handle->rest_handle->data,
892 handle->rest_handle->data_size);
893 data_json = json_loads (term_data,
894 JSON_DECODE_ANY,
895 &err);
896 GNUNET_assert (GNUNET_OK ==
897 GNUNET_JSON_parse (data_json, docspec,
898 NULL, NULL));
899 json_decref (data_json);
900 if (NULL == json_obj)
901 {
902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
903 "Unable to parse JSONAPI Object from %s\n",
904 term_data);
905 GNUNET_SCHEDULER_add_now (&do_error, handle);
906 return;
907 }
908 if (1 != GNUNET_JSONAPI_document_resource_count (json_obj))
909 {
910 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
911 "Cannot create more than 1 resource! (Got %d)\n",
912 GNUNET_JSONAPI_document_resource_count (json_obj));
913 GNUNET_JSONAPI_document_delete (json_obj);
914 GNUNET_SCHEDULER_add_now (&do_error, handle);
915 return;
916 }
917 json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0);
918 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res,
919 GNUNET_REST_JSONAPI_IDENTITY_TICKET))
920 {
921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922 "Unsupported JSON data type\n");
923 GNUNET_JSONAPI_document_delete (json_obj);
924 resp = GNUNET_REST_create_response (NULL);
925 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
926 cleanup_handle (handle);
927 return;
928 }
929 rnd_json = GNUNET_JSONAPI_resource_read_attr (json_res,
930 "rnd");
931 identity_json = GNUNET_JSONAPI_resource_read_attr (json_res,
932 "identity");
933 audience_json = GNUNET_JSONAPI_resource_read_attr (json_res,
934 "audience");
935 rnd_str = json_string_value (rnd_json);
936 identity_str = json_string_value (identity_json);
937 audience_str = json_string_value (audience_json);
938
939 GNUNET_STRINGS_string_to_data (rnd_str,
940 strlen (rnd_str),
941 &ticket.rnd,
942 sizeof (uint64_t));
943 GNUNET_STRINGS_string_to_data (identity_str,
944 strlen (identity_str),
945 &ticket.identity,
946 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
947 GNUNET_STRINGS_string_to_data (audience_str,
948 strlen (audience_str),
949 &ticket.audience,
950 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
951
952 for (ego_entry = handle->ego_head;
953 NULL != ego_entry;
954 ego_entry = ego_entry->next)
955 {
956 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
957 &tmp_pk);
958 if (0 == memcmp (&ticket.audience,
959 &tmp_pk,
960 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
961 break;
962 }
963 if (NULL == ego_entry)
964 {
965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
966 "Identity unknown (%s)\n", identity_str);
967 GNUNET_JSONAPI_document_delete (json_obj);
968 return;
969 }
970 identity_priv = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
971 handle->resp_object = GNUNET_JSONAPI_document_new ();
972 handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
973 handle->idp_op = GNUNET_IDENTITY_PROVIDER_ticket_consume (handle->idp,
974 identity_priv,
975 &ticket,
976 &consume_cont,
977 handle);
978 GNUNET_JSONAPI_document_delete (json_obj);
979}
980
981
822 982
823/** 983/**
824 * Respond to OPTIONS request 984 * Respond to OPTIONS request
@@ -859,6 +1019,7 @@ init_cont (struct RequestHandle *handle)
859 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES, &add_attribute_cont}, 1019 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_ATTRIBUTES, &add_attribute_cont},
860 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TICKETS, &list_tickets_cont}, 1020 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TICKETS, &list_tickets_cont},
861 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_REVOKE, &revoke_ticket_cont}, 1021 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_REVOKE, &revoke_ticket_cont},
1022 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_CONSUME, &consume_ticket_cont},
862 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER, 1023 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY_PROVIDER,
863 &options_cont}, 1024 &options_cont},
864 GNUNET_REST_HANDLER_END 1025 GNUNET_REST_HANDLER_END