aboutsummaryrefslogtreecommitdiff
path: root/src/identity/plugin_rest_identity.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-09-14 11:10:11 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-09-14 11:10:11 +0000
commitb0a03eeebfdedbb7b0070ef00514f321aafa49db (patch)
tree06233b8dc0c1bf8ee580367e772c9951a3d7cee6 /src/identity/plugin_rest_identity.c
parent134da182130592c78bdb5d348826825665a2ffab (diff)
downloadgnunet-b0a03eeebfdedbb7b0070ef00514f321aafa49db.tar.gz
gnunet-b0a03eeebfdedbb7b0070ef00514f321aafa49db.zip
- revert plugin move. Add new identity-token
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r--src/identity/plugin_rest_identity.c1080
1 files changed, 1080 insertions, 0 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
new file mode 100644
index 000000000..e5abe00ee
--- /dev/null
+++ b/src/identity/plugin_rest_identity.c
@@ -0,0 +1,1080 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
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_rest_lib.h"
31#include "microhttpd.h"
32#include <jansson.h>
33#include "gnunet_signatures.h"
34
35/**
36 * REST root namespace
37 */
38#define GNUNET_REST_API_NS_IDENTITY "/identity"
39
40/**
41 * State while collecting all egos
42 */
43#define ID_REST_STATE_INIT 0
44
45/**
46 * Done collecting egos
47 */
48#define ID_REST_STATE_POST_INIT 1
49
50/**
51 * Resource type
52 */
53#define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego"
54
55/**
56 * Name attribute
57 */
58#define GNUNET_REST_JSONAPI_IDENTITY_NAME "name"
59
60/**
61 * Attribute to rename "name" TODO we changed id to the pubkey
62 * so this can be unified with "name"
63 */
64#define GNUNET_REST_JSONAPI_IDENTITY_NEWNAME "newname"
65
66/**
67 * URL parameter to change the subsytem for ego
68 */
69#define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem"
70
71
72/**
73 * URL parameter to create a GNUid token for a specific audience
74 */
75#define GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN "create_token_for"
76
77/**
78 * Attribute containing the GNUid token if
79 * GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN was requested
80 */
81#define GNUNET_REST_JSONAPI_IDENTITY_GNUID "gnuid_token"
82
83/**
84 * Error messages
85 */
86#define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
87#define GNUNET_REST_ERROR_NO_DATA "No data"
88
89/**
90 * GNUid token lifetime
91 */
92#define GNUNET_GNUID_TOKEN_EXPIRATION_MICROSECONDS 300000000
93
94/**
95 * The configuration handle
96 */
97const struct GNUNET_CONFIGURATION_Handle *cfg;
98
99/**
100 * HTTP methods allows for this plugin
101 */
102static char* allow_methods;
103
104/**
105 * @brief struct returned by the initialization function of the plugin
106 */
107struct Plugin
108{
109 const struct GNUNET_CONFIGURATION_Handle *cfg;
110};
111
112/**
113 * The ego list
114 */
115struct EgoEntry
116{
117 /**
118 * DLL
119 */
120 struct EgoEntry *next;
121
122 /**
123 * DLL
124 */
125 struct EgoEntry *prev;
126
127 /**
128 * Ego Identifier
129 */
130 char *identifier;
131
132 /**
133 * Public key string
134 */
135 char *keystring;
136
137 /**
138 * The Ego
139 */
140 struct GNUNET_IDENTITY_Ego *ego;
141};
142
143
144struct RequestHandle
145{
146 /**
147 * Ego list
148 */
149 struct EgoEntry *ego_head;
150
151 /**
152 * Ego list
153 */
154 struct EgoEntry *ego_tail;
155
156 /**
157 * Handle to the rest connection
158 */
159 struct RestConnectionDataHandle *conndata_handle;
160
161 /**
162 * The processing state
163 */
164 int state;
165
166 /**
167 * Handle to GNS service.
168 */
169 struct GNUNET_IDENTITY_Handle *identity_handle;
170
171 /**
172 * IDENTITY Operation
173 */
174 struct GNUNET_IDENTITY_Operation *op;
175
176 /**
177 * Desired timeout for the lookup (default is no timeout).
178 */
179 struct GNUNET_TIME_Relative timeout;
180
181 /**
182 * ID of a task associated with the resolution process.
183 */
184 struct GNUNET_SCHEDULER_Task * timeout_task;
185
186 /**
187 * The plugin result processor
188 */
189 GNUNET_REST_ResultProcessor proc;
190
191 /**
192 * The closure of the result processor
193 */
194 void *proc_cls;
195
196 /**
197 * The name to look up
198 */
199 char *name;
200
201 /**
202 * The subsystem set from REST
203 */
204 char *subsys;
205
206 /**
207 * The url
208 */
209 char *url;
210
211 /**
212 * The data from the REST request
213 */
214 const char* data;
215
216 /**
217 * the length of the REST data
218 */
219 size_t data_size;
220
221 /**
222 * HTTP method
223 */
224 const char* method;
225
226 /**
227 * Error response message
228 */
229 char *emsg;
230
231};
232
233
234/**
235 * Cleanup lookup handle
236 * @param handle Handle to clean up
237 */
238static void
239cleanup_handle (struct RequestHandle *handle)
240{
241 struct EgoEntry *ego_entry;
242 struct EgoEntry *ego_tmp;
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Cleaning up\n");
245 if (NULL != handle->name)
246 GNUNET_free (handle->name);
247 if (NULL != handle->timeout_task)
248 GNUNET_SCHEDULER_cancel (handle->timeout_task);
249 if (NULL != handle->identity_handle)
250 GNUNET_IDENTITY_disconnect (handle->identity_handle);
251 if (NULL != handle->subsys)
252 GNUNET_free (handle->subsys);
253 if (NULL != handle->url)
254 GNUNET_free (handle->url);
255 if (NULL != handle->emsg)
256 GNUNET_free (handle->emsg);
257 for (ego_entry = handle->ego_head;
258 NULL != ego_entry;)
259 {
260 ego_tmp = ego_entry;
261 ego_entry = ego_entry->next;
262 GNUNET_free (ego_tmp->identifier);
263 GNUNET_free (ego_tmp->keystring);
264 GNUNET_free (ego_tmp);
265 }
266 GNUNET_free (handle);
267}
268
269
270/**
271 * Task run on shutdown. Cleans up everything.
272 *
273 * @param cls unused
274 * @param tc scheduler context
275 */
276static void
277do_error (void *cls,
278 const struct GNUNET_SCHEDULER_TaskContext *tc)
279{
280 struct RequestHandle *handle = cls;
281 struct MHD_Response *resp;
282 char *json_error;
283
284 GNUNET_asprintf (&json_error,
285 "{Error while processing request: %s}",
286 &handle->emsg);
287
288 resp = GNUNET_REST_create_json_response (json_error);
289 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
290 cleanup_handle (handle);
291 GNUNET_free (json_error);
292}
293
294/**
295 * Build a GNUid token for identity
296 * @param handle the handle
297 * @param ego_entry the ego to build the token for
298 * @param name name of the ego
299 * @param token_aud token audience
300 * @param token the resulting gnuid token
301 */
302static void
303make_gnuid_token (struct RequestHandle *handle,
304 struct EgoEntry *ego_entry,
305 const char *name,
306 const char *token_aud,
307 char **token)
308{
309 uint64_t time;
310 uint64_t lbl;
311 char *header_str;
312 char *payload_str;
313 char *header_base64;
314 char *payload_base64;
315 char *sig_str;
316 char *lbl_str;
317 json_t *header;
318 json_t *payload;
319 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
320 struct GNUNET_CRYPTO_EcdsaSignature sig;
321 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
322
323 time = GNUNET_TIME_absolute_get().abs_value_us;
324 lbl = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX);
325 GNUNET_STRINGS_base64_encode ((char*)&lbl, sizeof (uint64_t), &lbl_str);
326
327 header = json_object ();
328 json_object_set_new (header, "alg", json_string ("ED512"));
329 json_object_set_new (header, "typ", json_string ("JWT"));
330
331 payload = json_object ();
332 json_object_set_new (payload, "iss", json_string (ego_entry->keystring));
333 json_object_set_new (payload, "lbl", json_string (lbl_str));
334 json_object_set_new (payload, "sub", json_string (name));
335 json_object_set_new (payload, "nbf", json_integer (time));
336 json_object_set_new (payload, "iat", json_integer (time));
337 json_object_set_new (payload, "exp", json_integer (time+GNUNET_GNUID_TOKEN_EXPIRATION_MICROSECONDS));
338 json_object_set_new (payload, "aud", json_string (token_aud));
339 header_str = json_dumps (header, JSON_COMPACT);
340 GNUNET_STRINGS_base64_encode (header_str,
341 strlen (header_str),
342 &header_base64);
343 char* padding = strtok(header_base64, "=");
344 while (NULL != padding)
345 padding = strtok(NULL, "=");
346
347 payload_str = json_dumps (payload, JSON_COMPACT);
348 GNUNET_STRINGS_base64_encode (payload_str,
349 strlen (payload_str),
350 &payload_base64);
351 padding = strtok(payload_base64, "=");
352 while (NULL != padding)
353 padding = strtok(NULL, "=");
354
355 GNUNET_asprintf (token, "%s,%s", header_base64, payload_base64);
356 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
357 purpose =
358 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
359 strlen (*token));
360 purpose->size =
361 htonl (strlen (*token) + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
362 purpose->purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN);
363 memcpy (&purpose[1], *token, strlen (*token));
364 if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_sign (priv_key,
365 purpose,
366 &sig))
367 GNUNET_break(0);
368 GNUNET_free (*token);
369 sig_str = GNUNET_STRINGS_data_to_string_alloc (&sig,
370 sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
371 GNUNET_asprintf (token, "%s.%s.%s",
372 header_base64, payload_base64, sig_str);
373 GNUNET_free (sig_str);
374 GNUNET_free (header_str);
375 GNUNET_free (header_base64);
376 GNUNET_free (payload_str);
377 GNUNET_free (payload_base64);
378 GNUNET_free (purpose);
379 GNUNET_free (lbl_str);
380 json_decref (header);
381 json_decref (payload);
382}
383
384/**
385 * Callback for IDENTITY_get()
386 *
387 * @param cls the RequestHandle
388 * @param ego the Ego found
389 * @param ctx the context
390 * @param name the id of the ego
391 */
392static void
393get_ego_for_subsys (void *cls,
394 struct GNUNET_IDENTITY_Ego *ego,
395 void **ctx,
396 const char *name)
397{
398 struct RequestHandle *handle = cls;
399 struct JsonApiObject *json_object;
400 struct JsonApiResource *json_resource;
401 struct EgoEntry *ego_entry;
402 struct MHD_Response *resp;
403 json_t *name_json;
404 char *result_str;
405
406 json_object = GNUNET_REST_jsonapi_object_new ();
407
408 for (ego_entry = handle->ego_head;
409 NULL != ego_entry;
410 ego_entry = ego_entry->next)
411 {
412 if ( (NULL != name) && (0 != strcmp (name, ego_entry->identifier)) )
413 continue;
414 if (NULL == name)
415 continue;
416 json_resource = GNUNET_REST_jsonapi_resource_new
417 (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring);
418 name_json = json_string (ego_entry->identifier);
419 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
420 GNUNET_REST_JSONAPI_IDENTITY_NAME,
421 name_json);
422 json_decref (name_json);
423 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
424 break;
425 }
426 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
427 {
428 GNUNET_REST_jsonapi_object_delete (json_object);
429 handle->emsg = GNUNET_strdup("No identity matches results!");
430 GNUNET_SCHEDULER_add_now (&do_error, handle);
431 return;
432 }
433 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
435 resp = GNUNET_REST_create_json_response (result_str);
436 GNUNET_REST_jsonapi_object_delete (json_object);
437 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
438 GNUNET_free (result_str);
439 cleanup_handle (handle);
440}
441
442/**
443 * Create a response with requested ego(s)
444 *
445 * @param con the Rest handle
446 * @param url the requested url
447 * @param cls the request handle
448 */
449static void
450ego_info_response (struct RestConnectionDataHandle *con,
451 const char *url,
452 void *cls)
453{
454 const char *egoname;
455 char *result_str;
456 char *subsys_val;
457 char *create_token_for;
458 char *token;
459 char *keystring;
460 struct RequestHandle *handle = cls;
461 struct EgoEntry *ego_entry;
462 struct GNUNET_HashCode key;
463 struct MHD_Response *resp;
464 struct JsonApiObject *json_object;
465 struct JsonApiResource *json_resource;
466 json_t *name_str;
467 json_t *token_str;
468
469 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
470 {
471 resp = GNUNET_REST_create_json_response (NULL);
472 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
473 cleanup_handle (handle);
474 return;
475 }
476 egoname = NULL;
477 keystring = NULL;
478 if (strlen (GNUNET_REST_API_NS_IDENTITY) < strlen (handle->url))
479 {
480 keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY)+1];
481 //Return all egos
482 for (ego_entry = handle->ego_head;
483 NULL != ego_entry;
484 ego_entry = ego_entry->next)
485 {
486 if ( (NULL != keystring) && (0 != strcmp (keystring, ego_entry->keystring)) )
487 continue;
488 egoname = ego_entry->identifier;
489 }
490 }
491
492 if ( NULL == egoname ) {
493 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM,
494 strlen (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM),
495 &key);
496 if ( GNUNET_YES ==
497 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
498 &key) )
499 {
500 subsys_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
501 &key);
502 if (NULL != subsys_val)
503 {
504 GNUNET_asprintf (&handle->subsys, "%s", subsys_val);
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsys_val);
506 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
507 handle->subsys,
508 &get_ego_for_subsys,
509 handle);
510 return;
511 }
512 }
513 }
514
515 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN,
516 strlen (GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN),
517 &key);
518
519 //Token audience
520 create_token_for = NULL;
521 if ( GNUNET_YES ==
522 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
523 &key) )
524 create_token_for = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
525 &key);
526
527 json_object = GNUNET_REST_jsonapi_object_new ();
528
529 //Return all egos
530 for (ego_entry = handle->ego_head;
531 NULL != ego_entry;
532 ego_entry = ego_entry->next)
533 {
534 if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
535 continue;
536 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO,
537 ego_entry->keystring);
538 name_str = json_string (ego_entry->identifier);
539 GNUNET_REST_jsonapi_resource_add_attr (
540 json_resource,
541 GNUNET_REST_JSONAPI_IDENTITY_NAME,
542 name_str);
543 json_decref (name_str);
544 if (NULL != create_token_for)
545 {
546 make_gnuid_token (handle,
547 ego_entry,
548 ego_entry->identifier,
549 create_token_for,
550 &token);
551 token_str = json_string (token);
552 GNUNET_free (token);
553 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
554 GNUNET_REST_JSONAPI_IDENTITY_GNUID,
555 token_str);
556 json_decref (token_str);
557 }
558 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
559 }
560 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
561 {
562 GNUNET_REST_jsonapi_object_delete (json_object);
563 handle->emsg = GNUNET_strdup ("No identities found!");
564 GNUNET_SCHEDULER_add_now (&do_error, handle);
565 return;
566 }
567 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
569 resp = GNUNET_REST_create_json_response (result_str);
570 GNUNET_REST_jsonapi_object_delete (json_object);
571 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
572 GNUNET_free (result_str);
573 cleanup_handle (handle);
574}
575
576/**
577 * Processing finished
578 *
579 * @param cls request handle
580 * @param emsg error message
581 */
582static void
583do_finished (void *cls, const char *emsg)
584{
585 struct RequestHandle *handle = cls;
586 struct MHD_Response *resp;
587
588 handle->op = NULL;
589 if (NULL != emsg)
590 {
591 handle->emsg = GNUNET_strdup (emsg);
592 GNUNET_SCHEDULER_add_now (&do_error, handle);
593 return;
594 }
595 resp = GNUNET_REST_create_json_response (NULL);
596 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
597 cleanup_handle (handle);
598}
599
600/**
601 * Create a new ego
602 *
603 * @param con rest handle
604 * @param url url
605 * @param cls request handle
606 */
607static void
608ego_create_cont (struct RestConnectionDataHandle *con,
609 const char *url,
610 void *cls)
611{
612 struct RequestHandle *handle = cls;
613 struct EgoEntry *ego_entry;
614 struct MHD_Response *resp;
615 struct JsonApiObject *json_obj;
616 struct JsonApiResource *json_res;
617 json_t *egoname_json;
618 const char* egoname;
619 char term_data[handle->data_size+1];
620
621 if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url))
622 {
623 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
624 GNUNET_SCHEDULER_add_now (&do_error, handle);
625 return;
626 }
627 if (0 >= handle->data_size)
628 {
629 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
630 GNUNET_SCHEDULER_add_now (&do_error, handle);
631 return;
632 }
633 term_data[handle->data_size] = '\0';
634 memcpy (term_data, handle->data, handle->data_size);
635 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
636 if (NULL == json_obj)
637 {
638 GNUNET_SCHEDULER_add_now (&do_error, handle);
639 return;
640 }
641 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
642 {
643 GNUNET_REST_jsonapi_object_delete (json_obj);
644 handle->emsg = GNUNET_strdup ("Provided resource count invalid");
645 GNUNET_SCHEDULER_add_now (&do_error, handle);
646 return;
647 }
648 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
649 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
650 {
651 GNUNET_REST_jsonapi_object_delete (json_obj);
652 resp = GNUNET_REST_create_json_response (NULL);
653 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
654 cleanup_handle (handle);
655 return;
656 }
657 egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME);
658 if (!json_is_string (egoname_json))
659 {
660 GNUNET_REST_jsonapi_object_delete (json_obj);
661 handle->emsg = GNUNET_strdup ("No name provided");
662 GNUNET_SCHEDULER_add_now (&do_error, handle);
663 return;
664 }
665 egoname = json_string_value (egoname_json);
666 for (ego_entry = handle->ego_head;
667 NULL != ego_entry;
668 ego_entry = ego_entry->next)
669 {
670 if (0 == strcasecmp (egoname, ego_entry->identifier))
671 {
672 GNUNET_REST_jsonapi_object_delete (json_obj);
673 resp = GNUNET_REST_create_json_response (NULL);
674 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
675 cleanup_handle (handle);
676 return;
677 }
678 }
679 GNUNET_asprintf (&handle->name, "%s", egoname);
680 GNUNET_REST_jsonapi_object_delete (json_obj);
681 handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
682 handle->name,
683 &do_finished,
684 handle);
685}
686
687
688/**
689 * Handle ego edit request
690 *
691 * @param con rest connection handle
692 * @param url the url that is requested
693 * @param cls the RequestHandle
694 */
695static void
696ego_edit_cont (struct RestConnectionDataHandle *con,
697 const char *url,
698 void *cls)
699{
700 struct JsonApiObject *json_obj;
701 struct JsonApiResource *json_res;
702 struct RequestHandle *handle = cls;
703 struct EgoEntry *ego_entry;
704 struct MHD_Response *resp;
705 json_t *subsys_json;
706 json_t *name_json;
707 const char *keystring;
708 const char *subsys;
709 const char *newname;
710 char term_data[handle->data_size+1];
711 int ego_exists = GNUNET_NO;
712
713 if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url))
714 {
715 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
716 GNUNET_SCHEDULER_add_now (&do_error, handle);
717 return;
718 }
719
720 keystring = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
721
722 for (ego_entry = handle->ego_head;
723 NULL != ego_entry;
724 ego_entry = ego_entry->next)
725 {
726 if (0 != strcasecmp (keystring, ego_entry->keystring))
727 continue;
728 ego_exists = GNUNET_YES;
729 break;
730 }
731
732 if (GNUNET_NO == ego_exists)
733 {
734 resp = GNUNET_REST_create_json_response (NULL);
735 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
736 cleanup_handle (handle);
737 return;
738 }
739
740 if (0 >= handle->data_size)
741 {
742 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
743 GNUNET_SCHEDULER_add_now (&do_error, handle);
744 return;
745 }
746
747 term_data[handle->data_size] = '\0';
748 memcpy (term_data, handle->data, handle->data_size);
749 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
750
751 if (NULL == json_obj)
752 {
753 handle->emsg = GNUNET_strdup ("Data invalid");
754 GNUNET_SCHEDULER_add_now (&do_error, handle);
755 return;
756 }
757
758 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
759 {
760 GNUNET_REST_jsonapi_object_delete (json_obj);
761 handle->emsg = GNUNET_strdup ("Resource amount invalid");
762 GNUNET_SCHEDULER_add_now (&do_error, handle);
763 return;
764 }
765 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
766
767 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
768 {
769 GNUNET_REST_jsonapi_object_delete (json_obj);
770 handle->emsg = GNUNET_strdup ("Resource type invalid");
771 GNUNET_SCHEDULER_add_now (&do_error, handle);
772 return;
773 }
774
775 //This is a rename
776 name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res,
777 GNUNET_REST_JSONAPI_IDENTITY_NEWNAME);
778 if ((NULL != name_json) && json_is_string (name_json))
779 {
780 newname = json_string_value (name_json);
781 for (ego_entry = handle->ego_head;
782 NULL != ego_entry;
783 ego_entry = ego_entry->next)
784 {
785 if (0 == strcasecmp (newname, ego_entry->identifier) &&
786 0 != strcasecmp (keystring, ego_entry->keystring))
787 {
788 //Ego with same name not allowed
789 GNUNET_REST_jsonapi_object_delete (json_obj);
790 resp = GNUNET_REST_create_json_response (NULL);
791 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
792 cleanup_handle (handle);
793 return;
794 }
795 }
796 handle->op = GNUNET_IDENTITY_rename (handle->identity_handle,
797 ego_entry->identifier,
798 newname,
799 &do_finished,
800 handle);
801 GNUNET_REST_jsonapi_object_delete (json_obj);
802 return;
803 }
804
805 //Set subsystem
806 subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
807 if ( (NULL != subsys_json) && json_is_string (subsys_json))
808 {
809 subsys = json_string_value (subsys_json);
810 GNUNET_asprintf (&handle->subsys, "%s", subsys);
811 GNUNET_REST_jsonapi_object_delete (json_obj);
812 handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
813 handle->subsys,
814 ego_entry->ego,
815 &do_finished,
816 handle);
817 return;
818 }
819 GNUNET_REST_jsonapi_object_delete (json_obj);
820 handle->emsg = GNUNET_strdup ("Subsystem not provided");
821 GNUNET_SCHEDULER_add_now (&do_error, handle);
822}
823
824void
825ego_delete_cont (struct RestConnectionDataHandle *con_handle,
826 const char* url,
827 void *cls)
828{
829 const char *keystring;
830 struct EgoEntry *ego_entry;
831 struct MHD_Response *resp;
832 struct RequestHandle *handle = cls;
833 int ego_exists = GNUNET_NO;
834
835 if (strlen (GNUNET_REST_API_NS_IDENTITY) >= strlen (handle->url))
836 {
837 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
838 GNUNET_SCHEDULER_add_now (&do_error, handle);
839 return;
840 }
841
842 keystring = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
843 for (ego_entry = handle->ego_head;
844 NULL != ego_entry;
845 ego_entry = ego_entry->next)
846 {
847 if (0 != strcasecmp (keystring, ego_entry->keystring))
848 continue;
849 ego_exists = GNUNET_YES;
850 break;
851 }
852 if (GNUNET_NO == ego_exists)
853 {
854 resp = GNUNET_REST_create_json_response (NULL);
855 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
856 cleanup_handle (handle);
857 return;
858 }
859 handle->op = GNUNET_IDENTITY_delete (handle->identity_handle,
860 ego_entry->identifier,
861 &do_finished,
862 handle);
863
864}
865
866
867/**
868 * Respond to OPTIONS request
869 *
870 * @param con_handle the connection handle
871 * @param url the url
872 * @param cls the RequestHandle
873 */
874static void
875options_cont (struct RestConnectionDataHandle *con_handle,
876 const char* url,
877 void *cls)
878{
879 struct MHD_Response *resp;
880 struct RequestHandle *handle = cls;
881
882 //For now, independent of path return all options
883 resp = GNUNET_REST_create_json_response (NULL);
884 MHD_add_response_header (resp,
885 "Access-Control-Allow-Methods",
886 allow_methods);
887 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
888 cleanup_handle (handle);
889 return;
890}
891
892/**
893 * Handle rest request
894 *
895 * @param handle the request handle
896 */
897static void
898init_cont (struct RequestHandle *handle)
899{
900 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
901 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_info_response},
902 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create_cont},
903 {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &ego_edit_cont},
904 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete_cont},
905 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY, &options_cont},
906 GNUNET_REST_HANDLER_END
907 };
908
909 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle))
910 {
911 handle->emsg = GNUNET_strdup ("Request unsupported");
912 GNUNET_SCHEDULER_add_now (&do_error, handle);
913 }
914}
915
916/**
917 * If listing is enabled, prints information about the egos.
918 *
919 * This function is initially called for all egos and then again
920 * whenever a ego's identifier changes or if it is deleted. At the
921 * end of the initial pass over all egos, the function is once called
922 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
923 * be invoked in the future or that there was an error.
924 *
925 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
926 * this function is only called ONCE, and 'NULL' being passed in
927 * 'ego' does indicate an error (i.e. name is taken or no default
928 * value is known). If 'ego' is non-NULL and if '*ctx'
929 * is set in those callbacks, the value WILL be passed to a subsequent
930 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
931 * that one was not NULL).
932 *
933 * When an identity is renamed, this function is called with the
934 * (known) ego but the NEW identifier.
935 *
936 * When an identity is deleted, this function is called with the
937 * (known) ego and "NULL" for the 'identifier'. In this case,
938 * the 'ego' is henceforth invalid (and the 'ctx' should also be
939 * cleaned up).
940 *
941 * @param cls closure
942 * @param ego ego handle
943 * @param ctx context for application to store data for this ego
944 * (during the lifetime of this process, initially NULL)
945 * @param identifier identifier assigned by the user for this ego,
946 * NULL if the user just deleted the ego and it
947 * must thus no longer be used
948 */
949static void
950list_ego (void *cls,
951 struct GNUNET_IDENTITY_Ego *ego,
952 void **ctx,
953 const char *identifier)
954{
955 struct RequestHandle *handle = cls;
956 struct EgoEntry *ego_entry;
957 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
958
959 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
960 {
961 handle->state = ID_REST_STATE_POST_INIT;
962 init_cont (handle);
963 return;
964 }
965 if (ID_REST_STATE_INIT == handle->state) {
966 ego_entry = GNUNET_new (struct EgoEntry);
967 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
968 ego_entry->keystring =
969 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
970 ego_entry->ego = ego;
971 GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
972 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
973 }
974
975}
976
977/**
978 * Function processing the REST call
979 *
980 * @param method HTTP method
981 * @param url URL of the HTTP request
982 * @param data body of the HTTP request (optional)
983 * @param data_size length of the body
984 * @param proc callback function for the result
985 * @param proc_cls closure for callback function
986 * @return GNUNET_OK if request accepted
987 */
988static void
989rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle,
990 GNUNET_REST_ResultProcessor proc,
991 void *proc_cls)
992{
993 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
994
995
996
997 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
998
999 handle->proc_cls = proc_cls;
1000 handle->proc = proc;
1001 handle->state = ID_REST_STATE_INIT;
1002 handle->conndata_handle = conndata_handle;
1003 handle->data = conndata_handle->data;
1004 handle->data_size = conndata_handle->data_size;
1005 handle->method = conndata_handle->method;
1006 GNUNET_asprintf (&handle->url, "%s", conndata_handle->url);
1007 if (handle->url[strlen (handle->url)-1] == '/')
1008 handle->url[strlen (handle->url)-1] = '\0';
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Connecting...\n");
1011 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1012 &list_ego,
1013 handle);
1014 GNUNET_strdup ("Timeout");
1015 handle->timeout_task =
1016 GNUNET_SCHEDULER_add_delayed (handle->timeout,
1017 &do_error,
1018 handle);
1019
1020
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Connected\n");
1023}
1024
1025/**
1026 * Entry point for the plugin.
1027 *
1028 * @param cls Config info
1029 * @return NULL on error, otherwise the plugin context
1030 */
1031void *
1032libgnunet_plugin_rest_identity_init (void *cls)
1033{
1034 static struct Plugin plugin;
1035 struct GNUNET_REST_Plugin *api;
1036
1037 cfg = cls;
1038 if (NULL != plugin.cfg)
1039 return NULL; /* can only initialize once! */
1040 memset (&plugin, 0, sizeof (struct Plugin));
1041 plugin.cfg = cfg;
1042 api = GNUNET_new (struct GNUNET_REST_Plugin);
1043 api->cls = &plugin;
1044 api->name = GNUNET_REST_API_NS_IDENTITY;
1045 api->process_request = &rest_identity_process_request;
1046 GNUNET_asprintf (&allow_methods,
1047 "%s, %s, %s, %s, %s",
1048 MHD_HTTP_METHOD_GET,
1049 MHD_HTTP_METHOD_POST,
1050 MHD_HTTP_METHOD_PUT,
1051 MHD_HTTP_METHOD_DELETE,
1052 MHD_HTTP_METHOD_OPTIONS);
1053
1054 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1055 _("Identity REST API initialized\n"));
1056 return api;
1057}
1058
1059
1060/**
1061 * Exit point from the plugin.
1062 *
1063 * @param cls the plugin context (as returned by "init")
1064 * @return always NULL
1065 */
1066void *
1067libgnunet_plugin_rest_identity_done (void *cls)
1068{
1069 struct GNUNET_REST_Plugin *api = cls;
1070 struct Plugin *plugin = api->cls;
1071
1072 plugin->cfg = NULL;
1073 GNUNET_free_non_null (allow_methods);
1074 GNUNET_free (api);
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Identity REST plugin is finished\n");
1077 return NULL;
1078}
1079
1080/* end of plugin_rest_gns.c */