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 cookie expiration (in seconds)
125 #define OIDC_COOKIE_EXPIRATION 3
128 * OIDC cookie header key
130 #define OIDC_COOKIE_HEADER_KEY "cookie"
133 * OIDC cookie header information key
135 #define OIDC_AUTHORIZATION_HEADER_KEY "authorization"
138 * OIDC cookie header information key
140 #define OIDC_COOKIE_HEADER_INFORMATION_KEY "Identity="
143 * OIDC cookie header if user cancelled
145 #define OIDC_COOKIE_HEADER_ACCESS_DENIED "Identity=Denied"
148 * OIDC expected response_type while authorizing
150 #define OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE "code"
153 * OIDC expected scope part while authorizing
155 #define OIDC_EXPECTED_AUTHORIZATION_SCOPE "openid"
158 * OIDC error key for invalid client
160 #define OIDC_ERROR_KEY_INVALID_CLIENT "invalid_client"
163 * OIDC error key for invalid scopes
165 #define OIDC_ERROR_KEY_INVALID_SCOPE "invalid_scope"
168 * OIDC error key for invalid requests
170 #define OIDC_ERROR_KEY_INVALID_REQUEST "invalid_request"
173 * OIDC error key for invalid tokens
175 #define OIDC_ERROR_KEY_INVALID_TOKEN "invalid_token"
178 * OIDC error key for invalid cookies
180 #define OIDC_ERROR_KEY_INVALID_COOKIE "invalid_cookie"
183 * OIDC error key for generic server errors
185 #define OIDC_ERROR_KEY_SERVER_ERROR "server_error"
188 * OIDC error key for unsupported grants
190 #define OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE "unsupported_grant_type"
193 * OIDC error key for unsupported response types
195 #define OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE "unsupported_response_type"
198 * OIDC error key for unauthorized clients
200 #define OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT "unauthorized_client"
203 * OIDC error key for denied access
205 #define OIDC_ERROR_KEY_ACCESS_DENIED "access_denied"
209 * OIDC ignored parameter array
211 static char *OIDC_ignored_parameter_array[] = {"display",
220 * OIDC Hash map that keeps track of issued cookies
222 struct GNUNET_CONTAINER_MultiHashMap *OIDC_cookie_jar_map;
225 * Hash map that links the issued access token to the corresponding ticket and
228 struct GNUNET_CONTAINER_MultiHashMap *OIDC_access_token_map;
231 * The configuration handle
233 const struct GNUNET_CONFIGURATION_Handle *cfg;
236 * HTTP methods allows for this plugin
238 static char *allow_methods;
241 * @brief struct returned by the initialization function of the plugin
245 const struct GNUNET_CONFIGURATION_Handle *cfg;
249 * OIDC needed variables
251 struct OIDC_Variables
254 * The RP client public key
256 struct GNUNET_CRYPTO_EcdsaPublicKey client_pkey;
259 * The OIDC client id of the RP
264 * The OIDC redirect uri
269 * The list of oidc scopes
284 * The OIDC response type
289 * The identity chosen by the user to login
291 char *login_identity;
294 * User cancelled authorization/login
312 struct EgoEntry *next;
317 struct EgoEntry *prev;
332 struct GNUNET_IDENTITY_Ego *ego;
341 struct EgoEntry *ego_head;
346 struct EgoEntry *ego_tail;
351 struct EgoEntry *ego_entry;
354 * Pointer to ego private key
356 struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key;
361 struct OIDC_Variables *oidc;
364 * The processing state
369 * Handle to Identity service.
371 struct GNUNET_IDENTITY_Handle *identity_handle;
376 struct GNUNET_REST_RequestHandle *rest_handle;
381 struct GNUNET_GNS_Handle *gns_handle;
386 struct GNUNET_GNS_LookupRequest *gns_op;
389 * Handle to NAMESTORE
391 struct GNUNET_NAMESTORE_Handle *namestore_handle;
394 * Iterator for NAMESTORE
396 struct GNUNET_NAMESTORE_ZoneIterator *namestore_handle_it;
399 * Attribute claim list
401 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list;
406 struct GNUNET_IDENTITY_Operation *op;
411 struct GNUNET_RECLAIM_Handle *idp;
416 struct GNUNET_RECLAIM_Operation *idp_op;
421 struct GNUNET_RECLAIM_AttributeIterator *attr_it;
426 struct GNUNET_RECLAIM_TicketIterator *ticket_it;
431 struct GNUNET_RECLAIM_Ticket ticket;
434 * Desired timeout for the lookup (default is no timeout).
436 struct GNUNET_TIME_Relative timeout;
439 * ID of a task associated with the resolution process.
441 struct GNUNET_SCHEDULER_Task *timeout_task;
444 * The plugin result processor
446 GNUNET_REST_ResultProcessor proc;
449 * The closure of the result processor
459 * The tld for redirect
464 * The redirect prefix
466 char *redirect_prefix;
469 * The redirect suffix
471 char *redirect_suffix;
474 * Error response message
479 * Error response description
490 * Cleanup lookup handle
491 * @param handle Handle to clean up
494 cleanup_handle (struct RequestHandle *handle)
496 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_entry;
497 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_tmp;
498 struct EgoEntry *ego_entry;
499 struct EgoEntry *ego_tmp;
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
501 if (NULL != handle->timeout_task)
502 GNUNET_SCHEDULER_cancel (handle->timeout_task);
503 if (NULL != handle->identity_handle)
504 GNUNET_IDENTITY_disconnect (handle->identity_handle);
505 if (NULL != handle->attr_it)
506 GNUNET_RECLAIM_get_attributes_stop (handle->attr_it);
507 if (NULL != handle->ticket_it)
508 GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it);
509 if (NULL != handle->idp)
510 GNUNET_RECLAIM_disconnect (handle->idp);
511 GNUNET_free_non_null (handle->url);
512 GNUNET_free_non_null (handle->tld);
513 GNUNET_free_non_null (handle->redirect_prefix);
514 GNUNET_free_non_null (handle->redirect_suffix);
515 GNUNET_free_non_null (handle->emsg);
516 GNUNET_free_non_null (handle->edesc);
517 if (NULL != handle->gns_op)
518 GNUNET_GNS_lookup_cancel (handle->gns_op);
519 if (NULL != handle->gns_handle)
520 GNUNET_GNS_disconnect (handle->gns_handle);
522 if (NULL != handle->namestore_handle)
523 GNUNET_NAMESTORE_disconnect (handle->namestore_handle);
524 if (NULL != handle->oidc)
526 GNUNET_free_non_null (handle->oidc->client_id);
527 GNUNET_free_non_null (handle->oidc->login_identity);
528 GNUNET_free_non_null (handle->oidc->nonce);
529 GNUNET_free_non_null (handle->oidc->redirect_uri);
530 GNUNET_free_non_null (handle->oidc->response_type);
531 GNUNET_free_non_null (handle->oidc->scope);
532 GNUNET_free_non_null (handle->oidc->state);
533 json_decref (handle->oidc->response);
534 GNUNET_free (handle->oidc);
536 if (NULL != handle->attr_list)
538 for (claim_entry = handle->attr_list->list_head; NULL != claim_entry;)
540 claim_tmp = claim_entry;
541 claim_entry = claim_entry->next;
542 GNUNET_free (claim_tmp->claim);
543 GNUNET_free (claim_tmp);
545 GNUNET_free (handle->attr_list);
547 for (ego_entry = handle->ego_head; NULL != ego_entry;)
550 ego_entry = ego_entry->next;
551 GNUNET_free (ego_tmp->identifier);
552 GNUNET_free (ego_tmp->keystring);
553 GNUNET_free (ego_tmp);
555 GNUNET_free (handle);
559 cleanup_handle_delayed (void *cls)
561 cleanup_handle (cls);
566 * Task run on error, sends error message. Cleans up everything.
568 * @param cls the `struct RequestHandle`
573 struct RequestHandle *handle = cls;
574 struct MHD_Response *resp;
577 GNUNET_asprintf (&json_error,
578 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
580 (NULL != handle->edesc) ? handle->edesc : "",
581 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
582 (NULL != handle->oidc->state) ? handle->oidc->state : "",
583 (NULL != handle->oidc->state) ? "\"" : "");
584 if (0 == handle->response_code)
585 handle->response_code = MHD_HTTP_BAD_REQUEST;
586 resp = GNUNET_REST_create_response (json_error);
587 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
588 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
589 MHD_add_response_header (resp,
590 MHD_HTTP_HEADER_CONTENT_TYPE,
592 handle->proc (handle->proc_cls, resp, handle->response_code);
593 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
594 GNUNET_free (json_error);
599 * Task run on error in userinfo endpoint, sends error header. Cleans up
602 * @param cls the `struct RequestHandle`
605 do_userinfo_error (void *cls)
607 struct RequestHandle *handle = cls;
608 struct MHD_Response *resp;
611 GNUNET_asprintf (&error,
612 "error=\"%s\", error_description=\"%s\"",
614 (NULL != handle->edesc) ? handle->edesc : "");
615 resp = GNUNET_REST_create_response ("");
616 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
617 handle->proc (handle->proc_cls, resp, handle->response_code);
618 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
624 * Task run on error, sends error message and redirects. Cleans up everything.
626 * @param cls the `struct RequestHandle`
629 do_redirect_error (void *cls)
631 struct RequestHandle *handle = cls;
632 struct MHD_Response *resp;
634 GNUNET_asprintf (&redirect,
635 "%s?error=%s&error_description=%s%s%s",
636 handle->oidc->redirect_uri,
639 (NULL != handle->oidc->state) ? "&state=" : "",
640 (NULL != handle->oidc->state) ? handle->oidc->state : "");
641 resp = GNUNET_REST_create_response ("");
642 MHD_add_response_header (resp, "Location", redirect);
643 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
644 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
645 GNUNET_free (redirect);
649 * Task run on timeout, sends error message. Cleans up everything.
651 * @param cls the `struct RequestHandle`
654 do_timeout (void *cls)
656 struct RequestHandle *handle = cls;
658 handle->timeout_task = NULL;
663 * Return attributes for claim
665 * @param cls the request handle
668 return_userinfo_response (void *cls)
671 struct RequestHandle *handle = cls;
672 struct MHD_Response *resp;
674 result_str = json_dumps (handle->oidc->response, 0);
676 resp = GNUNET_REST_create_response (result_str);
677 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
678 GNUNET_free (result_str);
679 cleanup_handle (handle);
684 * Respond to OPTIONS request
686 * @param con_handle the connection handle
688 * @param cls the RequestHandle
691 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
695 struct MHD_Response *resp;
696 struct RequestHandle *handle = cls;
698 // For now, independent of path return all options
699 resp = GNUNET_REST_create_response (NULL);
700 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
701 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
702 cleanup_handle (handle);
708 * Interprets cookie header and pass its identity keystring to handle
711 cookie_identity_interpretation (struct RequestHandle *handle)
713 struct GNUNET_HashCode cache_key;
715 struct GNUNET_TIME_Absolute current_time, *relog_time;
716 char delimiter[] = "; ";
721 // gets identity of login try with cookie
722 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
723 strlen (OIDC_COOKIE_HEADER_KEY),
725 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
732 // splits cookies and find 'Identity' cookie
734 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
736 cookies = GNUNET_strdup (tmp_cookies);
737 token = strtok (cookies, delimiter);
738 handle->oidc->user_cancelled = GNUNET_NO;
739 handle->oidc->login_identity = NULL;
742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
743 "Unable to parse cookie: %s\n",
745 GNUNET_free (cookies);
749 while (NULL != token)
751 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
753 handle->oidc->user_cancelled = GNUNET_YES;
754 GNUNET_free (cookies);
757 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
759 token = strtok (NULL, delimiter);
763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
764 "No cookie value to process: %s\n",
766 GNUNET_free (cookies);
769 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
771 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
774 GNUNET_ERROR_TYPE_WARNING,
775 "Found cookie `%s', but no corresponding expiration entry present...\n",
777 GNUNET_free (cookies);
781 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
782 current_time = GNUNET_TIME_absolute_get ();
783 // 30 min after old login -> redirect to login
784 if (current_time.abs_value_us > relog_time->abs_value_us)
786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
787 "Found cookie `%s', but it is expired.\n",
789 GNUNET_free (cookies);
792 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
793 GNUNET_assert (NULL != value);
794 handle->oidc->login_identity = GNUNET_strdup (value);
795 GNUNET_free (cookies);
799 * Redirects to login page stored in configuration file
802 login_redirect (void *cls)
804 char *login_base_url;
806 struct MHD_Response *resp;
807 struct RequestHandle *handle = cls;
809 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
810 "reclaim-rest-plugin",
814 GNUNET_asprintf (&new_redirect,
815 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
817 OIDC_RESPONSE_TYPE_KEY,
818 handle->oidc->response_type,
820 handle->oidc->client_id,
821 OIDC_REDIRECT_URI_KEY,
822 handle->oidc->redirect_uri,
826 (NULL != handle->oidc->state) ? handle->oidc->state : "",
828 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "");
829 resp = GNUNET_REST_create_response ("");
830 MHD_add_response_header (resp, "Location", new_redirect);
831 GNUNET_free (login_base_url);
835 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
836 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
837 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
838 GNUNET_SCHEDULER_add_now (&do_error, handle);
841 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
842 GNUNET_free (new_redirect);
843 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
847 * Does internal server error when iteration failed.
850 oidc_iteration_error (void *cls)
852 struct RequestHandle *handle = cls;
853 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
854 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
855 GNUNET_SCHEDULER_add_now (&do_error, handle);
860 * Issues ticket and redirects to relying party with the authorization code as
861 * parameter. Otherwise redirects with error
864 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
866 struct RequestHandle *handle = cls;
867 struct MHD_Response *resp;
872 handle->idp_op = NULL;
875 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
876 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
877 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
880 handle->ticket = *ticket;
882 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
883 sizeof (struct GNUNET_RECLAIM_Ticket));
884 // TODO change if more attributes are needed (see max_age)
885 code_string = OIDC_build_authz_code (&handle->priv_key,
888 handle->oidc->nonce);
889 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
890 (NULL != handle->tld))
893 GNUNET_asprintf (&redirect_uri,
894 "%s.%s/%s?%s=%s&state=%s",
895 handle->redirect_prefix,
897 handle->redirect_suffix,
898 handle->oidc->response_type,
900 handle->oidc->state);
904 GNUNET_asprintf (&redirect_uri,
906 handle->oidc->redirect_uri,
907 handle->oidc->response_type,
909 handle->oidc->state);
911 resp = GNUNET_REST_create_response ("");
912 MHD_add_response_header (resp, "Location", redirect_uri);
913 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
914 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
915 GNUNET_free (redirect_uri);
916 GNUNET_free (ticket_str);
917 GNUNET_free (code_string);
921 oidc_collect_finished_cb (void *cls)
923 struct RequestHandle *handle = cls;
924 handle->attr_it = NULL;
925 handle->ticket_it = NULL;
926 if (NULL == handle->attr_list->list_head)
928 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
929 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
930 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
933 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
935 &handle->oidc->client_pkey,
937 &oidc_ticket_issue_cb,
943 * Collects all attributes for an ego if in scope parameter
946 oidc_attr_collect (void *cls,
947 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
948 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
950 struct RequestHandle *handle = cls;
951 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
952 char *scope_variables;
953 char *scope_variable;
954 char delimiter[] = " ";
956 if ((NULL == attr->name) || (NULL == attr->data))
958 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
962 scope_variables = GNUNET_strdup (handle->oidc->scope);
963 scope_variable = strtok (scope_variables, delimiter);
964 while (NULL != scope_variable)
966 if (0 == strcmp (attr->name, scope_variable))
968 scope_variable = strtok (NULL, delimiter);
970 if (NULL == scope_variable)
972 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
973 GNUNET_free (scope_variables);
976 GNUNET_free (scope_variables);
978 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
979 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name,
983 le->claim->id = attr->id;
984 le->claim->version = attr->version;
985 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
986 handle->attr_list->list_tail,
988 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
993 * Checks time and cookie and redirects accordingly
996 code_redirect (void *cls)
998 struct RequestHandle *handle = cls;
999 struct GNUNET_TIME_Absolute current_time;
1000 struct GNUNET_TIME_Absolute *relog_time;
1001 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1002 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1003 struct GNUNET_HashCode cache_key;
1004 char *identity_cookie;
1006 GNUNET_asprintf (&identity_cookie,
1008 handle->oidc->login_identity);
1009 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1010 GNUNET_free (identity_cookie);
1011 // No login time for identity -> redirect to login
1013 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1016 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1017 current_time = GNUNET_TIME_absolute_get ();
1018 // 30 min after old login -> redirect to login
1019 if (current_time.abs_value_us <= relog_time->abs_value_us)
1022 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1029 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1031 GNUNET_strdup ("The cookie of a login identity is not valid");
1032 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1035 // iterate over egos and compare their public key
1036 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1037 handle->ego_entry = handle->ego_entry->next)
1039 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1040 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1043 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1044 handle->idp = GNUNET_RECLAIM_connect (cfg);
1046 GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
1048 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1050 &oidc_iteration_error,
1054 &oidc_collect_finished_cb,
1059 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1067 build_redirect (void *cls)
1069 struct RequestHandle *handle = cls;
1070 struct MHD_Response *resp;
1073 if (GNUNET_YES == handle->oidc->user_cancelled)
1075 if ((NULL != handle->redirect_prefix) &&
1076 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1078 GNUNET_asprintf (&redirect_uri,
1079 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1080 handle->redirect_prefix,
1082 handle->redirect_suffix,
1084 "User denied access",
1085 handle->oidc->state);
1089 GNUNET_asprintf (&redirect_uri,
1090 "%s?error=%s&error_description=%s&state=%s",
1091 handle->oidc->redirect_uri,
1093 "User denied access",
1094 handle->oidc->state);
1096 resp = GNUNET_REST_create_response ("");
1097 MHD_add_response_header (resp, "Location", redirect_uri);
1098 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1099 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1100 GNUNET_free (redirect_uri);
1103 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1108 lookup_redirect_uri_result (void *cls,
1110 const struct GNUNET_GNSRECORD_Data *rd)
1112 struct RequestHandle *handle = cls;
1116 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1118 handle->gns_op = NULL;
1121 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1123 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1124 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1127 for (int i = 0; i < rd_count; i++)
1129 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1131 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1133 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1134 if (NULL == strstr (tmp, handle->oidc->client_id))
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "Redirect uri %s does not contain client_id %s\n",
1139 handle->oidc->client_id);
1143 pos = strrchr (tmp, (unsigned char) '.');
1146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1147 "Redirect uri %s contains client_id but is malformed\n",
1153 handle->redirect_prefix = GNUNET_strdup (tmp);
1154 tmp_key_str = pos + 1;
1155 pos = strchr (tmp_key_str, (unsigned char) '/');
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159 "Redirect uri %s contains client_id but is malformed\n",
1165 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1167 GNUNET_STRINGS_string_to_data (tmp_key_str,
1168 strlen (tmp_key_str),
1170 sizeof (redirect_zone));
1172 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1176 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1178 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1179 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1184 * Initiate redirect back to client.
1187 client_redirect (void *cls)
1189 struct RequestHandle *handle = cls;
1191 /* Lookup client redirect uri to verify request */
1193 GNUNET_GNS_lookup (handle->gns_handle,
1194 GNUNET_GNS_EMPTY_LABEL_AT,
1195 &handle->oidc->client_pkey,
1196 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1197 GNUNET_GNS_LO_DEFAULT,
1198 &lookup_redirect_uri_result,
1203 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1205 struct GNUNET_HashCode hc;
1207 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1208 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1213 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1216 return GNUNET_strdup (value);
1221 * Iteration over all results finished, build final
1224 * @param cls the `struct RequestHandle`
1227 build_authz_response (void *cls)
1229 struct RequestHandle *handle = cls;
1230 struct GNUNET_HashCode cache_key;
1232 char *expected_scope;
1233 char delimiter[] = " ";
1234 int number_of_ignored_parameter, iterator;
1237 // REQUIRED value: redirect_uri
1238 handle->oidc->redirect_uri =
1239 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1240 if (NULL == handle->oidc->redirect_uri)
1242 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1243 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1244 GNUNET_SCHEDULER_add_now (&do_error, handle);
1248 // REQUIRED value: response_type
1249 handle->oidc->response_type =
1250 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1251 if (NULL == handle->oidc->response_type)
1253 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1254 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1255 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1259 // REQUIRED value: scope
1260 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1261 if (NULL == handle->oidc->scope)
1263 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1264 handle->edesc = GNUNET_strdup ("missing parameter scope");
1265 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1269 // OPTIONAL value: nonce
1270 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1272 // TODO check other values if needed
1273 number_of_ignored_parameter =
1274 sizeof (OIDC_ignored_parameter_array) / sizeof (char *);
1275 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1277 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1278 strlen (OIDC_ignored_parameter_array[iterator]),
1281 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1285 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1286 GNUNET_asprintf (&handle->edesc,
1287 "Server will not handle parameter: %s",
1288 OIDC_ignored_parameter_array[iterator]);
1289 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1294 // We only support authorization code flows.
1295 if (0 != strcmp (handle->oidc->response_type,
1296 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1298 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1299 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1300 "obtaining this authorization code.");
1301 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1305 // Checks if scope contains 'openid'
1306 expected_scope = GNUNET_strdup (handle->oidc->scope);
1308 test = strtok (expected_scope, delimiter);
1309 while (NULL != test)
1311 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1313 test = strtok (NULL, delimiter);
1317 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1319 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1320 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1321 GNUNET_free (expected_scope);
1325 GNUNET_free (expected_scope);
1326 if ((NULL == handle->oidc->login_identity) &&
1327 (GNUNET_NO == handle->oidc->user_cancelled))
1328 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1330 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1334 * Iterate over tlds in config
1337 tld_iter (void *cls, const char *section, const char *option, const char *value)
1339 struct RequestHandle *handle = cls;
1340 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1343 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1348 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1349 handle->tld = GNUNET_strdup (option + 1);
1353 * Responds to authorization GET and url-encoded POST request
1355 * @param con_handle the connection handle
1356 * @param url the url
1357 * @param cls the RequestHandle
1360 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1364 struct RequestHandle *handle = cls;
1365 struct EgoEntry *tmp_ego;
1366 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1367 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1369 cookie_identity_interpretation (handle);
1371 // RECOMMENDED value: state - REQUIRED for answers
1372 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1374 // REQUIRED value: client_id
1375 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1376 if (NULL == handle->oidc->client_id)
1378 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1379 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1380 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1381 GNUNET_SCHEDULER_add_now (&do_error, handle);
1386 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1388 handle->oidc->client_id),
1389 &handle->oidc->client_pkey))
1391 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1392 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1393 "authorization code using this method.");
1394 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1395 GNUNET_SCHEDULER_add_now (&do_error, handle);
1399 // If we know this identity, translated the corresponding TLD
1400 // TODO: We might want to have a reverse lookup functionality for TLDs?
1401 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1403 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1404 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1405 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1407 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1408 handle->ego_entry = handle->ego_tail;
1411 if (NULL == handle->tld)
1412 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1413 if (NULL == handle->tld)
1414 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1415 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1419 * Combines an identity with a login time and responds OK to login request
1421 * @param con_handle the connection handle
1422 * @param url the url
1423 * @param cls the RequestHandle
1426 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1430 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1431 struct RequestHandle *handle = cls;
1432 struct GNUNET_HashCode cache_key;
1433 struct GNUNET_TIME_Absolute *current_time;
1434 struct GNUNET_TIME_Absolute *last_time;
1440 char term_data[handle->rest_handle->data_size + 1];
1441 term_data[handle->rest_handle->data_size] = '\0';
1442 GNUNET_memcpy (term_data,
1443 handle->rest_handle->data,
1444 handle->rest_handle->data_size);
1445 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1446 identity = json_object_get (root, "identity");
1447 if (! json_is_string (identity))
1449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1450 "Error parsing json string from %s\n",
1452 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1454 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1457 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1458 GNUNET_asprintf (&header_val,
1461 OIDC_COOKIE_EXPIRATION);
1462 MHD_add_response_header (resp, "Set-Cookie", header_val);
1463 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1464 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1466 if (0 != strcmp (json_string_value (identity), "Denied"))
1468 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1469 *current_time = GNUNET_TIME_relative_to_absolute (
1470 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1471 OIDC_COOKIE_EXPIRATION));
1473 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1474 GNUNET_free_non_null (last_time);
1475 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1480 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1481 GNUNET_free (cookie);
1482 GNUNET_free (header_val);
1484 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1488 check_authorization (struct RequestHandle *handle,
1489 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1491 struct GNUNET_HashCode cache_key;
1492 char *authorization;
1494 char *basic_authorization;
1497 char *expected_pass;
1499 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1500 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1502 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1506 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1507 handle->edesc = GNUNET_strdup ("missing authorization");
1508 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1509 return GNUNET_SYSERR;
1512 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1515 // split header in "Basic" and [content]
1516 credentials = strtok (authorization, " ");
1517 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1519 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1520 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1521 return GNUNET_SYSERR;
1523 credentials = strtok (NULL, " ");
1524 if (NULL == credentials)
1526 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1527 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1528 return GNUNET_SYSERR;
1530 GNUNET_STRINGS_base64_decode (credentials,
1531 strlen (credentials),
1532 (void **) &basic_authorization);
1534 if (NULL == basic_authorization)
1536 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1537 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1538 return GNUNET_SYSERR;
1540 client_id = strtok (basic_authorization, ":");
1541 if (NULL == client_id)
1543 GNUNET_free_non_null (basic_authorization);
1544 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1545 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1546 return GNUNET_SYSERR;
1548 pass = strtok (NULL, ":");
1551 GNUNET_free_non_null (basic_authorization);
1552 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1553 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1554 return GNUNET_SYSERR;
1557 // check client password
1558 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1559 "reclaim-rest-plugin",
1560 "OIDC_CLIENT_SECRET",
1563 if (0 != strcmp (expected_pass, pass))
1565 GNUNET_free_non_null (basic_authorization);
1566 GNUNET_free (expected_pass);
1567 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1568 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1569 return GNUNET_SYSERR;
1571 GNUNET_free (expected_pass);
1575 GNUNET_free_non_null (basic_authorization);
1576 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1577 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1578 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1579 return GNUNET_SYSERR;
1583 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1584 handle->ego_entry = handle->ego_entry->next)
1586 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1589 if (NULL == handle->ego_entry)
1591 GNUNET_free_non_null (basic_authorization);
1592 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1593 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1594 return GNUNET_SYSERR;
1596 GNUNET_STRINGS_string_to_data (client_id,
1599 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1601 GNUNET_free (basic_authorization);
1605 const struct EgoEntry *
1606 find_ego (struct RequestHandle *handle,
1607 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1609 struct EgoEntry *ego_entry;
1610 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1612 for (ego_entry = handle->ego_head; NULL != ego_entry;
1613 ego_entry = ego_entry->next)
1615 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1616 if (0 == GNUNET_memcmp (&pub_key, test_key))
1623 persist_access_token (const struct RequestHandle *handle,
1624 const char *access_token,
1625 const struct GNUNET_RECLAIM_Ticket *ticket)
1627 struct GNUNET_HashCode hc;
1628 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1630 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1631 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1632 *ticketbuf = *ticket;
1633 GNUNET_assert (GNUNET_SYSERR !=
1634 GNUNET_CONTAINER_multihashmap_put (
1635 OIDC_access_token_map,
1638 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1642 * Responds to token url-encoded POST request
1644 * @param con_handle the connection handle
1645 * @param url the url
1646 * @param cls the RequestHandle
1649 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1653 struct RequestHandle *handle = cls;
1654 const struct EgoEntry *ego_entry;
1655 struct GNUNET_TIME_Relative expiration_time;
1656 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl;
1657 struct GNUNET_RECLAIM_Ticket ticket;
1658 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1659 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1660 struct GNUNET_HashCode cache_key;
1661 struct MHD_Response *resp;
1664 char *json_response;
1671 * Check Authorization
1673 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1676 "OIDC authorization for token endpoint failed\n");
1677 GNUNET_SCHEDULER_add_now (&do_error, handle);
1685 // TODO Do not allow multiple equal parameter names
1686 // REQUIRED grant_type
1687 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1688 strlen (OIDC_GRANT_TYPE_KEY),
1690 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1691 if (NULL == grant_type)
1693 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1694 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1695 handle->response_code = MHD_HTTP_BAD_REQUEST;
1696 GNUNET_SCHEDULER_add_now (&do_error, handle);
1700 // Check parameter grant_type == "authorization_code"
1701 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1703 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1704 handle->response_code = MHD_HTTP_BAD_REQUEST;
1705 GNUNET_free (grant_type);
1706 GNUNET_SCHEDULER_add_now (&do_error, handle);
1709 GNUNET_free (grant_type);
1711 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1714 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1715 handle->edesc = GNUNET_strdup ("missing parameter code");
1716 handle->response_code = MHD_HTTP_BAD_REQUEST;
1717 GNUNET_SCHEDULER_add_now (&do_error, handle);
1720 ego_entry = find_ego (handle, &cid);
1721 if (NULL == ego_entry)
1723 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1724 handle->edesc = GNUNET_strdup ("Unknown client");
1725 handle->response_code = MHD_HTTP_BAD_REQUEST;
1727 GNUNET_SCHEDULER_add_now (&do_error, handle);
1730 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1732 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, &ticket, &cl, &nonce))
1734 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1735 handle->edesc = GNUNET_strdup ("invalid code");
1736 handle->response_code = MHD_HTTP_BAD_REQUEST;
1738 GNUNET_SCHEDULER_add_now (&do_error, handle);
1744 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1745 "reclaim-rest-plugin",
1749 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1750 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1751 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1752 GNUNET_SCHEDULER_add_now (&do_error, handle);
1757 // TODO OPTIONAL acr,amr,azp
1758 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1759 "reclaim-rest-plugin",
1763 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1764 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1765 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1766 GNUNET_SCHEDULER_add_now (&do_error, handle);
1769 id_token = OIDC_id_token_new (&ticket.audience,
1773 (NULL != nonce) ? nonce : NULL,
1775 access_token = OIDC_access_token_new ();
1776 OIDC_build_token_response (access_token,
1781 persist_access_token (handle, access_token, &ticket);
1782 resp = GNUNET_REST_create_response (json_response);
1783 MHD_add_response_header (resp, "Cache-Control", "no-store");
1784 MHD_add_response_header (resp, "Pragma", "no-cache");
1785 MHD_add_response_header (resp, "Content-Type", "application/json");
1786 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1787 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (cl);
1788 GNUNET_free (access_token);
1789 GNUNET_free (json_response);
1790 GNUNET_free (id_token);
1791 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1795 * Collects claims and stores them in handle
1798 consume_ticket (void *cls,
1799 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1800 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
1802 struct RequestHandle *handle = cls;
1806 if (NULL == identity)
1808 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1811 tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
1814 value = json_string (tmp_value);
1815 json_object_set_new (handle->oidc->response, attr->name, value);
1816 GNUNET_free (tmp_value);
1820 * Responds to userinfo GET and url-encoded POST request
1822 * @param con_handle the connection handle
1823 * @param url the url
1824 * @param cls the RequestHandle
1827 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1831 // TODO expiration time
1832 struct RequestHandle *handle = cls;
1833 char delimiter[] = " ";
1834 struct GNUNET_HashCode cache_key;
1835 char *authorization;
1836 char *authorization_type;
1837 char *authorization_access_token;
1838 struct GNUNET_RECLAIM_Ticket *ticket;
1839 const struct EgoEntry *ego_entry;
1840 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1842 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1843 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1845 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1849 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1850 handle->edesc = GNUNET_strdup ("No Access Token");
1851 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1852 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1856 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1859 // split header in "Bearer" and access_token
1860 authorization = GNUNET_strdup (authorization);
1861 authorization_type = strtok (authorization, delimiter);
1862 if ((NULL == authorization_type) ||
1863 (0 != strcmp ("Bearer", authorization_type)))
1865 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1866 handle->edesc = GNUNET_strdup ("No Access Token");
1867 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1868 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1869 GNUNET_free (authorization);
1872 authorization_access_token = strtok (NULL, delimiter);
1873 if (NULL == authorization_access_token)
1875 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1876 handle->edesc = GNUNET_strdup ("Access token missing");
1877 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1878 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1879 GNUNET_free (authorization);
1883 GNUNET_CRYPTO_hash (authorization_access_token,
1884 strlen (authorization_access_token),
1887 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
1890 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1891 handle->edesc = GNUNET_strdup ("The access token expired");
1892 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1893 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1894 GNUNET_free (authorization);
1898 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
1899 GNUNET_assert (NULL != ticket);
1900 ego_entry = find_ego (handle, &ticket->audience);
1901 if (NULL == ego_entry)
1903 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1904 handle->edesc = GNUNET_strdup ("The access token expired");
1905 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1906 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1907 GNUNET_free (authorization);
1911 handle->idp = GNUNET_RECLAIM_connect (cfg);
1912 handle->oidc->response = json_object ();
1913 json_object_set_new (handle->oidc->response,
1915 json_string (ego_entry->keystring));
1916 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1917 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
1922 GNUNET_free (authorization);
1927 * Handle rest request
1929 * @param handle the request handle
1932 init_cont (struct RequestHandle *handle)
1934 struct GNUNET_REST_RequestHandlerError err;
1935 static const struct GNUNET_REST_RequestHandler handlers[] =
1936 {{MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint},
1937 {MHD_HTTP_METHOD_POST,
1938 GNUNET_REST_API_NS_AUTHORIZE,
1939 &authorize_endpoint}, // url-encoded
1940 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont},
1941 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint},
1942 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint},
1943 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint},
1944 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont},
1945 GNUNET_REST_HANDLER_END};
1948 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
1950 handle->response_code = err.error_code;
1951 GNUNET_SCHEDULER_add_now (&do_error, handle);
1956 * If listing is enabled, prints information about the egos.
1958 * This function is initially called for all egos and then again
1959 * whenever a ego's identifier changes or if it is deleted. At the
1960 * end of the initial pass over all egos, the function is once called
1961 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
1962 * be invoked in the future or that there was an error.
1964 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
1965 * this function is only called ONCE, and 'NULL' being passed in
1966 * 'ego' does indicate an error (i.e. name is taken or no default
1967 * value is known). If 'ego' is non-NULL and if '*ctx'
1968 * is set in those callbacks, the value WILL be passed to a subsequent
1969 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
1970 * that one was not NULL).
1972 * When an identity is renamed, this function is called with the
1973 * (known) ego but the NEW identifier.
1975 * When an identity is deleted, this function is called with the
1976 * (known) ego and "NULL" for the 'identifier'. In this case,
1977 * the 'ego' is henceforth invalid (and the 'ctx' should also be
1980 * @param cls closure
1981 * @param ego ego handle
1982 * @param ctx context for application to store data for this ego
1983 * (during the lifetime of this process, initially NULL)
1984 * @param identifier identifier assigned by the user for this ego,
1985 * NULL if the user just deleted the ego and it
1986 * must thus no longer be used
1989 list_ego (void *cls,
1990 struct GNUNET_IDENTITY_Ego *ego,
1992 const char *identifier)
1994 struct RequestHandle *handle = cls;
1995 struct EgoEntry *ego_entry;
1996 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
1998 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2000 handle->state = ID_REST_STATE_POST_INIT;
2004 GNUNET_assert (NULL != ego);
2005 if (ID_REST_STATE_INIT == handle->state)
2007 ego_entry = GNUNET_new (struct EgoEntry);
2008 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2009 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2010 ego_entry->ego = ego;
2011 ego_entry->identifier = GNUNET_strdup (identifier);
2012 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2017 /* Ego renamed or added */
2018 if (identifier != NULL)
2020 for (ego_entry = handle->ego_head; NULL != ego_entry;
2021 ego_entry = ego_entry->next)
2023 if (ego_entry->ego == ego)
2026 GNUNET_free (ego_entry->identifier);
2027 ego_entry->identifier = GNUNET_strdup (identifier);
2031 if (NULL == ego_entry)
2034 ego_entry = GNUNET_new (struct EgoEntry);
2035 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2036 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2037 ego_entry->ego = ego;
2038 ego_entry->identifier = GNUNET_strdup (identifier);
2039 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2047 for (ego_entry = handle->ego_head; NULL != ego_entry;
2048 ego_entry = ego_entry->next)
2050 if (ego_entry->ego == ego)
2053 if (NULL != ego_entry)
2054 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2061 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2062 GNUNET_REST_ResultProcessor proc,
2065 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2066 handle->oidc = GNUNET_new (struct OIDC_Variables);
2067 if (NULL == OIDC_cookie_jar_map)
2068 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2069 if (NULL == OIDC_access_token_map)
2070 OIDC_access_token_map =
2071 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2072 handle->response_code = 0;
2073 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2074 handle->proc_cls = proc_cls;
2075 handle->proc = proc;
2076 handle->state = ID_REST_STATE_INIT;
2077 handle->rest_handle = rest_handle;
2079 handle->url = GNUNET_strdup (rest_handle->url);
2080 if (handle->url[strlen (handle->url) - 1] == '/')
2081 handle->url[strlen (handle->url) - 1] = '\0';
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2083 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2084 handle->gns_handle = GNUNET_GNS_connect (cfg);
2085 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2086 handle->timeout_task =
2087 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2092 * Entry point for the plugin.
2094 * @param cls Config info
2095 * @return NULL on error, otherwise the plugin context
2098 libgnunet_plugin_rest_openid_connect_init (void *cls)
2100 static struct Plugin plugin;
2101 struct GNUNET_REST_Plugin *api;
2104 if (NULL != plugin.cfg)
2105 return NULL; /* can only initialize once! */
2106 memset (&plugin, 0, sizeof (struct Plugin));
2108 api = GNUNET_new (struct GNUNET_REST_Plugin);
2110 api->name = GNUNET_REST_API_NS_OIDC;
2111 api->process_request = &rest_identity_process_request;
2112 GNUNET_asprintf (&allow_methods,
2113 "%s, %s, %s, %s, %s",
2114 MHD_HTTP_METHOD_GET,
2115 MHD_HTTP_METHOD_POST,
2116 MHD_HTTP_METHOD_PUT,
2117 MHD_HTTP_METHOD_DELETE,
2118 MHD_HTTP_METHOD_OPTIONS);
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 _ ("OpenID Connect REST API initialized\n"));
2127 * Exit point from the plugin.
2129 * @param cls the plugin context (as returned by "init")
2130 * @return always NULL
2133 libgnunet_plugin_rest_openid_connect_done (void *cls)
2135 struct GNUNET_REST_Plugin *api = cls;
2136 struct Plugin *plugin = api->cls;
2139 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2142 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2143 while (GNUNET_YES ==
2144 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2145 GNUNET_free_non_null (value);
2146 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2147 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2150 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2151 while (GNUNET_YES ==
2152 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2153 GNUNET_free_non_null (value);
2154 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2155 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2156 GNUNET_free_non_null (allow_methods);
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "OpenID Connect REST plugin is finished\n");
2163 /* end of plugin_rest_openid_connect.c */