- fix warnings
[oweals/gnunet.git] / src / identity-provider / plugin_rest_identity_provider.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2012-2015 GNUnet e.V.
4
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.
9
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.
14
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.
19    */
20 /**
21  * @author Martin Schanzenbach
22  * @file identity/plugin_rest_identity.c
23  * @brief GNUnet Namestore REST plugin
24  *
25  */
26
27 #include "platform.h"
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"
37 #include <jansson.h>
38 #include <inttypes.h>
39 #include "gnunet_signatures.h"
40 #include "gnunet_identity_provider_service.h"
41
42 /**
43  * REST root namespace
44  */
45 #define GNUNET_REST_API_NS_IDENTITY_PROVIDER "/idp"
46
47 /**
48  * Issue namespace
49  */
50 #define GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE "/idp/issue"
51
52 /**
53  * Check namespace TODO
54  */
55 #define GNUNET_REST_API_NS_IDENTITY_TOKEN_CHECK "/idp/check"
56
57 /**
58  * Token namespace
59  */
60 #define GNUNET_REST_API_NS_IDENTITY_OAUTH2_TOKEN "/idp/token"
61
62 /**
63  * The parameter name in which the ticket must be provided
64  */
65 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET "ticket"
66
67 /**
68  * The parameter name in which the expected nonce must be provided
69  */
70 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE "expected_nonce"
71
72 /**
73  * The parameter name in which the ticket must be provided
74  */
75 #define GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN "token"
76
77 /**
78  * The URL parameter name in which the nonce must be provided
79  */
80 #define GNUNET_IDENTITY_TOKEN_REQUEST_NONCE "nonce"
81
82 /**
83  * State while collecting all egos
84  */
85 #define ID_REST_STATE_INIT 0
86
87 /**
88  * Done collecting egos
89  */
90 #define ID_REST_STATE_POST_INIT 1
91
92 /**
93  * Resource type
94  */
95 #define GNUNET_REST_JSONAPI_IDENTITY_TOKEN "token"
96
97 /**
98  * URL parameter to create a GNUid token for a specific audience
99  */
100 #define GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST "audience"
101
102 /**
103  * URL parameter to create a GNUid token for a specific issuer (EGO)
104  */
105 #define GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST "issuer"
106
107 /**
108  * Attributes passed to issue request
109  */
110 #define GNUNET_IDENTITY_TOKEN_ATTR_LIST "requested_attrs"
111
112 /**
113  * Token expiration string
114  */
115 #define GNUNET_IDENTITY_TOKEN_EXP_STRING "expiration"
116
117 /**
118  * Error messages
119  */
120 #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
121 #define GNUNET_REST_ERROR_NO_DATA "No data"
122
123 /**
124  * GNUid token lifetime
125  */
126 #define GNUNET_GNUID_TOKEN_EXPIRATION_MICROSECONDS 300000000
127
128 /**
129  * The configuration handle
130  */
131 const struct GNUNET_CONFIGURATION_Handle *cfg;
132
133 /**
134  * HTTP methods allows for this plugin
135  */
136 static char* allow_methods;
137
138 /**
139  * @brief struct returned by the initialization function of the plugin
140  */
141 struct Plugin
142 {
143   const struct GNUNET_CONFIGURATION_Handle *cfg;
144 };
145
146 /**
147  * The ego list
148  */
149 struct EgoEntry
150 {
151   /**
152    * DLL
153    */
154   struct EgoEntry *next;
155
156   /**
157    * DLL
158    */
159   struct EgoEntry *prev;
160
161   /**
162    * Ego Identifier
163    */
164   char *identifier;
165
166   /**
167    * Public key string
168    */
169   char *keystring;
170
171   /**
172    * The Ego
173    */
174   struct GNUNET_IDENTITY_Ego *ego;
175 };
176
177
178 struct RequestHandle
179 {
180   /**
181    * Ego list
182    */
183   struct EgoEntry *ego_head;
184
185   /**
186    * Ego list
187    */
188   struct EgoEntry *ego_tail;
189
190   /**
191    * Selected ego
192    */
193   struct EgoEntry *ego_entry;
194
195   /**
196    * Ptr to current ego private key
197    */
198   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
199
200   /**
201    * Handle to the rest connection
202    */
203   struct GNUNET_REST_RequestHandle *conndata_handle;
204
205   /**
206    * The processing state
207    */
208   int state;
209
210   /**
211    * Handle to Identity service.
212    */
213   struct GNUNET_IDENTITY_Handle *identity_handle;
214
215   /**
216    * IDENTITY Operation
217    */
218   struct GNUNET_IDENTITY_Operation *op;
219
220   /**
221    * Identity Provider
222    */
223   struct GNUNET_IDENTITY_PROVIDER_Handle *idp;
224
225   /**
226    * Idp Operation
227    */
228   struct GNUNET_IDENTITY_PROVIDER_Operation *idp_op;
229
230   /**
231    * Handle to NS service
232    */
233   struct GNUNET_NAMESTORE_Handle *ns_handle;
234
235   /**
236    * NS iterator
237    */
238   struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
239
240   /**
241    * NS Handle
242    */
243   struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
244
245   /**
246    * Desired timeout for the lookup (default is no timeout).
247    */
248   struct GNUNET_TIME_Relative timeout;
249
250   /**
251    * ID of a task associated with the resolution process.
252    */
253   struct GNUNET_SCHEDULER_Task *timeout_task;
254
255   /**
256    * The plugin result processor
257    */
258   GNUNET_REST_ResultProcessor proc;
259
260   /**
261    * The closure of the result processor
262    */
263   void *proc_cls;
264
265   /**
266    * The url
267    */
268   char *url;
269
270   /**
271    * Error response message
272    */
273   char *emsg;
274
275   /**
276    * Reponse code
277    */
278   int response_code;
279
280   /**
281    * Response object
282    */
283   struct GNUNET_JSONAPI_Document *resp_object;
284
285 };
286
287
288 /**
289  * Cleanup lookup handle
290  * @param handle Handle to clean up
291  */
292 static void
293 cleanup_handle (struct RequestHandle *handle)
294 {
295   struct EgoEntry *ego_entry;
296   struct EgoEntry *ego_tmp;
297   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298               "Cleaning up\n");
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;
318        NULL != ego_entry;)
319   {
320     ego_tmp = ego_entry;
321     ego_entry = ego_entry->next;
322     GNUNET_free (ego_tmp->identifier);
323     GNUNET_free (ego_tmp->keystring);
324     GNUNET_free (ego_tmp);
325   }
326   GNUNET_free (handle);
327 }
328
329
330 /**
331  * Task run on error, sends error message.  Cleans up everything.
332  *
333  * @param cls the `struct RequestHandle`
334  */
335 static void
336 do_error (void *cls)
337 {
338   struct RequestHandle *handle = cls;
339   struct MHD_Response *resp;
340   char *json_error;
341
342   GNUNET_asprintf (&json_error,
343                    "{Error while processing request: %s}",
344                    handle->emsg);
345   resp = GNUNET_REST_create_response (json_error);
346   handle->proc (handle->proc_cls, resp, handle->response_code);
347   cleanup_handle (handle);
348   GNUNET_free (json_error);
349 }
350
351 /**
352  * Task run on timeout, sends error message.  Cleans up everything.
353  *
354  * @param cls the `struct RequestHandle`
355  */
356 static void
357 do_timeout (void *cls)
358 {
359   struct RequestHandle *handle = cls;
360
361   handle->timeout_task = NULL;
362   do_error (handle);
363 }
364
365
366 /**
367  * Task run on shutdown.  Cleans up everything.
368  *
369  * @param cls unused
370  */
371 static void
372 do_cleanup_handle_delayed (void *cls)
373 {
374   struct RequestHandle *handle = cls;
375
376   cleanup_handle (handle);
377 }
378
379
380 /**
381  * Get a ticket for identity
382  * @param cls the handle
383  * @param ticket the ticket returned from the idp
384  */
385 static void
386 token_creat_cont (void *cls,
387                   const char *label,
388                   const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket,
389                   const struct GNUNET_IDENTITY_PROVIDER_Token *token)
390 {
391   struct GNUNET_JSONAPI_Resource *json_resource;
392   struct RequestHandle *handle = cls;
393   struct MHD_Response *resp;
394   json_t *ticket_json;
395   json_t *token_json;
396   char *ticket_str;
397   char *token_str;
398   char *result_str;
399
400   if (NULL == ticket)
401   {
402     handle->emsg = GNUNET_strdup ("Error in token issue");
403     GNUNET_SCHEDULER_add_now (&do_error, handle);
404     return;
405   }
406
407   handle->resp_object = GNUNET_JSONAPI_document_new ();
408   json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
409                                                     label);
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,
416                                          ticket_json);
417   GNUNET_JSONAPI_resource_add_attr (json_resource,
418                                          GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TOKEN,
419                                          token_json);
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);
425
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_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);
432 }
433
434
435 /**
436  * Continueationf for token issue request
437  *
438  * @param con the Rest handle
439  * @param url the requested url
440  * @param cls the request handle
441  */
442 static void
443 issue_token_cont (struct GNUNET_REST_RequestHandle *con,
444                   const char *url,
445                   void *cls)
446 {
447   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
448   const char *egoname;
449
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;
458   char *ego_val;
459   char *audience;
460   char *exp_str;
461   char *nonce_str;
462   char *scopes;
463   uint64_t time;
464   uint64_t nonce;
465
466   if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url,
467                                                 GNUNET_REST_API_NS_IDENTITY_TOKEN_ISSUE))
468   {
469     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "URL invalid: %s\n", handle->url);
470     resp = GNUNET_REST_create_response (NULL);
471     handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
472     cleanup_handle (handle);
473     return;
474   }
475   egoname = NULL;
476   ego_entry = NULL;
477   GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
478                       strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
479                       &key);
480   if ( GNUNET_YES !=
481        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
482                                                &key) )
483   {
484     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485                 "Issuer not found\n");
486     GNUNET_SCHEDULER_add_now (&do_error, handle);
487     return;
488   }
489   ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
490                                                &key);
491   if (NULL == ego_val)
492   {
493     GNUNET_SCHEDULER_add_now (&do_error, handle);
494     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
495                 "Ego invalid: %s\n",
496                 ego_val);
497     return;
498   }
499   for (ego_entry = handle->ego_head;
500        NULL != ego_entry;
501        ego_entry = ego_entry->next)
502   {
503     if (0 != strcmp (ego_val, ego_entry->identifier))
504       continue;
505     egoname = ego_entry->identifier;
506     break;
507   }
508   if ( (NULL == egoname) ||
509        (NULL == ego_entry) )
510   {
511     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512                 "Ego not found: %s\n",
513                 ego_val);
514     GNUNET_SCHEDULER_add_now (&do_error, handle);
515     return;
516   }
517   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518               "Ego to issue token for: %s\n",
519               egoname);
520
521
522   //Meta info
523   GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_ATTR_LIST,
524                       strlen (GNUNET_IDENTITY_TOKEN_ATTR_LIST),
525                       &key);
526
527   scopes = NULL;
528   if ( GNUNET_YES !=
529        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
530                                                &key) )
531   {
532     handle->emsg = GNUNET_strdup ("Scopes missing!\n");
533     GNUNET_SCHEDULER_add_now (&do_error, handle);
534     return;
535   }
536   scopes = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
537                                               &key);
538
539
540   //Token audience
541   GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST,
542                       strlen (GNUNET_REST_JSONAPI_IDENTITY_AUD_REQUEST),
543                       &key);
544   audience = NULL;
545   if ( GNUNET_YES !=
546        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
547                                                &key) )
548   {
549     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550                 "Audience missing!\n");
551     GNUNET_SCHEDULER_add_now (&do_error, handle);
552     return;
553   }
554   audience = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
555                                                 &key);
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557               "Audience to issue token for: %s\n",
558               audience);
559
560   priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
561   GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
562                                       &pub_key);
563   GNUNET_STRINGS_string_to_data (audience,
564                                  strlen (audience),
565                                  &aud_key,
566                                  sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
567
568   //Remote nonce
569   nonce_str = NULL;
570   GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE,
571                       strlen (GNUNET_IDENTITY_TOKEN_REQUEST_NONCE),
572                       &key);
573   if ( GNUNET_YES !=
574        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
575                                                &key) )
576   {
577     handle->emsg = GNUNET_strdup ("Request nonce missing!\n");
578     GNUNET_SCHEDULER_add_now (&do_error, handle);
579     return;
580   }
581   nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
582                                                  &key);
583   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584               "Request nonce: %s\n",
585               nonce_str);
586   sscanf (nonce_str, "%"SCNu64, &nonce);
587
588   //Get expiration for token from URL parameter
589   GNUNET_CRYPTO_hash (GNUNET_IDENTITY_TOKEN_EXP_STRING,
590                       strlen (GNUNET_IDENTITY_TOKEN_EXP_STRING),
591                       &key);
592
593   exp_str = NULL;
594   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
595                                                             &key))
596   {
597     exp_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
598                                                  &key);
599   }
600   if (NULL == exp_str) {
601     handle->emsg = GNUNET_strdup ("No expiration given!\n");
602     GNUNET_SCHEDULER_add_now (&do_error, handle);
603     return;
604   }
605
606   if (GNUNET_OK !=
607       GNUNET_STRINGS_fancy_time_to_relative (exp_str,
608                                              &etime_rel))
609   {
610     handle->emsg = GNUNET_strdup ("Expiration invalid!\n");
611     GNUNET_SCHEDULER_add_now (&do_error, handle);
612     return;
613   }
614   time = GNUNET_TIME_absolute_get().abs_value_us;
615   exp_time.abs_value_us = time + etime_rel.rel_value_us;
616
617   handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
618   handle->idp_op = GNUNET_IDENTITY_PROVIDER_issue_token (handle->idp,
619                                                          priv_key,
620                                                          &aud_key,
621                                                          scopes,
622                                                          exp_time,
623                                                          nonce,
624                                                          &token_creat_cont,
625                                                          handle);
626
627 }
628
629
630 /**
631  * Build a GNUid token for identity
632  *
633  * @param cls the request handle
634  */
635 static void
636 return_token_list (void *cls)
637 {
638   char* result_str;
639   struct RequestHandle *handle = cls;
640   struct MHD_Response *resp;
641
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_response (result_str);
645   handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
646   GNUNET_free (result_str);
647   cleanup_handle (handle);
648 }
649
650 /**
651  * Collect all tokens for an ego
652  *
653  * TODO move this into the identity-provider service
654  *
655  */
656 static void
657 token_collect (void *cls,
658                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
659                const char *label,
660                unsigned int rd_count,
661                const struct GNUNET_GNSRECORD_Data *rd)
662 {
663   int i;
664   char* data;
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;
669   json_t *issuer;
670   json_t *token;
671
672   if (NULL == label)
673   {
674     ego_tmp = handle->ego_head;
675     GNUNET_CONTAINER_DLL_remove (handle->ego_head,
676                                  handle->ego_tail,
677                                  ego_tmp);
678     GNUNET_free (ego_tmp->identifier);
679     GNUNET_free (ego_tmp->keystring);
680     GNUNET_free (ego_tmp);
681
682     if (NULL == handle->ego_head)
683     {
684       //Done
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);
688       return;
689     }
690
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,
694                                                            priv_key,
695                                                            &token_collect,
696                                                            handle);
697     return;
698   }
699
700   for (i = 0; i < rd_count; i++)
701   {
702     if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN)
703     {
704       data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
705                                                rd[i].data,
706                                                rd[i].data_size);
707       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token: %s\n", data);
708       json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
709                                                         label);
710       issuer = json_string (handle->ego_head->identifier);
711       GNUNET_JSONAPI_resource_add_attr (json_resource,
712                                              GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
713                                              issuer);
714       json_decref (issuer);
715       token = json_string (data);
716       GNUNET_JSONAPI_resource_add_attr (json_resource,
717                                              GNUNET_REST_JSONAPI_IDENTITY_TOKEN,
718                                              token);
719       json_decref (token);
720
721       GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
722       GNUNET_free (data);
723     }
724   }
725
726   GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
727 }
728
729
730
731 /**
732  * Respond to OPTIONS request
733  *
734  * @param con_handle the connection handle
735  * @param url the url
736  * @param cls the RequestHandle
737  */
738 static void
739 list_token_cont (struct GNUNET_REST_RequestHandle *con_handle,
740                  const char* url,
741                  void *cls)
742 {
743   char* ego_val;
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;
749
750   GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST,
751                       strlen (GNUNET_REST_JSONAPI_IDENTITY_ISS_REQUEST),
752                       &key);
753
754   if ( GNUNET_YES !=
755        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
756                                                &key) )
757   {
758     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No issuer given.\n");
759     GNUNET_SCHEDULER_add_now (&do_error, handle);
760     return;
761   }
762   ego_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
763                                                &key);
764   //Remove non-matching egos
765   for (ego_entry = handle->ego_head;
766        NULL != ego_entry;)
767   {
768     ego_tmp = ego_entry;
769     ego_entry = ego_entry->next;
770     if (0 != strcmp (ego_val, ego_tmp->identifier))
771     {
772       GNUNET_CONTAINER_DLL_remove (handle->ego_head,
773                                    handle->ego_tail,
774                                    ego_tmp);
775       GNUNET_free (ego_tmp->identifier);
776       GNUNET_free (ego_tmp->keystring);
777       GNUNET_free (ego_tmp);
778     }
779   }
780   handle->resp_object = GNUNET_JSONAPI_document_new ();
781   if (NULL == handle->ego_head)
782   {
783     //Done
784     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No results.\n");
785     GNUNET_SCHEDULER_add_now (&return_token_list, handle);
786     return;
787   }
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,
791                                                          priv_key,
792                                                          &token_collect,
793                                                          handle);
794
795 }
796
797 /**
798  * Return token to requestor
799  *
800  * @param cls request handle
801  * @param token the token
802  */
803 static void
804 exchange_cont (void *cls,
805                const struct GNUNET_IDENTITY_PROVIDER_Token *token,
806                uint64_t ticket_nonce)
807 {
808   json_t *root;
809   struct RequestHandle *handle = cls;
810   struct MHD_Response *resp;
811   struct GNUNET_HashCode key;
812   char* result;
813   char* token_str;
814   char* nonce_str;
815   uint64_t expected_nonce;
816
817   //Get nonce
818   GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE,
819                       strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_EXPECTED_NONCE),
820                       &key);
821
822   if ( GNUNET_NO ==
823        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
824                                                &key) )
825   {
826     handle->emsg = GNUNET_strdup ("No nonce given.");
827     GNUNET_SCHEDULER_add_now (&do_error, handle);
828     return;
829   }
830   nonce_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
831                                                   &key);
832   GNUNET_assert (1 == sscanf (nonce_str, "%"SCNu64, &expected_nonce));
833
834   if (ticket_nonce != expected_nonce)
835   {
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);
841     return;
842   }
843
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);
849
850   result = json_dumps (root, JSON_INDENT(1));
851   resp = GNUNET_REST_create_response (result);
852   GNUNET_free (result);
853   handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
854   cleanup_handle (handle);
855   json_decref (root);
856 }
857
858
859 /**
860  *
861  * Callback called when identity for token exchange has been found
862  *
863  * @param cls request handle
864  * @param ego the identity to use as issuer
865  * @param ctx user context
866  * @param name identity name
867  *
868  */
869 static void
870 exchange_token_ticket_cb (void *cls,
871                           struct GNUNET_IDENTITY_Ego *ego,
872                           void **ctx,
873                           const char *name)
874 {
875   struct RequestHandle *handle = cls;
876   struct GNUNET_HashCode key;
877   struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket;
878   char* ticket_str;
879
880   handle->op = NULL;
881
882   if (NULL == ego)
883   {
884     handle->emsg = GNUNET_strdup ("No identity found.");
885     GNUNET_SCHEDULER_add_now (&do_error, handle);
886     return;
887   }
888
889   //Get ticket
890   GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET,
891                       strlen (GNUNET_REST_JSONAPI_IDENTITY_PROVIDER_TICKET),
892                       &key);
893
894   if ( GNUNET_NO ==
895        GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
896                                                &key) )
897   {
898     handle->emsg = GNUNET_strdup ("No ticket given.");
899     GNUNET_SCHEDULER_add_now (&do_error, handle);
900     return;
901   }
902   ticket_str = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
903                                                   &key);
904   handle->priv_key = GNUNET_IDENTITY_ego_get_private_key (ego);
905   GNUNET_IDENTITY_PROVIDER_string_to_ticket (ticket_str,
906                                              &ticket);
907
908   handle->idp = GNUNET_IDENTITY_PROVIDER_connect (cfg);
909   handle->idp_op = GNUNET_IDENTITY_PROVIDER_exchange_ticket (handle->idp,
910                                                              ticket,
911                                                              handle->priv_key,
912                                                              &exchange_cont,
913                                                              handle);
914   GNUNET_IDENTITY_PROVIDER_ticket_destroy (ticket);
915
916 }
917
918
919
920 /**
921  * Respond to issue request
922  *
923  * @param con_handle the connection handle
924  * @param url the url
925  * @param cls the RequestHandle
926  */
927 static void
928 exchange_token_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle,
929                             const char* url,
930                             void *cls)
931 {
932   struct RequestHandle *handle = cls;
933
934   //Get token from GNS
935   handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
936                                     "gns-master",
937                                     &exchange_token_ticket_cb,
938                                     handle);
939 }
940
941 /**
942  * Respond to OPTIONS request
943  *
944  * @param con_handle the connection handle
945  * @param url the url
946  * @param cls the RequestHandle
947  */
948 static void
949 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
950               const char* url,
951               void *cls)
952 {
953   struct MHD_Response *resp;
954   struct RequestHandle *handle = cls;
955
956   //For now, independent of path return all options
957   resp = GNUNET_REST_create_response (NULL);
958   MHD_add_response_header (resp,
959                            "Access-Control-Allow-Methods",
960                            allow_methods);
961   handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
962   cleanup_handle (handle);
963   return;
964 }
965
966 /**
967  * Handle rest request
968  *
969  * @param handle the request handle
970  */
971 static void
972 init_cont (struct RequestHandle *handle)
973 {
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
982   };
983
984   if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle,
985                                                handlers,
986                                                &err,
987                                                handle))
988   {
989     handle->response_code = err.error_code;
990     GNUNET_SCHEDULER_add_now (&do_error, handle);
991   }
992 }
993
994 /**
995  * If listing is enabled, prints information about the egos.
996  *
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.
1002  *
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).
1010  *
1011  * When an identity is renamed, this function is called with the
1012  * (known) ego but the NEW identifier.
1013  *
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
1017  * cleaned up).
1018  *
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
1026  */
1027 static void
1028 list_ego (void *cls,
1029           struct GNUNET_IDENTITY_Ego *ego,
1030           void **ctx,
1031           const char *identifier)
1032 {
1033   struct RequestHandle *handle = cls;
1034   struct EgoEntry *ego_entry;
1035   struct GNUNET_CRYPTO_EcdsaPublicKey pk;
1036
1037   if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
1038   {
1039     handle->state = ID_REST_STATE_POST_INIT;
1040     init_cont (handle);
1041     return;
1042   }
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);
1051   }
1052
1053 }
1054
1055 /**
1056  * Function processing the REST call
1057  *
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
1065  */
1066 static void
1067 rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
1068                               GNUNET_REST_ResultProcessor proc,
1069                               void *proc_cls)
1070 {
1071   struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
1072
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;
1078
1079
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,
1084               "Connecting...\n");
1085   handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1086                                                      &list_ego,
1087                                                      handle);
1088   handle->timeout_task =
1089     GNUNET_SCHEDULER_add_delayed (handle->timeout,
1090                                   &do_timeout,
1091                                   handle);
1092   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093               "Connected\n");
1094 }
1095
1096 /**
1097  * Entry point for the plugin.
1098  *
1099  * @param cls Config info
1100  * @return NULL on error, otherwise the plugin context
1101  */
1102 void *
1103 libgnunet_plugin_rest_identity_provider_init (void *cls)
1104 {
1105   static struct Plugin plugin;
1106   struct GNUNET_REST_Plugin *api;
1107
1108   cfg = cls;
1109   if (NULL != plugin.cfg)
1110     return NULL;                /* can only initialize once! */
1111   memset (&plugin, 0, sizeof (struct Plugin));
1112   plugin.cfg = cfg;
1113   api = GNUNET_new (struct GNUNET_REST_Plugin);
1114   api->cls = &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);
1124
1125   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1126               _("Identity Token REST API initialized\n"));
1127   return api;
1128 }
1129
1130
1131 /**
1132  * Exit point from the plugin.
1133  *
1134  * @param cls the plugin context (as returned by "init")
1135  * @return always NULL
1136  */
1137 void *
1138 libgnunet_plugin_rest_identity_provider_done (void *cls)
1139 {
1140   struct GNUNET_REST_Plugin *api = cls;
1141   struct Plugin *plugin = api->cls;
1142
1143   plugin->cfg = NULL;
1144   GNUNET_free_non_null (allow_methods);
1145   GNUNET_free (api);
1146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147               "Identity Token REST plugin is finished\n");
1148   return NULL;
1149 }
1150
1151 /* end of plugin_rest_gns.c */