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_non_null (handle->attr_it);
556 GNUNET_free (handle);
560 cleanup_handle_delayed (void *cls)
562 cleanup_handle (cls);
567 * Task run on error, sends error message. Cleans up everything.
569 * @param cls the `struct RequestHandle`
574 struct RequestHandle *handle = cls;
575 struct MHD_Response *resp;
578 GNUNET_asprintf (&json_error,
579 "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}",
581 (NULL != handle->edesc) ? handle->edesc : "",
582 (NULL != handle->oidc->state) ? ", \"state\":\"" : "",
583 (NULL != handle->oidc->state) ? handle->oidc->state : "",
584 (NULL != handle->oidc->state) ? "\"" : "");
585 if (0 == handle->response_code)
586 handle->response_code = MHD_HTTP_BAD_REQUEST;
587 resp = GNUNET_REST_create_response (json_error);
588 if (MHD_HTTP_UNAUTHORIZED == handle->response_code)
589 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic");
590 MHD_add_response_header (resp,
591 MHD_HTTP_HEADER_CONTENT_TYPE,
593 handle->proc (handle->proc_cls, resp, handle->response_code);
594 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
595 GNUNET_free (json_error);
600 * Task run on error in userinfo endpoint, sends error header. Cleans up
603 * @param cls the `struct RequestHandle`
606 do_userinfo_error (void *cls)
608 struct RequestHandle *handle = cls;
609 struct MHD_Response *resp;
612 GNUNET_asprintf (&error,
613 "error=\"%s\", error_description=\"%s\"",
615 (NULL != handle->edesc) ? handle->edesc : "");
616 resp = GNUNET_REST_create_response ("");
617 MHD_add_response_header (resp, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Bearer");
618 handle->proc (handle->proc_cls, resp, handle->response_code);
619 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
625 * Task run on error, sends error message and redirects. Cleans up everything.
627 * @param cls the `struct RequestHandle`
630 do_redirect_error (void *cls)
632 struct RequestHandle *handle = cls;
633 struct MHD_Response *resp;
635 GNUNET_asprintf (&redirect,
636 "%s?error=%s&error_description=%s%s%s",
637 handle->oidc->redirect_uri,
640 (NULL != handle->oidc->state) ? "&state=" : "",
641 (NULL != handle->oidc->state) ? handle->oidc->state : "");
642 resp = GNUNET_REST_create_response ("");
643 MHD_add_response_header (resp, "Location", redirect);
644 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
645 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
646 GNUNET_free (redirect);
650 * Task run on timeout, sends error message. Cleans up everything.
652 * @param cls the `struct RequestHandle`
655 do_timeout (void *cls)
657 struct RequestHandle *handle = cls;
659 handle->timeout_task = NULL;
664 * Return attributes for claim
666 * @param cls the request handle
669 return_userinfo_response (void *cls)
672 struct RequestHandle *handle = cls;
673 struct MHD_Response *resp;
675 result_str = json_dumps (handle->oidc->response, 0);
677 resp = GNUNET_REST_create_response (result_str);
678 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
679 GNUNET_free (result_str);
680 cleanup_handle (handle);
685 * Respond to OPTIONS request
687 * @param con_handle the connection handle
689 * @param cls the RequestHandle
692 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
696 struct MHD_Response *resp;
697 struct RequestHandle *handle = cls;
699 // For now, independent of path return all options
700 resp = GNUNET_REST_create_response (NULL);
701 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
702 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
703 cleanup_handle (handle);
709 * Interprets cookie header and pass its identity keystring to handle
712 cookie_identity_interpretation (struct RequestHandle *handle)
714 struct GNUNET_HashCode cache_key;
716 struct GNUNET_TIME_Absolute current_time, *relog_time;
717 char delimiter[] = "; ";
722 // gets identity of login try with cookie
723 GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY,
724 strlen (OIDC_COOKIE_HEADER_KEY),
726 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cookie found\n");
733 // splits cookies and find 'Identity' cookie
735 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
737 cookies = GNUNET_strdup (tmp_cookies);
738 token = strtok (cookies, delimiter);
739 handle->oidc->user_cancelled = GNUNET_NO;
740 handle->oidc->login_identity = NULL;
743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
744 "Unable to parse cookie: %s\n",
746 GNUNET_free (cookies);
750 while (NULL != token)
752 if (0 == strcmp (token, OIDC_COOKIE_HEADER_ACCESS_DENIED))
754 handle->oidc->user_cancelled = GNUNET_YES;
755 GNUNET_free (cookies);
758 if (NULL != strstr (token, OIDC_COOKIE_HEADER_INFORMATION_KEY))
760 token = strtok (NULL, delimiter);
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 "No cookie value to process: %s\n",
767 GNUNET_free (cookies);
770 GNUNET_CRYPTO_hash (token, strlen (token), &cache_key);
772 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
775 GNUNET_ERROR_TYPE_WARNING,
776 "Found cookie `%s', but no corresponding expiration entry present...\n",
778 GNUNET_free (cookies);
782 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
783 current_time = GNUNET_TIME_absolute_get ();
784 // 30 min after old login -> redirect to login
785 if (current_time.abs_value_us > relog_time->abs_value_us)
787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788 "Found cookie `%s', but it is expired.\n",
790 GNUNET_free (cookies);
793 value = strtok (token, OIDC_COOKIE_HEADER_INFORMATION_KEY);
794 GNUNET_assert (NULL != value);
795 handle->oidc->login_identity = GNUNET_strdup (value);
796 GNUNET_free (cookies);
800 * Redirects to login page stored in configuration file
803 login_redirect (void *cls)
805 char *login_base_url;
807 struct MHD_Response *resp;
808 struct RequestHandle *handle = cls;
810 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
811 "reclaim-rest-plugin",
815 GNUNET_asprintf (&new_redirect,
816 "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
818 OIDC_RESPONSE_TYPE_KEY,
819 handle->oidc->response_type,
821 handle->oidc->client_id,
822 OIDC_REDIRECT_URI_KEY,
823 handle->oidc->redirect_uri,
827 (NULL != handle->oidc->state) ? handle->oidc->state : "",
829 (NULL != handle->oidc->nonce) ? handle->oidc->nonce : "");
830 resp = GNUNET_REST_create_response ("");
831 MHD_add_response_header (resp, "Location", new_redirect);
832 GNUNET_free (login_base_url);
836 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
837 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
838 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
839 GNUNET_SCHEDULER_add_now (&do_error, handle);
842 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
843 GNUNET_free (new_redirect);
844 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
848 * Does internal server error when iteration failed.
851 oidc_iteration_error (void *cls)
853 struct RequestHandle *handle = cls;
854 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
855 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
856 GNUNET_SCHEDULER_add_now (&do_error, handle);
861 * Issues ticket and redirects to relying party with the authorization code as
862 * parameter. Otherwise redirects with error
865 oidc_ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
867 struct RequestHandle *handle = cls;
868 struct MHD_Response *resp;
873 handle->idp_op = NULL;
874 handle->ticket = *ticket;
877 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
878 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
879 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
883 GNUNET_STRINGS_data_to_string_alloc (&handle->ticket,
884 sizeof (struct GNUNET_RECLAIM_Ticket));
885 // TODO change if more attributes are needed (see max_age)
886 code_string = OIDC_build_authz_code (&handle->priv_key,
889 handle->oidc->nonce);
890 if ((NULL != handle->redirect_prefix) && (NULL != handle->redirect_suffix) &&
891 (NULL != handle->tld))
894 GNUNET_asprintf (&redirect_uri,
895 "%s.%s/%s?%s=%s&state=%s",
896 handle->redirect_prefix,
898 handle->redirect_suffix,
899 handle->oidc->response_type,
901 handle->oidc->state);
905 GNUNET_asprintf (&redirect_uri,
907 handle->oidc->redirect_uri,
908 handle->oidc->response_type,
910 handle->oidc->state);
912 resp = GNUNET_REST_create_response ("");
913 MHD_add_response_header (resp, "Location", redirect_uri);
914 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
915 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
916 GNUNET_free (redirect_uri);
917 GNUNET_free (ticket_str);
918 GNUNET_free (code_string);
922 oidc_collect_finished_cb (void *cls)
924 struct RequestHandle *handle = cls;
925 handle->attr_it = NULL;
926 handle->ticket_it = NULL;
927 if (NULL == handle->attr_list->list_head)
929 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
930 handle->edesc = GNUNET_strdup ("The requested scope is not available.");
931 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
934 handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp,
936 &handle->oidc->client_pkey,
938 &oidc_ticket_issue_cb,
944 * Collects all attributes for an ego if in scope parameter
947 oidc_attr_collect (void *cls,
948 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
949 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
951 struct RequestHandle *handle = cls;
952 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
953 char *scope_variables;
954 char *scope_variable;
955 char delimiter[] = " ";
957 if ((NULL == attr->name) || (NULL == attr->data))
959 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
963 scope_variables = GNUNET_strdup (handle->oidc->scope);
964 scope_variable = strtok (scope_variables, delimiter);
965 while (NULL != scope_variable)
967 if (0 == strcmp (attr->name, scope_variable))
969 scope_variable = strtok (NULL, delimiter);
971 if (NULL == scope_variable)
973 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
974 GNUNET_free (scope_variables);
977 GNUNET_free (scope_variables);
979 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
980 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name,
984 le->claim->id = attr->id;
985 le->claim->version = attr->version;
986 GNUNET_CONTAINER_DLL_insert (handle->attr_list->list_head,
987 handle->attr_list->list_tail,
989 GNUNET_RECLAIM_get_attributes_next (handle->attr_it);
994 * Checks time and cookie and redirects accordingly
997 code_redirect (void *cls)
999 struct RequestHandle *handle = cls;
1000 struct GNUNET_TIME_Absolute current_time;
1001 struct GNUNET_TIME_Absolute *relog_time;
1002 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1003 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pkey;
1004 struct GNUNET_HashCode cache_key;
1005 char *identity_cookie;
1007 GNUNET_asprintf (&identity_cookie,
1009 handle->oidc->login_identity);
1010 GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key);
1011 GNUNET_free (identity_cookie);
1012 // No login time for identity -> redirect to login
1014 GNUNET_CONTAINER_multihashmap_contains (OIDC_cookie_jar_map, &cache_key))
1017 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1018 current_time = GNUNET_TIME_absolute_get ();
1019 // 30 min after old login -> redirect to login
1020 if (current_time.abs_value_us <= relog_time->abs_value_us)
1023 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc
1030 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_COOKIE);
1032 GNUNET_strdup ("The cookie of a login identity is not valid");
1033 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1036 // iterate over egos and compare their public key
1037 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1038 handle->ego_entry = handle->ego_entry->next)
1040 GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey);
1041 if (0 == GNUNET_memcmp (&ego_pkey, &pubkey))
1044 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego);
1045 handle->idp = GNUNET_RECLAIM_connect (cfg);
1047 GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
1049 GNUNET_RECLAIM_get_attributes_start (handle->idp,
1051 &oidc_iteration_error,
1055 &oidc_collect_finished_cb,
1060 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1068 build_redirect (void *cls)
1070 struct RequestHandle *handle = cls;
1071 struct MHD_Response *resp;
1074 if (GNUNET_YES == handle->oidc->user_cancelled)
1076 if ((NULL != handle->redirect_prefix) &&
1077 (NULL != handle->redirect_suffix) && (NULL != handle->tld))
1079 GNUNET_asprintf (&redirect_uri,
1080 "%s.%s/%s?error=%s&error_description=%s&state=%s",
1081 handle->redirect_prefix,
1083 handle->redirect_suffix,
1085 "User denied access",
1086 handle->oidc->state);
1090 GNUNET_asprintf (&redirect_uri,
1091 "%s?error=%s&error_description=%s&state=%s",
1092 handle->oidc->redirect_uri,
1094 "User denied access",
1095 handle->oidc->state);
1097 resp = GNUNET_REST_create_response ("");
1098 MHD_add_response_header (resp, "Location", redirect_uri);
1099 handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND);
1100 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1101 GNUNET_free (redirect_uri);
1104 GNUNET_SCHEDULER_add_now (&code_redirect, handle);
1109 lookup_redirect_uri_result (void *cls,
1111 const struct GNUNET_GNSRECORD_Data *rd)
1113 struct RequestHandle *handle = cls;
1117 struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone;
1119 handle->gns_op = NULL;
1122 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1124 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1125 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1128 for (int i = 0; i < rd_count; i++)
1130 if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type)
1132 if (0 != strncmp (rd[i].data, handle->oidc->redirect_uri, rd[i].data_size))
1134 tmp = GNUNET_strndup (rd[i].data, rd[i].data_size);
1135 if (NULL == strstr (tmp, handle->oidc->client_id))
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "Redirect uri %s does not contain client_id %s\n",
1140 handle->oidc->client_id);
1144 pos = strrchr (tmp, (unsigned char) '.');
1147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1148 "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",
1164 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1166 GNUNET_STRINGS_string_to_data (tmp_key_str,
1167 strlen (tmp_key_str),
1169 sizeof (redirect_zone));
1171 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1175 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1177 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1178 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1183 * Initiate redirect back to client.
1186 client_redirect (void *cls)
1188 struct RequestHandle *handle = cls;
1190 /* Lookup client redirect uri to verify request */
1192 GNUNET_GNS_lookup (handle->gns_handle,
1193 GNUNET_GNS_EMPTY_LABEL_AT,
1194 &handle->oidc->client_pkey,
1195 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1196 GNUNET_GNS_LO_DEFAULT,
1197 &lookup_redirect_uri_result,
1202 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1204 struct GNUNET_HashCode hc;
1206 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1207 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1212 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1215 return GNUNET_strdup (value);
1220 * Iteration over all results finished, build final
1223 * @param cls the `struct RequestHandle`
1226 build_authz_response (void *cls)
1228 struct RequestHandle *handle = cls;
1229 struct GNUNET_HashCode cache_key;
1231 char *expected_scope;
1232 char delimiter[] = " ";
1233 int number_of_ignored_parameter, iterator;
1236 // REQUIRED value: redirect_uri
1237 handle->oidc->redirect_uri =
1238 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1239 if (NULL == handle->oidc->redirect_uri)
1241 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1242 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1243 GNUNET_SCHEDULER_add_now (&do_error, handle);
1247 // REQUIRED value: response_type
1248 handle->oidc->response_type =
1249 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1250 if (NULL == handle->oidc->response_type)
1252 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1253 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1254 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1258 // REQUIRED value: scope
1259 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1260 if (NULL == handle->oidc->scope)
1262 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1263 handle->edesc = GNUNET_strdup ("missing parameter scope");
1264 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1268 // OPTIONAL value: nonce
1269 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1271 // TODO check other values if needed
1272 number_of_ignored_parameter =
1273 sizeof (OIDC_ignored_parameter_array) / sizeof (char *);
1274 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1276 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1277 strlen (OIDC_ignored_parameter_array[iterator]),
1280 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1284 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1285 GNUNET_asprintf (&handle->edesc,
1286 "Server will not handle parameter: %s",
1287 OIDC_ignored_parameter_array[iterator]);
1288 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1293 // We only support authorization code flows.
1294 if (0 != strcmp (handle->oidc->response_type,
1295 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1297 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1298 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1299 "obtaining this authorization code.");
1300 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1304 // Checks if scope contains 'openid'
1305 expected_scope = GNUNET_strdup (handle->oidc->scope);
1307 test = strtok (expected_scope, delimiter);
1308 while (NULL != test)
1310 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1312 test = strtok (NULL, delimiter);
1316 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1318 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1319 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1320 GNUNET_free (expected_scope);
1324 GNUNET_free (expected_scope);
1325 if ((NULL == handle->oidc->login_identity) &&
1326 (GNUNET_NO == handle->oidc->user_cancelled))
1327 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1329 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1333 * Iterate over tlds in config
1336 tld_iter (void *cls, const char *section, const char *option, const char *value)
1338 struct RequestHandle *handle = cls;
1339 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1342 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1347 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1348 handle->tld = GNUNET_strdup (option + 1);
1352 * Responds to authorization GET and url-encoded POST request
1354 * @param con_handle the connection handle
1355 * @param url the url
1356 * @param cls the RequestHandle
1359 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1363 struct RequestHandle *handle = cls;
1364 struct EgoEntry *tmp_ego;
1365 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1366 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1368 cookie_identity_interpretation (handle);
1370 // RECOMMENDED value: state - REQUIRED for answers
1371 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1373 // REQUIRED value: client_id
1374 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1375 if (NULL == handle->oidc->client_id)
1377 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1378 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1379 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1380 GNUNET_SCHEDULER_add_now (&do_error, handle);
1385 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1387 handle->oidc->client_id),
1388 &handle->oidc->client_pkey))
1390 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1391 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1392 "authorization code using this method.");
1393 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1394 GNUNET_SCHEDULER_add_now (&do_error, handle);
1398 handle->ego_entry = handle->ego_head;
1400 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
1401 // If we know this identity, translated the corresponding TLD
1402 // TODO: We might want to have a reverse lookup functionality for TLDs?
1403 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1405 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1406 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1407 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1409 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1410 handle->ego_entry = handle->ego_tail;
1413 if (NULL == handle->tld)
1414 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1415 if (NULL == handle->tld)
1416 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1417 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1421 * Combines an identity with a login time and responds OK to login request
1423 * @param con_handle the connection handle
1424 * @param url the url
1425 * @param cls the RequestHandle
1428 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1432 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1433 struct RequestHandle *handle = cls;
1434 struct GNUNET_HashCode cache_key;
1435 struct GNUNET_TIME_Absolute *current_time;
1436 struct GNUNET_TIME_Absolute *last_time;
1442 char term_data[handle->rest_handle->data_size + 1];
1443 term_data[handle->rest_handle->data_size] = '\0';
1444 GNUNET_memcpy (term_data,
1445 handle->rest_handle->data,
1446 handle->rest_handle->data_size);
1447 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1448 identity = json_object_get (root, "identity");
1449 if (! json_is_string (identity))
1451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1452 "Error parsing json string from %s\n",
1454 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1456 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1459 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1460 GNUNET_asprintf (&header_val,
1463 OIDC_COOKIE_EXPIRATION);
1464 MHD_add_response_header (resp, "Set-Cookie", header_val);
1465 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1466 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1468 if (0 != strcmp (json_string_value (identity), "Denied"))
1470 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1471 *current_time = GNUNET_TIME_relative_to_absolute (
1472 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1473 OIDC_COOKIE_EXPIRATION));
1475 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1476 GNUNET_free_non_null (last_time);
1477 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1480 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1482 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1483 GNUNET_free (cookie);
1484 GNUNET_free (header_val);
1486 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1490 check_authorization (struct RequestHandle *handle,
1491 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1493 struct GNUNET_HashCode cache_key;
1494 char *authorization;
1496 char *basic_authorization;
1499 char *expected_pass;
1501 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1502 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1504 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1508 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1509 handle->edesc = GNUNET_strdup ("missing authorization");
1510 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1511 return GNUNET_SYSERR;
1514 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1517 // split header in "Basic" and [content]
1518 credentials = strtok (authorization, " ");
1519 if ((NULL == credentials) || (0 != strcmp ("Basic", credentials)))
1521 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1522 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1523 return GNUNET_SYSERR;
1525 credentials = strtok (NULL, " ");
1526 if (NULL == credentials)
1528 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1529 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1530 return GNUNET_SYSERR;
1532 GNUNET_STRINGS_base64_decode (credentials,
1533 strlen (credentials),
1534 (void **) &basic_authorization);
1536 if (NULL == basic_authorization)
1538 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1539 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1540 return GNUNET_SYSERR;
1542 client_id = strtok (basic_authorization, ":");
1543 if (NULL == client_id)
1545 GNUNET_free_non_null (basic_authorization);
1546 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1547 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1548 return GNUNET_SYSERR;
1550 pass = strtok (NULL, ":");
1553 GNUNET_free_non_null (basic_authorization);
1554 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1555 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1556 return GNUNET_SYSERR;
1559 // check client password
1560 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1561 "reclaim-rest-plugin",
1562 "OIDC_CLIENT_SECRET",
1565 if (0 != strcmp (expected_pass, pass))
1567 GNUNET_free_non_null (basic_authorization);
1568 GNUNET_free (expected_pass);
1569 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1570 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1571 return GNUNET_SYSERR;
1573 GNUNET_free (expected_pass);
1577 GNUNET_free_non_null (basic_authorization);
1578 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1579 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1580 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1581 return GNUNET_SYSERR;
1585 for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry;
1586 handle->ego_entry = handle->ego_entry->next)
1588 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1591 if (NULL == handle->ego_entry)
1593 GNUNET_free_non_null (basic_authorization);
1594 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1595 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1596 return GNUNET_SYSERR;
1598 GNUNET_STRINGS_string_to_data (client_id,
1601 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1603 GNUNET_free (basic_authorization);
1607 const struct EgoEntry *
1608 find_ego (struct RequestHandle *handle,
1609 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1611 struct EgoEntry *ego_entry;
1612 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1614 for (ego_entry = handle->ego_head; NULL != ego_entry;
1615 ego_entry = ego_entry->next)
1617 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1618 if (0 == GNUNET_memcmp (&pub_key, test_key))
1625 persist_access_token (const struct RequestHandle *handle,
1626 const char *access_token,
1627 const struct GNUNET_RECLAIM_Ticket *ticket)
1629 struct GNUNET_HashCode hc;
1630 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1632 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1633 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1634 *ticketbuf = *ticket;
1635 GNUNET_assert (GNUNET_SYSERR !=
1636 GNUNET_CONTAINER_multihashmap_put (
1637 OIDC_access_token_map,
1640 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1644 * Responds to token url-encoded POST request
1646 * @param con_handle the connection handle
1647 * @param url the url
1648 * @param cls the RequestHandle
1651 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1655 struct RequestHandle *handle = cls;
1656 const struct EgoEntry *ego_entry;
1657 struct GNUNET_TIME_Relative expiration_time;
1658 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl;
1659 struct GNUNET_RECLAIM_Ticket ticket;
1660 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1661 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1662 struct GNUNET_HashCode cache_key;
1663 struct MHD_Response *resp;
1666 char *json_response;
1673 * Check Authorization
1675 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1678 "OIDC authorization for token endpoint failed\n");
1679 GNUNET_SCHEDULER_add_now (&do_error, handle);
1687 // TODO Do not allow multiple equal parameter names
1688 // REQUIRED grant_type
1689 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1690 strlen (OIDC_GRANT_TYPE_KEY),
1692 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1693 if (NULL == grant_type)
1695 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1696 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1697 handle->response_code = MHD_HTTP_BAD_REQUEST;
1698 GNUNET_SCHEDULER_add_now (&do_error, handle);
1702 // Check parameter grant_type == "authorization_code"
1703 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1705 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1706 handle->response_code = MHD_HTTP_BAD_REQUEST;
1707 GNUNET_free (grant_type);
1708 GNUNET_SCHEDULER_add_now (&do_error, handle);
1711 GNUNET_free (grant_type);
1713 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1716 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1717 handle->edesc = GNUNET_strdup ("missing parameter code");
1718 handle->response_code = MHD_HTTP_BAD_REQUEST;
1719 GNUNET_SCHEDULER_add_now (&do_error, handle);
1722 ego_entry = find_ego (handle, &cid);
1723 if (NULL == ego_entry)
1725 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1726 handle->edesc = GNUNET_strdup ("Unknown client");
1727 handle->response_code = MHD_HTTP_BAD_REQUEST;
1729 GNUNET_SCHEDULER_add_now (&do_error, handle);
1732 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1734 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, &ticket, &cl, &nonce))
1736 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1737 handle->edesc = GNUNET_strdup ("invalid code");
1738 handle->response_code = MHD_HTTP_BAD_REQUEST;
1740 GNUNET_SCHEDULER_add_now (&do_error, handle);
1746 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1747 "reclaim-rest-plugin",
1751 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1752 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1753 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1754 GNUNET_SCHEDULER_add_now (&do_error, handle);
1759 // TODO OPTIONAL acr,amr,azp
1760 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1761 "reclaim-rest-plugin",
1765 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1766 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1767 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1768 GNUNET_SCHEDULER_add_now (&do_error, handle);
1771 id_token = OIDC_id_token_new (&ticket.audience,
1775 (NULL != nonce) ? nonce : NULL,
1777 access_token = OIDC_access_token_new ();
1778 OIDC_build_token_response (access_token,
1783 persist_access_token (handle, access_token, &ticket);
1784 resp = GNUNET_REST_create_response (json_response);
1785 MHD_add_response_header (resp, "Cache-Control", "no-store");
1786 MHD_add_response_header (resp, "Pragma", "no-cache");
1787 MHD_add_response_header (resp, "Content-Type", "application/json");
1788 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1789 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (cl);
1790 GNUNET_free (access_token);
1791 GNUNET_free (json_response);
1792 GNUNET_free (id_token);
1793 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1797 * Collects claims and stores them in handle
1800 consume_ticket (void *cls,
1801 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1802 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
1804 struct RequestHandle *handle = cls;
1808 if (NULL == identity)
1810 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1813 tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
1816 value = json_string (tmp_value);
1817 json_object_set_new (handle->oidc->response, attr->name, value);
1818 GNUNET_free (tmp_value);
1822 * Responds to userinfo GET and url-encoded POST request
1824 * @param con_handle the connection handle
1825 * @param url the url
1826 * @param cls the RequestHandle
1829 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1833 // TODO expiration time
1834 struct RequestHandle *handle = cls;
1835 char delimiter[] = " ";
1836 struct GNUNET_HashCode cache_key;
1837 char *authorization;
1838 char *authorization_type;
1839 char *authorization_access_token;
1840 struct GNUNET_RECLAIM_Ticket *ticket;
1841 const struct EgoEntry *ego_entry;
1842 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1844 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1845 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1847 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1851 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1852 handle->edesc = GNUNET_strdup ("No Access Token");
1853 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1854 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1858 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1861 // split header in "Bearer" and access_token
1862 authorization = GNUNET_strdup (authorization);
1863 authorization_type = strtok (authorization, delimiter);
1864 if (0 != strcmp ("Bearer", authorization_type))
1866 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1867 handle->edesc = GNUNET_strdup ("No Access Token");
1868 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1869 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1870 GNUNET_free (authorization);
1873 authorization_access_token = strtok (NULL, delimiter);
1874 if (NULL == authorization_access_token)
1876 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1877 handle->edesc = GNUNET_strdup ("Access token missing");
1878 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1879 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1880 GNUNET_free (authorization);
1884 GNUNET_CRYPTO_hash (authorization_access_token,
1885 strlen (authorization_access_token),
1888 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
1891 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1892 handle->edesc = GNUNET_strdup ("The access token expired");
1893 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1894 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1895 GNUNET_free (authorization);
1899 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
1900 GNUNET_assert (NULL != ticket);
1901 ego_entry = find_ego (handle, &ticket->audience);
1902 if (NULL == ego_entry)
1904 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1905 handle->edesc = GNUNET_strdup ("The access token expired");
1906 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1907 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1908 GNUNET_free (authorization);
1912 handle->idp = GNUNET_RECLAIM_connect (cfg);
1913 handle->oidc->response = json_object ();
1914 json_object_set_new (handle->oidc->response,
1916 json_string (ego_entry->keystring));
1917 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1918 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
1923 GNUNET_free (authorization);
1928 * Handle rest request
1930 * @param handle the request handle
1933 init_cont (struct RequestHandle *handle)
1935 struct GNUNET_REST_RequestHandlerError err;
1936 static const struct GNUNET_REST_RequestHandler handlers[] =
1937 {{MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint},
1938 {MHD_HTTP_METHOD_POST,
1939 GNUNET_REST_API_NS_AUTHORIZE,
1940 &authorize_endpoint}, // url-encoded
1941 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont},
1942 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint},
1943 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint},
1944 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint},
1945 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont},
1946 GNUNET_REST_HANDLER_END};
1949 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
1951 handle->response_code = err.error_code;
1952 GNUNET_SCHEDULER_add_now (&do_error, handle);
1957 * If listing is enabled, prints information about the egos.
1959 * This function is initially called for all egos and then again
1960 * whenever a ego's identifier changes or if it is deleted. At the
1961 * end of the initial pass over all egos, the function is once called
1962 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
1963 * be invoked in the future or that there was an error.
1965 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
1966 * this function is only called ONCE, and 'NULL' being passed in
1967 * 'ego' does indicate an error (i.e. name is taken or no default
1968 * value is known). If 'ego' is non-NULL and if '*ctx'
1969 * is set in those callbacks, the value WILL be passed to a subsequent
1970 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
1971 * that one was not NULL).
1973 * When an identity is renamed, this function is called with the
1974 * (known) ego but the NEW identifier.
1976 * When an identity is deleted, this function is called with the
1977 * (known) ego and "NULL" for the 'identifier'. In this case,
1978 * the 'ego' is henceforth invalid (and the 'ctx' should also be
1981 * @param cls closure
1982 * @param ego ego handle
1983 * @param ctx context for application to store data for this ego
1984 * (during the lifetime of this process, initially NULL)
1985 * @param identifier identifier assigned by the user for this ego,
1986 * NULL if the user just deleted the ego and it
1987 * must thus no longer be used
1990 list_ego (void *cls,
1991 struct GNUNET_IDENTITY_Ego *ego,
1993 const char *identifier)
1995 struct RequestHandle *handle = cls;
1996 struct EgoEntry *ego_entry;
1997 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
1999 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
2001 handle->state = ID_REST_STATE_POST_INIT;
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_destroy (OIDC_cookie_jar_map);
2149 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2150 while (GNUNET_YES ==
2151 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2152 GNUNET_free_non_null (value);
2153 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2154 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2155 GNUNET_free_non_null (allow_methods);
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 "OpenID Connect REST plugin is finished\n");
2162 /* end of plugin_rest_openid_connect.c */