2 This file is part of GNUnet.
3 Copyright (C) 2012-2018 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Martin Schanzenbach
22 * @author Philippe Buschmann
23 * @file identity/plugin_rest_openid_connect.c
24 * @brief GNUnet Namestore REST plugin
31 #include "gnunet_gns_service.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_identity_service.h"
34 #include "gnunet_namestore_service.h"
35 #include "gnunet_reclaim_lib.h"
36 #include "gnunet_reclaim_service.h"
37 #include "gnunet_rest_lib.h"
38 #include "gnunet_rest_plugin.h"
39 #include "gnunet_signatures.h"
40 #include "microhttpd.h"
41 #include "oidc_helper.h"
45 #define GNUNET_REST_API_NS_OIDC "/openid"
50 #define GNUNET_REST_API_NS_AUTHORIZE "/openid/authorize"
55 #define GNUNET_REST_API_NS_TOKEN "/openid/token"
60 #define GNUNET_REST_API_NS_USERINFO "/openid/userinfo"
65 #define GNUNET_REST_API_NS_LOGIN "/openid/login"
68 * State while collecting all egos
70 #define ID_REST_STATE_INIT 0
73 * Done collecting egos
75 #define ID_REST_STATE_POST_INIT 1
80 #define OIDC_GRANT_TYPE_KEY "grant_type"
85 #define OIDC_GRANT_TYPE_VALUE "authorization_code"
90 #define OIDC_CODE_KEY "code"
93 * OIDC response_type key
95 #define OIDC_RESPONSE_TYPE_KEY "response_type"
100 #define OIDC_CLIENT_ID_KEY "client_id"
105 #define OIDC_SCOPE_KEY "scope"
108 * OIDC redirect_uri key
110 #define OIDC_REDIRECT_URI_KEY "redirect_uri"
115 #define OIDC_STATE_KEY "state"
120 #define OIDC_NONCE_KEY "nonce"
125 #define OIDC_CLAIMS_KEY "claims"
128 * OIDC PKCE code challenge
130 #define OIDC_CODE_CHALLENGE_KEY "code_challenge"
133 * OIDC PKCE code verifier
135 #define OIDC_CODE_VERIFIER_KEY "code_verifier"
138 * OIDC cookie expiration (in seconds)
140 #define OIDC_COOKIE_EXPIRATION 3
143 * OIDC cookie header key
145 #define OIDC_COOKIE_HEADER_KEY "cookie"
148 * OIDC cookie header information key
150 #define OIDC_AUTHORIZATION_HEADER_KEY "authorization"
153 * OIDC cookie header information key
155 #define OIDC_COOKIE_HEADER_INFORMATION_KEY "Identity="
158 * OIDC cookie header if user cancelled
160 #define OIDC_COOKIE_HEADER_ACCESS_DENIED "Identity=Denied"
163 * OIDC expected response_type while authorizing
165 #define OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE "code"
168 * OIDC expected scope part while authorizing
170 #define OIDC_EXPECTED_AUTHORIZATION_SCOPE "openid"
173 * OIDC error key for invalid client
175 #define OIDC_ERROR_KEY_INVALID_CLIENT "invalid_client"
178 * OIDC error key for invalid scopes
180 #define OIDC_ERROR_KEY_INVALID_SCOPE "invalid_scope"
183 * OIDC error key for invalid requests
185 #define OIDC_ERROR_KEY_INVALID_REQUEST "invalid_request"
188 * OIDC error key for invalid tokens
190 #define OIDC_ERROR_KEY_INVALID_TOKEN "invalid_token"
193 * OIDC error key for invalid cookies
195 #define OIDC_ERROR_KEY_INVALID_COOKIE "invalid_cookie"
198 * OIDC error key for generic server errors
200 #define OIDC_ERROR_KEY_SERVER_ERROR "server_error"
203 * OIDC error key for unsupported grants
205 #define OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE "unsupported_grant_type"
208 * OIDC error key for unsupported response types
210 #define OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE "unsupported_response_type"
213 * OIDC error key for unauthorized clients
215 #define OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT "unauthorized_client"
218 * OIDC error key for denied access
220 #define OIDC_ERROR_KEY_ACCESS_DENIED "access_denied"
224 * OIDC ignored parameter array
226 static char *OIDC_ignored_parameter_array[] = { "display",
235 * OIDC Hash map that keeps track of issued cookies
237 struct GNUNET_CONTAINER_MultiHashMap *OIDC_cookie_jar_map;
240 * Hash map that links the issued access token to the corresponding ticket and
243 struct GNUNET_CONTAINER_MultiHashMap *OIDC_access_token_map;
246 * The configuration handle
248 const struct GNUNET_CONFIGURATION_Handle *cfg;
251 * HTTP methods allows for this plugin
253 static char *allow_methods;
256 * @brief struct returned by the initialization function of the plugin
260 const struct GNUNET_CONFIGURATION_Handle *cfg;
264 * OIDC needed variables
266 struct OIDC_Variables
269 * The RP client public key
271 struct GNUNET_CRYPTO_EcdsaPublicKey client_pkey;
274 * The OIDC client id of the RP
279 * The OIDC redirect uri
284 * The list of oidc scopes
304 * The OIDC response type
309 * The identity chosen by the user to login
311 char *login_identity;
314 * User cancelled authorization/login
319 * The PKCE code_challenge
321 char *code_challenge;
324 * The PKCE code_verifier
342 struct EgoEntry *next;
347 struct EgoEntry *prev;
362 struct GNUNET_IDENTITY_Ego *ego;
371 struct EgoEntry *ego_head;
376 struct EgoEntry *ego_tail;
381 struct EgoEntry *ego_entry;
384 * Pointer to ego private key
386 struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key;
391 struct OIDC_Variables *oidc;
394 * The processing state
399 * Handle to Identity service.
401 struct GNUNET_IDENTITY_Handle *identity_handle;
406 struct GNUNET_REST_RequestHandle *rest_handle;
411 struct GNUNET_GNS_Handle *gns_handle;
416 struct GNUNET_GNS_LookupRequest *gns_op;
419 * Handle to NAMESTORE
421 struct GNUNET_NAMESTORE_Handle *namestore_handle;
424 * Iterator for NAMESTORE
426 struct GNUNET_NAMESTORE_ZoneIterator *namestore_handle_it;
429 * Attribute claim list
431 struct GNUNET_RECLAIM_AttributeList *attr_list;
436 struct GNUNET_RECLAIM_AttestationList *attests_list;
442 struct GNUNET_IDENTITY_Operation *op;
447 struct GNUNET_RECLAIM_Handle *idp;
452 struct GNUNET_RECLAIM_Operation *idp_op;
457 struct GNUNET_RECLAIM_AttributeIterator *attr_it;
460 * Attestation iterator
462 struct GNUNET_RECLAIM_AttestationIterator *attest_it;
468 struct GNUNET_RECLAIM_TicketIterator *ticket_it;
473 struct GNUNET_RECLAIM_Ticket ticket;
476 * Desired timeout for the lookup (default is no timeout).
478 struct GNUNET_TIME_Relative timeout;
481 * ID of a task associated with the resolution process.
483 struct GNUNET_SCHEDULER_Task *timeout_task;
486 * The plugin result processor
488 GNUNET_REST_ResultProcessor proc;
491 * The closure of the result processor
501 * The tld for redirect
506 * The redirect prefix
508 char *redirect_prefix;
511 * The redirect suffix
513 char *redirect_suffix;
516 * Error response message
521 * Error response description
532 * Cleanup lookup handle
533 * @param handle Handle to clean up
536 cleanup_handle (struct RequestHandle *handle)
538 struct EgoEntry *ego_entry;
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
541 if (NULL != handle->timeout_task)
542 GNUNET_SCHEDULER_cancel (handle->timeout_task);
543 if (NULL != handle->identity_handle)
544 GNUNET_IDENTITY_disconnect (handle->identity_handle);
545 if (NULL != handle->attr_it)
546 GNUNET_RECLAIM_get_attributes_stop (handle->attr_it);
547 if (NULL != handle->attest_it)
548 GNUNET_RECLAIM_get_attestations_stop (handle->attest_it);
549 if (NULL != handle->ticket_it)
550 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
551 if (NULL != handle->idp_op)
552 GNUNET_RECLAIM_cancel (handle->idp_op);
553 if (NULL != handle->idp)
554 GNUNET_RECLAIM_disconnect (handle->idp);
555 GNUNET_free_non_null (handle->url);
556 GNUNET_free_non_null (handle->tld);
557 GNUNET_free_non_null (handle->redirect_prefix);
558 GNUNET_free_non_null (handle->redirect_suffix);
559 GNUNET_free_non_null (handle->emsg);
560 GNUNET_free_non_null (handle->edesc);
561 if (NULL != handle->gns_op)
562 GNUNET_GNS_lookup_cancel (handle->gns_op);
563 if (NULL != handle->gns_handle)
564 GNUNET_GNS_disconnect (handle->gns_handle);
566 if (NULL != handle->namestore_handle)
567 GNUNET_NAMESTORE_disconnect (handle->namestore_handle);
568 if (NULL != handle->oidc)
570 GNUNET_free_non_null (handle->oidc->client_id);
571 GNUNET_free_non_null (handle->oidc->login_identity);
572 GNUNET_free_non_null (handle->oidc->nonce);
573 GNUNET_free_non_null (handle->oidc->redirect_uri);
574 GNUNET_free_non_null (handle->oidc->response_type);
575 GNUNET_free_non_null (handle->oidc->scope);
576 GNUNET_free_non_null (handle->oidc->state);
577 json_decref (handle->oidc->response);
578 GNUNET_free (handle->oidc);
580 if (NULL!=handle->attr_list)
581 GNUNET_RECLAIM_attribute_list_destroy (handle->attr_list);
582 if (NULL!=handle->attests_list)
583 GNUNET_RECLAIM_attestation_list_destroy (handle->attests_list);
585 while (NULL != (ego_entry = handle->ego_head))
587 GNUNET_CONTAINER_DLL_remove (ego_entry,
590 GNUNET_free (ego_entry->identifier);
591 GNUNET_free (ego_entry->keystring);
592 GNUNET_free (ego_entry);
594 GNUNET_free (handle);
599 cleanup_handle_delayed (void *cls)
601 cleanup_handle (cls);
606 * Task run on error, sends error message. Cleans up everything.
608 * @param cls the `struct RequestHandle`
613 struct RequestHandle *handle = cls;
614 struct MHD_Response *resp;
617 GNUNET_asprintf (&json_error,
618 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
620 (NULL != handle->edesc) ? handle->edesc : "",
621 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
622 (NULL != handle->oidc->state) ? handle->oidc->state : "",
623 (NULL != handle->oidc->state) ? "\"" : "");
624 if (0 == handle->response_code)
625 handle->response_code = MHD_HTTP_BAD_REQUEST;
626 resp = GNUNET_REST_create_response (json_error);
627 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
628 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
629 MHD_add_response_header (resp,
630 MHD_HTTP_HEADER_CONTENT_TYPE,
632 handle->proc (handle->proc_cls, resp, handle->response_code);
633 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
634 GNUNET_free (json_error);
639 * Task run on error in userinfo endpoint, sends error header. Cleans up
642 * @param cls the `struct RequestHandle`
645 do_userinfo_error (void *cls)
647 struct RequestHandle *handle = cls;
648 struct MHD_Response *resp;
651 GNUNET_asprintf (&error,
652 "error=\"%s\", error_description=\"%s\"",
654 (NULL != handle->edesc) ? handle->edesc : "");
655 resp = GNUNET_REST_create_response ("");
656 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
657 handle->proc (handle->proc_cls, resp, handle->response_code);
658 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
664 * Task run on error, sends error message and redirects. Cleans up everything.
666 * @param cls the `struct RequestHandle`
669 do_redirect_error (void *cls)
671 struct RequestHandle *handle = cls;
672 struct MHD_Response *resp;
675 GNUNET_asprintf (&redirect,
676 "%s?error=%s&error_description=%s%s%s",
677 handle->oidc->redirect_uri,
680 (NULL != handle->oidc->state) ? "&state=" : "",
681 (NULL != handle->oidc->state) ? handle->oidc->state : "");
682 resp = GNUNET_REST_create_response ("");
683 MHD_add_response_header (resp, "Location", redirect);
684 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
685 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
686 GNUNET_free (redirect);
691 * Task run on timeout, sends error message. Cleans up everything.
693 * @param cls the `struct RequestHandle`
696 do_timeout (void *cls)
698 struct RequestHandle *handle = cls;
700 handle->timeout_task = NULL;
706 * Return attributes for claim
708 * @param cls the request handle
711 return_userinfo_response (void *cls)
714 struct RequestHandle *handle = cls;
715 struct MHD_Response *resp;
717 result_str = json_dumps (handle->oidc->response, 0);
718 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"ID-Token: %s\n",result_str);
719 resp = GNUNET_REST_create_response (result_str);
720 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
721 GNUNET_free (result_str);
722 cleanup_handle (handle);
727 * Respond to OPTIONS request
729 * @param con_handle the connection handle
731 * @param cls the RequestHandle
734 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
738 struct MHD_Response *resp;
739 struct RequestHandle *handle = cls;
741 // For now, independent of path return all options
742 resp = GNUNET_REST_create_response (NULL);
743 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
744 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
745 cleanup_handle (handle);
751 * Interprets cookie header and pass its identity keystring to handle
754 cookie_identity_interpretation (struct RequestHandle *handle)
756 struct GNUNET_HashCode cache_key;
758 struct GNUNET_TIME_Absolute current_time, *relog_time;
759 char delimiter[] = "; ";
764 // gets identity of login try with cookie
765 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
766 strlen (OIDC_COOKIE_HEADER_KEY),
768 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
775 // splits cookies and find 'Identity' cookie
777 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
779 cookies = GNUNET_strdup (tmp_cookies);
780 token = strtok (cookies, delimiter);
781 handle->oidc->user_cancelled = GNUNET_NO;
782 handle->oidc->login_identity = NULL;
785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
786 "Unable to parse cookie: %s\n",
788 GNUNET_free (cookies);
792 while (NULL != token)
794 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
796 handle->oidc->user_cancelled = GNUNET_YES;
797 GNUNET_free (cookies);
800 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
802 token = strtok (NULL, delimiter);
806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
807 "No cookie value to process: %s\n",
809 GNUNET_free (cookies);
812 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
814 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
817 GNUNET_ERROR_TYPE_WARNING,
818 "Found cookie `%s', but no corresponding expiration entry present...\n",
820 GNUNET_free (cookies);
824 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
825 current_time = GNUNET_TIME_absolute_get ();
826 // 30 min after old login -> redirect to login
827 if (current_time.abs_value_us > relog_time->abs_value_us)
829 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
830 "Found cookie `%s', but it is expired.\n",
832 GNUNET_free (cookies);
835 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
836 GNUNET_assert (NULL != value);
837 handle->oidc->login_identity = GNUNET_strdup (value);
838 GNUNET_free (cookies);
843 * Redirects to login page stored in configuration file
846 login_redirect (void *cls)
848 char *login_base_url;
850 struct MHD_Response *resp;
851 struct RequestHandle *handle = cls;
853 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
854 "reclaim-rest-plugin",
858 GNUNET_asprintf (&new_redirect,
859 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
861 OIDC_RESPONSE_TYPE_KEY,
862 handle->oidc->response_type,
864 handle->oidc->client_id,
865 OIDC_REDIRECT_URI_KEY,
866 handle->oidc->redirect_uri,
870 (NULL != handle->oidc->state) ? handle->oidc->state : "",
871 OIDC_CODE_CHALLENGE_KEY,
872 (NULL != handle->oidc->code_challenge) ?
873 handle->oidc->code_challenge : "",
875 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "",
877 (NULL != handle->oidc->claims) ? handle->oidc->claims :
879 resp = GNUNET_REST_create_response ("");
880 MHD_add_response_header (resp, "Location", new_redirect);
881 GNUNET_free (login_base_url);
885 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
886 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
887 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
888 GNUNET_SCHEDULER_add_now (&do_error, handle);
891 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
892 GNUNET_free (new_redirect);
893 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
898 * Does internal server error when iteration failed.
901 oidc_iteration_error (void *cls)
903 struct RequestHandle *handle = cls;
905 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
906 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
907 GNUNET_SCHEDULER_add_now (&do_error, handle);
912 * Issues ticket and redirects to relying party with the authorization code as
913 * parameter. Otherwise redirects with error
916 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
918 struct RequestHandle *handle = cls;
919 struct MHD_Response *resp;
924 handle->idp_op = NULL;
927 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
928 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
929 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
932 handle->ticket = *ticket;
934 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
935 sizeof(struct GNUNET_RECLAIM_Ticket));
936 // TODO change if more attributes are needed (see max_age)
937 code_string = OIDC_build_authz_code (&handle->priv_key,
940 handle->attests_list,
942 handle->oidc->code_challenge);
943 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
944 (NULL != handle->tld))
946 GNUNET_asprintf (&redirect_uri,
947 "%s.%s/%s?%s=%s&state=%s",
948 handle->redirect_prefix,
950 handle->redirect_suffix,
951 handle->oidc->response_type,
953 handle->oidc->state);
957 GNUNET_asprintf (&redirect_uri,
959 handle->oidc->redirect_uri,
960 handle->oidc->response_type,
962 handle->oidc->state);
964 resp = GNUNET_REST_create_response ("");
965 MHD_add_response_header (resp, "Location", redirect_uri);
966 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
967 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
968 GNUNET_free (redirect_uri);
969 GNUNET_free (ticket_str);
970 GNUNET_free (code_string);
975 oidc_attest_collect_finished_cb (void *cls)
977 struct RequestHandle *handle = cls;
979 handle->attest_it = NULL;
980 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
982 &handle->oidc->client_pkey,
984 &oidc_ticket_issue_cb,
990 * Collects all attributes for an ego if in scope parameter
993 oidc_attest_collect (void *cls,
994 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
995 const struct GNUNET_RECLAIM_Attestation *attest)
997 struct RequestHandle *handle = cls;
998 struct GNUNET_RECLAIM_AttributeListEntry *le;
1000 for (le = handle->attr_list->list_head; NULL != le; le = le->next)
1002 if (GNUNET_NO == GNUNET_RECLAIM_id_is_equal (&le->attribute->attestation,
1005 struct GNUNET_RECLAIM_AttestationListEntry *ale;
1006 ale = GNUNET_new (struct GNUNET_RECLAIM_AttestationListEntry);
1007 ale->attestation = GNUNET_RECLAIM_attestation_new (attest->name,
1011 GNUNET_CONTAINER_DLL_insert (handle->attests_list->list_head,
1012 handle->attests_list->list_tail,
1016 GNUNET_RECLAIM_get_attestations_next (handle->attest_it);
1021 oidc_attr_collect_finished_cb (void *cls)
1023 struct RequestHandle *handle = cls;
1025 handle->attr_it = NULL;
1026 handle->ticket_it = NULL;
1027 if (NULL == handle->attr_list->list_head)
1029 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1030 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
1031 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1034 handle->attests_list = GNUNET_new (struct GNUNET_RECLAIM_AttestationList);
1036 GNUNET_RECLAIM_get_attestations_start (handle->idp,
1038 &oidc_iteration_error,
1040 &oidc_attest_collect,
1042 &oidc_attest_collect_finished_cb,
1045 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
1047 &handle->oidc->client_pkey,
1049 &oidc_ticket_issue_cb,
1055 * Collects all attributes for an ego if in scope parameter
1058 oidc_attr_collect (void *cls,
1059 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1060 const struct GNUNET_RECLAIM_Attribute *attr)
1062 struct RequestHandle *handle = cls;
1063 struct GNUNET_RECLAIM_AttributeListEntry *le;
1064 char *scope_variables;
1065 char *scope_variable;
1066 char delimiter[] = " ";
1068 scope_variables = GNUNET_strdup (handle->oidc->scope);
1069 scope_variable = strtok (scope_variables, delimiter);
1070 while (NULL != scope_variable)
1072 if (0 == strcmp (attr->name, scope_variable))
1074 scope_variable = strtok (NULL, delimiter);
1076 if (NULL == scope_variable)
1078 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1079 GNUNET_free (scope_variables);
1080 // We can ignore this
1083 GNUNET_free (scope_variables);
1084 le = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
1085 le->attribute = GNUNET_RECLAIM_attribute_new (attr->name,
1090 le->attribute->id = attr->id;
1091 le->attribute->flag = attr->flag;
1092 le->attribute->attestation = attr->attestation;
1093 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
1094 handle->attr_list->list_tail,
1096 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1101 * Checks time and cookie and redirects accordingly
1104 code_redirect (void *cls)
1106 struct RequestHandle *handle = cls;
1107 struct GNUNET_TIME_Absolute current_time;
1108 struct GNUNET_TIME_Absolute *relog_time;
1109 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1110 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1111 struct GNUNET_HashCode cache_key;
1112 char *identity_cookie;
1114 GNUNET_asprintf (&identity_cookie,
1116 handle->oidc->login_identity);
1117 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1118 GNUNET_free (identity_cookie);
1119 // No login time for identity -> redirect to login
1121 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1124 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1125 current_time = GNUNET_TIME_absolute_get ();
1126 // 30 min after old login -> redirect to login
1127 if (current_time.abs_value_us <= relog_time->abs_value_us)
1130 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1137 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1139 GNUNET_strdup ("The cookie of a login identity is not valid");
1140 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1143 // iterate over egos and compare their public key
1144 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1145 handle->ego_entry = handle->ego_entry->next)
1147 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1148 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1151 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1152 handle->idp = GNUNET_RECLAIM_connect (cfg);
1154 GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
1156 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1158 &oidc_iteration_error,
1162 &oidc_attr_collect_finished_cb,
1167 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1175 build_redirect (void *cls)
1177 struct RequestHandle *handle = cls;
1178 struct MHD_Response *resp;
1181 if (GNUNET_YES == handle->oidc->user_cancelled)
1183 if ((NULL != handle->redirect_prefix) &&
1184 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1186 GNUNET_asprintf (&redirect_uri,
1187 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1188 handle->redirect_prefix,
1190 handle->redirect_suffix,
1192 "User denied access",
1193 handle->oidc->state);
1197 GNUNET_asprintf (&redirect_uri,
1198 "%s?error=%s&error_description=%s&state=%s",
1199 handle->oidc->redirect_uri,
1201 "User denied access",
1202 handle->oidc->state);
1204 resp = GNUNET_REST_create_response ("");
1205 MHD_add_response_header (resp, "Location", redirect_uri);
1206 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1207 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1208 GNUNET_free (redirect_uri);
1211 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1216 lookup_redirect_uri_result (void *cls,
1218 const struct GNUNET_GNSRECORD_Data *rd)
1220 struct RequestHandle *handle = cls;
1224 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1226 handle->gns_op = NULL;
1229 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1231 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1232 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1235 for (int i = 0; i < rd_count; i++)
1237 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1239 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1241 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1242 if (NULL == strstr (tmp, handle->oidc->client_id))
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Redirect uri %s does not contain client_id %s\n",
1247 handle->oidc->client_id);
1251 pos = strrchr (tmp, (unsigned char) '.');
1254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1255 "Redirect uri %s contains client_id but is malformed\n",
1261 handle->redirect_prefix = GNUNET_strdup (tmp);
1262 tmp_key_str = pos + 1;
1263 pos = strchr (tmp_key_str, (unsigned char) '/');
1266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1267 "Redirect uri %s contains client_id but is malformed\n",
1273 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1275 GNUNET_STRINGS_string_to_data (tmp_key_str,
1276 strlen (tmp_key_str),
1278 sizeof(redirect_zone));
1280 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1284 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1286 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1287 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1292 * Initiate redirect back to client.
1295 client_redirect (void *cls)
1297 struct RequestHandle *handle = cls;
1299 /* Lookup client redirect uri to verify request */
1301 GNUNET_GNS_lookup (handle->gns_handle,
1302 GNUNET_GNS_EMPTY_LABEL_AT,
1303 &handle->oidc->client_pkey,
1304 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1305 GNUNET_GNS_LO_DEFAULT,
1306 &lookup_redirect_uri_result,
1312 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1314 struct GNUNET_HashCode hc;
1317 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1318 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1323 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1326 return GNUNET_strdup (value);
1331 * Iteration over all results finished, build final
1334 * @param cls the `struct RequestHandle`
1337 build_authz_response (void *cls)
1339 struct RequestHandle *handle = cls;
1340 struct GNUNET_HashCode cache_key;
1342 char *expected_scope;
1343 char delimiter[] = " ";
1344 int number_of_ignored_parameter, iterator;
1347 // REQUIRED value: redirect_uri
1348 handle->oidc->redirect_uri =
1349 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1350 if (NULL == handle->oidc->redirect_uri)
1352 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1353 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1354 GNUNET_SCHEDULER_add_now (&do_error, handle);
1358 // REQUIRED value: response_type
1359 handle->oidc->response_type =
1360 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1361 if (NULL == handle->oidc->response_type)
1363 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1364 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1365 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1369 // REQUIRED value: scope
1370 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1371 if (NULL == handle->oidc->scope)
1373 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1374 handle->edesc = GNUNET_strdup ("missing parameter scope");
1375 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1379 // OPTIONAL value: nonce
1380 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1382 // OPTIONAL value: claims
1383 handle->oidc->claims = get_url_parameter_copy (handle, OIDC_CLAIMS_KEY);
1385 // TODO check other values if needed
1386 number_of_ignored_parameter =
1387 sizeof(OIDC_ignored_parameter_array) / sizeof(char *);
1388 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1390 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1391 strlen (OIDC_ignored_parameter_array[iterator]),
1394 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1398 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1399 GNUNET_asprintf (&handle->edesc,
1400 "Server will not handle parameter: %s",
1401 OIDC_ignored_parameter_array[iterator]);
1402 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1407 // We only support authorization code flows.
1408 if (0 != strcmp (handle->oidc->response_type,
1409 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1411 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1412 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1413 "obtaining this authorization code.");
1414 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1418 // Checks if scope contains 'openid'
1419 expected_scope = GNUNET_strdup (handle->oidc->scope);
1421 test = strtok (expected_scope, delimiter);
1422 while (NULL != test)
1424 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1426 test = strtok (NULL, delimiter);
1430 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1432 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1433 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1434 GNUNET_free (expected_scope);
1438 GNUNET_free (expected_scope);
1439 if ((NULL == handle->oidc->login_identity) &&
1440 (GNUNET_NO == handle->oidc->user_cancelled))
1441 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1443 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1448 * Iterate over tlds in config
1451 tld_iter (void *cls, const char *section, const char *option, const char *value)
1453 struct RequestHandle *handle = cls;
1454 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1457 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1462 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1463 handle->tld = GNUNET_strdup (option + 1);
1468 * Responds to authorization GET and url-encoded POST request
1470 * @param con_handle the connection handle
1471 * @param url the url
1472 * @param cls the RequestHandle
1475 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1479 struct RequestHandle *handle = cls;
1480 struct EgoEntry *tmp_ego;
1481 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1482 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1484 cookie_identity_interpretation (handle);
1486 // RECOMMENDED value: state - REQUIRED for answers
1487 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1489 // REQUIRED value: client_id
1490 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1491 if (NULL == handle->oidc->client_id)
1493 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1494 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1495 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1496 GNUNET_SCHEDULER_add_now (&do_error, handle);
1500 // OPTIONAL value: code_challenge
1501 handle->oidc->code_challenge = get_url_parameter_copy (handle,
1502 OIDC_CODE_CHALLENGE_KEY);
1503 if (NULL == handle->oidc->code_challenge)
1505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1506 "OAuth authorization request does not contain PKCE parameters!\n");
1510 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1512 handle->oidc->client_id),
1513 &handle->oidc->client_pkey))
1515 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1516 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1517 "authorization code using this method.");
1518 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1519 GNUNET_SCHEDULER_add_now (&do_error, handle);
1523 // If we know this identity, translated the corresponding TLD
1524 // TODO: We might want to have a reverse lookup functionality for TLDs?
1525 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1527 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1528 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1529 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1531 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1532 handle->ego_entry = handle->ego_tail;
1535 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scope: %s\n",GNUNET_strdup (
1537 handle->oidc->scope));
1538 if (NULL == handle->tld)
1539 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1540 if (NULL == handle->tld)
1541 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1542 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1547 * Combines an identity with a login time and responds OK to login request
1549 * @param con_handle the connection handle
1550 * @param url the url
1551 * @param cls the RequestHandle
1554 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1558 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1559 struct RequestHandle *handle = cls;
1560 struct GNUNET_HashCode cache_key;
1561 struct GNUNET_TIME_Absolute *current_time;
1562 struct GNUNET_TIME_Absolute *last_time;
1568 char term_data[handle->rest_handle->data_size + 1];
1570 term_data[handle->rest_handle->data_size] = '\0';
1571 GNUNET_memcpy (term_data,
1572 handle->rest_handle->data,
1573 handle->rest_handle->data_size);
1574 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1575 identity = json_object_get (root, "identity");
1576 if (! json_is_string (identity))
1578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1579 "Error parsing json string from %s\n",
1581 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1583 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1586 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1587 GNUNET_asprintf (&header_val,
1590 OIDC_COOKIE_EXPIRATION);
1591 MHD_add_response_header (resp, "Set-Cookie", header_val);
1592 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1593 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1595 if (0 != strcmp (json_string_value (identity), "Denied"))
1597 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1598 *current_time = GNUNET_TIME_relative_to_absolute (
1599 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1600 OIDC_COOKIE_EXPIRATION));
1602 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1603 GNUNET_free_non_null (last_time);
1604 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1607 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1609 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1610 GNUNET_free (cookie);
1611 GNUNET_free (header_val);
1613 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1618 check_authorization (struct RequestHandle *handle,
1619 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1621 struct GNUNET_HashCode cache_key;
1622 char *authorization;
1624 char *basic_authorization;
1627 char *expected_pass;
1629 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1630 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1632 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1636 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1637 handle->edesc = GNUNET_strdup ("missing authorization");
1638 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1639 return GNUNET_SYSERR;
1642 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1645 // split header in "Basic" and [content]
1646 credentials = strtok (authorization, " ");
1647 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1649 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1650 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1651 return GNUNET_SYSERR;
1653 credentials = strtok (NULL, " ");
1654 if (NULL == credentials)
1656 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1657 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1658 return GNUNET_SYSERR;
1660 GNUNET_STRINGS_base64_decode (credentials,
1661 strlen (credentials),
1662 (void **) &basic_authorization);
1664 if (NULL == basic_authorization)
1666 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1667 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1668 return GNUNET_SYSERR;
1670 client_id = strtok (basic_authorization, ":");
1671 if (NULL == client_id)
1673 GNUNET_free_non_null (basic_authorization);
1674 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1675 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1676 return GNUNET_SYSERR;
1678 pass = strtok (NULL, ":");
1681 GNUNET_free_non_null (basic_authorization);
1682 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1683 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1684 return GNUNET_SYSERR;
1687 // check client password
1688 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1689 "reclaim-rest-plugin",
1690 "OIDC_CLIENT_SECRET",
1693 if (0 != strcmp (expected_pass, pass))
1695 GNUNET_free_non_null (basic_authorization);
1696 GNUNET_free (expected_pass);
1697 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1698 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1699 return GNUNET_SYSERR;
1701 GNUNET_free (expected_pass);
1705 GNUNET_free_non_null (basic_authorization);
1706 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1707 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1708 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1709 return GNUNET_SYSERR;
1713 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1714 handle->ego_entry = handle->ego_entry->next)
1716 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1719 if (NULL == handle->ego_entry)
1721 GNUNET_free_non_null (basic_authorization);
1722 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1723 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1724 return GNUNET_SYSERR;
1726 GNUNET_STRINGS_string_to_data (client_id,
1729 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
1731 GNUNET_free (basic_authorization);
1736 const struct EgoEntry *
1737 find_ego (struct RequestHandle *handle,
1738 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1740 struct EgoEntry *ego_entry;
1741 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1743 for (ego_entry = handle->ego_head; NULL != ego_entry;
1744 ego_entry = ego_entry->next)
1746 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1747 if (0 == GNUNET_memcmp (&pub_key, test_key))
1755 persist_access_token (const struct RequestHandle *handle,
1756 const char *access_token,
1757 const struct GNUNET_RECLAIM_Ticket *ticket)
1759 struct GNUNET_HashCode hc;
1760 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1762 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1763 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1764 *ticketbuf = *ticket;
1765 GNUNET_assert (GNUNET_SYSERR !=
1766 GNUNET_CONTAINER_multihashmap_put (
1767 OIDC_access_token_map,
1770 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1775 * Responds to token url-encoded POST request
1777 * @param con_handle the connection handle
1778 * @param url the url
1779 * @param cls the RequestHandle
1782 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1786 struct RequestHandle *handle = cls;
1787 const struct EgoEntry *ego_entry;
1788 struct GNUNET_TIME_Relative expiration_time;
1789 struct GNUNET_RECLAIM_AttributeList *cl = NULL;
1790 struct GNUNET_RECLAIM_AttestationList *al = NULL;
1791 struct GNUNET_RECLAIM_Ticket ticket;
1792 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1793 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1794 struct GNUNET_HashCode cache_key;
1795 struct MHD_Response *resp;
1798 char *json_response;
1803 char *code_verifier;
1806 * Check Authorization
1808 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1811 "OIDC authorization for token endpoint failed\n");
1812 GNUNET_SCHEDULER_add_now (&do_error, handle);
1820 // TODO Do not allow multiple equal parameter names
1821 // REQUIRED grant_type
1822 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1823 strlen (OIDC_GRANT_TYPE_KEY),
1825 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1826 if (NULL == grant_type)
1828 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1829 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1830 handle->response_code = MHD_HTTP_BAD_REQUEST;
1831 GNUNET_SCHEDULER_add_now (&do_error, handle);
1835 // Check parameter grant_type == "authorization_code"
1836 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1838 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1839 handle->response_code = MHD_HTTP_BAD_REQUEST;
1840 GNUNET_free (grant_type);
1841 GNUNET_SCHEDULER_add_now (&do_error, handle);
1844 GNUNET_free (grant_type);
1846 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1849 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1850 handle->edesc = GNUNET_strdup ("missing parameter code");
1851 handle->response_code = MHD_HTTP_BAD_REQUEST;
1852 GNUNET_SCHEDULER_add_now (&do_error, handle);
1855 ego_entry = find_ego (handle, &cid);
1856 if (NULL == ego_entry)
1858 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1859 handle->edesc = GNUNET_strdup ("Unknown client");
1860 handle->response_code = MHD_HTTP_BAD_REQUEST;
1862 GNUNET_SCHEDULER_add_now (&do_error, handle);
1865 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1867 // REQUIRED code verifier
1868 code_verifier = get_url_parameter_copy (handle, OIDC_CODE_VERIFIER_KEY);
1869 if (NULL == code_verifier)
1871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1872 "OAuth authorization request does not contain PKCE parameters!\n");
1877 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, code_verifier, &ticket,
1880 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1881 handle->edesc = GNUNET_strdup ("invalid code");
1882 handle->response_code = MHD_HTTP_BAD_REQUEST;
1884 GNUNET_SCHEDULER_add_now (&do_error, handle);
1890 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1891 "reclaim-rest-plugin",
1895 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1896 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1897 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1898 GNUNET_SCHEDULER_add_now (&do_error, handle);
1903 // TODO OPTIONAL acr,amr,azp
1904 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1905 "reclaim-rest-plugin",
1909 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1910 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1911 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1912 GNUNET_SCHEDULER_add_now (&do_error, handle);
1915 id_token = OIDC_id_token_new (&ticket.audience,
1920 (NULL != nonce) ? nonce : NULL,
1922 access_token = OIDC_access_token_new ();
1923 OIDC_build_token_response (access_token,
1928 persist_access_token (handle, access_token, &ticket);
1929 resp = GNUNET_REST_create_response (json_response);
1930 MHD_add_response_header (resp, "Cache-Control", "no-store");
1931 MHD_add_response_header (resp, "Pragma", "no-cache");
1932 MHD_add_response_header (resp, "Content-Type", "application/json");
1933 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1934 GNUNET_RECLAIM_attribute_list_destroy (cl);
1935 GNUNET_RECLAIM_attestation_list_destroy (al);
1936 GNUNET_free (access_token);
1937 GNUNET_free (json_response);
1938 GNUNET_free (id_token);
1939 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1944 * Collects claims and stores them in handle
1947 consume_ticket (void *cls,
1948 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1949 const struct GNUNET_RECLAIM_Attribute *attr,
1950 const struct GNUNET_RECLAIM_Attestation *attest)
1952 struct RequestHandle *handle = cls;
1953 handle->idp_op = NULL;
1955 if (NULL == identity)
1957 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1960 if (GNUNET_YES == GNUNET_RECLAIM_id_is_zero (&attr->attestation))
1964 tmp_value = GNUNET_RECLAIM_attribute_value_to_string (attr->type,
1967 value = json_string (tmp_value);
1968 json_object_set_new (handle->oidc->response, attr->name, value);
1969 GNUNET_free (tmp_value);
1972 json_t *claim_sources;
1973 json_t *claim_sources_jwt;
1974 json_t *claim_names;
1975 char *attest_val_str;
1976 claim_sources = json_object_get (handle->oidc->response,"_claim_sources");
1977 claim_names = json_object_get (handle->oidc->response,"_claim_names");
1979 GNUNET_RECLAIM_attestation_value_to_string (attest->type,
1982 if ((NULL == claim_sources) && (NULL == claim_names) )
1984 claim_sources = json_object ();
1985 claim_names = json_object ();
1989 GNUNET_asprintf (&source_name, "src%d", i);
1990 while (NULL != (claim_sources_jwt = json_object_get (claim_sources,
1993 if (0 == strcmp (json_string_value (json_object_get (claim_sources_jwt,
1997 // Adapt only the claim names
1998 json_object_set_new (claim_names, attr->data,
1999 json_string (source_name));
2000 json_object_set (handle->oidc->response,
2001 "_claim_names", claim_names);
2005 GNUNET_free (source_name);
2006 GNUNET_asprintf (&source_name, "src%d", i);
2010 if (NULL == claim_sources_jwt)
2012 claim_sources_jwt = json_object ();
2013 // Set the JWT for names
2014 json_object_set_new (claim_names, attr->data,
2015 json_string (source_name));
2016 // Set the JWT for the inner source
2017 json_object_set_new (claim_sources_jwt, "JWT",
2018 json_string (attest_val_str));
2019 // Set the JWT for the source
2020 json_object_set_new (claim_sources, source_name, claim_sources_jwt);
2022 json_object_set (handle->oidc->response, "_claim_names", claim_names);
2023 json_object_set (handle->oidc->response, "_claim_sources",claim_sources);
2026 json_decref (claim_sources);
2027 json_decref (claim_names);
2028 json_decref (claim_sources_jwt);
2029 GNUNET_free (attest_val_str);
2034 * Responds to userinfo GET and url-encoded POST request
2036 * @param con_handle the connection handle
2037 * @param url the url
2038 * @param cls the RequestHandle
2041 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2045 // TODO expiration time
2046 struct RequestHandle *handle = cls;
2047 char delimiter[] = " ";
2048 struct GNUNET_HashCode cache_key;
2049 char *authorization;
2050 char *authorization_type;
2051 char *authorization_access_token;
2052 struct GNUNET_RECLAIM_Ticket *ticket;
2053 const struct EgoEntry *ego_entry;
2054 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
2056 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
2057 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
2059 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
2063 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2064 handle->edesc = GNUNET_strdup ("No Access Token");
2065 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2066 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2070 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
2073 // split header in "Bearer" and access_token
2074 authorization = GNUNET_strdup (authorization);
2075 authorization_type = strtok (authorization, delimiter);
2076 if ((NULL == authorization_type) ||
2077 (0 != strcmp ("Bearer", authorization_type)))
2079 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2080 handle->edesc = GNUNET_strdup ("No Access Token");
2081 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2082 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2083 GNUNET_free (authorization);
2086 authorization_access_token = strtok (NULL, delimiter);
2087 if (NULL == authorization_access_token)
2089 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2090 handle->edesc = GNUNET_strdup ("Access token missing");
2091 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2092 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2093 GNUNET_free (authorization);
2097 GNUNET_CRYPTO_hash (authorization_access_token,
2098 strlen (authorization_access_token),
2101 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
2104 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2105 handle->edesc = GNUNET_strdup ("The access token expired");
2106 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2107 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2108 GNUNET_free (authorization);
2112 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
2113 GNUNET_assert (NULL != ticket);
2114 ego_entry = find_ego (handle, &ticket->audience);
2115 if (NULL == ego_entry)
2117 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2118 handle->edesc = GNUNET_strdup ("The access token expired");
2119 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2120 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2121 GNUNET_free (authorization);
2125 handle->idp = GNUNET_RECLAIM_connect (cfg);
2126 handle->oidc->response = json_object ();
2127 json_object_set_new (handle->oidc->response,
2129 json_string (ego_entry->keystring));
2130 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
2131 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
2136 GNUNET_free (authorization);
2141 * Handle rest request
2143 * @param handle the request handle
2146 init_cont (struct RequestHandle *handle)
2148 struct GNUNET_REST_RequestHandlerError err;
2149 static const struct GNUNET_REST_RequestHandler handlers[] =
2150 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint },
2151 { MHD_HTTP_METHOD_POST,
2152 GNUNET_REST_API_NS_AUTHORIZE,
2153 &authorize_endpoint }, // url-encoded
2154 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont },
2155 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint },
2156 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2157 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2158 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont },
2159 GNUNET_REST_HANDLER_END };
2162 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
2164 handle->response_code = err.error_code;
2165 GNUNET_SCHEDULER_add_now (&do_error, handle);
2171 * If listing is enabled, prints information about the egos.
2173 * This function is initially called for all egos and then again
2174 * whenever a ego's identifier changes or if it is deleted. At the
2175 * end of the initial pass over all egos, the function is once called
2176 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
2177 * be invoked in the future or that there was an error.
2179 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
2180 * this function is only called ONCE, and 'NULL' being passed in
2181 * 'ego' does indicate an error (i.e. name is taken or no default
2182 * value is known). If 'ego' is non-NULL and if '*ctx'
2183 * is set in those callbacks, the value WILL be passed to a subsequent
2184 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
2185 * that one was not NULL).
2187 * When an identity is renamed, this function is called with the
2188 * (known) ego but the NEW identifier.
2190 * When an identity is deleted, this function is called with the
2191 * (known) ego and "NULL" for the 'identifier'. In this case,
2192 * the 'ego' is henceforth invalid (and the 'ctx' should also be
2195 * @param cls closure
2196 * @param ego ego handle
2197 * @param ctx context for application to store data for this ego
2198 * (during the lifetime of this process, initially NULL)
2199 * @param identifier identifier assigned by the user for this ego,
2200 * NULL if the user just deleted the ego and it
2201 * must thus no longer be used
2204 list_ego (void *cls,
2205 struct GNUNET_IDENTITY_Ego *ego,
2207 const char *identifier)
2209 struct RequestHandle *handle = cls;
2210 struct EgoEntry *ego_entry;
2211 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
2213 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2215 handle->state = ID_REST_STATE_POST_INIT;
2219 GNUNET_assert (NULL != ego);
2220 if (ID_REST_STATE_INIT == handle->state)
2223 ego_entry = GNUNET_new (struct EgoEntry);
2224 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2225 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2226 ego_entry->ego = ego;
2227 ego_entry->identifier = GNUNET_strdup (identifier);
2228 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2233 /* Ego renamed or added */
2234 if (identifier != NULL)
2236 for (ego_entry = handle->ego_head; NULL != ego_entry;
2237 ego_entry = ego_entry->next)
2239 if (ego_entry->ego == ego)
2242 GNUNET_free (ego_entry->identifier);
2243 ego_entry->identifier = GNUNET_strdup (identifier);
2247 if (NULL == ego_entry)
2250 ego_entry = GNUNET_new (struct EgoEntry);
2251 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2252 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2253 ego_entry->ego = ego;
2254 ego_entry->identifier = GNUNET_strdup (identifier);
2255 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2263 for (ego_entry = handle->ego_head; NULL != ego_entry;
2264 ego_entry = ego_entry->next)
2266 if (ego_entry->ego == ego)
2269 if (NULL != ego_entry)
2270 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2278 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2279 GNUNET_REST_ResultProcessor proc,
2282 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2284 handle->oidc = GNUNET_new (struct OIDC_Variables);
2285 if (NULL == OIDC_cookie_jar_map)
2286 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2287 if (NULL == OIDC_access_token_map)
2288 OIDC_access_token_map =
2289 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2290 handle->response_code = 0;
2291 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2292 handle->proc_cls = proc_cls;
2293 handle->proc = proc;
2294 handle->state = ID_REST_STATE_INIT;
2295 handle->rest_handle = rest_handle;
2297 handle->url = GNUNET_strdup (rest_handle->url);
2298 if (handle->url[strlen (handle->url) - 1] == '/')
2299 handle->url[strlen (handle->url) - 1] = '\0';
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2301 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2302 handle->gns_handle = GNUNET_GNS_connect (cfg);
2303 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2304 handle->timeout_task =
2305 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2311 * Entry point for the plugin.
2313 * @param cls Config info
2314 * @return NULL on error, otherwise the plugin context
2317 libgnunet_plugin_rest_openid_connect_init (void *cls)
2319 static struct Plugin plugin;
2320 struct GNUNET_REST_Plugin *api;
2323 if (NULL != plugin.cfg)
2324 return NULL; /* can only initialize once! */
2325 memset (&plugin, 0, sizeof(struct Plugin));
2327 api = GNUNET_new (struct GNUNET_REST_Plugin);
2329 api->name = GNUNET_REST_API_NS_OIDC;
2330 api->process_request = &rest_identity_process_request;
2331 GNUNET_asprintf (&allow_methods,
2332 "%s, %s, %s, %s, %s",
2333 MHD_HTTP_METHOD_GET,
2334 MHD_HTTP_METHOD_POST,
2335 MHD_HTTP_METHOD_PUT,
2336 MHD_HTTP_METHOD_DELETE,
2337 MHD_HTTP_METHOD_OPTIONS);
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2340 _ ("OpenID Connect REST API initialized\n"));
2346 * Exit point from the plugin.
2348 * @param cls the plugin context (as returned by "init")
2349 * @return always NULL
2352 libgnunet_plugin_rest_openid_connect_done (void *cls)
2354 struct GNUNET_REST_Plugin *api = cls;
2355 struct Plugin *plugin = api->cls;
2359 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2362 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2363 while (GNUNET_YES ==
2364 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2365 GNUNET_free_non_null (value);
2366 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2367 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2370 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2371 while (GNUNET_YES ==
2372 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2373 GNUNET_free_non_null (value);
2374 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2375 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2376 GNUNET_free_non_null (allow_methods);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 "OpenID Connect REST plugin is finished\n");
2384 /* end of plugin_rest_openid_connect.c */