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;
539 struct EgoEntry *ego_tmp;
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
542 if (NULL != handle->timeout_task)
543 GNUNET_SCHEDULER_cancel (handle->timeout_task);
544 if (NULL != handle->identity_handle)
545 GNUNET_IDENTITY_disconnect (handle->identity_handle);
546 if (NULL != handle->attr_it)
547 GNUNET_RECLAIM_get_attributes_stop (handle->attr_it);
548 if (NULL != handle->attest_it)
549 GNUNET_RECLAIM_get_attestations_stop (handle->attest_it);
550 if (NULL != handle->ticket_it)
551 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
552 if (NULL != handle->idp)
553 GNUNET_RECLAIM_disconnect (handle->idp);
554 GNUNET_free_non_null (handle->url);
555 GNUNET_free_non_null (handle->tld);
556 GNUNET_free_non_null (handle->redirect_prefix);
557 GNUNET_free_non_null (handle->redirect_suffix);
558 GNUNET_free_non_null (handle->emsg);
559 GNUNET_free_non_null (handle->edesc);
560 if (NULL != handle->gns_op)
561 GNUNET_GNS_lookup_cancel (handle->gns_op);
562 if (NULL != handle->gns_handle)
563 GNUNET_GNS_disconnect (handle->gns_handle);
565 if (NULL != handle->namestore_handle)
566 GNUNET_NAMESTORE_disconnect (handle->namestore_handle);
567 if (NULL != handle->oidc)
569 GNUNET_free_non_null (handle->oidc->client_id);
570 GNUNET_free_non_null (handle->oidc->login_identity);
571 GNUNET_free_non_null (handle->oidc->nonce);
572 GNUNET_free_non_null (handle->oidc->redirect_uri);
573 GNUNET_free_non_null (handle->oidc->response_type);
574 GNUNET_free_non_null (handle->oidc->scope);
575 GNUNET_free_non_null (handle->oidc->state);
576 json_decref (handle->oidc->response);
577 GNUNET_free (handle->oidc);
579 GNUNET_RECLAIM_attribute_list_destroy (handle->attr_list);
580 GNUNET_RECLAIM_attestation_list_destroy (handle->attests_list);
582 for (ego_entry = handle->ego_head; NULL != ego_entry;)
585 ego_entry = ego_entry->next;
586 GNUNET_free (ego_tmp->identifier);
587 GNUNET_free (ego_tmp->keystring);
588 GNUNET_free (ego_tmp);
590 GNUNET_free (handle);
595 cleanup_handle_delayed (void *cls)
597 cleanup_handle (cls);
602 * Task run on error, sends error message. Cleans up everything.
604 * @param cls the `struct RequestHandle`
609 struct RequestHandle *handle = cls;
610 struct MHD_Response *resp;
613 GNUNET_asprintf (&json_error,
614 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
616 (NULL != handle->edesc) ? handle->edesc : "",
617 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
618 (NULL != handle->oidc->state) ? handle->oidc->state : "",
619 (NULL != handle->oidc->state) ? "\"" : "");
620 if (0 == handle->response_code)
621 handle->response_code = MHD_HTTP_BAD_REQUEST;
622 resp = GNUNET_REST_create_response (json_error);
623 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
624 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
625 MHD_add_response_header (resp,
626 MHD_HTTP_HEADER_CONTENT_TYPE,
628 handle->proc (handle->proc_cls, resp, handle->response_code);
629 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
630 GNUNET_free (json_error);
635 * Task run on error in userinfo endpoint, sends error header. Cleans up
638 * @param cls the `struct RequestHandle`
641 do_userinfo_error (void *cls)
643 struct RequestHandle *handle = cls;
644 struct MHD_Response *resp;
647 GNUNET_asprintf (&error,
648 "error=\"%s\", error_description=\"%s\"",
650 (NULL != handle->edesc) ? handle->edesc : "");
651 resp = GNUNET_REST_create_response ("");
652 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
653 handle->proc (handle->proc_cls, resp, handle->response_code);
654 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
660 * Task run on error, sends error message and redirects. Cleans up everything.
662 * @param cls the `struct RequestHandle`
665 do_redirect_error (void *cls)
667 struct RequestHandle *handle = cls;
668 struct MHD_Response *resp;
671 GNUNET_asprintf (&redirect,
672 "%s?error=%s&error_description=%s%s%s",
673 handle->oidc->redirect_uri,
676 (NULL != handle->oidc->state) ? "&state=" : "",
677 (NULL != handle->oidc->state) ? handle->oidc->state : "");
678 resp = GNUNET_REST_create_response ("");
679 MHD_add_response_header (resp, "Location", redirect);
680 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
681 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
682 GNUNET_free (redirect);
687 * Task run on timeout, sends error message. Cleans up everything.
689 * @param cls the `struct RequestHandle`
692 do_timeout (void *cls)
694 struct RequestHandle *handle = cls;
696 handle->timeout_task = NULL;
702 * Return attributes for claim
704 * @param cls the request handle
707 return_userinfo_response (void *cls)
710 struct RequestHandle *handle = cls;
711 struct MHD_Response *resp;
713 result_str = json_dumps (handle->oidc->response, 0);
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"ID-Token: %s\n",result_str);
715 resp = GNUNET_REST_create_response (result_str);
716 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
717 GNUNET_free (result_str);
718 cleanup_handle (handle);
723 * Respond to OPTIONS request
725 * @param con_handle the connection handle
727 * @param cls the RequestHandle
730 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
734 struct MHD_Response *resp;
735 struct RequestHandle *handle = cls;
737 // For now, independent of path return all options
738 resp = GNUNET_REST_create_response (NULL);
739 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
740 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
741 cleanup_handle (handle);
747 * Interprets cookie header and pass its identity keystring to handle
750 cookie_identity_interpretation (struct RequestHandle *handle)
752 struct GNUNET_HashCode cache_key;
754 struct GNUNET_TIME_Absolute current_time, *relog_time;
755 char delimiter[] = "; ";
760 // gets identity of login try with cookie
761 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
762 strlen (OIDC_COOKIE_HEADER_KEY),
764 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
771 // splits cookies and find 'Identity' cookie
773 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
775 cookies = GNUNET_strdup (tmp_cookies);
776 token = strtok (cookies, delimiter);
777 handle->oidc->user_cancelled = GNUNET_NO;
778 handle->oidc->login_identity = NULL;
781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
782 "Unable to parse cookie: %s\n",
784 GNUNET_free (cookies);
788 while (NULL != token)
790 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
792 handle->oidc->user_cancelled = GNUNET_YES;
793 GNUNET_free (cookies);
796 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
798 token = strtok (NULL, delimiter);
802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
803 "No cookie value to process: %s\n",
805 GNUNET_free (cookies);
808 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
810 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
813 GNUNET_ERROR_TYPE_WARNING,
814 "Found cookie `%s', but no corresponding expiration entry present...\n",
816 GNUNET_free (cookies);
820 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
821 current_time = GNUNET_TIME_absolute_get ();
822 // 30 min after old login -> redirect to login
823 if (current_time.abs_value_us > relog_time->abs_value_us)
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 "Found cookie `%s', but it is expired.\n",
828 GNUNET_free (cookies);
831 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
832 GNUNET_assert (NULL != value);
833 handle->oidc->login_identity = GNUNET_strdup (value);
834 GNUNET_free (cookies);
839 * Redirects to login page stored in configuration file
842 login_redirect (void *cls)
844 char *login_base_url;
846 struct MHD_Response *resp;
847 struct RequestHandle *handle = cls;
849 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
850 "reclaim-rest-plugin",
854 GNUNET_asprintf (&new_redirect,
855 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
857 OIDC_RESPONSE_TYPE_KEY,
858 handle->oidc->response_type,
860 handle->oidc->client_id,
861 OIDC_REDIRECT_URI_KEY,
862 handle->oidc->redirect_uri,
866 (NULL != handle->oidc->state) ? handle->oidc->state : "",
867 OIDC_CODE_CHALLENGE_KEY,
868 (NULL != handle->oidc->code_challenge) ?
869 handle->oidc->code_challenge : "",
871 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "",
873 (NULL != handle->oidc->claims) ? handle->oidc->claims :
875 resp = GNUNET_REST_create_response ("");
876 MHD_add_response_header (resp, "Location", new_redirect);
877 GNUNET_free (login_base_url);
881 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
882 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
883 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
884 GNUNET_SCHEDULER_add_now (&do_error, handle);
887 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
888 GNUNET_free (new_redirect);
889 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
894 * Does internal server error when iteration failed.
897 oidc_iteration_error (void *cls)
899 struct RequestHandle *handle = cls;
901 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
902 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
903 GNUNET_SCHEDULER_add_now (&do_error, handle);
908 * Issues ticket and redirects to relying party with the authorization code as
909 * parameter. Otherwise redirects with error
912 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
914 struct RequestHandle *handle = cls;
915 struct MHD_Response *resp;
920 handle->idp_op = NULL;
923 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
924 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
925 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
928 handle->ticket = *ticket;
930 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
931 sizeof(struct GNUNET_RECLAIM_Ticket));
932 // TODO change if more attributes are needed (see max_age)
933 code_string = OIDC_build_authz_code (&handle->priv_key,
936 handle->attests_list,
938 handle->oidc->code_challenge);
939 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
940 (NULL != handle->tld))
942 GNUNET_asprintf (&redirect_uri,
943 "%s.%s/%s?%s=%s&state=%s",
944 handle->redirect_prefix,
946 handle->redirect_suffix,
947 handle->oidc->response_type,
949 handle->oidc->state);
953 GNUNET_asprintf (&redirect_uri,
955 handle->oidc->redirect_uri,
956 handle->oidc->response_type,
958 handle->oidc->state);
960 resp = GNUNET_REST_create_response ("");
961 MHD_add_response_header (resp, "Location", redirect_uri);
962 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
963 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
964 GNUNET_free (redirect_uri);
965 GNUNET_free (ticket_str);
966 GNUNET_free (code_string);
971 oidc_attest_collect_finished_cb (void *cls)
973 struct RequestHandle *handle = cls;
975 handle->attest_it = NULL;
976 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
978 &handle->oidc->client_pkey,
980 &oidc_ticket_issue_cb,
986 * Collects all attributes for an ego if in scope parameter
989 oidc_attest_collect (void *cls,
990 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
991 const struct GNUNET_RECLAIM_Attestation *attest)
993 struct RequestHandle *handle = cls;
994 struct GNUNET_RECLAIM_AttributeListEntry *le;
996 for (le = handle->attr_list->list_head; NULL != le; le = le->next)
998 if (GNUNET_NO == GNUNET_RECLAIM_id_is_equal (&le->attribute->attestation,
1001 struct GNUNET_RECLAIM_AttestationListEntry *ale;
1002 ale = GNUNET_new (struct GNUNET_RECLAIM_AttestationListEntry);
1003 ale->attestation = GNUNET_RECLAIM_attestation_new (attest->name,
1007 GNUNET_CONTAINER_DLL_insert (handle->attests_list->list_head,
1008 handle->attests_list->list_tail,
1012 GNUNET_RECLAIM_get_attestations_next (handle->attest_it);
1017 oidc_attr_collect_finished_cb (void *cls)
1019 struct RequestHandle *handle = cls;
1021 handle->attr_it = NULL;
1022 handle->ticket_it = NULL;
1023 if (NULL == handle->attr_list->list_head)
1025 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1026 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
1027 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1030 handle->attests_list = GNUNET_new (struct GNUNET_RECLAIM_AttestationList);
1032 GNUNET_RECLAIM_get_attestations_start (handle->idp,
1034 &oidc_iteration_error,
1036 &oidc_attest_collect,
1038 &oidc_attest_collect_finished_cb,
1041 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
1043 &handle->oidc->client_pkey,
1045 &oidc_ticket_issue_cb,
1051 * Collects all attributes for an ego if in scope parameter
1054 oidc_attr_collect (void *cls,
1055 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1056 const struct GNUNET_RECLAIM_Attribute *attr)
1058 struct RequestHandle *handle = cls;
1059 struct GNUNET_RECLAIM_AttributeListEntry *le;
1060 char *scope_variables;
1061 char *scope_variable;
1062 char delimiter[] = " ";
1064 scope_variables = GNUNET_strdup (handle->oidc->scope);
1065 scope_variable = strtok (scope_variables, delimiter);
1066 while (NULL != scope_variable)
1068 if (0 == strcmp (attr->name, scope_variable))
1070 scope_variable = strtok (NULL, delimiter);
1072 if (NULL == scope_variable)
1074 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1075 GNUNET_free (scope_variables);
1076 // We can ignore this
1079 GNUNET_free (scope_variables);
1080 le = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);
1081 le->attribute = GNUNET_RECLAIM_attribute_new (attr->name,
1086 le->attribute->id = attr->id;
1087 le->attribute->flag = attr->flag;
1088 le->attribute->attestation = attr->attestation;
1089 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
1090 handle->attr_list->list_tail,
1092 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1097 * Checks time and cookie and redirects accordingly
1100 code_redirect (void *cls)
1102 struct RequestHandle *handle = cls;
1103 struct GNUNET_TIME_Absolute current_time;
1104 struct GNUNET_TIME_Absolute *relog_time;
1105 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1106 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1107 struct GNUNET_HashCode cache_key;
1108 char *identity_cookie;
1110 GNUNET_asprintf (&identity_cookie,
1112 handle->oidc->login_identity);
1113 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1114 GNUNET_free (identity_cookie);
1115 // No login time for identity -> redirect to login
1117 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1120 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1121 current_time = GNUNET_TIME_absolute_get ();
1122 // 30 min after old login -> redirect to login
1123 if (current_time.abs_value_us <= relog_time->abs_value_us)
1126 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1133 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1135 GNUNET_strdup ("The cookie of a login identity is not valid");
1136 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1139 // iterate over egos and compare their public key
1140 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1141 handle->ego_entry = handle->ego_entry->next)
1143 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1144 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1147 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1148 handle->idp = GNUNET_RECLAIM_connect (cfg);
1150 GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
1152 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1154 &oidc_iteration_error,
1158 &oidc_attr_collect_finished_cb,
1163 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1171 build_redirect (void *cls)
1173 struct RequestHandle *handle = cls;
1174 struct MHD_Response *resp;
1177 if (GNUNET_YES == handle->oidc->user_cancelled)
1179 if ((NULL != handle->redirect_prefix) &&
1180 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1182 GNUNET_asprintf (&redirect_uri,
1183 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1184 handle->redirect_prefix,
1186 handle->redirect_suffix,
1188 "User denied access",
1189 handle->oidc->state);
1193 GNUNET_asprintf (&redirect_uri,
1194 "%s?error=%s&error_description=%s&state=%s",
1195 handle->oidc->redirect_uri,
1197 "User denied access",
1198 handle->oidc->state);
1200 resp = GNUNET_REST_create_response ("");
1201 MHD_add_response_header (resp, "Location", redirect_uri);
1202 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1203 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1204 GNUNET_free (redirect_uri);
1207 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1212 lookup_redirect_uri_result (void *cls,
1214 const struct GNUNET_GNSRECORD_Data *rd)
1216 struct RequestHandle *handle = cls;
1220 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1222 handle->gns_op = NULL;
1225 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1227 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1228 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1231 for (int i = 0; i < rd_count; i++)
1233 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1235 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1237 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1238 if (NULL == strstr (tmp, handle->oidc->client_id))
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "Redirect uri %s does not contain client_id %s\n",
1243 handle->oidc->client_id);
1247 pos = strrchr (tmp, (unsigned char) '.');
1250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1251 "Redirect uri %s contains client_id but is malformed\n",
1257 handle->redirect_prefix = GNUNET_strdup (tmp);
1258 tmp_key_str = pos + 1;
1259 pos = strchr (tmp_key_str, (unsigned char) '/');
1262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1263 "Redirect uri %s contains client_id but is malformed\n",
1269 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1271 GNUNET_STRINGS_string_to_data (tmp_key_str,
1272 strlen (tmp_key_str),
1274 sizeof(redirect_zone));
1276 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1280 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1282 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1283 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1288 * Initiate redirect back to client.
1291 client_redirect (void *cls)
1293 struct RequestHandle *handle = cls;
1295 /* Lookup client redirect uri to verify request */
1297 GNUNET_GNS_lookup (handle->gns_handle,
1298 GNUNET_GNS_EMPTY_LABEL_AT,
1299 &handle->oidc->client_pkey,
1300 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1301 GNUNET_GNS_LO_DEFAULT,
1302 &lookup_redirect_uri_result,
1308 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1310 struct GNUNET_HashCode hc;
1313 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1314 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1319 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1322 return GNUNET_strdup (value);
1327 * Iteration over all results finished, build final
1330 * @param cls the `struct RequestHandle`
1333 build_authz_response (void *cls)
1335 struct RequestHandle *handle = cls;
1336 struct GNUNET_HashCode cache_key;
1338 char *expected_scope;
1339 char delimiter[] = " ";
1340 int number_of_ignored_parameter, iterator;
1343 // REQUIRED value: redirect_uri
1344 handle->oidc->redirect_uri =
1345 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1346 if (NULL == handle->oidc->redirect_uri)
1348 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1349 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1350 GNUNET_SCHEDULER_add_now (&do_error, handle);
1354 // REQUIRED value: response_type
1355 handle->oidc->response_type =
1356 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1357 if (NULL == handle->oidc->response_type)
1359 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1360 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1361 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1365 // REQUIRED value: scope
1366 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1367 if (NULL == handle->oidc->scope)
1369 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1370 handle->edesc = GNUNET_strdup ("missing parameter scope");
1371 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1375 // OPTIONAL value: nonce
1376 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1378 // OPTIONAL value: claims
1379 handle->oidc->claims = get_url_parameter_copy (handle, OIDC_CLAIMS_KEY);
1381 // TODO check other values if needed
1382 number_of_ignored_parameter =
1383 sizeof(OIDC_ignored_parameter_array) / sizeof(char *);
1384 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1386 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1387 strlen (OIDC_ignored_parameter_array[iterator]),
1390 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1394 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1395 GNUNET_asprintf (&handle->edesc,
1396 "Server will not handle parameter: %s",
1397 OIDC_ignored_parameter_array[iterator]);
1398 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1403 // We only support authorization code flows.
1404 if (0 != strcmp (handle->oidc->response_type,
1405 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1407 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1408 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1409 "obtaining this authorization code.");
1410 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1414 // Checks if scope contains 'openid'
1415 expected_scope = GNUNET_strdup (handle->oidc->scope);
1417 test = strtok (expected_scope, delimiter);
1418 while (NULL != test)
1420 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1422 test = strtok (NULL, delimiter);
1426 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1428 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1429 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1430 GNUNET_free (expected_scope);
1434 GNUNET_free (expected_scope);
1435 if ((NULL == handle->oidc->login_identity) &&
1436 (GNUNET_NO == handle->oidc->user_cancelled))
1437 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1439 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1444 * Iterate over tlds in config
1447 tld_iter (void *cls, const char *section, const char *option, const char *value)
1449 struct RequestHandle *handle = cls;
1450 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1453 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1458 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1459 handle->tld = GNUNET_strdup (option + 1);
1464 * Responds to authorization GET and url-encoded POST request
1466 * @param con_handle the connection handle
1467 * @param url the url
1468 * @param cls the RequestHandle
1471 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1475 struct RequestHandle *handle = cls;
1476 struct EgoEntry *tmp_ego;
1477 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1478 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1480 cookie_identity_interpretation (handle);
1482 // RECOMMENDED value: state - REQUIRED for answers
1483 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1485 // REQUIRED value: client_id
1486 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1487 if (NULL == handle->oidc->client_id)
1489 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1490 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1491 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1492 GNUNET_SCHEDULER_add_now (&do_error, handle);
1496 // OPTIONAL value: code_challenge
1497 handle->oidc->code_challenge = get_url_parameter_copy (handle,
1498 OIDC_CODE_CHALLENGE_KEY);
1499 if (NULL == handle->oidc->code_challenge)
1501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1502 "OAuth authorization request does not contain PKCE parameters!\n");
1506 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1508 handle->oidc->client_id),
1509 &handle->oidc->client_pkey))
1511 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1512 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1513 "authorization code using this method.");
1514 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1515 GNUNET_SCHEDULER_add_now (&do_error, handle);
1519 // If we know this identity, translated the corresponding TLD
1520 // TODO: We might want to have a reverse lookup functionality for TLDs?
1521 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1523 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1524 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1525 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1527 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1528 handle->ego_entry = handle->ego_tail;
1531 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scope: %s\n",GNUNET_strdup (
1533 handle->oidc->scope));
1534 if (NULL == handle->tld)
1535 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1536 if (NULL == handle->tld)
1537 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1538 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1543 * Combines an identity with a login time and responds OK to login request
1545 * @param con_handle the connection handle
1546 * @param url the url
1547 * @param cls the RequestHandle
1550 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1554 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1555 struct RequestHandle *handle = cls;
1556 struct GNUNET_HashCode cache_key;
1557 struct GNUNET_TIME_Absolute *current_time;
1558 struct GNUNET_TIME_Absolute *last_time;
1564 char term_data[handle->rest_handle->data_size + 1];
1566 term_data[handle->rest_handle->data_size] = '\0';
1567 GNUNET_memcpy (term_data,
1568 handle->rest_handle->data,
1569 handle->rest_handle->data_size);
1570 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1571 identity = json_object_get (root, "identity");
1572 if (! json_is_string (identity))
1574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1575 "Error parsing json string from %s\n",
1577 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1579 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1582 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1583 GNUNET_asprintf (&header_val,
1586 OIDC_COOKIE_EXPIRATION);
1587 MHD_add_response_header (resp, "Set-Cookie", header_val);
1588 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1589 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1591 if (0 != strcmp (json_string_value (identity), "Denied"))
1593 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1594 *current_time = GNUNET_TIME_relative_to_absolute (
1595 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1596 OIDC_COOKIE_EXPIRATION));
1598 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1599 GNUNET_free_non_null (last_time);
1600 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1603 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1605 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1606 GNUNET_free (cookie);
1607 GNUNET_free (header_val);
1609 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1614 check_authorization (struct RequestHandle *handle,
1615 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1617 struct GNUNET_HashCode cache_key;
1618 char *authorization;
1620 char *basic_authorization;
1623 char *expected_pass;
1625 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1626 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1628 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1632 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1633 handle->edesc = GNUNET_strdup ("missing authorization");
1634 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1635 return GNUNET_SYSERR;
1638 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1641 // split header in "Basic" and [content]
1642 credentials = strtok (authorization, " ");
1643 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1645 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1646 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1647 return GNUNET_SYSERR;
1649 credentials = strtok (NULL, " ");
1650 if (NULL == credentials)
1652 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1653 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1654 return GNUNET_SYSERR;
1656 GNUNET_STRINGS_base64_decode (credentials,
1657 strlen (credentials),
1658 (void **) &basic_authorization);
1660 if (NULL == basic_authorization)
1662 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1663 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1664 return GNUNET_SYSERR;
1666 client_id = strtok (basic_authorization, ":");
1667 if (NULL == client_id)
1669 GNUNET_free_non_null (basic_authorization);
1670 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1671 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1672 return GNUNET_SYSERR;
1674 pass = strtok (NULL, ":");
1677 GNUNET_free_non_null (basic_authorization);
1678 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1679 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1680 return GNUNET_SYSERR;
1683 // check client password
1684 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1685 "reclaim-rest-plugin",
1686 "OIDC_CLIENT_SECRET",
1689 if (0 != strcmp (expected_pass, pass))
1691 GNUNET_free_non_null (basic_authorization);
1692 GNUNET_free (expected_pass);
1693 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1694 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1695 return GNUNET_SYSERR;
1697 GNUNET_free (expected_pass);
1701 GNUNET_free_non_null (basic_authorization);
1702 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1703 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1704 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1705 return GNUNET_SYSERR;
1709 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1710 handle->ego_entry = handle->ego_entry->next)
1712 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1715 if (NULL == handle->ego_entry)
1717 GNUNET_free_non_null (basic_authorization);
1718 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1719 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1720 return GNUNET_SYSERR;
1722 GNUNET_STRINGS_string_to_data (client_id,
1725 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
1727 GNUNET_free (basic_authorization);
1732 const struct EgoEntry *
1733 find_ego (struct RequestHandle *handle,
1734 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1736 struct EgoEntry *ego_entry;
1737 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1739 for (ego_entry = handle->ego_head; NULL != ego_entry;
1740 ego_entry = ego_entry->next)
1742 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1743 if (0 == GNUNET_memcmp (&pub_key, test_key))
1751 persist_access_token (const struct RequestHandle *handle,
1752 const char *access_token,
1753 const struct GNUNET_RECLAIM_Ticket *ticket)
1755 struct GNUNET_HashCode hc;
1756 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1758 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1759 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1760 *ticketbuf = *ticket;
1761 GNUNET_assert (GNUNET_SYSERR !=
1762 GNUNET_CONTAINER_multihashmap_put (
1763 OIDC_access_token_map,
1766 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1771 * Responds to token url-encoded POST request
1773 * @param con_handle the connection handle
1774 * @param url the url
1775 * @param cls the RequestHandle
1778 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1782 struct RequestHandle *handle = cls;
1783 const struct EgoEntry *ego_entry;
1784 struct GNUNET_TIME_Relative expiration_time;
1785 struct GNUNET_RECLAIM_AttributeList *cl;
1786 struct GNUNET_RECLAIM_AttestationList *al;
1787 struct GNUNET_RECLAIM_Ticket ticket;
1788 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1789 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1790 struct GNUNET_HashCode cache_key;
1791 struct MHD_Response *resp;
1794 char *json_response;
1799 char *code_verifier;
1802 * Check Authorization
1804 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1807 "OIDC authorization for token endpoint failed\n");
1808 GNUNET_SCHEDULER_add_now (&do_error, handle);
1816 // TODO Do not allow multiple equal parameter names
1817 // REQUIRED grant_type
1818 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1819 strlen (OIDC_GRANT_TYPE_KEY),
1821 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1822 if (NULL == grant_type)
1824 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1825 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1826 handle->response_code = MHD_HTTP_BAD_REQUEST;
1827 GNUNET_SCHEDULER_add_now (&do_error, handle);
1831 // Check parameter grant_type == "authorization_code"
1832 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1834 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1835 handle->response_code = MHD_HTTP_BAD_REQUEST;
1836 GNUNET_free (grant_type);
1837 GNUNET_SCHEDULER_add_now (&do_error, handle);
1840 GNUNET_free (grant_type);
1842 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1845 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1846 handle->edesc = GNUNET_strdup ("missing parameter code");
1847 handle->response_code = MHD_HTTP_BAD_REQUEST;
1848 GNUNET_SCHEDULER_add_now (&do_error, handle);
1851 ego_entry = find_ego (handle, &cid);
1852 if (NULL == ego_entry)
1854 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1855 handle->edesc = GNUNET_strdup ("Unknown client");
1856 handle->response_code = MHD_HTTP_BAD_REQUEST;
1858 GNUNET_SCHEDULER_add_now (&do_error, handle);
1861 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1863 // REQUIRED code verifier
1864 code_verifier = get_url_parameter_copy (handle, OIDC_CODE_VERIFIER_KEY);
1865 if (NULL == code_verifier)
1867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1868 "OAuth authorization request does not contain PKCE parameters!\n");
1873 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, code_verifier, &ticket,
1876 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1877 handle->edesc = GNUNET_strdup ("invalid code");
1878 handle->response_code = MHD_HTTP_BAD_REQUEST;
1880 GNUNET_SCHEDULER_add_now (&do_error, handle);
1886 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1887 "reclaim-rest-plugin",
1891 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1892 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1893 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1894 GNUNET_SCHEDULER_add_now (&do_error, handle);
1899 // TODO OPTIONAL acr,amr,azp
1900 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1901 "reclaim-rest-plugin",
1905 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1906 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1907 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1908 GNUNET_SCHEDULER_add_now (&do_error, handle);
1911 id_token = OIDC_id_token_new (&ticket.audience,
1916 (NULL != nonce) ? nonce : NULL,
1918 access_token = OIDC_access_token_new ();
1919 OIDC_build_token_response (access_token,
1924 persist_access_token (handle, access_token, &ticket);
1925 resp = GNUNET_REST_create_response (json_response);
1926 MHD_add_response_header (resp, "Cache-Control", "no-store");
1927 MHD_add_response_header (resp, "Pragma", "no-cache");
1928 MHD_add_response_header (resp, "Content-Type", "application/json");
1929 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1930 GNUNET_RECLAIM_attribute_list_destroy (cl);
1931 GNUNET_RECLAIM_attestation_list_destroy (al);
1932 GNUNET_free (access_token);
1933 GNUNET_free (json_response);
1934 GNUNET_free (id_token);
1935 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1940 * Collects claims and stores them in handle
1943 consume_ticket (void *cls,
1944 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1945 const struct GNUNET_RECLAIM_Attribute *attr,
1946 const struct GNUNET_RECLAIM_Attestation *attest)
1948 struct RequestHandle *handle = cls;
1949 if (NULL == identity)
1951 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1954 if (GNUNET_YES == GNUNET_RECLAIM_id_is_zero (&attr->attestation))
1958 tmp_value = GNUNET_RECLAIM_attribute_value_to_string (attr->type,
1961 value = json_string (tmp_value);
1962 json_object_set_new (handle->oidc->response, attr->name, value);
1963 GNUNET_free (tmp_value);
1966 json_t *claim_sources;
1967 json_t *claim_sources_jwt;
1968 json_t *claim_names;
1969 char *attest_val_str;
1970 claim_sources = json_object_get (handle->oidc->response,"_claim_sources");
1971 claim_names = json_object_get (handle->oidc->response,"_claim_names");
1973 GNUNET_RECLAIM_attestation_value_to_string (attest->type,
1976 if ((NULL == claim_sources) && (NULL == claim_names) )
1978 claim_sources = json_object ();
1979 claim_names = json_object ();
1983 GNUNET_asprintf (&source_name, "src%d", i);
1984 while (NULL != (claim_sources_jwt = json_object_get (claim_sources,
1987 if (0 == strcmp (json_string_value (json_object_get (claim_sources_jwt,
1991 // Adapt only the claim names
1992 json_object_set_new (claim_names, attr->data,
1993 json_string (source_name));
1994 json_object_set (handle->oidc->response,
1995 "_claim_names", claim_names);
1999 GNUNET_free (source_name);
2000 GNUNET_asprintf (&source_name, "src%d", i);
2004 if (NULL == claim_sources_jwt)
2006 claim_sources_jwt = json_object ();
2007 // Set the JWT for names
2008 json_object_set_new (claim_names, attr->data,
2009 json_string (source_name));
2010 // Set the JWT for the inner source
2011 json_object_set_new (claim_sources_jwt, "JWT",
2012 json_string (attest_val_str));
2013 // Set the JWT for the source
2014 json_object_set_new (claim_sources, source_name, claim_sources_jwt);
2016 json_object_set (handle->oidc->response, "_claim_names", claim_names);
2017 json_object_set (handle->oidc->response, "_claim_sources",claim_sources);
2020 json_decref (claim_sources);
2021 json_decref (claim_names);
2022 json_decref (claim_sources_jwt);
2023 GNUNET_free (attest_val_str);
2028 * Responds to userinfo GET and url-encoded POST request
2030 * @param con_handle the connection handle
2031 * @param url the url
2032 * @param cls the RequestHandle
2035 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
2039 // TODO expiration time
2040 struct RequestHandle *handle = cls;
2041 char delimiter[] = " ";
2042 struct GNUNET_HashCode cache_key;
2043 char *authorization;
2044 char *authorization_type;
2045 char *authorization_access_token;
2046 struct GNUNET_RECLAIM_Ticket *ticket;
2047 const struct EgoEntry *ego_entry;
2048 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
2050 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
2051 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
2053 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
2057 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2058 handle->edesc = GNUNET_strdup ("No Access Token");
2059 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2060 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2064 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
2067 // split header in "Bearer" and access_token
2068 authorization = GNUNET_strdup (authorization);
2069 authorization_type = strtok (authorization, delimiter);
2070 if ((NULL == authorization_type) ||
2071 (0 != strcmp ("Bearer", authorization_type)))
2073 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2074 handle->edesc = GNUNET_strdup ("No Access Token");
2075 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2076 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2077 GNUNET_free (authorization);
2080 authorization_access_token = strtok (NULL, delimiter);
2081 if (NULL == authorization_access_token)
2083 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2084 handle->edesc = GNUNET_strdup ("Access token missing");
2085 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2086 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2087 GNUNET_free (authorization);
2091 GNUNET_CRYPTO_hash (authorization_access_token,
2092 strlen (authorization_access_token),
2095 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
2098 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2099 handle->edesc = GNUNET_strdup ("The access token expired");
2100 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2101 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2102 GNUNET_free (authorization);
2106 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
2107 GNUNET_assert (NULL != ticket);
2108 ego_entry = find_ego (handle, &ticket->audience);
2109 if (NULL == ego_entry)
2111 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
2112 handle->edesc = GNUNET_strdup ("The access token expired");
2113 handle->response_code = MHD_HTTP_UNAUTHORIZED;
2114 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
2115 GNUNET_free (authorization);
2119 handle->idp = GNUNET_RECLAIM_connect (cfg);
2120 handle->oidc->response = json_object ();
2121 json_object_set_new (handle->oidc->response,
2123 json_string (ego_entry->keystring));
2124 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
2125 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
2130 GNUNET_free (authorization);
2135 * Handle rest request
2137 * @param handle the request handle
2140 init_cont (struct RequestHandle *handle)
2142 struct GNUNET_REST_RequestHandlerError err;
2143 static const struct GNUNET_REST_RequestHandler handlers[] =
2144 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint },
2145 { MHD_HTTP_METHOD_POST,
2146 GNUNET_REST_API_NS_AUTHORIZE,
2147 &authorize_endpoint }, // url-encoded
2148 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont },
2149 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint },
2150 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2151 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2152 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont },
2153 GNUNET_REST_HANDLER_END };
2156 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
2158 handle->response_code = err.error_code;
2159 GNUNET_SCHEDULER_add_now (&do_error, handle);
2165 * If listing is enabled, prints information about the egos.
2167 * This function is initially called for all egos and then again
2168 * whenever a ego's identifier changes or if it is deleted. At the
2169 * end of the initial pass over all egos, the function is once called
2170 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
2171 * be invoked in the future or that there was an error.
2173 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
2174 * this function is only called ONCE, and 'NULL' being passed in
2175 * 'ego' does indicate an error (i.e. name is taken or no default
2176 * value is known). If 'ego' is non-NULL and if '*ctx'
2177 * is set in those callbacks, the value WILL be passed to a subsequent
2178 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
2179 * that one was not NULL).
2181 * When an identity is renamed, this function is called with the
2182 * (known) ego but the NEW identifier.
2184 * When an identity is deleted, this function is called with the
2185 * (known) ego and "NULL" for the 'identifier'. In this case,
2186 * the 'ego' is henceforth invalid (and the 'ctx' should also be
2189 * @param cls closure
2190 * @param ego ego handle
2191 * @param ctx context for application to store data for this ego
2192 * (during the lifetime of this process, initially NULL)
2193 * @param identifier identifier assigned by the user for this ego,
2194 * NULL if the user just deleted the ego and it
2195 * must thus no longer be used
2198 list_ego (void *cls,
2199 struct GNUNET_IDENTITY_Ego *ego,
2201 const char *identifier)
2203 struct RequestHandle *handle = cls;
2204 struct EgoEntry *ego_entry;
2205 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
2207 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2209 handle->state = ID_REST_STATE_POST_INIT;
2213 GNUNET_assert (NULL != ego);
2214 if (ID_REST_STATE_INIT == handle->state)
2217 ego_entry = GNUNET_new (struct EgoEntry);
2218 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2219 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2220 ego_entry->ego = ego;
2221 ego_entry->identifier = GNUNET_strdup (identifier);
2222 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2227 /* Ego renamed or added */
2228 if (identifier != NULL)
2230 for (ego_entry = handle->ego_head; NULL != ego_entry;
2231 ego_entry = ego_entry->next)
2233 if (ego_entry->ego == ego)
2236 GNUNET_free (ego_entry->identifier);
2237 ego_entry->identifier = GNUNET_strdup (identifier);
2241 if (NULL == ego_entry)
2244 ego_entry = GNUNET_new (struct EgoEntry);
2245 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2246 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2247 ego_entry->ego = ego;
2248 ego_entry->identifier = GNUNET_strdup (identifier);
2249 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2257 for (ego_entry = handle->ego_head; NULL != ego_entry;
2258 ego_entry = ego_entry->next)
2260 if (ego_entry->ego == ego)
2263 if (NULL != ego_entry)
2264 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2272 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2273 GNUNET_REST_ResultProcessor proc,
2276 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2278 handle->oidc = GNUNET_new (struct OIDC_Variables);
2279 if (NULL == OIDC_cookie_jar_map)
2280 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2281 if (NULL == OIDC_access_token_map)
2282 OIDC_access_token_map =
2283 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2284 handle->response_code = 0;
2285 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2286 handle->proc_cls = proc_cls;
2287 handle->proc = proc;
2288 handle->state = ID_REST_STATE_INIT;
2289 handle->rest_handle = rest_handle;
2291 handle->url = GNUNET_strdup (rest_handle->url);
2292 if (handle->url[strlen (handle->url) - 1] == '/')
2293 handle->url[strlen (handle->url) - 1] = '\0';
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2295 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2296 handle->gns_handle = GNUNET_GNS_connect (cfg);
2297 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2298 handle->timeout_task =
2299 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2305 * Entry point for the plugin.
2307 * @param cls Config info
2308 * @return NULL on error, otherwise the plugin context
2311 libgnunet_plugin_rest_openid_connect_init (void *cls)
2313 static struct Plugin plugin;
2314 struct GNUNET_REST_Plugin *api;
2317 if (NULL != plugin.cfg)
2318 return NULL; /* can only initialize once! */
2319 memset (&plugin, 0, sizeof(struct Plugin));
2321 api = GNUNET_new (struct GNUNET_REST_Plugin);
2323 api->name = GNUNET_REST_API_NS_OIDC;
2324 api->process_request = &rest_identity_process_request;
2325 GNUNET_asprintf (&allow_methods,
2326 "%s, %s, %s, %s, %s",
2327 MHD_HTTP_METHOD_GET,
2328 MHD_HTTP_METHOD_POST,
2329 MHD_HTTP_METHOD_PUT,
2330 MHD_HTTP_METHOD_DELETE,
2331 MHD_HTTP_METHOD_OPTIONS);
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 _ ("OpenID Connect REST API initialized\n"));
2340 * Exit point from the plugin.
2342 * @param cls the plugin context (as returned by "init")
2343 * @return always NULL
2346 libgnunet_plugin_rest_openid_connect_done (void *cls)
2348 struct GNUNET_REST_Plugin *api = cls;
2349 struct Plugin *plugin = api->cls;
2353 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2356 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2357 while (GNUNET_YES ==
2358 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2359 GNUNET_free_non_null (value);
2360 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2361 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2364 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2365 while (GNUNET_YES ==
2366 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2367 GNUNET_free_non_null (value);
2368 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2369 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2370 GNUNET_free_non_null (allow_methods);
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 "OpenID Connect REST plugin is finished\n");
2378 /* end of plugin_rest_openid_connect.c */