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"
123 * OIDC PKCE code challenge
125 #define OIDC_CODE_CHALLENGE_KEY "code_challenge"
128 * OIDC PKCE code verifier
130 #define OIDC_CODE_VERIFIER_KEY "code_verifier"
133 * OIDC cookie expiration (in seconds)
135 #define OIDC_COOKIE_EXPIRATION 3
138 * OIDC cookie header key
140 #define OIDC_COOKIE_HEADER_KEY "cookie"
143 * OIDC cookie header information key
145 #define OIDC_AUTHORIZATION_HEADER_KEY "authorization"
148 * OIDC cookie header information key
150 #define OIDC_COOKIE_HEADER_INFORMATION_KEY "Identity="
153 * OIDC cookie header if user cancelled
155 #define OIDC_COOKIE_HEADER_ACCESS_DENIED "Identity=Denied"
158 * OIDC expected response_type while authorizing
160 #define OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE "code"
163 * OIDC expected scope part while authorizing
165 #define OIDC_EXPECTED_AUTHORIZATION_SCOPE "openid"
168 * OIDC error key for invalid client
170 #define OIDC_ERROR_KEY_INVALID_CLIENT "invalid_client"
173 * OIDC error key for invalid scopes
175 #define OIDC_ERROR_KEY_INVALID_SCOPE "invalid_scope"
178 * OIDC error key for invalid requests
180 #define OIDC_ERROR_KEY_INVALID_REQUEST "invalid_request"
183 * OIDC error key for invalid tokens
185 #define OIDC_ERROR_KEY_INVALID_TOKEN "invalid_token"
188 * OIDC error key for invalid cookies
190 #define OIDC_ERROR_KEY_INVALID_COOKIE "invalid_cookie"
193 * OIDC error key for generic server errors
195 #define OIDC_ERROR_KEY_SERVER_ERROR "server_error"
198 * OIDC error key for unsupported grants
200 #define OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE "unsupported_grant_type"
203 * OIDC error key for unsupported response types
205 #define OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE "unsupported_response_type"
208 * OIDC error key for unauthorized clients
210 #define OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT "unauthorized_client"
213 * OIDC error key for denied access
215 #define OIDC_ERROR_KEY_ACCESS_DENIED "access_denied"
219 * OIDC ignored parameter array
221 static char *OIDC_ignored_parameter_array[] = { "display",
230 * OIDC Hash map that keeps track of issued cookies
232 struct GNUNET_CONTAINER_MultiHashMap *OIDC_cookie_jar_map;
235 * Hash map that links the issued access token to the corresponding ticket and
238 struct GNUNET_CONTAINER_MultiHashMap *OIDC_access_token_map;
241 * The configuration handle
243 const struct GNUNET_CONFIGURATION_Handle *cfg;
246 * HTTP methods allows for this plugin
248 static char *allow_methods;
251 * @brief struct returned by the initialization function of the plugin
255 const struct GNUNET_CONFIGURATION_Handle *cfg;
259 * OIDC needed variables
261 struct OIDC_Variables
264 * The RP client public key
266 struct GNUNET_CRYPTO_EcdsaPublicKey client_pkey;
269 * The OIDC client id of the RP
274 * The OIDC redirect uri
279 * The list of oidc scopes
294 * The OIDC response type
299 * The identity chosen by the user to login
301 char *login_identity;
304 * User cancelled authorization/login
309 * The PKCE code_challenge
311 char *code_challenge;
314 * The PKCE code_verifier
332 struct EgoEntry *next;
337 struct EgoEntry *prev;
352 struct GNUNET_IDENTITY_Ego *ego;
361 struct EgoEntry *ego_head;
366 struct EgoEntry *ego_tail;
371 struct EgoEntry *ego_entry;
374 * Pointer to ego private key
376 struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key;
381 struct OIDC_Variables *oidc;
384 * The processing state
389 * Handle to Identity service.
391 struct GNUNET_IDENTITY_Handle *identity_handle;
396 struct GNUNET_REST_RequestHandle *rest_handle;
401 struct GNUNET_GNS_Handle *gns_handle;
406 struct GNUNET_GNS_LookupRequest *gns_op;
409 * Handle to NAMESTORE
411 struct GNUNET_NAMESTORE_Handle *namestore_handle;
414 * Iterator for NAMESTORE
416 struct GNUNET_NAMESTORE_ZoneIterator *namestore_handle_it;
419 * Attribute claim list
421 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list;
426 struct GNUNET_IDENTITY_Operation *op;
431 struct GNUNET_RECLAIM_Handle *idp;
436 struct GNUNET_RECLAIM_Operation *idp_op;
441 struct GNUNET_RECLAIM_AttributeIterator *attr_it;
446 struct GNUNET_RECLAIM_TicketIterator *ticket_it;
451 struct GNUNET_RECLAIM_Ticket ticket;
454 * Desired timeout for the lookup (default is no timeout).
456 struct GNUNET_TIME_Relative timeout;
459 * ID of a task associated with the resolution process.
461 struct GNUNET_SCHEDULER_Task *timeout_task;
464 * The plugin result processor
466 GNUNET_REST_ResultProcessor proc;
469 * The closure of the result processor
479 * The tld for redirect
484 * The redirect prefix
486 char *redirect_prefix;
489 * The redirect suffix
491 char *redirect_suffix;
494 * Error response message
499 * Error response description
510 * Cleanup lookup handle
511 * @param handle Handle to clean up
514 cleanup_handle (struct RequestHandle *handle)
516 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_entry;
517 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_tmp;
518 struct EgoEntry *ego_entry;
519 struct EgoEntry *ego_tmp;
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
522 if (NULL != handle->timeout_task)
523 GNUNET_SCHEDULER_cancel (handle->timeout_task);
524 if (NULL != handle->identity_handle)
525 GNUNET_IDENTITY_disconnect (handle->identity_handle);
526 if (NULL != handle->attr_it)
527 GNUNET_RECLAIM_get_attributes_stop (handle->attr_it);
528 if (NULL != handle->ticket_it)
529 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
530 if (NULL != handle->idp)
531 GNUNET_RECLAIM_disconnect (handle->idp);
532 GNUNET_free_non_null (handle->url);
533 GNUNET_free_non_null (handle->tld);
534 GNUNET_free_non_null (handle->redirect_prefix);
535 GNUNET_free_non_null (handle->redirect_suffix);
536 GNUNET_free_non_null (handle->emsg);
537 GNUNET_free_non_null (handle->edesc);
538 if (NULL != handle->gns_op)
539 GNUNET_GNS_lookup_cancel (handle->gns_op);
540 if (NULL != handle->gns_handle)
541 GNUNET_GNS_disconnect (handle->gns_handle);
543 if (NULL != handle->namestore_handle)
544 GNUNET_NAMESTORE_disconnect (handle->namestore_handle);
545 if (NULL != handle->oidc)
547 GNUNET_free_non_null (handle->oidc->client_id);
548 GNUNET_free_non_null (handle->oidc->login_identity);
549 GNUNET_free_non_null (handle->oidc->nonce);
550 GNUNET_free_non_null (handle->oidc->redirect_uri);
551 GNUNET_free_non_null (handle->oidc->response_type);
552 GNUNET_free_non_null (handle->oidc->scope);
553 GNUNET_free_non_null (handle->oidc->state);
554 json_decref (handle->oidc->response);
555 GNUNET_free (handle->oidc);
557 if (NULL != handle->attr_list)
559 for (claim_entry = handle->attr_list->list_head; NULL != claim_entry;)
561 claim_tmp = claim_entry;
562 claim_entry = claim_entry->next;
563 GNUNET_free (claim_tmp->claim);
564 GNUNET_free (claim_tmp);
566 GNUNET_free (handle->attr_list);
568 for (ego_entry = handle->ego_head; NULL != ego_entry;)
571 ego_entry = ego_entry->next;
572 GNUNET_free (ego_tmp->identifier);
573 GNUNET_free (ego_tmp->keystring);
574 GNUNET_free (ego_tmp);
576 GNUNET_free (handle);
581 cleanup_handle_delayed (void *cls)
583 cleanup_handle (cls);
588 * Task run on error, sends error message. Cleans up everything.
590 * @param cls the `struct RequestHandle`
595 struct RequestHandle *handle = cls;
596 struct MHD_Response *resp;
599 GNUNET_asprintf (&json_error,
600 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
602 (NULL != handle->edesc) ? handle->edesc : "",
603 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
604 (NULL != handle->oidc->state) ? handle->oidc->state : "",
605 (NULL != handle->oidc->state) ? "\"" : "");
606 if (0 == handle->response_code)
607 handle->response_code = MHD_HTTP_BAD_REQUEST;
608 resp = GNUNET_REST_create_response (json_error);
609 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
610 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
611 MHD_add_response_header (resp,
612 MHD_HTTP_HEADER_CONTENT_TYPE,
614 handle->proc (handle->proc_cls, resp, handle->response_code);
615 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
616 GNUNET_free (json_error);
621 * Task run on error in userinfo endpoint, sends error header. Cleans up
624 * @param cls the `struct RequestHandle`
627 do_userinfo_error (void *cls)
629 struct RequestHandle *handle = cls;
630 struct MHD_Response *resp;
633 GNUNET_asprintf (&error,
634 "error=\"%s\", error_description=\"%s\"",
636 (NULL != handle->edesc) ? handle->edesc : "");
637 resp = GNUNET_REST_create_response ("");
638 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
639 handle->proc (handle->proc_cls, resp, handle->response_code);
640 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
646 * Task run on error, sends error message and redirects. Cleans up everything.
648 * @param cls the `struct RequestHandle`
651 do_redirect_error (void *cls)
653 struct RequestHandle *handle = cls;
654 struct MHD_Response *resp;
657 GNUNET_asprintf (&redirect,
658 "%s?error=%s&error_description=%s%s%s",
659 handle->oidc->redirect_uri,
662 (NULL != handle->oidc->state) ? "&state=" : "",
663 (NULL != handle->oidc->state) ? handle->oidc->state : "");
664 resp = GNUNET_REST_create_response ("");
665 MHD_add_response_header (resp, "Location", redirect);
666 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
667 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
668 GNUNET_free (redirect);
673 * Task run on timeout, sends error message. Cleans up everything.
675 * @param cls the `struct RequestHandle`
678 do_timeout (void *cls)
680 struct RequestHandle *handle = cls;
682 handle->timeout_task = NULL;
688 * Return attributes for claim
690 * @param cls the request handle
693 return_userinfo_response (void *cls)
696 struct RequestHandle *handle = cls;
697 struct MHD_Response *resp;
699 result_str = json_dumps (handle->oidc->response, 0);
701 resp = GNUNET_REST_create_response (result_str);
702 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
703 GNUNET_free (result_str);
704 cleanup_handle (handle);
709 * Respond to OPTIONS request
711 * @param con_handle the connection handle
713 * @param cls the RequestHandle
716 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
720 struct MHD_Response *resp;
721 struct RequestHandle *handle = cls;
723 // For now, independent of path return all options
724 resp = GNUNET_REST_create_response (NULL);
725 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
726 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
727 cleanup_handle (handle);
733 * Interprets cookie header and pass its identity keystring to handle
736 cookie_identity_interpretation (struct RequestHandle *handle)
738 struct GNUNET_HashCode cache_key;
740 struct GNUNET_TIME_Absolute current_time, *relog_time;
741 char delimiter[] = "; ";
746 // gets identity of login try with cookie
747 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
748 strlen (OIDC_COOKIE_HEADER_KEY),
750 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
757 // splits cookies and find 'Identity' cookie
759 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
761 cookies = GNUNET_strdup (tmp_cookies);
762 token = strtok (cookies, delimiter);
763 handle->oidc->user_cancelled = GNUNET_NO;
764 handle->oidc->login_identity = NULL;
767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768 "Unable to parse cookie: %s\n",
770 GNUNET_free (cookies);
774 while (NULL != token)
776 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
778 handle->oidc->user_cancelled = GNUNET_YES;
779 GNUNET_free (cookies);
782 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
784 token = strtok (NULL, delimiter);
788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
789 "No cookie value to process: %s\n",
791 GNUNET_free (cookies);
794 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
796 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
799 GNUNET_ERROR_TYPE_WARNING,
800 "Found cookie `%s', but no corresponding expiration entry present...\n",
802 GNUNET_free (cookies);
806 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
807 current_time = GNUNET_TIME_absolute_get ();
808 // 30 min after old login -> redirect to login
809 if (current_time.abs_value_us > relog_time->abs_value_us)
811 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
812 "Found cookie `%s', but it is expired.\n",
814 GNUNET_free (cookies);
817 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
818 GNUNET_assert (NULL != value);
819 handle->oidc->login_identity = GNUNET_strdup (value);
820 GNUNET_free (cookies);
825 * Redirects to login page stored in configuration file
828 login_redirect (void *cls)
830 char *login_base_url;
832 struct MHD_Response *resp;
833 struct RequestHandle *handle = cls;
835 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
836 "reclaim-rest-plugin",
840 GNUNET_asprintf (&new_redirect,
841 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
843 OIDC_RESPONSE_TYPE_KEY,
844 handle->oidc->response_type,
846 handle->oidc->client_id,
847 OIDC_REDIRECT_URI_KEY,
848 handle->oidc->redirect_uri,
852 (NULL != handle->oidc->state) ? handle->oidc->state : "",
853 OIDC_CODE_CHALLENGE_KEY,
854 (NULL != handle->oidc->code_challenge) ?
855 handle->oidc->code_challenge : "",
857 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "");
858 resp = GNUNET_REST_create_response ("");
859 MHD_add_response_header (resp, "Location", new_redirect);
860 GNUNET_free (login_base_url);
864 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
865 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
866 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
867 GNUNET_SCHEDULER_add_now (&do_error, handle);
870 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
871 GNUNET_free (new_redirect);
872 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
877 * Does internal server error when iteration failed.
880 oidc_iteration_error (void *cls)
882 struct RequestHandle *handle = cls;
884 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
885 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
886 GNUNET_SCHEDULER_add_now (&do_error, handle);
891 * Issues ticket and redirects to relying party with the authorization code as
892 * parameter. Otherwise redirects with error
895 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
897 struct RequestHandle *handle = cls;
898 struct MHD_Response *resp;
903 handle->idp_op = NULL;
906 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
907 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
908 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
911 handle->ticket = *ticket;
913 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
914 sizeof(struct GNUNET_RECLAIM_Ticket));
915 // TODO change if more attributes are needed (see max_age)
916 code_string = OIDC_build_authz_code (&handle->priv_key,
920 handle->oidc->code_challenge);
921 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
922 (NULL != handle->tld))
924 GNUNET_asprintf (&redirect_uri,
925 "%s.%s/%s?%s=%s&state=%s",
926 handle->redirect_prefix,
928 handle->redirect_suffix,
929 handle->oidc->response_type,
931 handle->oidc->state);
935 GNUNET_asprintf (&redirect_uri,
937 handle->oidc->redirect_uri,
938 handle->oidc->response_type,
940 handle->oidc->state);
942 resp = GNUNET_REST_create_response ("");
943 MHD_add_response_header (resp, "Location", redirect_uri);
944 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
945 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
946 GNUNET_free (redirect_uri);
947 GNUNET_free (ticket_str);
948 GNUNET_free (code_string);
953 oidc_collect_finished_cb (void *cls)
955 struct RequestHandle *handle = cls;
957 handle->attr_it = NULL;
958 handle->ticket_it = NULL;
959 if (NULL == handle->attr_list->list_head)
961 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
962 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
963 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
966 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
968 &handle->oidc->client_pkey,
970 &oidc_ticket_issue_cb,
976 * Collects all attributes for an ego if in scope parameter
979 oidc_attr_collect (void *cls,
980 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
981 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
982 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attest,
983 const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *reference)
985 struct RequestHandle *handle = cls;
986 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
987 char *scope_variables;
988 char *scope_variable;
989 char delimiter[] = " ";
991 if ((NULL == attr->name) || (NULL == attr->data))
993 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
997 scope_variables = GNUNET_strdup (handle->oidc->scope);
998 scope_variable = strtok (scope_variables, delimiter);
999 while (NULL != scope_variable)
1001 if (0 == strcmp (attr->name, scope_variable))
1003 scope_variable = strtok (NULL, delimiter);
1005 if (NULL == scope_variable)
1007 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1008 GNUNET_free (scope_variables);
1011 GNUNET_free (scope_variables);
1013 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
1014 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name,
1018 le->claim->id = attr->id;
1019 le->claim->flag = attr->flag;
1020 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
1021 handle->attr_list->list_tail,
1023 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1028 * Checks time and cookie and redirects accordingly
1031 code_redirect (void *cls)
1033 struct RequestHandle *handle = cls;
1034 struct GNUNET_TIME_Absolute current_time;
1035 struct GNUNET_TIME_Absolute *relog_time;
1036 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1037 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1038 struct GNUNET_HashCode cache_key;
1039 char *identity_cookie;
1041 GNUNET_asprintf (&identity_cookie,
1043 handle->oidc->login_identity);
1044 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1045 GNUNET_free (identity_cookie);
1046 // No login time for identity -> redirect to login
1048 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1051 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1052 current_time = GNUNET_TIME_absolute_get ();
1053 // 30 min after old login -> redirect to login
1054 if (current_time.abs_value_us <= relog_time->abs_value_us)
1057 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1064 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1066 GNUNET_strdup ("The cookie of a login identity is not valid");
1067 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1070 // iterate over egos and compare their public key
1071 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1072 handle->ego_entry = handle->ego_entry->next)
1074 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1075 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1078 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1079 handle->idp = GNUNET_RECLAIM_connect (cfg);
1081 GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
1083 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1085 &oidc_iteration_error,
1089 &oidc_collect_finished_cb,
1094 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1102 build_redirect (void *cls)
1104 struct RequestHandle *handle = cls;
1105 struct MHD_Response *resp;
1108 if (GNUNET_YES == handle->oidc->user_cancelled)
1110 if ((NULL != handle->redirect_prefix) &&
1111 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1113 GNUNET_asprintf (&redirect_uri,
1114 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1115 handle->redirect_prefix,
1117 handle->redirect_suffix,
1119 "User denied access",
1120 handle->oidc->state);
1124 GNUNET_asprintf (&redirect_uri,
1125 "%s?error=%s&error_description=%s&state=%s",
1126 handle->oidc->redirect_uri,
1128 "User denied access",
1129 handle->oidc->state);
1131 resp = GNUNET_REST_create_response ("");
1132 MHD_add_response_header (resp, "Location", redirect_uri);
1133 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1134 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1135 GNUNET_free (redirect_uri);
1138 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1143 lookup_redirect_uri_result (void *cls,
1145 const struct GNUNET_GNSRECORD_Data *rd)
1147 struct RequestHandle *handle = cls;
1151 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1153 handle->gns_op = NULL;
1156 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1158 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1159 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1162 for (int i = 0; i < rd_count; i++)
1164 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1166 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1168 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1169 if (NULL == strstr (tmp, handle->oidc->client_id))
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 "Redirect uri %s does not contain client_id %s\n",
1174 handle->oidc->client_id);
1178 pos = strrchr (tmp, (unsigned char) '.');
1181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1182 "Redirect uri %s contains client_id but is malformed\n",
1188 handle->redirect_prefix = GNUNET_strdup (tmp);
1189 tmp_key_str = pos + 1;
1190 pos = strchr (tmp_key_str, (unsigned char) '/');
1193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1194 "Redirect uri %s contains client_id but is malformed\n",
1200 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1202 GNUNET_STRINGS_string_to_data (tmp_key_str,
1203 strlen (tmp_key_str),
1205 sizeof(redirect_zone));
1207 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1211 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1213 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1214 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1219 * Initiate redirect back to client.
1222 client_redirect (void *cls)
1224 struct RequestHandle *handle = cls;
1226 /* Lookup client redirect uri to verify request */
1228 GNUNET_GNS_lookup (handle->gns_handle,
1229 GNUNET_GNS_EMPTY_LABEL_AT,
1230 &handle->oidc->client_pkey,
1231 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1232 GNUNET_GNS_LO_DEFAULT,
1233 &lookup_redirect_uri_result,
1239 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1241 struct GNUNET_HashCode hc;
1244 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1245 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1250 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1253 return GNUNET_strdup (value);
1258 * Iteration over all results finished, build final
1261 * @param cls the `struct RequestHandle`
1264 build_authz_response (void *cls)
1266 struct RequestHandle *handle = cls;
1267 struct GNUNET_HashCode cache_key;
1269 char *expected_scope;
1270 char delimiter[] = " ";
1271 int number_of_ignored_parameter, iterator;
1274 // REQUIRED value: redirect_uri
1275 handle->oidc->redirect_uri =
1276 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1277 if (NULL == handle->oidc->redirect_uri)
1279 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1280 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1281 GNUNET_SCHEDULER_add_now (&do_error, handle);
1285 // REQUIRED value: response_type
1286 handle->oidc->response_type =
1287 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1288 if (NULL == handle->oidc->response_type)
1290 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1291 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1292 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1296 // REQUIRED value: scope
1297 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1298 if (NULL == handle->oidc->scope)
1300 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1301 handle->edesc = GNUNET_strdup ("missing parameter scope");
1302 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1306 // OPTIONAL value: nonce
1307 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1309 // TODO check other values if needed
1310 number_of_ignored_parameter =
1311 sizeof(OIDC_ignored_parameter_array) / sizeof(char *);
1312 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1314 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1315 strlen (OIDC_ignored_parameter_array[iterator]),
1318 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1322 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1323 GNUNET_asprintf (&handle->edesc,
1324 "Server will not handle parameter: %s",
1325 OIDC_ignored_parameter_array[iterator]);
1326 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1331 // We only support authorization code flows.
1332 if (0 != strcmp (handle->oidc->response_type,
1333 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1335 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1336 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1337 "obtaining this authorization code.");
1338 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1342 // Checks if scope contains 'openid'
1343 expected_scope = GNUNET_strdup (handle->oidc->scope);
1345 test = strtok (expected_scope, delimiter);
1346 while (NULL != test)
1348 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1350 test = strtok (NULL, delimiter);
1354 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1356 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1357 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1358 GNUNET_free (expected_scope);
1362 GNUNET_free (expected_scope);
1363 if ((NULL == handle->oidc->login_identity) &&
1364 (GNUNET_NO == handle->oidc->user_cancelled))
1365 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1367 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1372 * Iterate over tlds in config
1375 tld_iter (void *cls, const char *section, const char *option, const char *value)
1377 struct RequestHandle *handle = cls;
1378 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1381 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1386 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1387 handle->tld = GNUNET_strdup (option + 1);
1392 * Responds to authorization GET and url-encoded POST request
1394 * @param con_handle the connection handle
1395 * @param url the url
1396 * @param cls the RequestHandle
1399 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1403 struct RequestHandle *handle = cls;
1404 struct EgoEntry *tmp_ego;
1405 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1406 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1408 cookie_identity_interpretation (handle);
1410 // RECOMMENDED value: state - REQUIRED for answers
1411 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1413 // REQUIRED value: client_id
1414 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1415 if (NULL == handle->oidc->client_id)
1417 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1418 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1419 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1420 GNUNET_SCHEDULER_add_now (&do_error, handle);
1424 // OPTIONAL value: code_challenge
1425 handle->oidc->code_challenge = get_url_parameter_copy (handle,
1426 OIDC_CODE_CHALLENGE_KEY);
1427 if (NULL == handle->oidc->code_challenge)
1429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1430 "OAuth authorization request does not contain PKCE parameters!\n");
1434 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1436 handle->oidc->client_id),
1437 &handle->oidc->client_pkey))
1439 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1440 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1441 "authorization code using this method.");
1442 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1443 GNUNET_SCHEDULER_add_now (&do_error, handle);
1447 // If we know this identity, translated the corresponding TLD
1448 // TODO: We might want to have a reverse lookup functionality for TLDs?
1449 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1451 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1452 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1453 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1455 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1456 handle->ego_entry = handle->ego_tail;
1459 if (NULL == handle->tld)
1460 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1461 if (NULL == handle->tld)
1462 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1463 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1468 * Combines an identity with a login time and responds OK to login request
1470 * @param con_handle the connection handle
1471 * @param url the url
1472 * @param cls the RequestHandle
1475 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1479 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1480 struct RequestHandle *handle = cls;
1481 struct GNUNET_HashCode cache_key;
1482 struct GNUNET_TIME_Absolute *current_time;
1483 struct GNUNET_TIME_Absolute *last_time;
1489 char term_data[handle->rest_handle->data_size + 1];
1491 term_data[handle->rest_handle->data_size] = '\0';
1492 GNUNET_memcpy (term_data,
1493 handle->rest_handle->data,
1494 handle->rest_handle->data_size);
1495 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1496 identity = json_object_get (root, "identity");
1497 if (! json_is_string (identity))
1499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1500 "Error parsing json string from %s\n",
1502 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1504 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1507 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1508 GNUNET_asprintf (&header_val,
1511 OIDC_COOKIE_EXPIRATION);
1512 MHD_add_response_header (resp, "Set-Cookie", header_val);
1513 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1514 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1516 if (0 != strcmp (json_string_value (identity), "Denied"))
1518 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1519 *current_time = GNUNET_TIME_relative_to_absolute (
1520 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1521 OIDC_COOKIE_EXPIRATION));
1523 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1524 GNUNET_free_non_null (last_time);
1525 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1528 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1530 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1531 GNUNET_free (cookie);
1532 GNUNET_free (header_val);
1534 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1539 check_authorization (struct RequestHandle *handle,
1540 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1542 struct GNUNET_HashCode cache_key;
1543 char *authorization;
1545 char *basic_authorization;
1548 char *expected_pass;
1550 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1551 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1553 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1557 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1558 handle->edesc = GNUNET_strdup ("missing authorization");
1559 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1560 return GNUNET_SYSERR;
1563 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1566 // split header in "Basic" and [content]
1567 credentials = strtok (authorization, " ");
1568 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1570 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1571 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1572 return GNUNET_SYSERR;
1574 credentials = strtok (NULL, " ");
1575 if (NULL == credentials)
1577 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1578 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1579 return GNUNET_SYSERR;
1581 GNUNET_STRINGS_base64_decode (credentials,
1582 strlen (credentials),
1583 (void **) &basic_authorization);
1585 if (NULL == basic_authorization)
1587 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1588 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1589 return GNUNET_SYSERR;
1591 client_id = strtok (basic_authorization, ":");
1592 if (NULL == client_id)
1594 GNUNET_free_non_null (basic_authorization);
1595 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1596 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1597 return GNUNET_SYSERR;
1599 pass = strtok (NULL, ":");
1602 GNUNET_free_non_null (basic_authorization);
1603 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1604 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1605 return GNUNET_SYSERR;
1608 // check client password
1609 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1610 "reclaim-rest-plugin",
1611 "OIDC_CLIENT_SECRET",
1614 if (0 != strcmp (expected_pass, pass))
1616 GNUNET_free_non_null (basic_authorization);
1617 GNUNET_free (expected_pass);
1618 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1619 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1620 return GNUNET_SYSERR;
1622 GNUNET_free (expected_pass);
1626 GNUNET_free_non_null (basic_authorization);
1627 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1628 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1629 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1630 return GNUNET_SYSERR;
1634 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1635 handle->ego_entry = handle->ego_entry->next)
1637 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1640 if (NULL == handle->ego_entry)
1642 GNUNET_free_non_null (basic_authorization);
1643 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1644 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1645 return GNUNET_SYSERR;
1647 GNUNET_STRINGS_string_to_data (client_id,
1650 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
1652 GNUNET_free (basic_authorization);
1657 const struct EgoEntry *
1658 find_ego (struct RequestHandle *handle,
1659 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1661 struct EgoEntry *ego_entry;
1662 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1664 for (ego_entry = handle->ego_head; NULL != ego_entry;
1665 ego_entry = ego_entry->next)
1667 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1668 if (0 == GNUNET_memcmp (&pub_key, test_key))
1676 persist_access_token (const struct RequestHandle *handle,
1677 const char *access_token,
1678 const struct GNUNET_RECLAIM_Ticket *ticket)
1680 struct GNUNET_HashCode hc;
1681 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1683 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1684 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1685 *ticketbuf = *ticket;
1686 GNUNET_assert (GNUNET_SYSERR !=
1687 GNUNET_CONTAINER_multihashmap_put (
1688 OIDC_access_token_map,
1691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1696 * Responds to token url-encoded POST request
1698 * @param con_handle the connection handle
1699 * @param url the url
1700 * @param cls the RequestHandle
1703 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1707 struct RequestHandle *handle = cls;
1708 const struct EgoEntry *ego_entry;
1709 struct GNUNET_TIME_Relative expiration_time;
1710 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl;
1711 struct GNUNET_RECLAIM_Ticket ticket;
1712 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1713 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1714 struct GNUNET_HashCode cache_key;
1715 struct MHD_Response *resp;
1718 char *json_response;
1723 char *code_verifier;
1726 * Check Authorization
1728 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1731 "OIDC authorization for token endpoint failed\n");
1732 GNUNET_SCHEDULER_add_now (&do_error, handle);
1740 // TODO Do not allow multiple equal parameter names
1741 // REQUIRED grant_type
1742 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1743 strlen (OIDC_GRANT_TYPE_KEY),
1745 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1746 if (NULL == grant_type)
1748 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1749 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1750 handle->response_code = MHD_HTTP_BAD_REQUEST;
1751 GNUNET_SCHEDULER_add_now (&do_error, handle);
1755 // Check parameter grant_type == "authorization_code"
1756 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1758 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1759 handle->response_code = MHD_HTTP_BAD_REQUEST;
1760 GNUNET_free (grant_type);
1761 GNUNET_SCHEDULER_add_now (&do_error, handle);
1764 GNUNET_free (grant_type);
1766 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1769 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1770 handle->edesc = GNUNET_strdup ("missing parameter code");
1771 handle->response_code = MHD_HTTP_BAD_REQUEST;
1772 GNUNET_SCHEDULER_add_now (&do_error, handle);
1775 ego_entry = find_ego (handle, &cid);
1776 if (NULL == ego_entry)
1778 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1779 handle->edesc = GNUNET_strdup ("Unknown client");
1780 handle->response_code = MHD_HTTP_BAD_REQUEST;
1782 GNUNET_SCHEDULER_add_now (&do_error, handle);
1785 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1787 // REQUIRED code verifier
1788 code_verifier = get_url_parameter_copy (handle, OIDC_CODE_VERIFIER_KEY);
1789 if (NULL == code_verifier)
1791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1792 "OAuth authorization request does not contain PKCE parameters!\n");
1797 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, code_verifier, &ticket,
1800 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1801 handle->edesc = GNUNET_strdup ("invalid code");
1802 handle->response_code = MHD_HTTP_BAD_REQUEST;
1804 GNUNET_SCHEDULER_add_now (&do_error, handle);
1810 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1811 "reclaim-rest-plugin",
1815 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1816 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1817 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1818 GNUNET_SCHEDULER_add_now (&do_error, handle);
1823 // TODO OPTIONAL acr,amr,azp
1824 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1825 "reclaim-rest-plugin",
1829 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1830 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1831 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1832 GNUNET_SCHEDULER_add_now (&do_error, handle);
1835 id_token = OIDC_id_token_new (&ticket.audience,
1839 (NULL != nonce) ? nonce : NULL,
1841 access_token = OIDC_access_token_new ();
1842 OIDC_build_token_response (access_token,
1847 persist_access_token (handle, access_token, &ticket);
1848 resp = GNUNET_REST_create_response (json_response);
1849 MHD_add_response_header (resp, "Cache-Control", "no-store");
1850 MHD_add_response_header (resp, "Pragma", "no-cache");
1851 MHD_add_response_header (resp, "Content-Type", "application/json");
1852 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1853 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (cl);
1854 GNUNET_free (access_token);
1855 GNUNET_free (json_response);
1856 GNUNET_free (id_token);
1857 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1862 * Collects claims and stores them in handle
1865 consume_ticket (void *cls,
1866 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1867 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
1868 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attest,
1869 const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *reference)
1871 struct RequestHandle *handle = cls;
1875 if (NULL == identity)
1877 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1880 tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
1883 value = json_string (tmp_value);
1884 json_object_set_new (handle->oidc->response, attr->name, value);
1885 GNUNET_free (tmp_value);
1890 * Responds to userinfo GET and url-encoded POST request
1892 * @param con_handle the connection handle
1893 * @param url the url
1894 * @param cls the RequestHandle
1897 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1901 // TODO expiration time
1902 struct RequestHandle *handle = cls;
1903 char delimiter[] = " ";
1904 struct GNUNET_HashCode cache_key;
1905 char *authorization;
1906 char *authorization_type;
1907 char *authorization_access_token;
1908 struct GNUNET_RECLAIM_Ticket *ticket;
1909 const struct EgoEntry *ego_entry;
1910 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1912 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1913 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1915 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1919 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1920 handle->edesc = GNUNET_strdup ("No Access Token");
1921 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1922 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1926 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1929 // split header in "Bearer" and access_token
1930 authorization = GNUNET_strdup (authorization);
1931 authorization_type = strtok (authorization, delimiter);
1932 if ((NULL == authorization_type) ||
1933 (0 != strcmp ("Bearer", authorization_type)))
1935 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1936 handle->edesc = GNUNET_strdup ("No Access Token");
1937 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1938 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1939 GNUNET_free (authorization);
1942 authorization_access_token = strtok (NULL, delimiter);
1943 if (NULL == authorization_access_token)
1945 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1946 handle->edesc = GNUNET_strdup ("Access token missing");
1947 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1948 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1949 GNUNET_free (authorization);
1953 GNUNET_CRYPTO_hash (authorization_access_token,
1954 strlen (authorization_access_token),
1957 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
1960 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1961 handle->edesc = GNUNET_strdup ("The access token expired");
1962 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1963 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1964 GNUNET_free (authorization);
1968 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
1969 GNUNET_assert (NULL != ticket);
1970 ego_entry = find_ego (handle, &ticket->audience);
1971 if (NULL == ego_entry)
1973 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1974 handle->edesc = GNUNET_strdup ("The access token expired");
1975 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1976 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1977 GNUNET_free (authorization);
1981 handle->idp = GNUNET_RECLAIM_connect (cfg);
1982 handle->oidc->response = json_object ();
1983 json_object_set_new (handle->oidc->response,
1985 json_string (ego_entry->keystring));
1986 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1987 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
1992 GNUNET_free (authorization);
1997 * Handle rest request
1999 * @param handle the request handle
2002 init_cont (struct RequestHandle *handle)
2004 struct GNUNET_REST_RequestHandlerError err;
2005 static const struct GNUNET_REST_RequestHandler handlers[] =
2006 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint },
2007 { MHD_HTTP_METHOD_POST,
2008 GNUNET_REST_API_NS_AUTHORIZE,
2009 &authorize_endpoint }, // url-encoded
2010 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont },
2011 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint },
2012 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2013 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
2014 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont },
2015 GNUNET_REST_HANDLER_END };
2018 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
2020 handle->response_code = err.error_code;
2021 GNUNET_SCHEDULER_add_now (&do_error, handle);
2027 * If listing is enabled, prints information about the egos.
2029 * This function is initially called for all egos and then again
2030 * whenever a ego's identifier changes or if it is deleted. At the
2031 * end of the initial pass over all egos, the function is once called
2032 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
2033 * be invoked in the future or that there was an error.
2035 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
2036 * this function is only called ONCE, and 'NULL' being passed in
2037 * 'ego' does indicate an error (i.e. name is taken or no default
2038 * value is known). If 'ego' is non-NULL and if '*ctx'
2039 * is set in those callbacks, the value WILL be passed to a subsequent
2040 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
2041 * that one was not NULL).
2043 * When an identity is renamed, this function is called with the
2044 * (known) ego but the NEW identifier.
2046 * When an identity is deleted, this function is called with the
2047 * (known) ego and "NULL" for the 'identifier'. In this case,
2048 * the 'ego' is henceforth invalid (and the 'ctx' should also be
2051 * @param cls closure
2052 * @param ego ego handle
2053 * @param ctx context for application to store data for this ego
2054 * (during the lifetime of this process, initially NULL)
2055 * @param identifier identifier assigned by the user for this ego,
2056 * NULL if the user just deleted the ego and it
2057 * must thus no longer be used
2060 list_ego (void *cls,
2061 struct GNUNET_IDENTITY_Ego *ego,
2063 const char *identifier)
2065 struct RequestHandle *handle = cls;
2066 struct EgoEntry *ego_entry;
2067 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
2069 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2071 handle->state = ID_REST_STATE_POST_INIT;
2075 GNUNET_assert (NULL != ego);
2076 if (ID_REST_STATE_INIT == handle->state)
2079 ego_entry = GNUNET_new (struct EgoEntry);
2080 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2081 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2082 ego_entry->ego = ego;
2083 ego_entry->identifier = GNUNET_strdup (identifier);
2084 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2089 /* Ego renamed or added */
2090 if (identifier != NULL)
2092 for (ego_entry = handle->ego_head; NULL != ego_entry;
2093 ego_entry = ego_entry->next)
2095 if (ego_entry->ego == ego)
2098 GNUNET_free (ego_entry->identifier);
2099 ego_entry->identifier = GNUNET_strdup (identifier);
2103 if (NULL == ego_entry)
2106 ego_entry = GNUNET_new (struct EgoEntry);
2107 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2108 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2109 ego_entry->ego = ego;
2110 ego_entry->identifier = GNUNET_strdup (identifier);
2111 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2119 for (ego_entry = handle->ego_head; NULL != ego_entry;
2120 ego_entry = ego_entry->next)
2122 if (ego_entry->ego == ego)
2125 if (NULL != ego_entry)
2126 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2134 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2135 GNUNET_REST_ResultProcessor proc,
2138 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2140 handle->oidc = GNUNET_new (struct OIDC_Variables);
2141 if (NULL == OIDC_cookie_jar_map)
2142 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2143 if (NULL == OIDC_access_token_map)
2144 OIDC_access_token_map =
2145 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2146 handle->response_code = 0;
2147 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2148 handle->proc_cls = proc_cls;
2149 handle->proc = proc;
2150 handle->state = ID_REST_STATE_INIT;
2151 handle->rest_handle = rest_handle;
2153 handle->url = GNUNET_strdup (rest_handle->url);
2154 if (handle->url[strlen (handle->url) - 1] == '/')
2155 handle->url[strlen (handle->url) - 1] = '\0';
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2157 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2158 handle->gns_handle = GNUNET_GNS_connect (cfg);
2159 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2160 handle->timeout_task =
2161 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2167 * Entry point for the plugin.
2169 * @param cls Config info
2170 * @return NULL on error, otherwise the plugin context
2173 libgnunet_plugin_rest_openid_connect_init (void *cls)
2175 static struct Plugin plugin;
2176 struct GNUNET_REST_Plugin *api;
2179 if (NULL != plugin.cfg)
2180 return NULL; /* can only initialize once! */
2181 memset (&plugin, 0, sizeof(struct Plugin));
2183 api = GNUNET_new (struct GNUNET_REST_Plugin);
2185 api->name = GNUNET_REST_API_NS_OIDC;
2186 api->process_request = &rest_identity_process_request;
2187 GNUNET_asprintf (&allow_methods,
2188 "%s, %s, %s, %s, %s",
2189 MHD_HTTP_METHOD_GET,
2190 MHD_HTTP_METHOD_POST,
2191 MHD_HTTP_METHOD_PUT,
2192 MHD_HTTP_METHOD_DELETE,
2193 MHD_HTTP_METHOD_OPTIONS);
2195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2196 _ ("OpenID Connect REST API initialized\n"));
2202 * Exit point from the plugin.
2204 * @param cls the plugin context (as returned by "init")
2205 * @return always NULL
2208 libgnunet_plugin_rest_openid_connect_done (void *cls)
2210 struct GNUNET_REST_Plugin *api = cls;
2211 struct Plugin *plugin = api->cls;
2215 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2218 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2219 while (GNUNET_YES ==
2220 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2221 GNUNET_free_non_null (value);
2222 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2223 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2226 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2227 while (GNUNET_YES ==
2228 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2229 GNUNET_free_non_null (value);
2230 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2231 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2232 GNUNET_free_non_null (allow_methods);
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "OpenID Connect REST plugin is finished\n");
2240 /* end of plugin_rest_openid_connect.c */