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);
580 cleanup_handle_delayed (void *cls)
582 cleanup_handle (cls);
587 * Task run on error, sends error message. Cleans up everything.
589 * @param cls the `struct RequestHandle`
594 struct RequestHandle *handle = cls;
595 struct MHD_Response *resp;
598 GNUNET_asprintf (&json_error,
599 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
601 (NULL != handle->edesc) ? handle->edesc : "",
602 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
603 (NULL != handle->oidc->state) ? handle->oidc->state : "",
604 (NULL != handle->oidc->state) ? "\"" : "");
605 if (0 == handle->response_code)
606 handle->response_code = MHD_HTTP_BAD_REQUEST;
607 resp = GNUNET_REST_create_response (json_error);
608 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
609 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
610 MHD_add_response_header (resp,
611 MHD_HTTP_HEADER_CONTENT_TYPE,
613 handle->proc (handle->proc_cls, resp, handle->response_code);
614 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
615 GNUNET_free (json_error);
620 * Task run on error in userinfo endpoint, sends error header. Cleans up
623 * @param cls the `struct RequestHandle`
626 do_userinfo_error (void *cls)
628 struct RequestHandle *handle = cls;
629 struct MHD_Response *resp;
632 GNUNET_asprintf (&error,
633 "error=\"%s\", error_description=\"%s\"",
635 (NULL != handle->edesc) ? handle->edesc : "");
636 resp = GNUNET_REST_create_response ("");
637 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
638 handle->proc (handle->proc_cls, resp, handle->response_code);
639 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
645 * Task run on error, sends error message and redirects. Cleans up everything.
647 * @param cls the `struct RequestHandle`
650 do_redirect_error (void *cls)
652 struct RequestHandle *handle = cls;
653 struct MHD_Response *resp;
656 GNUNET_asprintf (&redirect,
657 "%s?error=%s&error_description=%s%s%s",
658 handle->oidc->redirect_uri,
661 (NULL != handle->oidc->state) ? "&state=" : "",
662 (NULL != handle->oidc->state) ? handle->oidc->state : "");
663 resp = GNUNET_REST_create_response ("");
664 MHD_add_response_header (resp, "Location", redirect);
665 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
666 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
667 GNUNET_free (redirect);
671 * Task run on timeout, sends error message. Cleans up everything.
673 * @param cls the `struct RequestHandle`
676 do_timeout (void *cls)
678 struct RequestHandle *handle = cls;
680 handle->timeout_task = NULL;
685 * Return attributes for claim
687 * @param cls the request handle
690 return_userinfo_response (void *cls)
693 struct RequestHandle *handle = cls;
694 struct MHD_Response *resp;
696 result_str = json_dumps (handle->oidc->response, 0);
698 resp = GNUNET_REST_create_response (result_str);
699 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
700 GNUNET_free (result_str);
701 cleanup_handle (handle);
706 * Respond to OPTIONS request
708 * @param con_handle the connection handle
710 * @param cls the RequestHandle
713 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
717 struct MHD_Response *resp;
718 struct RequestHandle *handle = cls;
720 // For now, independent of path return all options
721 resp = GNUNET_REST_create_response (NULL);
722 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
723 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
724 cleanup_handle (handle);
730 * Interprets cookie header and pass its identity keystring to handle
733 cookie_identity_interpretation (struct RequestHandle *handle)
735 struct GNUNET_HashCode cache_key;
737 struct GNUNET_TIME_Absolute current_time, *relog_time;
738 char delimiter[] = "; ";
743 // gets identity of login try with cookie
744 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
745 strlen (OIDC_COOKIE_HEADER_KEY),
747 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
754 // splits cookies and find 'Identity' cookie
756 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
758 cookies = GNUNET_strdup (tmp_cookies);
759 token = strtok (cookies, delimiter);
760 handle->oidc->user_cancelled = GNUNET_NO;
761 handle->oidc->login_identity = NULL;
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 "Unable to parse cookie: %s\n",
767 GNUNET_free (cookies);
771 while (NULL != token)
773 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
775 handle->oidc->user_cancelled = GNUNET_YES;
776 GNUNET_free (cookies);
779 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
781 token = strtok (NULL, delimiter);
785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
786 "No cookie value to process: %s\n",
788 GNUNET_free (cookies);
791 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
793 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
796 GNUNET_ERROR_TYPE_WARNING,
797 "Found cookie `%s', but no corresponding expiration entry present...\n",
799 GNUNET_free (cookies);
803 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
804 current_time = GNUNET_TIME_absolute_get ();
805 // 30 min after old login -> redirect to login
806 if (current_time.abs_value_us > relog_time->abs_value_us)
808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
809 "Found cookie `%s', but it is expired.\n",
811 GNUNET_free (cookies);
814 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
815 GNUNET_assert (NULL != value);
816 handle->oidc->login_identity = GNUNET_strdup (value);
817 GNUNET_free (cookies);
821 * Redirects to login page stored in configuration file
824 login_redirect (void *cls)
826 char *login_base_url;
828 struct MHD_Response *resp;
829 struct RequestHandle *handle = cls;
831 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
832 "reclaim-rest-plugin",
836 GNUNET_asprintf (&new_redirect,
837 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
839 OIDC_RESPONSE_TYPE_KEY,
840 handle->oidc->response_type,
842 handle->oidc->client_id,
843 OIDC_REDIRECT_URI_KEY,
844 handle->oidc->redirect_uri,
848 (NULL != handle->oidc->state) ? handle->oidc->state : "",
849 OIDC_CODE_CHALLENGE_KEY,
850 (NULL != handle->oidc->code_challenge) ?
851 handle->oidc->code_challenge : "",
853 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "");
854 resp = GNUNET_REST_create_response ("");
855 MHD_add_response_header (resp, "Location", new_redirect);
856 GNUNET_free (login_base_url);
860 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
861 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
862 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
863 GNUNET_SCHEDULER_add_now (&do_error, handle);
866 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
867 GNUNET_free (new_redirect);
868 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
872 * Does internal server error when iteration failed.
875 oidc_iteration_error (void *cls)
877 struct RequestHandle *handle = cls;
879 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
880 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
881 GNUNET_SCHEDULER_add_now (&do_error, handle);
886 * Issues ticket and redirects to relying party with the authorization code as
887 * parameter. Otherwise redirects with error
890 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
892 struct RequestHandle *handle = cls;
893 struct MHD_Response *resp;
898 handle->idp_op = NULL;
901 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
902 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
903 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
906 handle->ticket = *ticket;
908 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
909 sizeof(struct GNUNET_RECLAIM_Ticket));
910 // TODO change if more attributes are needed (see max_age)
911 code_string = OIDC_build_authz_code (&handle->priv_key,
915 handle->oidc->code_challenge);
916 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
917 (NULL != handle->tld))
919 GNUNET_asprintf (&redirect_uri,
920 "%s.%s/%s?%s=%s&state=%s",
921 handle->redirect_prefix,
923 handle->redirect_suffix,
924 handle->oidc->response_type,
926 handle->oidc->state);
930 GNUNET_asprintf (&redirect_uri,
932 handle->oidc->redirect_uri,
933 handle->oidc->response_type,
935 handle->oidc->state);
937 resp = GNUNET_REST_create_response ("");
938 MHD_add_response_header (resp, "Location", redirect_uri);
939 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
940 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
941 GNUNET_free (redirect_uri);
942 GNUNET_free (ticket_str);
943 GNUNET_free (code_string);
947 oidc_collect_finished_cb (void *cls)
949 struct RequestHandle *handle = cls;
951 handle->attr_it = NULL;
952 handle->ticket_it = NULL;
953 if (NULL == handle->attr_list->list_head)
955 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
956 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
957 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
960 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
962 &handle->oidc->client_pkey,
964 &oidc_ticket_issue_cb,
970 * Collects all attributes for an ego if in scope parameter
973 oidc_attr_collect (void *cls,
974 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
975 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
977 struct RequestHandle *handle = cls;
978 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
979 char *scope_variables;
980 char *scope_variable;
981 char delimiter[] = " ";
983 if ((NULL == attr->name) || (NULL == attr->data))
985 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
989 scope_variables = GNUNET_strdup (handle->oidc->scope);
990 scope_variable = strtok (scope_variables, delimiter);
991 while (NULL != scope_variable)
993 if (0 == strcmp (attr->name, scope_variable))
995 scope_variable = strtok (NULL, delimiter);
997 if (NULL == scope_variable)
999 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1000 GNUNET_free (scope_variables);
1003 GNUNET_free (scope_variables);
1005 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
1006 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name,
1010 le->claim->id = attr->id;
1011 le->claim->version = attr->version;
1012 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
1013 handle->attr_list->list_tail,
1015 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
1020 * Checks time and cookie and redirects accordingly
1023 code_redirect (void *cls)
1025 struct RequestHandle *handle = cls;
1026 struct GNUNET_TIME_Absolute current_time;
1027 struct GNUNET_TIME_Absolute *relog_time;
1028 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1029 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1030 struct GNUNET_HashCode cache_key;
1031 char *identity_cookie;
1033 GNUNET_asprintf (&identity_cookie,
1035 handle->oidc->login_identity);
1036 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1037 GNUNET_free (identity_cookie);
1038 // No login time for identity -> redirect to login
1040 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1043 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1044 current_time = GNUNET_TIME_absolute_get ();
1045 // 30 min after old login -> redirect to login
1046 if (current_time.abs_value_us <= relog_time->abs_value_us)
1049 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1056 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1058 GNUNET_strdup ("The cookie of a login identity is not valid");
1059 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1062 // iterate over egos and compare their public key
1063 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1064 handle->ego_entry = handle->ego_entry->next)
1066 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1067 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1070 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1071 handle->idp = GNUNET_RECLAIM_connect (cfg);
1073 GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
1075 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1077 &oidc_iteration_error,
1081 &oidc_collect_finished_cb,
1086 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1094 build_redirect (void *cls)
1096 struct RequestHandle *handle = cls;
1097 struct MHD_Response *resp;
1100 if (GNUNET_YES == handle->oidc->user_cancelled)
1102 if ((NULL != handle->redirect_prefix) &&
1103 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1105 GNUNET_asprintf (&redirect_uri,
1106 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1107 handle->redirect_prefix,
1109 handle->redirect_suffix,
1111 "User denied access",
1112 handle->oidc->state);
1116 GNUNET_asprintf (&redirect_uri,
1117 "%s?error=%s&error_description=%s&state=%s",
1118 handle->oidc->redirect_uri,
1120 "User denied access",
1121 handle->oidc->state);
1123 resp = GNUNET_REST_create_response ("");
1124 MHD_add_response_header (resp, "Location", redirect_uri);
1125 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1126 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1127 GNUNET_free (redirect_uri);
1130 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1135 lookup_redirect_uri_result (void *cls,
1137 const struct GNUNET_GNSRECORD_Data *rd)
1139 struct RequestHandle *handle = cls;
1143 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1145 handle->gns_op = NULL;
1148 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1150 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1151 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1154 for (int i = 0; i < rd_count; i++)
1156 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1158 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1160 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1161 if (NULL == strstr (tmp, handle->oidc->client_id))
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Redirect uri %s does not contain client_id %s\n",
1166 handle->oidc->client_id);
1170 pos = strrchr (tmp, (unsigned char) '.');
1173 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1174 "Redirect uri %s contains client_id but is malformed\n",
1180 handle->redirect_prefix = GNUNET_strdup (tmp);
1181 tmp_key_str = pos + 1;
1182 pos = strchr (tmp_key_str, (unsigned char) '/');
1185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1186 "Redirect uri %s contains client_id but is malformed\n",
1192 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1194 GNUNET_STRINGS_string_to_data (tmp_key_str,
1195 strlen (tmp_key_str),
1197 sizeof(redirect_zone));
1199 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1203 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1205 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1206 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1211 * Initiate redirect back to client.
1214 client_redirect (void *cls)
1216 struct RequestHandle *handle = cls;
1218 /* Lookup client redirect uri to verify request */
1220 GNUNET_GNS_lookup (handle->gns_handle,
1221 GNUNET_GNS_EMPTY_LABEL_AT,
1222 &handle->oidc->client_pkey,
1223 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1224 GNUNET_GNS_LO_DEFAULT,
1225 &lookup_redirect_uri_result,
1230 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1232 struct GNUNET_HashCode hc;
1235 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1236 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1241 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1244 return GNUNET_strdup (value);
1249 * Iteration over all results finished, build final
1252 * @param cls the `struct RequestHandle`
1255 build_authz_response (void *cls)
1257 struct RequestHandle *handle = cls;
1258 struct GNUNET_HashCode cache_key;
1260 char *expected_scope;
1261 char delimiter[] = " ";
1262 int number_of_ignored_parameter, iterator;
1265 // REQUIRED value: redirect_uri
1266 handle->oidc->redirect_uri =
1267 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1268 if (NULL == handle->oidc->redirect_uri)
1270 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1271 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1272 GNUNET_SCHEDULER_add_now (&do_error, handle);
1276 // REQUIRED value: response_type
1277 handle->oidc->response_type =
1278 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1279 if (NULL == handle->oidc->response_type)
1281 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1282 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1283 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1287 // REQUIRED value: scope
1288 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1289 if (NULL == handle->oidc->scope)
1291 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1292 handle->edesc = GNUNET_strdup ("missing parameter scope");
1293 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1297 // OPTIONAL value: nonce
1298 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1300 // TODO check other values if needed
1301 number_of_ignored_parameter =
1302 sizeof(OIDC_ignored_parameter_array) / sizeof(char *);
1303 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1305 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1306 strlen (OIDC_ignored_parameter_array[iterator]),
1309 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1313 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1314 GNUNET_asprintf (&handle->edesc,
1315 "Server will not handle parameter: %s",
1316 OIDC_ignored_parameter_array[iterator]);
1317 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1322 // We only support authorization code flows.
1323 if (0 != strcmp (handle->oidc->response_type,
1324 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1326 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1327 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1328 "obtaining this authorization code.");
1329 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1333 // Checks if scope contains 'openid'
1334 expected_scope = GNUNET_strdup (handle->oidc->scope);
1336 test = strtok (expected_scope, delimiter);
1337 while (NULL != test)
1339 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1341 test = strtok (NULL, delimiter);
1345 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1347 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1348 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1349 GNUNET_free (expected_scope);
1353 GNUNET_free (expected_scope);
1354 if ((NULL == handle->oidc->login_identity) &&
1355 (GNUNET_NO == handle->oidc->user_cancelled))
1356 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1358 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1362 * Iterate over tlds in config
1365 tld_iter (void *cls, const char *section, const char *option, const char *value)
1367 struct RequestHandle *handle = cls;
1368 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1371 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1376 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1377 handle->tld = GNUNET_strdup (option + 1);
1381 * Responds to authorization GET and url-encoded POST request
1383 * @param con_handle the connection handle
1384 * @param url the url
1385 * @param cls the RequestHandle
1388 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1392 struct RequestHandle *handle = cls;
1393 struct EgoEntry *tmp_ego;
1394 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1395 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1397 cookie_identity_interpretation (handle);
1399 // RECOMMENDED value: state - REQUIRED for answers
1400 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1402 // REQUIRED value: client_id
1403 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1404 if (NULL == handle->oidc->client_id)
1406 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1407 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1408 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1409 GNUNET_SCHEDULER_add_now (&do_error, handle);
1413 // OPTIONAL value: code_challenge
1414 handle->oidc->code_challenge = get_url_parameter_copy (handle,
1415 OIDC_CODE_CHALLENGE_KEY);
1416 if (NULL == handle->oidc->code_challenge)
1418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1419 "OAuth authorization request does not contain PKCE parameters!\n");
1423 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1425 handle->oidc->client_id),
1426 &handle->oidc->client_pkey))
1428 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1429 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1430 "authorization code using this method.");
1431 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1432 GNUNET_SCHEDULER_add_now (&do_error, handle);
1436 // If we know this identity, translated the corresponding TLD
1437 // TODO: We might want to have a reverse lookup functionality for TLDs?
1438 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1440 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1441 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1442 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1444 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1445 handle->ego_entry = handle->ego_tail;
1448 if (NULL == handle->tld)
1449 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1450 if (NULL == handle->tld)
1451 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1452 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1456 * Combines an identity with a login time and responds OK to login request
1458 * @param con_handle the connection handle
1459 * @param url the url
1460 * @param cls the RequestHandle
1463 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1467 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1468 struct RequestHandle *handle = cls;
1469 struct GNUNET_HashCode cache_key;
1470 struct GNUNET_TIME_Absolute *current_time;
1471 struct GNUNET_TIME_Absolute *last_time;
1477 char term_data[handle->rest_handle->data_size + 1];
1479 term_data[handle->rest_handle->data_size] = '\0';
1480 GNUNET_memcpy (term_data,
1481 handle->rest_handle->data,
1482 handle->rest_handle->data_size);
1483 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1484 identity = json_object_get (root, "identity");
1485 if (! json_is_string (identity))
1487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1488 "Error parsing json string from %s\n",
1490 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1492 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1495 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1496 GNUNET_asprintf (&header_val,
1499 OIDC_COOKIE_EXPIRATION);
1500 MHD_add_response_header (resp, "Set-Cookie", header_val);
1501 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1502 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1504 if (0 != strcmp (json_string_value (identity), "Denied"))
1506 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1507 *current_time = GNUNET_TIME_relative_to_absolute (
1508 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1509 OIDC_COOKIE_EXPIRATION));
1511 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1512 GNUNET_free_non_null (last_time);
1513 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1518 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1519 GNUNET_free (cookie);
1520 GNUNET_free (header_val);
1522 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1526 check_authorization (struct RequestHandle *handle,
1527 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1529 struct GNUNET_HashCode cache_key;
1530 char *authorization;
1532 char *basic_authorization;
1535 char *expected_pass;
1537 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1538 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1540 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1544 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1545 handle->edesc = GNUNET_strdup ("missing authorization");
1546 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1547 return GNUNET_SYSERR;
1550 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1553 // split header in "Basic" and [content]
1554 credentials = strtok (authorization, " ");
1555 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1557 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1558 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1559 return GNUNET_SYSERR;
1561 credentials = strtok (NULL, " ");
1562 if (NULL == credentials)
1564 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1565 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1566 return GNUNET_SYSERR;
1568 GNUNET_STRINGS_base64_decode (credentials,
1569 strlen (credentials),
1570 (void **) &basic_authorization);
1572 if (NULL == basic_authorization)
1574 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1575 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1576 return GNUNET_SYSERR;
1578 client_id = strtok (basic_authorization, ":");
1579 if (NULL == client_id)
1581 GNUNET_free_non_null (basic_authorization);
1582 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1583 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1584 return GNUNET_SYSERR;
1586 pass = strtok (NULL, ":");
1589 GNUNET_free_non_null (basic_authorization);
1590 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1591 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1592 return GNUNET_SYSERR;
1595 // check client password
1596 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1597 "reclaim-rest-plugin",
1598 "OIDC_CLIENT_SECRET",
1601 if (0 != strcmp (expected_pass, pass))
1603 GNUNET_free_non_null (basic_authorization);
1604 GNUNET_free (expected_pass);
1605 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1606 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1607 return GNUNET_SYSERR;
1609 GNUNET_free (expected_pass);
1613 GNUNET_free_non_null (basic_authorization);
1614 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1615 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1616 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1617 return GNUNET_SYSERR;
1621 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1622 handle->ego_entry = handle->ego_entry->next)
1624 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1627 if (NULL == handle->ego_entry)
1629 GNUNET_free_non_null (basic_authorization);
1630 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1631 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1632 return GNUNET_SYSERR;
1634 GNUNET_STRINGS_string_to_data (client_id,
1637 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
1639 GNUNET_free (basic_authorization);
1643 const struct EgoEntry *
1644 find_ego (struct RequestHandle *handle,
1645 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1647 struct EgoEntry *ego_entry;
1648 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1650 for (ego_entry = handle->ego_head; NULL != ego_entry;
1651 ego_entry = ego_entry->next)
1653 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1654 if (0 == GNUNET_memcmp (&pub_key, test_key))
1661 persist_access_token (const struct RequestHandle *handle,
1662 const char *access_token,
1663 const struct GNUNET_RECLAIM_Ticket *ticket)
1665 struct GNUNET_HashCode hc;
1666 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1668 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1669 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1670 *ticketbuf = *ticket;
1671 GNUNET_assert (GNUNET_SYSERR !=
1672 GNUNET_CONTAINER_multihashmap_put (
1673 OIDC_access_token_map,
1676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1680 * Responds to token url-encoded POST request
1682 * @param con_handle the connection handle
1683 * @param url the url
1684 * @param cls the RequestHandle
1687 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1691 struct RequestHandle *handle = cls;
1692 const struct EgoEntry *ego_entry;
1693 struct GNUNET_TIME_Relative expiration_time;
1694 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl;
1695 struct GNUNET_RECLAIM_Ticket ticket;
1696 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1697 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1698 struct GNUNET_HashCode cache_key;
1699 struct MHD_Response *resp;
1702 char *json_response;
1707 char *code_verifier;
1710 * Check Authorization
1712 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1715 "OIDC authorization for token endpoint failed\n");
1716 GNUNET_SCHEDULER_add_now (&do_error, handle);
1724 // TODO Do not allow multiple equal parameter names
1725 // REQUIRED grant_type
1726 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1727 strlen (OIDC_GRANT_TYPE_KEY),
1729 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1730 if (NULL == grant_type)
1732 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1733 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1734 handle->response_code = MHD_HTTP_BAD_REQUEST;
1735 GNUNET_SCHEDULER_add_now (&do_error, handle);
1739 // Check parameter grant_type == "authorization_code"
1740 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1742 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1743 handle->response_code = MHD_HTTP_BAD_REQUEST;
1744 GNUNET_free (grant_type);
1745 GNUNET_SCHEDULER_add_now (&do_error, handle);
1748 GNUNET_free (grant_type);
1750 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1753 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1754 handle->edesc = GNUNET_strdup ("missing parameter code");
1755 handle->response_code = MHD_HTTP_BAD_REQUEST;
1756 GNUNET_SCHEDULER_add_now (&do_error, handle);
1759 ego_entry = find_ego (handle, &cid);
1760 if (NULL == ego_entry)
1762 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1763 handle->edesc = GNUNET_strdup ("Unknown client");
1764 handle->response_code = MHD_HTTP_BAD_REQUEST;
1766 GNUNET_SCHEDULER_add_now (&do_error, handle);
1769 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1771 // REQUIRED code verifier
1772 code_verifier = get_url_parameter_copy (handle, OIDC_CODE_VERIFIER_KEY);
1773 if (NULL == code_verifier)
1775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1776 "OAuth authorization request does not contain PKCE parameters!\n");
1781 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, code_verifier, &ticket,
1784 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1785 handle->edesc = GNUNET_strdup ("invalid code");
1786 handle->response_code = MHD_HTTP_BAD_REQUEST;
1788 GNUNET_SCHEDULER_add_now (&do_error, handle);
1794 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1795 "reclaim-rest-plugin",
1799 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1800 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1801 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1802 GNUNET_SCHEDULER_add_now (&do_error, handle);
1807 // TODO OPTIONAL acr,amr,azp
1808 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1809 "reclaim-rest-plugin",
1813 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1814 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1815 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1816 GNUNET_SCHEDULER_add_now (&do_error, handle);
1819 id_token = OIDC_id_token_new (&ticket.audience,
1823 (NULL != nonce) ? nonce : NULL,
1825 access_token = OIDC_access_token_new ();
1826 OIDC_build_token_response (access_token,
1831 persist_access_token (handle, access_token, &ticket);
1832 resp = GNUNET_REST_create_response (json_response);
1833 MHD_add_response_header (resp, "Cache-Control", "no-store");
1834 MHD_add_response_header (resp, "Pragma", "no-cache");
1835 MHD_add_response_header (resp, "Content-Type", "application/json");
1836 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1837 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (cl);
1838 GNUNET_free (access_token);
1839 GNUNET_free (json_response);
1840 GNUNET_free (id_token);
1841 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1845 * Collects claims and stores them in handle
1848 consume_ticket (void *cls,
1849 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1850 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
1852 struct RequestHandle *handle = cls;
1856 if (NULL == identity)
1858 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1861 tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
1864 value = json_string (tmp_value);
1865 json_object_set_new (handle->oidc->response, attr->name, value);
1866 GNUNET_free (tmp_value);
1870 * Responds to userinfo GET and url-encoded POST request
1872 * @param con_handle the connection handle
1873 * @param url the url
1874 * @param cls the RequestHandle
1877 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1881 // TODO expiration time
1882 struct RequestHandle *handle = cls;
1883 char delimiter[] = " ";
1884 struct GNUNET_HashCode cache_key;
1885 char *authorization;
1886 char *authorization_type;
1887 char *authorization_access_token;
1888 struct GNUNET_RECLAIM_Ticket *ticket;
1889 const struct EgoEntry *ego_entry;
1890 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1892 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1893 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1895 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1899 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1900 handle->edesc = GNUNET_strdup ("No Access Token");
1901 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1902 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1906 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1909 // split header in "Bearer" and access_token
1910 authorization = GNUNET_strdup (authorization);
1911 authorization_type = strtok (authorization, delimiter);
1912 if ((NULL == authorization_type) ||
1913 (0 != strcmp ("Bearer", authorization_type)))
1915 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1916 handle->edesc = GNUNET_strdup ("No Access Token");
1917 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1918 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1919 GNUNET_free (authorization);
1922 authorization_access_token = strtok (NULL, delimiter);
1923 if (NULL == authorization_access_token)
1925 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1926 handle->edesc = GNUNET_strdup ("Access token missing");
1927 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1928 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1929 GNUNET_free (authorization);
1933 GNUNET_CRYPTO_hash (authorization_access_token,
1934 strlen (authorization_access_token),
1937 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
1940 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1941 handle->edesc = GNUNET_strdup ("The access token expired");
1942 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1943 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1944 GNUNET_free (authorization);
1948 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
1949 GNUNET_assert (NULL != ticket);
1950 ego_entry = find_ego (handle, &ticket->audience);
1951 if (NULL == ego_entry)
1953 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1954 handle->edesc = GNUNET_strdup ("The access token expired");
1955 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1956 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1957 GNUNET_free (authorization);
1961 handle->idp = GNUNET_RECLAIM_connect (cfg);
1962 handle->oidc->response = json_object ();
1963 json_object_set_new (handle->oidc->response,
1965 json_string (ego_entry->keystring));
1966 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1967 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
1972 GNUNET_free (authorization);
1977 * Handle rest request
1979 * @param handle the request handle
1982 init_cont (struct RequestHandle *handle)
1984 struct GNUNET_REST_RequestHandlerError err;
1985 static const struct GNUNET_REST_RequestHandler handlers[] =
1986 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint },
1987 { MHD_HTTP_METHOD_POST,
1988 GNUNET_REST_API_NS_AUTHORIZE,
1989 &authorize_endpoint }, // url-encoded
1990 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont },
1991 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint },
1992 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
1993 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint },
1994 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont },
1995 GNUNET_REST_HANDLER_END };
1998 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
2000 handle->response_code = err.error_code;
2001 GNUNET_SCHEDULER_add_now (&do_error, handle);
2006 * If listing is enabled, prints information about the egos.
2008 * This function is initially called for all egos and then again
2009 * whenever a ego's identifier changes or if it is deleted. At the
2010 * end of the initial pass over all egos, the function is once called
2011 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
2012 * be invoked in the future or that there was an error.
2014 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
2015 * this function is only called ONCE, and 'NULL' being passed in
2016 * 'ego' does indicate an error (i.e. name is taken or no default
2017 * value is known). If 'ego' is non-NULL and if '*ctx'
2018 * is set in those callbacks, the value WILL be passed to a subsequent
2019 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
2020 * that one was not NULL).
2022 * When an identity is renamed, this function is called with the
2023 * (known) ego but the NEW identifier.
2025 * When an identity is deleted, this function is called with the
2026 * (known) ego and "NULL" for the 'identifier'. In this case,
2027 * the 'ego' is henceforth invalid (and the 'ctx' should also be
2030 * @param cls closure
2031 * @param ego ego handle
2032 * @param ctx context for application to store data for this ego
2033 * (during the lifetime of this process, initially NULL)
2034 * @param identifier identifier assigned by the user for this ego,
2035 * NULL if the user just deleted the ego and it
2036 * must thus no longer be used
2039 list_ego (void *cls,
2040 struct GNUNET_IDENTITY_Ego *ego,
2042 const char *identifier)
2044 struct RequestHandle *handle = cls;
2045 struct EgoEntry *ego_entry;
2046 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
2048 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2050 handle->state = ID_REST_STATE_POST_INIT;
2054 GNUNET_assert (NULL != ego);
2055 if (ID_REST_STATE_INIT == handle->state)
2058 ego_entry = GNUNET_new (struct EgoEntry);
2059 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2060 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2061 ego_entry->ego = ego;
2062 ego_entry->identifier = GNUNET_strdup (identifier);
2063 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2068 /* Ego renamed or added */
2069 if (identifier != NULL)
2071 for (ego_entry = handle->ego_head; NULL != ego_entry;
2072 ego_entry = ego_entry->next)
2074 if (ego_entry->ego == ego)
2077 GNUNET_free (ego_entry->identifier);
2078 ego_entry->identifier = GNUNET_strdup (identifier);
2082 if (NULL == ego_entry)
2085 ego_entry = GNUNET_new (struct EgoEntry);
2086 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2087 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2088 ego_entry->ego = ego;
2089 ego_entry->identifier = GNUNET_strdup (identifier);
2090 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2098 for (ego_entry = handle->ego_head; NULL != ego_entry;
2099 ego_entry = ego_entry->next)
2101 if (ego_entry->ego == ego)
2104 if (NULL != ego_entry)
2105 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2112 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2113 GNUNET_REST_ResultProcessor proc,
2116 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2118 handle->oidc = GNUNET_new (struct OIDC_Variables);
2119 if (NULL == OIDC_cookie_jar_map)
2120 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2121 if (NULL == OIDC_access_token_map)
2122 OIDC_access_token_map =
2123 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2124 handle->response_code = 0;
2125 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2126 handle->proc_cls = proc_cls;
2127 handle->proc = proc;
2128 handle->state = ID_REST_STATE_INIT;
2129 handle->rest_handle = rest_handle;
2131 handle->url = GNUNET_strdup (rest_handle->url);
2132 if (handle->url[strlen (handle->url) - 1] == '/')
2133 handle->url[strlen (handle->url) - 1] = '\0';
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2135 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2136 handle->gns_handle = GNUNET_GNS_connect (cfg);
2137 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2138 handle->timeout_task =
2139 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2144 * Entry point for the plugin.
2146 * @param cls Config info
2147 * @return NULL on error, otherwise the plugin context
2150 libgnunet_plugin_rest_openid_connect_init (void *cls)
2152 static struct Plugin plugin;
2153 struct GNUNET_REST_Plugin *api;
2156 if (NULL != plugin.cfg)
2157 return NULL; /* can only initialize once! */
2158 memset (&plugin, 0, sizeof(struct Plugin));
2160 api = GNUNET_new (struct GNUNET_REST_Plugin);
2162 api->name = GNUNET_REST_API_NS_OIDC;
2163 api->process_request = &rest_identity_process_request;
2164 GNUNET_asprintf (&allow_methods,
2165 "%s, %s, %s, %s, %s",
2166 MHD_HTTP_METHOD_GET,
2167 MHD_HTTP_METHOD_POST,
2168 MHD_HTTP_METHOD_PUT,
2169 MHD_HTTP_METHOD_DELETE,
2170 MHD_HTTP_METHOD_OPTIONS);
2172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2173 _ ("OpenID Connect REST API initialized\n"));
2179 * Exit point from the plugin.
2181 * @param cls the plugin context (as returned by "init")
2182 * @return always NULL
2185 libgnunet_plugin_rest_openid_connect_done (void *cls)
2187 struct GNUNET_REST_Plugin *api = cls;
2188 struct Plugin *plugin = api->cls;
2192 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2195 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2196 while (GNUNET_YES ==
2197 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2198 GNUNET_free_non_null (value);
2199 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2200 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2203 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2204 while (GNUNET_YES ==
2205 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2206 GNUNET_free_non_null (value);
2207 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2208 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2209 GNUNET_free_non_null (allow_methods);
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 "OpenID Connect REST plugin is finished\n");
2216 /* end of plugin_rest_openid_connect.c */