2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Martin Schanzenbach
20 * @author Philippe Buschmann
21 * @file identity/plugin_rest_openid_connect.c
22 * @brief GNUnet Namestore REST plugin
27 #include "gnunet_rest_plugin.h"
28 #include "gnunet_identity_service.h"
29 #include "gnunet_gns_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_rest_lib.h"
33 #include "gnunet_jsonapi_lib.h"
34 #include "gnunet_jsonapi_util.h"
35 #include "microhttpd.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_reclaim_attribute_lib.h"
40 #include "gnunet_reclaim_service.h"
46 #define GNUNET_REST_API_NS_OIDC "/openid"
51 #define GNUNET_REST_API_NS_AUTHORIZE "/openid/authorize"
56 #define GNUNET_REST_API_NS_TOKEN "/openid/token"
61 #define GNUNET_REST_API_NS_USERINFO "/openid/userinfo"
66 #define GNUNET_REST_API_NS_LOGIN "/openid/login"
71 #define GNUNET_REST_JSONAPI_RECLAIM_ATTRIBUTE "attribute"
76 #define GNUNET_REST_JSONAPI_IDENTITY_TICKET "ticket"
82 #define GNUNET_REST_JSONAPI_RECLAIM_ATTRIBUTE_VALUE "value"
85 * State while collecting all egos
87 #define ID_REST_STATE_INIT 0
90 * Done collecting egos
92 #define ID_REST_STATE_POST_INIT 1
97 #define OIDC_GRANT_TYPE_KEY "grant_type"
100 * OIDC grant_type key
102 #define OIDC_GRANT_TYPE_VALUE "authorization_code"
107 #define OIDC_CODE_KEY "code"
110 * OIDC response_type key
112 #define OIDC_RESPONSE_TYPE_KEY "response_type"
117 #define OIDC_CLIENT_ID_KEY "client_id"
122 #define OIDC_SCOPE_KEY "scope"
125 * OIDC redirect_uri key
127 #define OIDC_REDIRECT_URI_KEY "redirect_uri"
132 #define OIDC_STATE_KEY "state"
137 #define OIDC_NONCE_KEY "nonce"
140 * OIDC cookie header key
142 #define OIDC_COOKIE_HEADER_KEY "cookie"
145 * OIDC cookie header information key
147 #define OIDC_AUTHORIZATION_HEADER_KEY "authorization"
150 * OIDC cookie header information key
152 #define OIDC_COOKIE_HEADER_INFORMATION_KEY "Identity="
155 * OIDC expected response_type while authorizing
157 #define OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE "code"
160 * OIDC expected scope part while authorizing
162 #define OIDC_EXPECTED_AUTHORIZATION_SCOPE "openid"
165 * OIDC ignored parameter array
167 static char* OIDC_ignored_parameter_array [] =
179 * OIDC authorized identities and times hashmap
181 struct GNUNET_CONTAINER_MultiHashMap *OIDC_identity_login_time;
184 * OIDC authorized identities and times hashmap
186 struct GNUNET_CONTAINER_MultiHashMap *OIDC_identity_grants;
189 * OIDC ticket/code use only once
191 struct GNUNET_CONTAINER_MultiHashMap *OIDC_ticket_once;
194 * OIDC access_token to ticket and ego
196 struct GNUNET_CONTAINER_MultiHashMap *OIDC_interpret_access_token;
199 * The configuration handle
201 const struct GNUNET_CONFIGURATION_Handle *cfg;
204 * HTTP methods allows for this plugin
206 static char* allow_methods;
209 * @brief struct returned by the initialization function of the plugin
213 const struct GNUNET_CONFIGURATION_Handle *cfg;
217 * OIDC needed variables
219 struct OIDC_Variables
222 * The RP client public key
224 struct GNUNET_CRYPTO_EcdsaPublicKey client_pkey;
227 * The OIDC client id of the RP
232 * The OIDC redirect uri
237 * The list of oidc scopes
252 * The OIDC response type
257 * The identity chosen by the user to login
259 char *login_identity;
276 struct EgoEntry *next;
281 struct EgoEntry *prev;
296 struct GNUNET_IDENTITY_Ego *ego;
305 struct EgoEntry *ego_head;
310 struct EgoEntry *ego_tail;
315 struct EgoEntry *ego_entry;
318 * Pointer to ego private key
320 struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key;
325 struct OIDC_Variables *oidc;
328 * The processing state
333 * Handle to Identity service.
335 struct GNUNET_IDENTITY_Handle *identity_handle;
340 struct GNUNET_REST_RequestHandle *rest_handle;
343 * Handle to NAMESTORE
345 struct GNUNET_NAMESTORE_Handle *namestore_handle;
348 * Iterator for NAMESTORE
350 struct GNUNET_NAMESTORE_ZoneIterator *namestore_handle_it;
353 * Attribute claim list
355 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list;
360 struct GNUNET_IDENTITY_Operation *op;
365 struct GNUNET_RECLAIM_Handle *idp;
370 struct GNUNET_RECLAIM_Operation *idp_op;
375 struct GNUNET_RECLAIM_AttributeIterator *attr_it;
380 struct GNUNET_RECLAIM_TicketIterator *ticket_it;
385 struct GNUNET_RECLAIM_Ticket ticket;
388 * Desired timeout for the lookup (default is no timeout).
390 struct GNUNET_TIME_Relative timeout;
393 * ID of a task associated with the resolution process.
395 struct GNUNET_SCHEDULER_Task *timeout_task;
398 * The plugin result processor
400 GNUNET_REST_ResultProcessor proc;
403 * The closure of the result processor
413 * The tld for redirect
418 * Error response message
423 * Error response description
435 struct GNUNET_JSONAPI_Document *resp_object;
440 * Cleanup lookup handle
441 * @param handle Handle to clean up
444 cleanup_handle (struct RequestHandle *handle)
446 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_entry;
447 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_tmp;
448 struct EgoEntry *ego_entry;
449 struct EgoEntry *ego_tmp;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 if (NULL != handle->resp_object)
453 GNUNET_JSONAPI_document_delete (handle->resp_object);
454 if (NULL != handle->timeout_task)
455 GNUNET_SCHEDULER_cancel (handle->timeout_task);
456 if (NULL != handle->identity_handle)
457 GNUNET_IDENTITY_disconnect (handle->identity_handle);
458 if (NULL != handle->attr_it)
459 GNUNET_RECLAIM_get_attributes_stop (handle->attr_it);
460 if (NULL != handle->ticket_it)
461 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
462 if (NULL != handle->idp)
463 GNUNET_RECLAIM_disconnect (handle->idp);
464 if (NULL != handle->url)
465 GNUNET_free (handle->url);
466 if (NULL != handle->tld)
467 GNUNET_free (handle->tld);
468 if (NULL != handle->emsg)
469 GNUNET_free (handle->emsg);
470 if (NULL != handle->edesc)
471 GNUNET_free (handle->edesc);
472 if (NULL != handle->namestore_handle)
473 GNUNET_NAMESTORE_disconnect (handle->namestore_handle);
474 if (NULL != handle->oidc)
476 if (NULL != handle->oidc->client_id)
477 GNUNET_free(handle->oidc->client_id);
478 if (NULL != handle->oidc->login_identity)
479 GNUNET_free(handle->oidc->login_identity);
480 if (NULL != handle->oidc->nonce)
481 GNUNET_free(handle->oidc->nonce);
482 if (NULL != handle->oidc->redirect_uri)
483 GNUNET_free(handle->oidc->redirect_uri);
484 if (NULL != handle->oidc->response_type)
485 GNUNET_free(handle->oidc->response_type);
486 if (NULL != handle->oidc->scope)
487 GNUNET_free(handle->oidc->scope);
488 if (NULL != handle->oidc->state)
489 GNUNET_free(handle->oidc->state);
490 if (NULL != handle->oidc->response)
491 json_decref(handle->oidc->response);
492 GNUNET_free(handle->oidc);
494 if ( NULL != handle->attr_list )
496 for (claim_entry = handle->attr_list->list_head;
497 NULL != claim_entry;)
499 claim_tmp = claim_entry;
500 claim_entry = claim_entry->next;
501 GNUNET_free(claim_tmp->claim);
502 GNUNET_free(claim_tmp);
504 GNUNET_free (handle->attr_list);
506 for (ego_entry = handle->ego_head;
510 ego_entry = ego_entry->next;
511 GNUNET_free (ego_tmp->identifier);
512 GNUNET_free (ego_tmp->keystring);
513 GNUNET_free (ego_tmp);
515 if (NULL != handle->attr_it)
517 GNUNET_free(handle->attr_it);
519 GNUNET_free (handle);
523 cleanup_handle_delayed (void *cls)
525 cleanup_handle (cls);
530 * Task run on error, sends error message. Cleans up everything.
532 * @param cls the `struct RequestHandle`
537 struct RequestHandle *handle = cls;
538 struct MHD_Response *resp;
541 GNUNET_asprintf (&json_error, "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
543 (NULL != handle->edesc) ? handle->edesc : "",
544 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
545 (NULL != handle->oidc->state) ? handle->oidc->state : "",
546 (NULL != handle->oidc->state) ? "\"" : "");
547 if ( 0 == handle->response_code )
549 handle->response_code = MHD_HTTP_BAD_REQUEST;
551 resp = GNUNET_REST_create_response (json_error);
552 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
554 MHD_add_response_header(resp, "WWW-Authenticate", "Basic");
556 MHD_add_response_header (resp, "Content-Type", "application/json");
557 handle->proc (handle->proc_cls, resp, handle->response_code);
558 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
559 GNUNET_free (json_error);
564 * Task run on error in userinfo endpoint, sends error header. Cleans up
567 * @param cls the `struct RequestHandle`
570 do_userinfo_error (void *cls)
572 struct RequestHandle *handle = cls;
573 struct MHD_Response *resp;
576 GNUNET_asprintf (&error, "error=\"%s\", error_description=\"%s\"",
578 (NULL != handle->edesc) ? handle->edesc : "");
579 resp = GNUNET_REST_create_response ("");
580 MHD_add_response_header(resp, "WWW-Authenticate", error);
581 handle->proc (handle->proc_cls, resp, handle->response_code);
582 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
588 * Task run on error, sends error message and redirects. Cleans up everything.
590 * @param cls the `struct RequestHandle`
593 do_redirect_error (void *cls)
595 struct RequestHandle *handle = cls;
596 struct MHD_Response *resp;
598 GNUNET_asprintf (&redirect,
599 "%s?error=%s&error_description=%s%s%s",
600 handle->oidc->redirect_uri, handle->emsg, handle->edesc,
601 (NULL != handle->oidc->state) ? "&state=" : "",
602 (NULL != handle->oidc->state) ? handle->oidc->state : "");
603 resp = GNUNET_REST_create_response ("");
604 MHD_add_response_header (resp, "Location", redirect);
605 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
606 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
607 GNUNET_free (redirect);
611 * Task run on timeout, sends error message. Cleans up everything.
613 * @param cls the `struct RequestHandle`
616 do_timeout (void *cls)
618 struct RequestHandle *handle = cls;
620 handle->timeout_task = NULL;
625 * Return attributes for claim
627 * @param cls the request handle
630 return_userinfo_response (void *cls)
633 struct RequestHandle *handle = cls;
634 struct MHD_Response *resp;
636 result_str = json_dumps (handle->oidc->response, 0);
638 resp = GNUNET_REST_create_response (result_str);
639 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
640 GNUNET_free (result_str);
641 cleanup_handle (handle);
645 * Returns base64 encoded string without padding
647 * @param string the string to encode
648 * @return base64 encoded string
651 base_64_encode(const char *s)
656 GNUNET_STRINGS_base64_encode(s, strlen(s), &enc);
657 tmp = strrchr (enc, '=');
663 * Respond to OPTIONS request
665 * @param con_handle the connection handle
667 * @param cls the RequestHandle
670 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
674 struct MHD_Response *resp;
675 struct RequestHandle *handle = cls;
677 //For now, independent of path return all options
678 resp = GNUNET_REST_create_response (NULL);
679 MHD_add_response_header (resp,
680 "Access-Control-Allow-Methods",
682 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
683 cleanup_handle (handle);
688 * Interprets cookie header and pass its identity keystring to handle
691 cookie_identity_interpretation (struct RequestHandle *handle)
693 struct GNUNET_HashCode cache_key;
695 struct GNUNET_TIME_Absolute current_time, *relog_time;
696 char delimiter[] = "; ";
698 //gets identity of login try with cookie
699 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY, strlen (OIDC_COOKIE_HEADER_KEY),
701 if ( GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->header_param_map,
704 //splits cookies and find 'Identity' cookie
705 cookies = GNUNET_CONTAINER_multihashmap_get ( handle->rest_handle->header_param_map, &cache_key);
706 handle->oidc->login_identity = strtok(cookies, delimiter);
708 while ( NULL != handle->oidc->login_identity )
710 if ( NULL != strstr (handle->oidc->login_identity, OIDC_COOKIE_HEADER_INFORMATION_KEY) )
714 handle->oidc->login_identity = strtok (NULL, delimiter);
716 GNUNET_CRYPTO_hash (handle->oidc->login_identity, strlen (handle->oidc->login_identity),
718 if ( GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (OIDC_identity_login_time, &cache_key) )
720 relog_time = GNUNET_CONTAINER_multihashmap_get (OIDC_identity_login_time,
722 current_time = GNUNET_TIME_absolute_get ();
723 // 30 min after old login -> redirect to login
724 if ( current_time.abs_value_us <= relog_time->abs_value_us )
726 handle->oidc->login_identity = strtok(handle->oidc->login_identity, OIDC_COOKIE_HEADER_INFORMATION_KEY);
727 handle->oidc->login_identity = GNUNET_strdup(handle->oidc->login_identity);
729 handle->oidc->login_identity = NULL;
734 handle->oidc->login_identity = NULL;
740 * Redirects to login page stored in configuration file
743 login_redirection(void *cls)
745 char *login_base_url;
747 struct MHD_Response *resp;
748 struct RequestHandle *handle = cls;
751 == GNUNET_CONFIGURATION_get_value_string (cfg, "reclaim-rest-plugin",
752 "address", &login_base_url) )
754 GNUNET_asprintf (&new_redirect, "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
756 OIDC_RESPONSE_TYPE_KEY,
757 handle->oidc->response_type,
759 handle->oidc->client_id,
760 OIDC_REDIRECT_URI_KEY,
761 handle->oidc->redirect_uri,
765 (NULL != handle->oidc->state) ? handle->oidc->state : "",
767 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "");
768 resp = GNUNET_REST_create_response ("");
769 MHD_add_response_header (resp, "Location", new_redirect);
770 GNUNET_free(login_base_url);
774 handle->emsg = GNUNET_strdup("server_error");
775 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
776 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
777 GNUNET_SCHEDULER_add_now (&do_error, handle);
780 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
781 GNUNET_free(new_redirect);
782 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
786 * Does internal server error when iteration failed.
789 oidc_iteration_error (void *cls)
791 struct RequestHandle *handle = cls;
792 handle->emsg = GNUNET_strdup("INTERNAL_SERVER_ERROR");
793 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
794 GNUNET_SCHEDULER_add_now (&do_error, handle);
798 parse_authz_code (const char* code,
799 struct GNUNET_RECLAIM_Ticket **ticket,
806 json_t *signature_json;
807 const char *ticket_str;
808 const char *signature_str;
809 const char *nonce_str;
811 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
812 struct GNUNET_CRYPTO_EcdsaSignature signature;
813 size_t signature_payload_len;
816 GNUNET_STRINGS_base64_decode (code,
818 (void**)&code_output);
819 code_json = json_loads (code_output, 0 , &error);
820 GNUNET_free (code_output);
821 ticket_json = json_object_get (code_json, "ticket");
822 nonce_json = json_object_get (code_json, "nonce");
823 signature_json = json_object_get (code_json, "signature");
827 if ((NULL == ticket_json || !json_is_string (ticket_json)) ||
828 (NULL == signature_json || !json_is_string (signature_json)))
830 json_decref (code_json);
831 return GNUNET_SYSERR;
833 ticket_str = json_string_value (ticket_json);
834 signature_str = json_string_value (signature_json);
836 if (NULL != nonce_json)
837 nonce_str = json_string_value (nonce_json);
838 signature_payload_len = sizeof (struct GNUNET_RECLAIM_Ticket);
839 if (NULL != nonce_str)
840 signature_payload_len += strlen (nonce_str);
841 purpose = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
842 signature_payload_len);
843 purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + signature_payload_len);
844 purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
845 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (ticket_str,
848 sizeof (struct GNUNET_RECLAIM_Ticket)))
850 GNUNET_free (purpose);
851 json_decref (code_json);
852 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
853 "Cannot parse ticket!\n");
854 return GNUNET_SYSERR;
856 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (signature_str,
857 strlen (signature_str),
859 sizeof (struct GNUNET_CRYPTO_EcdsaSignature)))
861 GNUNET_free (purpose);
862 json_decref (code_json);
863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
864 "Cannot parse signature!\n");
865 return GNUNET_SYSERR;
867 *ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
870 sizeof (struct GNUNET_RECLAIM_Ticket));
871 if (NULL != nonce_str)
872 memcpy (&purpose[1] + sizeof (struct GNUNET_RECLAIM_Ticket),
875 if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN,
878 &(*ticket)->identity))
880 GNUNET_free (purpose);
881 GNUNET_free (*ticket);
882 json_decref (code_json);
884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
885 "Signature of authZ code invalid!\n");
886 return GNUNET_SYSERR;
888 *nonce = GNUNET_strdup (nonce_str);
893 build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
894 const struct GNUNET_RECLAIM_Ticket *ticket,
899 char *signature_payload;
902 size_t signature_payload_len;
903 struct GNUNET_CRYPTO_EcdsaSignature signature;
904 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
906 signature_payload_len = sizeof (struct GNUNET_RECLAIM_Ticket);
908 signature_payload_len += strlen (nonce);
910 signature_payload = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + signature_payload_len);
911 purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *)signature_payload;
912 purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + signature_payload_len);
913 purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
916 sizeof (struct GNUNET_RECLAIM_Ticket));
918 memcpy (&purpose[1] + sizeof (struct GNUNET_RECLAIM_Ticket),
921 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (issuer,
925 GNUNET_free (signature_payload);
928 signature_str = GNUNET_STRINGS_data_to_string_alloc (&signature,
930 ticket_str = GNUNET_STRINGS_data_to_string_alloc (ticket,
931 sizeof (struct GNUNET_RECLAIM_Ticket));
933 code_json = json_object ();
934 json_object_set_new (code_json,
936 json_string (ticket_str));
938 json_object_set_new (code_json,
940 json_string (nonce));
941 json_object_set_new (code_json,
943 json_string (signature_str));
944 authz_code = json_dumps (code_json,
945 JSON_INDENT(0) | JSON_COMPACT);
946 GNUNET_free (signature_payload);
947 GNUNET_free (signature_str);
948 GNUNET_free (ticket_str);
949 json_decref (code_json);
955 get_client_name_result (void *cls,
956 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
958 unsigned int rd_count,
959 const struct GNUNET_GNSRECORD_Data *rd)
961 struct RequestHandle *handle = cls;
962 struct MHD_Response *resp;
965 char *code_json_string;
966 char *code_base64_final_string;
971 ticket_str = GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
972 sizeof (struct GNUNET_RECLAIM_Ticket));
973 //TODO add signature to code payload over nonce and ticket _and_ use jansson here!
974 //TODO change if more attributes are needed (see max_age)
975 code_json_string = build_authz_code (&handle->priv_key,
977 handle->oidc->nonce);
978 /*GNUNET_asprintf (&code_json_string, "{\"ticket\":\"%s\"%s%s%s}",
980 (NULL != handle->oidc->nonce) ? ", \"nonce\":\"" : "",
981 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "",
982 (NULL != handle->oidc->nonce) ? "\"" : "");*/
983 code_base64_final_string = base_64_encode(code_json_string);
984 tmp = GNUNET_strdup (handle->oidc->redirect_uri);
985 redirect_path = strtok (tmp, "/");
986 redirect_path = strtok (NULL, "/");
987 redirect_path = strtok (NULL, "/");
988 tmp_prefix = GNUNET_strdup (handle->oidc->redirect_uri);
989 prefix = strrchr (tmp_prefix,
990 (unsigned char) '.');
992 GNUNET_asprintf (&redirect_uri, "%s.%s/%s?%s=%s&state=%s",
996 handle->oidc->response_type,
997 code_base64_final_string, handle->oidc->state);
998 resp = GNUNET_REST_create_response ("");
999 MHD_add_response_header (resp, "Location", redirect_uri);
1000 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1001 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1003 GNUNET_free (tmp_prefix);
1004 GNUNET_free (redirect_uri);
1005 GNUNET_free (ticket_str);
1006 GNUNET_free (code_json_string);
1007 GNUNET_free (code_base64_final_string);
1012 get_client_name_error (void *cls)
1014 struct RequestHandle *handle = cls;
1016 handle->emsg = GNUNET_strdup("server_error");
1017 handle->edesc = GNUNET_strdup("Server cannot generate ticket, no name found for client.");
1018 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1022 * Issues ticket and redirects to relying party with the authorization code as
1023 * parameter. Otherwise redirects with error
1026 oidc_ticket_issue_cb (void* cls,
1027 const struct GNUNET_RECLAIM_Ticket *ticket)
1029 struct RequestHandle *handle = cls;
1030 handle->idp_op = NULL;
1031 handle->ticket = *ticket;
1032 if (NULL != ticket) {
1033 GNUNET_NAMESTORE_zone_to_name (handle->namestore_handle,
1035 &handle->oidc->client_pkey,
1036 &get_client_name_error,
1038 &get_client_name_result,
1042 handle->emsg = GNUNET_strdup("server_error");
1043 handle->edesc = GNUNET_strdup("Server cannot generate ticket.");
1044 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1048 oidc_collect_finished_cb (void *cls)
1050 struct RequestHandle *handle = cls;
1051 handle->attr_it = NULL;
1052 handle->ticket_it = NULL;
1053 if (NULL == handle->attr_list->list_head)
1055 handle->emsg = GNUNET_strdup("invalid_scope");
1056 handle->edesc = GNUNET_strdup("The requested scope is not available.");
1057 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1060 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
1062 &handle->oidc->client_pkey,
1064 &oidc_ticket_issue_cb,
1070 * Collects all attributes for an ego if in scope parameter
1073 oidc_attr_collect (void *cls,
1074 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1075 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
1077 struct RequestHandle *handle = cls;
1078 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
1079 char* scope_variables;
1080 char* scope_variable;
1081 char delimiter[]=" ";
1083 if ( (NULL == attr->name) || (NULL == attr->data) )
1085 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1089 scope_variables = GNUNET_strdup(handle->oidc->scope);
1090 scope_variable = strtok (scope_variables, delimiter);
1091 while (NULL != scope_variable)
1093 if ( 0 == strcmp (attr->name, scope_variable) )
1097 scope_variable = strtok (NULL, delimiter);
1099 if ( NULL == scope_variable )
1101 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1102 GNUNET_free(scope_variables);
1105 GNUNET_free(scope_variables);
1107 le = GNUNET_new(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
1108 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name, attr->type,
1109 attr->data, attr->data_size);
1110 GNUNET_CONTAINER_DLL_insert(handle->attr_list->list_head,
1111 handle->attr_list->list_tail, le);
1112 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1117 * Checks time and cookie and redirects accordingly
1120 login_check (void *cls)
1122 struct RequestHandle *handle = cls;
1123 struct GNUNET_TIME_Absolute current_time, *relog_time;
1124 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey, ego_pkey;
1125 struct GNUNET_HashCode cache_key;
1126 char *identity_cookie;
1128 GNUNET_asprintf (&identity_cookie, "Identity=%s", handle->oidc->login_identity);
1129 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1130 GNUNET_free(identity_cookie);
1131 //No login time for identity -> redirect to login
1133 == GNUNET_CONTAINER_multihashmap_contains (OIDC_identity_login_time,
1136 relog_time = GNUNET_CONTAINER_multihashmap_get (OIDC_identity_login_time,
1138 current_time = GNUNET_TIME_absolute_get ();
1139 // 30 min after old login -> redirect to login
1140 if ( current_time.abs_value_us <= relog_time->abs_value_us )
1143 != GNUNET_CRYPTO_ecdsa_public_key_from_string (
1144 handle->oidc->login_identity,
1145 strlen (handle->oidc->login_identity), &pubkey) )
1147 handle->emsg = GNUNET_strdup("invalid_cookie");
1148 handle->edesc = GNUNET_strdup(
1149 "The cookie of a login identity is not valid");
1150 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1153 // iterate over egos and compare their public key
1154 for (handle->ego_entry = handle->ego_head;
1155 NULL != handle->ego_entry; handle->ego_entry = handle->ego_entry->next)
1157 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1159 == memcmp (&ego_pkey, &pubkey,
1160 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) )
1162 handle->priv_key = *GNUNET_IDENTITY_ego_get_private_key (
1163 handle->ego_entry->ego);
1164 handle->resp_object = GNUNET_JSONAPI_document_new ();
1165 handle->idp = GNUNET_RECLAIM_connect (cfg);
1166 handle->attr_list = GNUNET_new(
1167 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
1168 handle->attr_it = GNUNET_RECLAIM_get_attributes_start (
1169 handle->idp, &handle->priv_key, &oidc_iteration_error, handle,
1170 &oidc_attr_collect, handle, &oidc_collect_finished_cb, handle);
1174 //handle->emsg = GNUNET_strdup("invalid_cookie");
1175 //handle->edesc = GNUNET_strdup(
1176 // "The cookie of the login identity is not valid");
1177 //GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1178 GNUNET_SCHEDULER_add_now (&login_redirection,handle);
1185 * Iteration over all results finished, build final
1188 * @param cls the `struct RequestHandle`
1191 build_authz_response (void *cls)
1193 struct RequestHandle *handle = cls;
1194 struct GNUNET_HashCode cache_key;
1196 char *expected_scope;
1197 char delimiter[]=" ";
1198 int number_of_ignored_parameter, iterator;
1201 // REQUIRED value: redirect_uri
1202 GNUNET_CRYPTO_hash (OIDC_REDIRECT_URI_KEY, strlen (OIDC_REDIRECT_URI_KEY),
1204 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1207 handle->emsg=GNUNET_strdup("invalid_request");
1208 handle->edesc=GNUNET_strdup("missing parameter redirect_uri");
1209 GNUNET_SCHEDULER_add_now (&do_error, handle);
1212 handle->oidc->redirect_uri = GNUNET_strdup (GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map,
1215 // REQUIRED value: response_type
1216 GNUNET_CRYPTO_hash (OIDC_RESPONSE_TYPE_KEY, strlen (OIDC_RESPONSE_TYPE_KEY),
1218 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1221 handle->emsg=GNUNET_strdup("invalid_request");
1222 handle->edesc=GNUNET_strdup("missing parameter response_type");
1223 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1226 handle->oidc->response_type = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map,
1228 handle->oidc->response_type = GNUNET_strdup (handle->oidc->response_type);
1230 // REQUIRED value: scope
1231 GNUNET_CRYPTO_hash (OIDC_SCOPE_KEY, strlen (OIDC_SCOPE_KEY), &cache_key);
1232 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1235 handle->emsg=GNUNET_strdup("invalid_request");
1236 handle->edesc=GNUNET_strdup("missing parameter scope");
1237 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1240 handle->oidc->scope = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map,
1242 handle->oidc->scope = GNUNET_strdup(handle->oidc->scope);
1244 //OPTIONAL value: nonce
1245 GNUNET_CRYPTO_hash (OIDC_NONCE_KEY, strlen (OIDC_NONCE_KEY), &cache_key);
1246 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1249 handle->oidc->nonce = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map,
1251 handle->oidc->nonce = GNUNET_strdup (handle->oidc->nonce);
1254 //TODO check other values if needed
1255 number_of_ignored_parameter = sizeof(OIDC_ignored_parameter_array) / sizeof(char *);
1256 for( iterator = 0; iterator < number_of_ignored_parameter; iterator++ )
1258 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1259 strlen(OIDC_ignored_parameter_array[iterator]),
1261 if(GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(handle->rest_handle->url_param_map,
1264 handle->emsg=GNUNET_strdup("access_denied");
1265 GNUNET_asprintf (&handle->edesc, "Server will not handle parameter: %s",
1266 OIDC_ignored_parameter_array[iterator]);
1267 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1272 // Checks if response_type is 'code'
1273 if( 0 != strcmp( handle->oidc->response_type, OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE ) )
1275 handle->emsg=GNUNET_strdup("unsupported_response_type");
1276 handle->edesc=GNUNET_strdup("The authorization server does not support "
1277 "obtaining this authorization code.");
1278 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1282 // Checks if scope contains 'openid'
1283 expected_scope = GNUNET_strdup(handle->oidc->scope);
1285 test = strtok (expected_scope, delimiter);
1286 while (NULL != test)
1288 if ( 0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope) )
1292 test = strtok (NULL, delimiter);
1296 handle->emsg = GNUNET_strdup("invalid_scope");
1297 handle->edesc=GNUNET_strdup("The requested scope is invalid, unknown, or "
1299 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1300 GNUNET_free(expected_scope);
1304 GNUNET_free(expected_scope);
1306 if( NULL != handle->oidc->login_identity )
1308 GNUNET_SCHEDULER_add_now(&login_check,handle);
1312 GNUNET_SCHEDULER_add_now(&login_redirection,handle);
1316 * Responds to authorization GET and url-encoded POST request
1318 * @param con_handle the connection handle
1319 * @param url the url
1320 * @param cls the RequestHandle
1323 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1327 struct RequestHandle *handle = cls;
1328 struct GNUNET_HashCode cache_key;
1329 struct EgoEntry *tmp_ego;
1330 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1331 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1333 cookie_identity_interpretation(handle);
1335 //RECOMMENDED value: state - REQUIRED for answers
1336 GNUNET_CRYPTO_hash (OIDC_STATE_KEY, strlen (OIDC_STATE_KEY), &cache_key);
1337 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1340 handle->oidc->state = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map,
1342 handle->oidc->state = GNUNET_strdup (handle->oidc->state);
1345 // REQUIRED value: client_id
1346 GNUNET_CRYPTO_hash (OIDC_CLIENT_ID_KEY, strlen (OIDC_CLIENT_ID_KEY),
1348 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1351 handle->emsg=GNUNET_strdup("invalid_request");
1352 handle->edesc=GNUNET_strdup("missing parameter client_id");
1353 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1354 GNUNET_SCHEDULER_add_now (&do_error, handle);
1357 handle->oidc->client_id = GNUNET_strdup (GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map,
1361 != GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1362 strlen (handle->oidc->client_id),
1363 &handle->oidc->client_pkey) )
1365 handle->emsg = GNUNET_strdup("unauthorized_client");
1366 handle->edesc = GNUNET_strdup("The client is not authorized to request an "
1367 "authorization code using this method.");
1368 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1369 GNUNET_SCHEDULER_add_now (&do_error, handle);
1374 if ( NULL == handle->ego_head )
1376 handle->emsg = GNUNET_strdup("server_error");
1377 handle->edesc = GNUNET_strdup ("Egos are missing");
1378 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1379 GNUNET_SCHEDULER_add_now (&do_error, handle);
1383 handle->ego_entry = handle->ego_head;
1384 handle->priv_key = *GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
1385 //If we know this identity, translated the corresponding TLD
1386 //TODO: We might want to have a reverse lookup functionality for TLDs?
1387 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1389 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1390 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key,
1392 if ( 0 == memcmp (&pkey, &handle->oidc->client_pkey,
1393 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) )
1395 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1396 handle->ego_entry = handle->ego_tail;
1399 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1403 * Combines an identity with a login time and responds OK to login request
1405 * @param con_handle the connection handle
1406 * @param url the url
1407 * @param cls the RequestHandle
1410 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1414 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1415 struct RequestHandle *handle = cls;
1416 struct GNUNET_HashCode cache_key;
1417 struct GNUNET_TIME_Absolute *current_time;
1418 struct GNUNET_TIME_Absolute *last_time;
1423 char term_data[handle->rest_handle->data_size+1];
1424 term_data[handle->rest_handle->data_size] = '\0';
1425 GNUNET_memcpy (term_data, handle->rest_handle->data, handle->rest_handle->data_size);
1426 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1427 identity = json_object_get (root, "identity");
1428 if ( json_is_string(identity) )
1430 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1431 MHD_add_response_header (resp, "Set-Cookie", cookie);
1432 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1433 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1435 current_time = GNUNET_new(struct GNUNET_TIME_Absolute);
1436 *current_time = GNUNET_TIME_relative_to_absolute (
1437 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1439 last_time = GNUNET_CONTAINER_multihashmap_get(OIDC_identity_login_time, &cache_key);
1440 if (NULL != last_time)
1442 GNUNET_free(last_time);
1444 GNUNET_CONTAINER_multihashmap_put (
1445 OIDC_identity_login_time, &cache_key, current_time,
1446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1448 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1449 GNUNET_free(cookie);
1453 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1456 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1461 * Responds to token url-encoded POST request
1463 * @param con_handle the connection handle
1464 * @param url the url
1465 * @param cls the RequestHandle
1468 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1472 //TODO static strings
1473 struct RequestHandle *handle = cls;
1474 struct GNUNET_HashCode cache_key;
1475 char *authorization, *credentials;
1476 char delimiter[]=" ";
1477 char delimiter_user_psw[]=":";
1478 char *grant_type, *code;
1479 char *user_psw = NULL, *client_id, *psw;
1481 int client_exists = GNUNET_NO;
1482 struct MHD_Response *resp;
1483 char *json_response;
1488 * Check Authorization
1490 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1491 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1493 if ( GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->header_param_map,
1496 handle->emsg=GNUNET_strdup("invalid_client");
1497 handle->edesc=GNUNET_strdup("missing authorization");
1498 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1499 GNUNET_SCHEDULER_add_now (&do_error, handle);
1502 authorization = GNUNET_CONTAINER_multihashmap_get ( handle->rest_handle->header_param_map, &cache_key);
1504 //split header in "Basic" and [content]
1505 credentials = strtok (authorization, delimiter);
1506 if (0 != strcmp ("Basic",credentials))
1508 handle->emsg=GNUNET_strdup("invalid_client");
1509 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1510 GNUNET_SCHEDULER_add_now (&do_error, handle);
1513 credentials = strtok(NULL, delimiter);
1514 if (NULL == credentials)
1516 handle->emsg=GNUNET_strdup("invalid_client");
1517 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1518 GNUNET_SCHEDULER_add_now (&do_error, handle);
1521 GNUNET_STRINGS_base64_decode (credentials, strlen (credentials), (void**)&user_psw);
1523 if ( NULL == user_psw )
1525 handle->emsg=GNUNET_strdup("invalid_client");
1526 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1527 GNUNET_SCHEDULER_add_now (&do_error, handle);
1530 client_id = strtok (user_psw, delimiter_user_psw);
1531 if ( NULL == client_id )
1533 GNUNET_free_non_null(user_psw);
1534 handle->emsg=GNUNET_strdup("invalid_client");
1535 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1536 GNUNET_SCHEDULER_add_now (&do_error, handle);
1539 psw = strtok (NULL, delimiter_user_psw);
1542 GNUNET_free_non_null(user_psw);
1543 handle->emsg=GNUNET_strdup("invalid_client");
1544 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1545 GNUNET_SCHEDULER_add_now (&do_error, handle);
1549 //check client password
1551 == GNUNET_CONFIGURATION_get_value_string (cfg, "reclaim-rest-plugin",
1552 "psw", &expected_psw) )
1554 if (0 != strcmp (expected_psw, psw))
1556 GNUNET_free_non_null(user_psw);
1557 GNUNET_free(expected_psw);
1558 handle->emsg=GNUNET_strdup("invalid_client");
1559 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1560 GNUNET_SCHEDULER_add_now (&do_error, handle);
1563 GNUNET_free(expected_psw);
1567 GNUNET_free_non_null(user_psw);
1568 handle->emsg = GNUNET_strdup("server_error");
1569 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1570 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1571 GNUNET_SCHEDULER_add_now (&do_error, handle);
1576 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry->next; )
1578 if ( 0 == strcmp(handle->ego_entry->keystring, client_id))
1580 client_exists = GNUNET_YES;
1583 handle->ego_entry = handle->ego_entry->next;
1585 if (GNUNET_NO == client_exists)
1587 GNUNET_free_non_null(user_psw);
1588 handle->emsg=GNUNET_strdup("invalid_client");
1589 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1590 GNUNET_SCHEDULER_add_now (&do_error, handle);
1598 //TODO Do not allow multiple equal parameter names
1599 //REQUIRED grant_type
1600 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY, strlen (OIDC_GRANT_TYPE_KEY), &cache_key);
1602 == GNUNET_CONTAINER_multihashmap_contains (
1603 handle->rest_handle->url_param_map, &cache_key) )
1605 GNUNET_free_non_null(user_psw);
1606 handle->emsg = GNUNET_strdup("invalid_request");
1607 handle->edesc = GNUNET_strdup("missing parameter grant_type");
1608 handle->response_code = MHD_HTTP_BAD_REQUEST;
1609 GNUNET_SCHEDULER_add_now (&do_error, handle);
1612 grant_type = GNUNET_CONTAINER_multihashmap_get (
1613 handle->rest_handle->url_param_map, &cache_key);
1616 GNUNET_CRYPTO_hash (OIDC_CODE_KEY, strlen (OIDC_CODE_KEY), &cache_key);
1618 == GNUNET_CONTAINER_multihashmap_contains (
1619 handle->rest_handle->url_param_map, &cache_key) )
1621 GNUNET_free_non_null(user_psw);
1622 handle->emsg = GNUNET_strdup("invalid_request");
1623 handle->edesc = GNUNET_strdup("missing parameter code");
1624 handle->response_code = MHD_HTTP_BAD_REQUEST;
1625 GNUNET_SCHEDULER_add_now (&do_error, handle);
1628 code = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
1631 //REQUIRED redirect_uri
1632 GNUNET_CRYPTO_hash (OIDC_REDIRECT_URI_KEY, strlen (OIDC_REDIRECT_URI_KEY),
1635 == GNUNET_CONTAINER_multihashmap_contains (
1636 handle->rest_handle->url_param_map, &cache_key) )
1638 GNUNET_free_non_null(user_psw);
1639 handle->emsg = GNUNET_strdup("invalid_request");
1640 handle->edesc = GNUNET_strdup("missing parameter redirect_uri");
1641 handle->response_code = MHD_HTTP_BAD_REQUEST;
1642 GNUNET_SCHEDULER_add_now (&do_error, handle);
1646 //Check parameter grant_type == "authorization_code"
1647 if (0 != strcmp(OIDC_GRANT_TYPE_VALUE, grant_type))
1649 GNUNET_free_non_null(user_psw);
1650 handle->emsg=GNUNET_strdup("unsupported_grant_type");
1651 handle->response_code = MHD_HTTP_BAD_REQUEST;
1652 GNUNET_SCHEDULER_add_now (&do_error, handle);
1655 GNUNET_CRYPTO_hash (code, strlen (code), &cache_key);
1658 == GNUNET_CONTAINER_multihashmap_put (OIDC_ticket_once,
1661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) )
1663 GNUNET_free_non_null(user_psw);
1664 handle->emsg = GNUNET_strdup("invalid_request");
1665 handle->edesc = GNUNET_strdup("Cannot use the same code more than once");
1666 handle->response_code = MHD_HTTP_BAD_REQUEST;
1667 GNUNET_SCHEDULER_add_now (&do_error, handle);
1672 struct GNUNET_RECLAIM_Ticket *ticket;
1673 if(GNUNET_OK != parse_authz_code (code,
1677 GNUNET_free_non_null(user_psw);
1678 handle->emsg = GNUNET_strdup("invalid_request");
1679 handle->edesc = GNUNET_strdup("invalid code");
1680 handle->response_code = MHD_HTTP_BAD_REQUEST;
1681 GNUNET_SCHEDULER_add_now (&do_error, handle);
1685 // this is the current client (relying party)
1686 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1687 GNUNET_IDENTITY_ego_get_public_key(handle->ego_entry->ego,&pub_key);
1688 if (0 != memcmp(&pub_key,&ticket->audience,sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)))
1690 GNUNET_free_non_null(user_psw);
1691 handle->emsg = GNUNET_strdup("invalid_request");
1692 handle->edesc = GNUNET_strdup("invalid code");
1693 handle->response_code = MHD_HTTP_BAD_REQUEST;
1694 GNUNET_SCHEDULER_add_now (&do_error, handle);
1695 GNUNET_free(ticket);
1700 struct GNUNET_TIME_Relative expiration_time;
1702 != GNUNET_CONFIGURATION_get_value_time(cfg, "reclaim-rest-plugin",
1703 "expiration_time", &expiration_time) )
1705 GNUNET_free_non_null(user_psw);
1706 handle->emsg = GNUNET_strdup("server_error");
1707 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1708 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1709 GNUNET_SCHEDULER_add_now (&do_error, handle);
1710 GNUNET_free(ticket);
1714 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
1716 //TODO OPTIONAL acr,amr,azp
1718 struct EgoEntry *ego_entry;
1719 for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next)
1721 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1722 if (0 == memcmp (&pub_key, &ticket->audience, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)))
1727 if ( NULL == ego_entry )
1729 GNUNET_free_non_null(user_psw);
1730 handle->emsg = GNUNET_strdup("invalid_request");
1731 handle->edesc = GNUNET_strdup("invalid code...");
1732 handle->response_code = MHD_HTTP_BAD_REQUEST;
1733 GNUNET_SCHEDULER_add_now (&do_error, handle);
1734 GNUNET_free(ticket);
1738 != GNUNET_CONFIGURATION_get_value_string (cfg, "reclaim-rest-plugin",
1739 "jwt_secret", &jwt_secret) )
1741 GNUNET_free_non_null(user_psw);
1742 handle->emsg = GNUNET_strdup("invalid_request");
1743 handle->edesc = GNUNET_strdup("No signing secret configured!");
1744 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1745 GNUNET_SCHEDULER_add_now (&do_error, handle);
1746 GNUNET_free(ticket);
1749 char *id_token = jwt_create_from_list(&ticket->audience,
1753 (NULL != nonce) ? nonce : NULL,
1756 //Create random access_token
1757 char* access_token_number;
1759 uint64_t random_number;
1760 random_number = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1761 GNUNET_asprintf(&access_token_number, "%" PRIu64, random_number);
1762 GNUNET_STRINGS_base64_encode(access_token_number,strlen(access_token_number),&access_token);
1766 //TODO OPTIONAL add refresh_token and scope
1767 GNUNET_asprintf (&json_response,
1768 "{ \"access_token\" : \"%s\", "
1769 "\"token_type\" : \"Bearer\", "
1770 "\"expires_in\" : %d, "
1771 "\"id_token\" : \"%s\"}",
1775 GNUNET_CRYPTO_hash(access_token, strlen(access_token), &cache_key);
1776 char *id_ticket_combination;
1777 char *ticket_string;
1778 ticket_string = GNUNET_STRINGS_data_to_string_alloc (ticket,
1779 sizeof (struct GNUNET_RECLAIM_Ticket));
1780 GNUNET_asprintf(&id_ticket_combination,
1784 GNUNET_CONTAINER_multihashmap_put(OIDC_interpret_access_token,
1786 id_ticket_combination,
1787 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1789 resp = GNUNET_REST_create_response (json_response);
1790 MHD_add_response_header (resp, "Cache-Control", "no-store");
1791 MHD_add_response_header (resp, "Pragma", "no-cache");
1792 MHD_add_response_header (resp, "Content-Type", "application/json");
1793 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1795 GNUNET_RECLAIM_ATTRIBUTE_list_destroy(cl);
1796 GNUNET_free(access_token_number);
1797 GNUNET_free(access_token);
1798 GNUNET_free(user_psw);
1799 GNUNET_free(json_response);
1800 GNUNET_free(ticket);
1801 GNUNET_free(id_token);
1802 GNUNET_SCHEDULER_add_now(&cleanup_handle_delayed, handle);
1806 * Collects claims and stores them in handle
1809 consume_ticket (void *cls,
1810 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1811 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
1813 struct RequestHandle *handle = cls;
1817 if (NULL == identity)
1819 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1823 tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
1827 value = json_string (tmp_value);
1830 json_object_set_new (handle->oidc->response,
1833 GNUNET_free (tmp_value);
1837 * Responds to userinfo GET and url-encoded POST request
1839 * @param con_handle the connection handle
1840 * @param url the url
1841 * @param cls the RequestHandle
1844 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1845 const char* url, void *cls)
1847 //TODO expiration time
1848 struct RequestHandle *handle = cls;
1849 char delimiter[] = " ";
1850 char delimiter_db[] = ";";
1851 struct GNUNET_HashCode cache_key;
1852 char *authorization, *authorization_type, *authorization_access_token;
1853 char *client_ticket, *client, *ticket_str;
1854 struct GNUNET_RECLAIM_Ticket *ticket;
1856 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1857 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1860 == GNUNET_CONTAINER_multihashmap_contains (
1861 handle->rest_handle->header_param_map, &cache_key) )
1863 handle->emsg = GNUNET_strdup("invalid_token");
1864 handle->edesc = GNUNET_strdup("No Access Token");
1865 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1866 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1869 authorization = GNUNET_CONTAINER_multihashmap_get (
1870 handle->rest_handle->header_param_map, &cache_key);
1872 //split header in "Bearer" and access_token
1873 authorization = GNUNET_strdup(authorization);
1874 authorization_type = strtok (authorization, delimiter);
1875 if ( 0 != strcmp ("Bearer", authorization_type) )
1877 handle->emsg = GNUNET_strdup("invalid_token");
1878 handle->edesc = GNUNET_strdup("No Access Token");
1879 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1880 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1881 GNUNET_free(authorization);
1884 authorization_access_token = strtok (NULL, delimiter);
1885 if ( NULL == authorization_access_token )
1887 handle->emsg = GNUNET_strdup("invalid_token");
1888 handle->edesc = GNUNET_strdup("No Access Token");
1889 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1890 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1891 GNUNET_free(authorization);
1895 GNUNET_CRYPTO_hash (authorization_access_token,
1896 strlen (authorization_access_token),
1898 if ( GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (OIDC_interpret_access_token,
1901 handle->emsg = GNUNET_strdup("invalid_token");
1902 handle->edesc = GNUNET_strdup("The Access Token expired");
1903 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1904 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1905 GNUNET_free(authorization);
1909 client_ticket = GNUNET_CONTAINER_multihashmap_get(OIDC_interpret_access_token,
1911 client_ticket = GNUNET_strdup(client_ticket);
1912 client = strtok(client_ticket,delimiter_db);
1915 handle->emsg = GNUNET_strdup("invalid_token");
1916 handle->edesc = GNUNET_strdup("The Access Token expired");
1917 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1918 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1919 GNUNET_free(authorization);
1920 GNUNET_free(client_ticket);
1923 handle->ego_entry = handle->ego_head;
1924 for(; NULL != handle->ego_entry; handle->ego_entry = handle->ego_entry->next)
1926 if (0 == strcmp(handle->ego_entry->keystring,client))
1931 if (NULL == handle->ego_entry)
1933 handle->emsg = GNUNET_strdup("invalid_token");
1934 handle->edesc = GNUNET_strdup("The Access Token expired");
1935 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1936 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1937 GNUNET_free(authorization);
1938 GNUNET_free(client_ticket);
1941 ticket_str = strtok(NULL, delimiter_db);
1942 if (NULL == ticket_str)
1944 handle->emsg = GNUNET_strdup("invalid_token");
1945 handle->edesc = GNUNET_strdup("The Access Token expired");
1946 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1947 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1948 GNUNET_free(authorization);
1949 GNUNET_free(client_ticket);
1952 ticket = GNUNET_new(struct GNUNET_RECLAIM_Ticket);
1954 != GNUNET_STRINGS_string_to_data (ticket_str,
1955 strlen (ticket_str),
1957 sizeof(struct GNUNET_RECLAIM_Ticket)))
1959 handle->emsg = GNUNET_strdup("invalid_token");
1960 handle->edesc = GNUNET_strdup("The Access Token expired");
1961 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1962 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1963 GNUNET_free(ticket);
1964 GNUNET_free(authorization);
1965 GNUNET_free(client_ticket);
1969 handle->idp = GNUNET_RECLAIM_connect (cfg);
1970 handle->oidc->response = json_object();
1971 json_object_set_new( handle->oidc->response, "sub", json_string( handle->ego_entry->keystring));
1972 handle->idp_op = GNUNET_RECLAIM_ticket_consume (
1974 GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego),
1978 GNUNET_free(ticket);
1979 GNUNET_free(authorization);
1980 GNUNET_free(client_ticket);
1986 * Handle rest request
1988 * @param handle the request handle
1991 init_cont (struct RequestHandle *handle)
1993 struct GNUNET_REST_RequestHandlerError err;
1994 static const struct GNUNET_REST_RequestHandler handlers[] = {
1995 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint},
1996 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint}, //url-encoded
1997 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont},
1998 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint },
1999 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2000 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2001 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC,
2003 GNUNET_REST_HANDLER_END
2006 if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle,
2011 handle->response_code = err.error_code;
2012 GNUNET_SCHEDULER_add_now (&do_error, handle);
2017 * If listing is enabled, prints information about the egos.
2019 * This function is initially called for all egos and then again
2020 * whenever a ego's identifier changes or if it is deleted. At the
2021 * end of the initial pass over all egos, the function is once called
2022 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
2023 * be invoked in the future or that there was an error.
2025 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
2026 * this function is only called ONCE, and 'NULL' being passed in
2027 * 'ego' does indicate an error (i.e. name is taken or no default
2028 * value is known). If 'ego' is non-NULL and if '*ctx'
2029 * is set in those callbacks, the value WILL be passed to a subsequent
2030 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
2031 * that one was not NULL).
2033 * When an identity is renamed, this function is called with the
2034 * (known) ego but the NEW identifier.
2036 * When an identity is deleted, this function is called with the
2037 * (known) ego and "NULL" for the 'identifier'. In this case,
2038 * the 'ego' is henceforth invalid (and the 'ctx' should also be
2041 * @param cls closure
2042 * @param ego ego handle
2043 * @param ctx context for application to store data for this ego
2044 * (during the lifetime of this process, initially NULL)
2045 * @param identifier identifier assigned by the user for this ego,
2046 * NULL if the user just deleted the ego and it
2047 * must thus no longer be used
2050 list_ego (void *cls,
2051 struct GNUNET_IDENTITY_Ego *ego,
2053 const char *identifier)
2055 struct RequestHandle *handle = cls;
2056 struct EgoEntry *ego_entry;
2057 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
2059 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2061 handle->state = ID_REST_STATE_POST_INIT;
2065 if (ID_REST_STATE_INIT == handle->state) {
2066 ego_entry = GNUNET_new (struct EgoEntry);
2067 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2068 ego_entry->keystring =
2069 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2070 ego_entry->ego = ego;
2071 ego_entry->identifier = GNUNET_strdup (identifier);
2072 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
2075 /* Ego renamed or added */
2076 if (identifier != NULL) {
2077 for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) {
2078 if (ego_entry->ego == ego) {
2080 GNUNET_free (ego_entry->identifier);
2081 ego_entry->identifier = GNUNET_strdup (identifier);
2085 if (NULL == ego_entry) {
2087 ego_entry = GNUNET_new (struct EgoEntry);
2088 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2089 ego_entry->keystring =
2090 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2091 ego_entry->ego = ego;
2092 ego_entry->identifier = GNUNET_strdup (identifier);
2093 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
2097 for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) {
2098 if (ego_entry->ego == ego)
2101 if (NULL != ego_entry)
2102 GNUNET_CONTAINER_DLL_remove(handle->ego_head,handle->ego_tail, ego_entry);
2108 rest_identity_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
2109 GNUNET_REST_ResultProcessor proc,
2112 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2113 handle->oidc = GNUNET_new (struct OIDC_Variables);
2114 if ( NULL == OIDC_identity_login_time )
2115 OIDC_identity_login_time = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2116 if ( NULL == OIDC_identity_grants )
2117 OIDC_identity_grants = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2118 if ( NULL == OIDC_ticket_once )
2119 OIDC_ticket_once = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2120 if ( NULL == OIDC_interpret_access_token )
2121 OIDC_interpret_access_token = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2122 handle->response_code = 0;
2123 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2124 handle->proc_cls = proc_cls;
2125 handle->proc = proc;
2126 handle->state = ID_REST_STATE_INIT;
2127 handle->rest_handle = rest_handle;
2129 handle->url = GNUNET_strdup (rest_handle->url);
2130 if (handle->url[strlen (handle->url)-1] == '/')
2131 handle->url[strlen (handle->url)-1] = '\0';
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2134 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
2137 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2138 handle->timeout_task =
2139 GNUNET_SCHEDULER_add_delayed (handle->timeout,
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 * Entry point for the plugin.
2149 * @param cls Config info
2150 * @return NULL on error, otherwise the plugin context
2153 libgnunet_plugin_rest_openid_connect_init (void *cls)
2155 static struct Plugin plugin;
2156 struct GNUNET_REST_Plugin *api;
2159 if (NULL != plugin.cfg)
2160 return NULL; /* can only initialize once! */
2161 memset (&plugin, 0, sizeof (struct Plugin));
2163 api = GNUNET_new (struct GNUNET_REST_Plugin);
2165 api->name = GNUNET_REST_API_NS_OIDC;
2166 api->process_request = &rest_identity_process_request;
2167 GNUNET_asprintf (&allow_methods,
2168 "%s, %s, %s, %s, %s",
2169 MHD_HTTP_METHOD_GET,
2170 MHD_HTTP_METHOD_POST,
2171 MHD_HTTP_METHOD_PUT,
2172 MHD_HTTP_METHOD_DELETE,
2173 MHD_HTTP_METHOD_OPTIONS);
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 _("Identity Provider REST API initialized\n"));
2182 * Exit point from the plugin.
2184 * @param cls the plugin context (as returned by "init")
2185 * @return always NULL
2188 libgnunet_plugin_rest_openid_connect_done (void *cls)
2190 struct GNUNET_REST_Plugin *api = cls;
2191 struct Plugin *plugin = api->cls;
2194 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2196 hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (
2197 OIDC_identity_login_time);
2198 while (GNUNET_YES ==
2199 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2204 GNUNET_CONTAINER_multihashmap_destroy(OIDC_identity_login_time);
2205 hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_identity_grants);
2206 while (GNUNET_YES ==
2207 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2212 GNUNET_CONTAINER_multihashmap_destroy(OIDC_identity_grants);
2213 hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_ticket_once);
2214 while (GNUNET_YES ==
2215 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2220 GNUNET_CONTAINER_multihashmap_destroy(OIDC_ticket_once);
2221 hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_interpret_access_token);
2222 while (GNUNET_YES ==
2223 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2228 GNUNET_CONTAINER_multihashmap_destroy(OIDC_interpret_access_token);
2229 GNUNET_CONTAINER_multihashmap_iterator_destroy(hashmap_it);
2230 GNUNET_free_non_null (allow_methods);
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2233 "Identity Provider REST plugin is finished\n");
2237 /* end of plugin_rest_identity_provider.c */