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_attribute_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;
462 struct GNUNET_RECLAIM_TicketIterator *ticket_it;
467 struct GNUNET_RECLAIM_Ticket ticket;
470 * Desired timeout for the lookup (default is no timeout).
472 struct GNUNET_TIME_Relative timeout;
475 * ID of a task associated with the resolution process.
477 struct GNUNET_SCHEDULER_Task *timeout_task;
480 * The plugin result processor
482 GNUNET_REST_ResultProcessor proc;
485 * The closure of the result processor
495 * The tld for redirect
500 * The redirect prefix
502 char *redirect_prefix;
505 * The redirect suffix
507 char *redirect_suffix;
510 * Error response message
515 * Error response description
526 * Cleanup lookup handle
527 * @param handle Handle to clean up
530 cleanup_handle (struct RequestHandle *handle)
532 struct GNUNET_RECLAIM_AttributeListEntry *claim_entry;
533 struct EgoEntry *ego_entry;
534 struct EgoEntry *ego_tmp;
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
537 if (NULL != handle->timeout_task)
538 GNUNET_SCHEDULER_cancel (handle->timeout_task);
539 if (NULL != handle->identity_handle)
540 GNUNET_IDENTITY_disconnect (handle->identity_handle);
541 if (NULL != handle->attr_it)
542 GNUNET_RECLAIM_get_attributes_stop (handle->attr_it);
543 if (NULL != handle->ticket_it)
544 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
545 if (NULL != handle->idp)
546 GNUNET_RECLAIM_disconnect (handle->idp);
547 GNUNET_free_non_null (handle->url);
548 GNUNET_free_non_null (handle->tld);
549 GNUNET_free_non_null (handle->redirect_prefix);
550 GNUNET_free_non_null (handle->redirect_suffix);
551 GNUNET_free_non_null (handle->emsg);
552 GNUNET_free_non_null (handle->edesc);
553 if (NULL != handle->gns_op)
554 GNUNET_GNS_lookup_cancel (handle->gns_op);
555 if (NULL != handle->gns_handle)
556 GNUNET_GNS_disconnect (handle->gns_handle);
558 if (NULL != handle->namestore_handle)
559 GNUNET_NAMESTORE_disconnect (handle->namestore_handle);
560 if (NULL != handle->oidc)
562 GNUNET_free_non_null (handle->oidc->client_id);
563 GNUNET_free_non_null (handle->oidc->login_identity);
564 GNUNET_free_non_null (handle->oidc->nonce);
565 GNUNET_free_non_null (handle->oidc->redirect_uri);
566 GNUNET_free_non_null (handle->oidc->response_type);
567 GNUNET_free_non_null (handle->oidc->scope);
568 GNUNET_free_non_null (handle->oidc->state);
569 json_decref (handle->oidc->response);
570 GNUNET_free (handle->oidc);
572 GNUNET_RECLAIM_attribute_list_destroy (handle->attr_list);
573 GNUNET_RECLAIM_attestation_list_destroy (handle->attests_list);
575 for (ego_entry = handle->ego_head; NULL != ego_entry;)
578 ego_entry = ego_entry->next;
579 GNUNET_free (ego_tmp->identifier);
580 GNUNET_free (ego_tmp->keystring);
581 GNUNET_free (ego_tmp);
583 GNUNET_free (handle);
588 cleanup_handle_delayed (void *cls)
590 cleanup_handle (cls);
595 * Task run on error, sends error message. Cleans up everything.
597 * @param cls the `struct RequestHandle`
602 struct RequestHandle *handle = cls;
603 struct MHD_Response *resp;
606 GNUNET_asprintf (&json_error,
607 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
609 (NULL != handle->edesc) ? handle->edesc : "",
610 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
611 (NULL != handle->oidc->state) ? handle->oidc->state : "",
612 (NULL != handle->oidc->state) ? "\"" : "");
613 if (0 == handle->response_code)
614 handle->response_code = MHD_HTTP_BAD_REQUEST;
615 resp = GNUNET_REST_create_response (json_error);
616 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
617 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
618 MHD_add_response_header (resp,
619 MHD_HTTP_HEADER_CONTENT_TYPE,
621 handle->proc (handle->proc_cls, resp, handle->response_code);
622 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
623 GNUNET_free (json_error);
628 * Task run on error in userinfo endpoint, sends error header. Cleans up
631 * @param cls the `struct RequestHandle`
634 do_userinfo_error (void *cls)
636 struct RequestHandle *handle = cls;
637 struct MHD_Response *resp;
640 GNUNET_asprintf (&error,
641 "error=\"%s\", error_description=\"%s\"",
643 (NULL != handle->edesc) ? handle->edesc : "");
644 resp = GNUNET_REST_create_response ("");
645 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
646 handle->proc (handle->proc_cls, resp, handle->response_code);
647 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
653 * Task run on error, sends error message and redirects. Cleans up everything.
655 * @param cls the `struct RequestHandle`
658 do_redirect_error (void *cls)
660 struct RequestHandle *handle = cls;
661 struct MHD_Response *resp;
664 GNUNET_asprintf (&redirect,
665 "%s?error=%s&error_description=%s%s%s",
666 handle->oidc->redirect_uri,
669 (NULL != handle->oidc->state) ? "&state=" : "",
670 (NULL != handle->oidc->state) ? handle->oidc->state : "");
671 resp = GNUNET_REST_create_response ("");
672 MHD_add_response_header (resp, "Location", redirect);
673 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
674 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
675 GNUNET_free (redirect);
680 * Task run on timeout, sends error message. Cleans up everything.
682 * @param cls the `struct RequestHandle`
685 do_timeout (void *cls)
687 struct RequestHandle *handle = cls;
689 handle->timeout_task = NULL;
695 * Return attributes for claim
697 * @param cls the request handle
700 return_userinfo_response (void *cls)
703 struct RequestHandle *handle = cls;
704 struct MHD_Response *resp;
706 result_str = json_dumps (handle->oidc->response, 0);
707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"ID-Token: %s\n",result_str);
708 resp = GNUNET_REST_create_response (result_str);
709 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
710 GNUNET_free (result_str);
711 cleanup_handle (handle);
716 * Respond to OPTIONS request
718 * @param con_handle the connection handle
720 * @param cls the RequestHandle
723 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
727 struct MHD_Response *resp;
728 struct RequestHandle *handle = cls;
730 // For now, independent of path return all options
731 resp = GNUNET_REST_create_response (NULL);
732 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
733 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
734 cleanup_handle (handle);
740 * Interprets cookie header and pass its identity keystring to handle
743 cookie_identity_interpretation (struct RequestHandle *handle)
745 struct GNUNET_HashCode cache_key;
747 struct GNUNET_TIME_Absolute current_time, *relog_time;
748 char delimiter[] = "; ";
753 // gets identity of login try with cookie
754 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
755 strlen (OIDC_COOKIE_HEADER_KEY),
757 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
764 // splits cookies and find 'Identity' cookie
766 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
768 cookies = GNUNET_strdup (tmp_cookies);
769 token = strtok (cookies, delimiter);
770 handle->oidc->user_cancelled = GNUNET_NO;
771 handle->oidc->login_identity = NULL;
774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
775 "Unable to parse cookie: %s\n",
777 GNUNET_free (cookies);
781 while (NULL != token)
783 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
785 handle->oidc->user_cancelled = GNUNET_YES;
786 GNUNET_free (cookies);
789 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
791 token = strtok (NULL, delimiter);
795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
796 "No cookie value to process: %s\n",
798 GNUNET_free (cookies);
801 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
803 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
806 GNUNET_ERROR_TYPE_WARNING,
807 "Found cookie `%s', but no corresponding expiration entry present...\n",
809 GNUNET_free (cookies);
813 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
814 current_time = GNUNET_TIME_absolute_get ();
815 // 30 min after old login -> redirect to login
816 if (current_time.abs_value_us > relog_time->abs_value_us)
818 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
819 "Found cookie `%s', but it is expired.\n",
821 GNUNET_free (cookies);
824 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
825 GNUNET_assert (NULL != value);
826 handle->oidc->login_identity = GNUNET_strdup (value);
827 GNUNET_free (cookies);
832 * Redirects to login page stored in configuration file
835 login_redirect (void *cls)
837 char *login_base_url;
839 struct MHD_Response *resp;
840 struct RequestHandle *handle = cls;
842 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
843 "reclaim-rest-plugin",
847 GNUNET_asprintf (&new_redirect,
848 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
850 OIDC_RESPONSE_TYPE_KEY,
851 handle->oidc->response_type,
853 handle->oidc->client_id,
854 OIDC_REDIRECT_URI_KEY,
855 handle->oidc->redirect_uri,
859 (NULL != handle->oidc->state) ? handle->oidc->state : "",
860 OIDC_CODE_CHALLENGE_KEY,
861 (NULL != handle->oidc->code_challenge) ?
862 handle->oidc->code_challenge : "",
864 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "",
866 (NULL != handle->oidc->claims) ? handle->oidc->claims :
868 resp = GNUNET_REST_create_response ("");
869 MHD_add_response_header (resp, "Location", new_redirect);
870 GNUNET_free (login_base_url);
874 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
875 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
876 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
877 GNUNET_SCHEDULER_add_now (&do_error, handle);
880 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
881 GNUNET_free (new_redirect);
882 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
887 * Does internal server error when iteration failed.
890 oidc_iteration_error (void *cls)
892 struct RequestHandle *handle = cls;
894 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
895 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
896 GNUNET_SCHEDULER_add_now (&do_error, handle);
901 * Issues ticket and redirects to relying party with the authorization code as
902 * parameter. Otherwise redirects with error
905 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
907 struct RequestHandle *handle = cls;
908 struct MHD_Response *resp;
913 handle->idp_op = NULL;
916 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
917 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
918 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
921 handle->ticket = *ticket;
923 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
924 sizeof(struct GNUNET_RECLAIM_Ticket));
925 // TODO change if more attributes are needed (see max_age)
926 code_string = OIDC_build_authz_code (&handle->priv_key,
929 handle->attests_list,
931 handle->oidc->code_challenge);
932 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
933 (NULL != handle->tld))
935 GNUNET_asprintf (&redirect_uri,
936 "%s.%s/%s?%s=%s&state=%s",
937 handle->redirect_prefix,
939 handle->redirect_suffix,
940 handle->oidc->response_type,
942 handle->oidc->state);
946 GNUNET_asprintf (&redirect_uri,
948 handle->oidc->redirect_uri,
949 handle->oidc->response_type,
951 handle->oidc->state);
953 resp = GNUNET_REST_create_response ("");
954 MHD_add_response_header (resp, "Location", redirect_uri);
955 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
956 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
957 GNUNET_free (redirect_uri);
958 GNUNET_free (ticket_str);
959 GNUNET_free (code_string);
964 oidc_collect_finished_cb (void *cls)
966 struct RequestHandle *handle = cls;
968 handle->attr_it = NULL;
969 handle->ticket_it = NULL;
970 if (NULL == handle->attr_list->list_head)
972 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
973 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
974 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
977 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
979 &handle->oidc->client_pkey,
981 &oidc_ticket_issue_cb,
987 * Collects all attributes for an ego if in scope parameter
990 oidc_attr_collect (void *cls,
991 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
992 const struct GNUNET_RECLAIM_Attribute *attr,
993 const struct GNUNET_RECLAIM_Attestation *attest)
995 struct RequestHandle *handle = cls;
996 struct GNUNET_RECLAIM_AttributeListEntry *le;
997 char *scope_variables;
998 char *scope_variable;
999 char delimiter[] = " ";
1001 scope_variables = GNUNET_strdup (handle->oidc->scope);
1002 scope_variable = strtok (scope_variables, delimiter);
1003 while (NULL != scope_variable)
1005 if (0 == strcmp (attr->name, scope_variable))
1007 scope_variable = strtok (NULL, delimiter);
1009 if (NULL == scope_variable)
1011 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1012 GNUNET_free (scope_variables);
1013 // We can ignore this
1016 GNUNET_free (scope_variables);
1017 le = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
1018 le->attribute = GNUNET_RECLAIM_attribute_new (attr->name,
1023 le->attribute->id = attr->id;
1024 le->attribute->flag = attr->flag;
1025 le->attribute->attestation = attr->attestation;
1026 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
1027 handle->attr_list->list_tail,
1029 if (GNUNET_NO == GNUNET_RECLAIM_id_is_zero (&attr->attestation))
1031 struct GNUNET_RECLAIM_AttestationListEntry *ale;
1032 ale = GNUNET_new (struct GNUNET_RECLAIM_AttestationListEntry);
1033 ale->attestation = GNUNET_RECLAIM_attestation_new (attest->name,
1037 GNUNET_CONTAINER_DLL_insert (handle->attests_list->list_head,
1038 handle->attests_list->list_tail,
1041 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1046 * Checks time and cookie and redirects accordingly
1049 code_redirect (void *cls)
1051 struct RequestHandle *handle = cls;
1052 struct GNUNET_TIME_Absolute current_time;
1053 struct GNUNET_TIME_Absolute *relog_time;
1054 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1055 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1056 struct GNUNET_HashCode cache_key;
1057 char *identity_cookie;
1059 GNUNET_asprintf (&identity_cookie,
1061 handle->oidc->login_identity);
1062 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1063 GNUNET_free (identity_cookie);
1064 // No login time for identity -> redirect to login
1066 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1069 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1070 current_time = GNUNET_TIME_absolute_get ();
1071 // 30 min after old login -> redirect to login
1072 if (current_time.abs_value_us <= relog_time->abs_value_us)
1075 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1082 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1084 GNUNET_strdup ("The cookie of a login identity is not valid");
1085 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1088 // iterate over egos and compare their public key
1089 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1090 handle->ego_entry = handle->ego_entry->next)
1092 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1093 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1096 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1097 handle->idp = GNUNET_RECLAIM_connect (cfg);
1099 GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
1101 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1103 &oidc_iteration_error,
1107 &oidc_collect_finished_cb,
1112 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1120 build_redirect (void *cls)
1122 struct RequestHandle *handle = cls;
1123 struct MHD_Response *resp;
1126 if (GNUNET_YES == handle->oidc->user_cancelled)
1128 if ((NULL != handle->redirect_prefix) &&
1129 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1131 GNUNET_asprintf (&redirect_uri,
1132 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1133 handle->redirect_prefix,
1135 handle->redirect_suffix,
1137 "User denied access",
1138 handle->oidc->state);
1142 GNUNET_asprintf (&redirect_uri,
1143 "%s?error=%s&error_description=%s&state=%s",
1144 handle->oidc->redirect_uri,
1146 "User denied access",
1147 handle->oidc->state);
1149 resp = GNUNET_REST_create_response ("");
1150 MHD_add_response_header (resp, "Location", redirect_uri);
1151 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1152 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1153 GNUNET_free (redirect_uri);
1156 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1161 lookup_redirect_uri_result (void *cls,
1163 const struct GNUNET_GNSRECORD_Data *rd)
1165 struct RequestHandle *handle = cls;
1169 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1171 handle->gns_op = NULL;
1174 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1176 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1177 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1180 for (int i = 0; i < rd_count; i++)
1182 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1184 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1186 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1187 if (NULL == strstr (tmp, handle->oidc->client_id))
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Redirect uri %s does not contain client_id %s\n",
1192 handle->oidc->client_id);
1196 pos = strrchr (tmp, (unsigned char) '.');
1199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1200 "Redirect uri %s contains client_id but is malformed\n",
1206 handle->redirect_prefix = GNUNET_strdup (tmp);
1207 tmp_key_str = pos + 1;
1208 pos = strchr (tmp_key_str, (unsigned char) '/');
1211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1212 "Redirect uri %s contains client_id but is malformed\n",
1218 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1220 GNUNET_STRINGS_string_to_data (tmp_key_str,
1221 strlen (tmp_key_str),
1223 sizeof(redirect_zone));
1225 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
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);
1237 * Initiate redirect back to client.
1240 client_redirect (void *cls)
1242 struct RequestHandle *handle = cls;
1244 /* Lookup client redirect uri to verify request */
1246 GNUNET_GNS_lookup (handle->gns_handle,
1247 GNUNET_GNS_EMPTY_LABEL_AT,
1248 &handle->oidc->client_pkey,
1249 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1250 GNUNET_GNS_LO_DEFAULT,
1251 &lookup_redirect_uri_result,
1257 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1259 struct GNUNET_HashCode hc;
1262 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1263 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1268 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1271 return GNUNET_strdup (value);
1276 * Iteration over all results finished, build final
1279 * @param cls the `struct RequestHandle`
1282 build_authz_response (void *cls)
1284 struct RequestHandle *handle = cls;
1285 struct GNUNET_HashCode cache_key;
1287 char *expected_scope;
1288 char delimiter[] = " ";
1289 int number_of_ignored_parameter, iterator;
1292 // REQUIRED value: redirect_uri
1293 handle->oidc->redirect_uri =
1294 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1295 if (NULL == handle->oidc->redirect_uri)
1297 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1298 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1299 GNUNET_SCHEDULER_add_now (&do_error, handle);
1303 // REQUIRED value: response_type
1304 handle->oidc->response_type =
1305 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1306 if (NULL == handle->oidc->response_type)
1308 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1309 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1310 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1314 // REQUIRED value: scope
1315 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1316 if (NULL == handle->oidc->scope)
1318 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1319 handle->edesc = GNUNET_strdup ("missing parameter scope");
1320 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1324 // OPTIONAL value: nonce
1325 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1327 // OPTIONAL value: claims
1328 handle->oidc->claims = get_url_parameter_copy (handle, OIDC_CLAIMS_KEY);
1330 // TODO check other values if needed
1331 number_of_ignored_parameter =
1332 sizeof(OIDC_ignored_parameter_array) / sizeof(char *);
1333 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1335 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1336 strlen (OIDC_ignored_parameter_array[iterator]),
1339 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1343 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1344 GNUNET_asprintf (&handle->edesc,
1345 "Server will not handle parameter: %s",
1346 OIDC_ignored_parameter_array[iterator]);
1347 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1352 // We only support authorization code flows.
1353 if (0 != strcmp (handle->oidc->response_type,
1354 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1356 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1357 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1358 "obtaining this authorization code.");
1359 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1363 // Checks if scope contains 'openid'
1364 expected_scope = GNUNET_strdup (handle->oidc->scope);
1366 test = strtok (expected_scope, delimiter);
1367 while (NULL != test)
1369 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1371 test = strtok (NULL, delimiter);
1375 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1377 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1378 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1379 GNUNET_free (expected_scope);
1383 GNUNET_free (expected_scope);
1384 if ((NULL == handle->oidc->login_identity) &&
1385 (GNUNET_NO == handle->oidc->user_cancelled))
1386 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1388 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1393 * Iterate over tlds in config
1396 tld_iter (void *cls, const char *section, const char *option, const char *value)
1398 struct RequestHandle *handle = cls;
1399 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1402 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1407 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1408 handle->tld = GNUNET_strdup (option + 1);
1413 * Responds to authorization GET and url-encoded POST request
1415 * @param con_handle the connection handle
1416 * @param url the url
1417 * @param cls the RequestHandle
1420 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1424 struct RequestHandle *handle = cls;
1425 struct EgoEntry *tmp_ego;
1426 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1427 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1429 cookie_identity_interpretation (handle);
1431 // RECOMMENDED value: state - REQUIRED for answers
1432 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1434 // REQUIRED value: client_id
1435 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1436 if (NULL == handle->oidc->client_id)
1438 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1439 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1440 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1441 GNUNET_SCHEDULER_add_now (&do_error, handle);
1445 // OPTIONAL value: code_challenge
1446 handle->oidc->code_challenge = get_url_parameter_copy (handle,
1447 OIDC_CODE_CHALLENGE_KEY);
1448 if (NULL == handle->oidc->code_challenge)
1450 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1451 "OAuth authorization request does not contain PKCE parameters!\n");
1455 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1457 handle->oidc->client_id),
1458 &handle->oidc->client_pkey))
1460 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1461 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1462 "authorization code using this method.");
1463 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1464 GNUNET_SCHEDULER_add_now (&do_error, handle);
1468 // If we know this identity, translated the corresponding TLD
1469 // TODO: We might want to have a reverse lookup functionality for TLDs?
1470 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1472 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1473 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1474 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1476 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1477 handle->ego_entry = handle->ego_tail;
1480 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scope: %s\n",GNUNET_strdup (
1482 handle->oidc->scope));
1483 if (NULL == handle->tld)
1484 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1485 if (NULL == handle->tld)
1486 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1487 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1492 * Combines an identity with a login time and responds OK to login request
1494 * @param con_handle the connection handle
1495 * @param url the url
1496 * @param cls the RequestHandle
1499 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1503 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1504 struct RequestHandle *handle = cls;
1505 struct GNUNET_HashCode cache_key;
1506 struct GNUNET_TIME_Absolute *current_time;
1507 struct GNUNET_TIME_Absolute *last_time;
1513 char term_data[handle->rest_handle->data_size + 1];
1515 term_data[handle->rest_handle->data_size] = '\0';
1516 GNUNET_memcpy (term_data,
1517 handle->rest_handle->data,
1518 handle->rest_handle->data_size);
1519 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1520 identity = json_object_get (root, "identity");
1521 if (! json_is_string (identity))
1523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1524 "Error parsing json string from %s\n",
1526 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1528 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1531 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1532 GNUNET_asprintf (&header_val,
1535 OIDC_COOKIE_EXPIRATION);
1536 MHD_add_response_header (resp, "Set-Cookie", header_val);
1537 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1538 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1540 if (0 != strcmp (json_string_value (identity), "Denied"))
1542 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1543 *current_time = GNUNET_TIME_relative_to_absolute (
1544 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1545 OIDC_COOKIE_EXPIRATION));
1547 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1548 GNUNET_free_non_null (last_time);
1549 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1552 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1554 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1555 GNUNET_free (cookie);
1556 GNUNET_free (header_val);
1558 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1563 check_authorization (struct RequestHandle *handle,
1564 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1566 struct GNUNET_HashCode cache_key;
1567 char *authorization;
1569 char *basic_authorization;
1572 char *expected_pass;
1574 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1575 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1577 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1581 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1582 handle->edesc = GNUNET_strdup ("missing authorization");
1583 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1584 return GNUNET_SYSERR;
1587 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1590 // split header in "Basic" and [content]
1591 credentials = strtok (authorization, " ");
1592 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1594 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1595 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1596 return GNUNET_SYSERR;
1598 credentials = strtok (NULL, " ");
1599 if (NULL == credentials)
1601 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1602 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1603 return GNUNET_SYSERR;
1605 GNUNET_STRINGS_base64_decode (credentials,
1606 strlen (credentials),
1607 (void **) &basic_authorization);
1609 if (NULL == basic_authorization)
1611 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1612 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1613 return GNUNET_SYSERR;
1615 client_id = strtok (basic_authorization, ":");
1616 if (NULL == client_id)
1618 GNUNET_free_non_null (basic_authorization);
1619 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1620 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1621 return GNUNET_SYSERR;
1623 pass = strtok (NULL, ":");
1626 GNUNET_free_non_null (basic_authorization);
1627 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1628 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1629 return GNUNET_SYSERR;
1632 // check client password
1633 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1634 "reclaim-rest-plugin",
1635 "OIDC_CLIENT_SECRET",
1638 if (0 != strcmp (expected_pass, pass))
1640 GNUNET_free_non_null (basic_authorization);
1641 GNUNET_free (expected_pass);
1642 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1643 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1644 return GNUNET_SYSERR;
1646 GNUNET_free (expected_pass);
1650 GNUNET_free_non_null (basic_authorization);
1651 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1652 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1653 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1654 return GNUNET_SYSERR;
1658 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1659 handle->ego_entry = handle->ego_entry->next)
1661 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1664 if (NULL == handle->ego_entry)
1666 GNUNET_free_non_null (basic_authorization);
1667 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1668 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1669 return GNUNET_SYSERR;
1671 GNUNET_STRINGS_string_to_data (client_id,
1674 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
1676 GNUNET_free (basic_authorization);
1681 const struct EgoEntry *
1682 find_ego (struct RequestHandle *handle,
1683 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1685 struct EgoEntry *ego_entry;
1686 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1688 for (ego_entry = handle->ego_head; NULL != ego_entry;
1689 ego_entry = ego_entry->next)
1691 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1692 if (0 == GNUNET_memcmp (&pub_key, test_key))
1700 persist_access_token (const struct RequestHandle *handle,
1701 const char *access_token,
1702 const struct GNUNET_RECLAIM_Ticket *ticket)
1704 struct GNUNET_HashCode hc;
1705 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1707 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1708 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1709 *ticketbuf = *ticket;
1710 GNUNET_assert (GNUNET_SYSERR !=
1711 GNUNET_CONTAINER_multihashmap_put (
1712 OIDC_access_token_map,
1715 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1720 * Responds to token url-encoded POST request
1722 * @param con_handle the connection handle
1723 * @param url the url
1724 * @param cls the RequestHandle
1727 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1731 struct RequestHandle *handle = cls;
1732 const struct EgoEntry *ego_entry;
1733 struct GNUNET_TIME_Relative expiration_time;
1734 struct GNUNET_RECLAIM_AttributeList *cl;
1735 struct GNUNET_RECLAIM_AttestationList *al;
1736 struct GNUNET_RECLAIM_Ticket ticket;
1737 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1738 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1739 struct GNUNET_HashCode cache_key;
1740 struct MHD_Response *resp;
1743 char *json_response;
1748 char *code_verifier;
1751 * Check Authorization
1753 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1756 "OIDC authorization for token endpoint failed\n");
1757 GNUNET_SCHEDULER_add_now (&do_error, handle);
1765 // TODO Do not allow multiple equal parameter names
1766 // REQUIRED grant_type
1767 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1768 strlen (OIDC_GRANT_TYPE_KEY),
1770 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1771 if (NULL == grant_type)
1773 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1774 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1775 handle->response_code = MHD_HTTP_BAD_REQUEST;
1776 GNUNET_SCHEDULER_add_now (&do_error, handle);
1780 // Check parameter grant_type == "authorization_code"
1781 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1783 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1784 handle->response_code = MHD_HTTP_BAD_REQUEST;
1785 GNUNET_free (grant_type);
1786 GNUNET_SCHEDULER_add_now (&do_error, handle);
1789 GNUNET_free (grant_type);
1791 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1794 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1795 handle->edesc = GNUNET_strdup ("missing parameter code");
1796 handle->response_code = MHD_HTTP_BAD_REQUEST;
1797 GNUNET_SCHEDULER_add_now (&do_error, handle);
1800 ego_entry = find_ego (handle, &cid);
1801 if (NULL == ego_entry)
1803 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1804 handle->edesc = GNUNET_strdup ("Unknown client");
1805 handle->response_code = MHD_HTTP_BAD_REQUEST;
1807 GNUNET_SCHEDULER_add_now (&do_error, handle);
1810 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1812 // REQUIRED code verifier
1813 code_verifier = get_url_parameter_copy (handle, OIDC_CODE_VERIFIER_KEY);
1814 if (NULL == code_verifier)
1816 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1817 "OAuth authorization request does not contain PKCE parameters!\n");
1822 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, code_verifier, &ticket,
1825 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1826 handle->edesc = GNUNET_strdup ("invalid code");
1827 handle->response_code = MHD_HTTP_BAD_REQUEST;
1829 GNUNET_SCHEDULER_add_now (&do_error, handle);
1835 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1836 "reclaim-rest-plugin",
1840 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1841 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1842 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1843 GNUNET_SCHEDULER_add_now (&do_error, handle);
1848 // TODO OPTIONAL acr,amr,azp
1849 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1850 "reclaim-rest-plugin",
1854 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1855 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1856 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1857 GNUNET_SCHEDULER_add_now (&do_error, handle);
1860 id_token = OIDC_id_token_new (&ticket.audience,
1865 (NULL != nonce) ? nonce : NULL,
1867 access_token = OIDC_access_token_new ();
1868 OIDC_build_token_response (access_token,
1873 persist_access_token (handle, access_token, &ticket);
1874 resp = GNUNET_REST_create_response (json_response);
1875 MHD_add_response_header (resp, "Cache-Control", "no-store");
1876 MHD_add_response_header (resp, "Pragma", "no-cache");
1877 MHD_add_response_header (resp, "Content-Type", "application/json");
1878 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1879 GNUNET_RECLAIM_attribute_list_destroy (cl);
1880 GNUNET_RECLAIM_attestation_list_destroy (al);
1881 GNUNET_free (access_token);
1882 GNUNET_free (json_response);
1883 GNUNET_free (id_token);
1884 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1889 * Collects claims and stores them in handle
1892 consume_ticket (void *cls,
1893 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1894 const struct GNUNET_RECLAIM_Attribute *attr,
1895 const struct GNUNET_RECLAIM_Attestation *attest)
1897 struct RequestHandle *handle = cls;
1898 if (NULL == identity)
1900 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1903 if (GNUNET_YES == GNUNET_RECLAIM_id_is_zero (&attr->attestation))
1907 tmp_value = GNUNET_RECLAIM_attribute_value_to_string (attr->type,
1910 value = json_string (tmp_value);
1911 json_object_set_new (handle->oidc->response, attr->name, value);
1912 GNUNET_free (tmp_value);
1915 json_t *claim_sources;
1916 json_t *claim_sources_jwt;
1917 json_t *claim_names;
1918 char *attest_val_str;
1919 claim_sources = json_object_get (handle->oidc->response,"_claim_sources");
1920 claim_names = json_object_get (handle->oidc->response,"_claim_names");
1922 GNUNET_RECLAIM_attestation_value_to_string (attest->type,
1925 if ((NULL == claim_sources) && (NULL == claim_names) )
1927 claim_sources = json_object ();
1928 claim_names = json_object ();
1932 GNUNET_asprintf (&source_name, "src%d", i);
1933 while (NULL != (claim_sources_jwt = json_object_get (claim_sources,
1936 if (0 == strcmp (json_string_value (json_object_get (claim_sources_jwt,
1940 // Adapt only the claim names
1941 json_object_set_new (claim_names, attr->data,
1942 json_string (source_name));
1943 json_object_set (handle->oidc->response,
1944 "_claim_names", claim_names);
1948 GNUNET_free (source_name);
1949 GNUNET_asprintf (&source_name, "src%d", i);
1953 if (NULL == claim_sources_jwt)
1955 claim_sources_jwt = json_object ();
1956 // Set the JWT for names
1957 json_object_set_new (claim_names, attr->data,
1958 json_string (source_name));
1959 // Set the JWT for the inner source
1960 json_object_set_new (claim_sources_jwt, "JWT",
1961 json_string (attest_val_str));
1962 // Set the JWT for the source
1963 json_object_set_new (claim_sources, source_name, claim_sources_jwt);
1965 json_object_set (handle->oidc->response, "_claim_names", claim_names);
1966 json_object_set (handle->oidc->response, "_claim_sources",claim_sources);
1969 json_decref (claim_sources);
1970 json_decref (claim_names);
1971 json_decref (claim_sources_jwt);
1972 GNUNET_free (attest_val_str);
1977 * Responds to userinfo GET and url-encoded POST request
1979 * @param con_handle the connection handle
1980 * @param url the url
1981 * @param cls the RequestHandle
1984 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1988 // TODO expiration time
1989 struct RequestHandle *handle = cls;
1990 char delimiter[] = " ";
1991 struct GNUNET_HashCode cache_key;
1992 char *authorization;
1993 char *authorization_type;
1994 char *authorization_access_token;
1995 struct GNUNET_RECLAIM_Ticket *ticket;
1996 const struct EgoEntry *ego_entry;
1997 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1999 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
2000 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
2002 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
2006 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2007 handle->edesc = GNUNET_strdup ("No Access Token");
2008 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2009 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2013 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
2016 // split header in "Bearer" and access_token
2017 authorization = GNUNET_strdup (authorization);
2018 authorization_type = strtok (authorization, delimiter);
2019 if ((NULL == authorization_type) ||
2020 (0 != strcmp ("Bearer", authorization_type)))
2022 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2023 handle->edesc = GNUNET_strdup ("No Access Token");
2024 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2025 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2026 GNUNET_free (authorization);
2029 authorization_access_token = strtok (NULL, delimiter);
2030 if (NULL == authorization_access_token)
2032 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2033 handle->edesc = GNUNET_strdup ("Access token missing");
2034 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2035 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2036 GNUNET_free (authorization);
2040 GNUNET_CRYPTO_hash (authorization_access_token,
2041 strlen (authorization_access_token),
2044 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
2047 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2048 handle->edesc = GNUNET_strdup ("The access token expired");
2049 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2050 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2051 GNUNET_free (authorization);
2055 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
2056 GNUNET_assert (NULL != ticket);
2057 ego_entry = find_ego (handle, &ticket->audience);
2058 if (NULL == ego_entry)
2060 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2061 handle->edesc = GNUNET_strdup ("The access token expired");
2062 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2063 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2064 GNUNET_free (authorization);
2068 handle->idp = GNUNET_RECLAIM_connect (cfg);
2069 handle->oidc->response = json_object ();
2070 json_object_set_new (handle->oidc->response,
2072 json_string (ego_entry->keystring));
2073 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
2074 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
2079 GNUNET_free (authorization);
2084 * Handle rest request
2086 * @param handle the request handle
2089 init_cont (struct RequestHandle *handle)
2091 struct GNUNET_REST_RequestHandlerError err;
2092 static const struct GNUNET_REST_RequestHandler handlers[] =
2093 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint },
2094 { MHD_HTTP_METHOD_POST,
2095 GNUNET_REST_API_NS_AUTHORIZE,
2096 &authorize_endpoint }, // url-encoded
2097 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont },
2098 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint },
2099 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2100 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2101 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont },
2102 GNUNET_REST_HANDLER_END };
2105 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
2107 handle->response_code = err.error_code;
2108 GNUNET_SCHEDULER_add_now (&do_error, handle);
2114 * If listing is enabled, prints information about the egos.
2116 * This function is initially called for all egos and then again
2117 * whenever a ego's identifier changes or if it is deleted. At the
2118 * end of the initial pass over all egos, the function is once called
2119 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
2120 * be invoked in the future or that there was an error.
2122 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
2123 * this function is only called ONCE, and 'NULL' being passed in
2124 * 'ego' does indicate an error (i.e. name is taken or no default
2125 * value is known). If 'ego' is non-NULL and if '*ctx'
2126 * is set in those callbacks, the value WILL be passed to a subsequent
2127 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
2128 * that one was not NULL).
2130 * When an identity is renamed, this function is called with the
2131 * (known) ego but the NEW identifier.
2133 * When an identity is deleted, this function is called with the
2134 * (known) ego and "NULL" for the 'identifier'. In this case,
2135 * the 'ego' is henceforth invalid (and the 'ctx' should also be
2138 * @param cls closure
2139 * @param ego ego handle
2140 * @param ctx context for application to store data for this ego
2141 * (during the lifetime of this process, initially NULL)
2142 * @param identifier identifier assigned by the user for this ego,
2143 * NULL if the user just deleted the ego and it
2144 * must thus no longer be used
2147 list_ego (void *cls,
2148 struct GNUNET_IDENTITY_Ego *ego,
2150 const char *identifier)
2152 struct RequestHandle *handle = cls;
2153 struct EgoEntry *ego_entry;
2154 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
2156 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2158 handle->state = ID_REST_STATE_POST_INIT;
2162 GNUNET_assert (NULL != ego);
2163 if (ID_REST_STATE_INIT == handle->state)
2166 ego_entry = GNUNET_new (struct EgoEntry);
2167 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2168 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2169 ego_entry->ego = ego;
2170 ego_entry->identifier = GNUNET_strdup (identifier);
2171 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2176 /* Ego renamed or added */
2177 if (identifier != NULL)
2179 for (ego_entry = handle->ego_head; NULL != ego_entry;
2180 ego_entry = ego_entry->next)
2182 if (ego_entry->ego == ego)
2185 GNUNET_free (ego_entry->identifier);
2186 ego_entry->identifier = GNUNET_strdup (identifier);
2190 if (NULL == ego_entry)
2193 ego_entry = GNUNET_new (struct EgoEntry);
2194 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2195 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2196 ego_entry->ego = ego;
2197 ego_entry->identifier = GNUNET_strdup (identifier);
2198 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2206 for (ego_entry = handle->ego_head; NULL != ego_entry;
2207 ego_entry = ego_entry->next)
2209 if (ego_entry->ego == ego)
2212 if (NULL != ego_entry)
2213 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2221 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2222 GNUNET_REST_ResultProcessor proc,
2225 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2227 handle->oidc = GNUNET_new (struct OIDC_Variables);
2228 if (NULL == OIDC_cookie_jar_map)
2229 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2230 if (NULL == OIDC_access_token_map)
2231 OIDC_access_token_map =
2232 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2233 handle->response_code = 0;
2234 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2235 handle->proc_cls = proc_cls;
2236 handle->proc = proc;
2237 handle->state = ID_REST_STATE_INIT;
2238 handle->rest_handle = rest_handle;
2240 handle->url = GNUNET_strdup (rest_handle->url);
2241 if (handle->url[strlen (handle->url) - 1] == '/')
2242 handle->url[strlen (handle->url) - 1] = '\0';
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2244 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2245 handle->gns_handle = GNUNET_GNS_connect (cfg);
2246 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2247 handle->timeout_task =
2248 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2254 * Entry point for the plugin.
2256 * @param cls Config info
2257 * @return NULL on error, otherwise the plugin context
2260 libgnunet_plugin_rest_openid_connect_init (void *cls)
2262 static struct Plugin plugin;
2263 struct GNUNET_REST_Plugin *api;
2266 if (NULL != plugin.cfg)
2267 return NULL; /* can only initialize once! */
2268 memset (&plugin, 0, sizeof(struct Plugin));
2270 api = GNUNET_new (struct GNUNET_REST_Plugin);
2272 api->name = GNUNET_REST_API_NS_OIDC;
2273 api->process_request = &rest_identity_process_request;
2274 GNUNET_asprintf (&allow_methods,
2275 "%s, %s, %s, %s, %s",
2276 MHD_HTTP_METHOD_GET,
2277 MHD_HTTP_METHOD_POST,
2278 MHD_HTTP_METHOD_PUT,
2279 MHD_HTTP_METHOD_DELETE,
2280 MHD_HTTP_METHOD_OPTIONS);
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 _ ("OpenID Connect REST API initialized\n"));
2289 * Exit point from the plugin.
2291 * @param cls the plugin context (as returned by "init")
2292 * @return always NULL
2295 libgnunet_plugin_rest_openid_connect_done (void *cls)
2297 struct GNUNET_REST_Plugin *api = cls;
2298 struct Plugin *plugin = api->cls;
2302 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2305 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2306 while (GNUNET_YES ==
2307 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2308 GNUNET_free_non_null (value);
2309 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2310 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2313 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2314 while (GNUNET_YES ==
2315 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2316 GNUNET_free_non_null (value);
2317 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2318 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2319 GNUNET_free_non_null (allow_methods);
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "OpenID Connect REST plugin is finished\n");
2327 /* end of plugin_rest_openid_connect.c */