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);
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;
873 handle->ticket = *ticket;
876 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
877 handle->edesc = GNUNET_strdup ("Server cannot generate ticket.");
878 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
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",
1139 handle->oidc->client_id);
1144 pos = strrchr (tmp, (unsigned char) '.');
1146 handle->redirect_prefix = GNUNET_strdup (tmp);
1147 tmp_key_str = pos + 1;
1148 pos = strchr (tmp_key_str, (unsigned char) '/');
1150 handle->redirect_suffix = GNUNET_strdup (pos + 1);
1152 GNUNET_STRINGS_string_to_data (tmp_key_str,
1153 strlen (tmp_key_str),
1155 sizeof (redirect_zone));
1157 GNUNET_SCHEDULER_add_now (&build_redirect, handle);
1161 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1163 GNUNET_strdup ("Server cannot generate ticket, redirect uri not found.");
1164 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1169 * Initiate redirect back to client.
1172 client_redirect (void *cls)
1174 struct RequestHandle *handle = cls;
1176 /* Lookup client redirect uri to verify request */
1178 GNUNET_GNS_lookup (handle->gns_handle,
1179 GNUNET_GNS_EMPTY_LABEL_AT,
1180 &handle->oidc->client_pkey,
1181 GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT,
1182 GNUNET_GNS_LO_DEFAULT,
1183 &lookup_redirect_uri_result,
1188 get_url_parameter_copy (const struct RequestHandle *handle, const char *key)
1190 struct GNUNET_HashCode hc;
1192 GNUNET_CRYPTO_hash (key, strlen (key), &hc);
1193 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1198 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, &hc);
1201 return GNUNET_strdup (value);
1206 * Iteration over all results finished, build final
1209 * @param cls the `struct RequestHandle`
1212 build_authz_response (void *cls)
1214 struct RequestHandle *handle = cls;
1215 struct GNUNET_HashCode cache_key;
1217 char *expected_scope;
1218 char delimiter[] = " ";
1219 int number_of_ignored_parameter, iterator;
1222 // REQUIRED value: redirect_uri
1223 handle->oidc->redirect_uri =
1224 get_url_parameter_copy (handle, OIDC_REDIRECT_URI_KEY);
1225 if (NULL == handle->oidc->redirect_uri)
1227 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1228 handle->edesc = GNUNET_strdup ("missing parameter redirect_uri");
1229 GNUNET_SCHEDULER_add_now (&do_error, handle);
1233 // REQUIRED value: response_type
1234 handle->oidc->response_type =
1235 get_url_parameter_copy (handle, OIDC_RESPONSE_TYPE_KEY);
1236 if (NULL == handle->oidc->response_type)
1238 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1239 handle->edesc = GNUNET_strdup ("missing parameter response_type");
1240 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1244 // REQUIRED value: scope
1245 handle->oidc->scope = get_url_parameter_copy (handle, OIDC_SCOPE_KEY);
1246 if (NULL == handle->oidc->scope)
1248 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1249 handle->edesc = GNUNET_strdup ("missing parameter scope");
1250 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1254 // OPTIONAL value: nonce
1255 handle->oidc->nonce = get_url_parameter_copy (handle, OIDC_NONCE_KEY);
1257 // TODO check other values if needed
1258 number_of_ignored_parameter =
1259 sizeof (OIDC_ignored_parameter_array) / sizeof (char *);
1260 for (iterator = 0; iterator < number_of_ignored_parameter; iterator++)
1262 GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator],
1263 strlen (OIDC_ignored_parameter_array[iterator]),
1266 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1270 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_ACCESS_DENIED);
1271 GNUNET_asprintf (&handle->edesc,
1272 "Server will not handle parameter: %s",
1273 OIDC_ignored_parameter_array[iterator]);
1274 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1279 // We only support authorization code flows.
1280 if (0 != strcmp (handle->oidc->response_type,
1281 OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE))
1283 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_RESPONSE_TYPE);
1284 handle->edesc = GNUNET_strdup ("The authorization server does not support "
1285 "obtaining this authorization code.");
1286 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1290 // Checks if scope contains 'openid'
1291 expected_scope = GNUNET_strdup (handle->oidc->scope);
1293 test = strtok (expected_scope, delimiter);
1294 while (NULL != test)
1296 if (0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope))
1298 test = strtok (NULL, delimiter);
1302 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_SCOPE);
1304 GNUNET_strdup ("The requested scope is invalid, unknown, or malformed.");
1305 GNUNET_SCHEDULER_add_now (&do_redirect_error, handle);
1306 GNUNET_free (expected_scope);
1310 GNUNET_free (expected_scope);
1311 if ((NULL == handle->oidc->login_identity) &&
1312 (GNUNET_NO == handle->oidc->user_cancelled))
1313 GNUNET_SCHEDULER_add_now (&login_redirect, handle);
1315 GNUNET_SCHEDULER_add_now (&client_redirect, handle);
1319 * Iterate over tlds in config
1322 tld_iter (void *cls, const char *section, const char *option, const char *value)
1324 struct RequestHandle *handle = cls;
1325 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1328 GNUNET_CRYPTO_ecdsa_public_key_from_string (value, strlen (value), &pkey))
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping non key %s\n", value);
1333 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1334 handle->tld = GNUNET_strdup (option + 1);
1338 * Responds to authorization GET and url-encoded POST request
1340 * @param con_handle the connection handle
1341 * @param url the url
1342 * @param cls the RequestHandle
1345 authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1349 struct RequestHandle *handle = cls;
1350 struct EgoEntry *tmp_ego;
1351 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
1352 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1354 cookie_identity_interpretation (handle);
1356 // RECOMMENDED value: state - REQUIRED for answers
1357 handle->oidc->state = get_url_parameter_copy (handle, OIDC_STATE_KEY);
1359 // REQUIRED value: client_id
1360 handle->oidc->client_id = get_url_parameter_copy (handle, OIDC_CLIENT_ID_KEY);
1361 if (NULL == handle->oidc->client_id)
1363 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1364 handle->edesc = GNUNET_strdup ("missing parameter client_id");
1365 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1366 GNUNET_SCHEDULER_add_now (&do_error, handle);
1371 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id,
1373 handle->oidc->client_id),
1374 &handle->oidc->client_pkey))
1376 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNAUTHORIZED_CLIENT);
1377 handle->edesc = GNUNET_strdup ("The client is not authorized to request an "
1378 "authorization code using this method.");
1379 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1380 GNUNET_SCHEDULER_add_now (&do_error, handle);
1384 if (NULL == handle->ego_head)
1386 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1387 handle->edesc = GNUNET_strdup ("Egos are missing");
1388 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1389 GNUNET_SCHEDULER_add_now (&do_error, handle);
1393 handle->ego_entry = handle->ego_head;
1395 *GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego);
1396 // If we know this identity, translated the corresponding TLD
1397 // TODO: We might want to have a reverse lookup functionality for TLDs?
1398 for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next)
1400 priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego);
1401 GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, &pkey);
1402 if (0 == GNUNET_memcmp (&pkey, &handle->oidc->client_pkey))
1404 handle->tld = GNUNET_strdup (tmp_ego->identifier);
1405 handle->ego_entry = handle->ego_tail;
1408 if (NULL == handle->tld)
1409 GNUNET_CONFIGURATION_iterate_section_values (cfg, "gns", tld_iter, handle);
1410 if (NULL == handle->tld)
1411 handle->tld = GNUNET_strdup (handle->oidc->client_id);
1412 GNUNET_SCHEDULER_add_now (&build_authz_response, handle);
1416 * Combines an identity with a login time and responds OK to login request
1418 * @param con_handle the connection handle
1419 * @param url the url
1420 * @param cls the RequestHandle
1423 login_cont (struct GNUNET_REST_RequestHandle *con_handle,
1427 struct MHD_Response *resp = GNUNET_REST_create_response ("");
1428 struct RequestHandle *handle = cls;
1429 struct GNUNET_HashCode cache_key;
1430 struct GNUNET_TIME_Absolute *current_time;
1431 struct GNUNET_TIME_Absolute *last_time;
1437 char term_data[handle->rest_handle->data_size + 1];
1438 term_data[handle->rest_handle->data_size] = '\0';
1439 GNUNET_memcpy (term_data,
1440 handle->rest_handle->data,
1441 handle->rest_handle->data_size);
1442 root = json_loads (term_data, JSON_DECODE_ANY, &error);
1443 identity = json_object_get (root, "identity");
1444 if (! json_is_string (identity))
1446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1447 "Error parsing json string from %s\n",
1449 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
1451 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1454 GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity));
1455 GNUNET_asprintf (&header_val,
1458 OIDC_COOKIE_EXPIRATION);
1459 MHD_add_response_header (resp, "Set-Cookie", header_val);
1460 MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST");
1461 GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key);
1463 if (0 != strcmp (json_string_value (identity), "Denied"))
1465 current_time = GNUNET_new (struct GNUNET_TIME_Absolute);
1466 *current_time = GNUNET_TIME_relative_to_absolute (
1467 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (),
1468 OIDC_COOKIE_EXPIRATION));
1470 GNUNET_CONTAINER_multihashmap_get (OIDC_cookie_jar_map, &cache_key);
1471 GNUNET_free_non_null (last_time);
1472 GNUNET_CONTAINER_multihashmap_put (OIDC_cookie_jar_map,
1475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1477 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1478 GNUNET_free (cookie);
1479 GNUNET_free (header_val);
1481 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1485 check_authorization (struct RequestHandle *handle,
1486 struct GNUNET_CRYPTO_EcdsaPublicKey *cid)
1488 struct GNUNET_HashCode cache_key;
1489 char *authorization;
1491 char *basic_authorization;
1494 char *expected_pass;
1496 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1497 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1499 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1503 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1504 handle->edesc = GNUNET_strdup ("missing authorization");
1505 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1506 return GNUNET_SYSERR;
1509 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1512 // split header in "Basic" and [content]
1513 credentials = strtok (authorization, " ");
1514 if (0 != strcmp ("Basic", credentials))
1516 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1517 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1518 return GNUNET_SYSERR;
1520 credentials = strtok (NULL, " ");
1521 if (NULL == credentials)
1523 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1524 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1525 return GNUNET_SYSERR;
1527 GNUNET_STRINGS_base64_decode (credentials,
1528 strlen (credentials),
1529 (void **) &basic_authorization);
1531 if (NULL == basic_authorization)
1533 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1534 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1535 return GNUNET_SYSERR;
1537 client_id = strtok (basic_authorization, ":");
1538 if (NULL == client_id)
1540 GNUNET_free_non_null (basic_authorization);
1541 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1542 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1543 return GNUNET_SYSERR;
1545 pass = strtok (NULL, ":");
1548 GNUNET_free_non_null (basic_authorization);
1549 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1550 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1551 return GNUNET_SYSERR;
1554 // check client password
1555 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
1556 "reclaim-rest-plugin",
1557 "OIDC_CLIENT_SECRET",
1560 if (0 != strcmp (expected_pass, pass))
1562 GNUNET_free_non_null (basic_authorization);
1563 GNUNET_free (expected_pass);
1564 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1565 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1566 return GNUNET_SYSERR;
1568 GNUNET_free (expected_pass);
1572 GNUNET_free_non_null (basic_authorization);
1573 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1574 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1575 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1576 return GNUNET_SYSERR;
1580 for (handle->ego_entry = handle->ego_head;
1581 NULL != handle->ego_entry;
1582 handle->ego_entry = handle->ego_entry->next)
1584 if (0 == strcmp (handle->ego_entry->keystring, client_id))
1587 if (NULL == handle->ego_entry)
1589 GNUNET_free_non_null (basic_authorization);
1590 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_CLIENT);
1591 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1592 return GNUNET_SYSERR;
1594 GNUNET_STRINGS_string_to_data (client_id,
1597 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1599 GNUNET_free (basic_authorization);
1603 const struct EgoEntry *
1604 find_ego (struct RequestHandle *handle,
1605 struct GNUNET_CRYPTO_EcdsaPublicKey *test_key)
1607 struct EgoEntry *ego_entry;
1608 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1610 for (ego_entry = handle->ego_head; NULL != ego_entry;
1611 ego_entry = ego_entry->next)
1613 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key);
1614 if (0 == GNUNET_memcmp (&pub_key, test_key))
1621 persist_access_token (const struct RequestHandle *handle,
1622 const char *access_token,
1623 const struct GNUNET_RECLAIM_Ticket *ticket)
1625 struct GNUNET_HashCode hc;
1626 struct GNUNET_RECLAIM_Ticket *ticketbuf;
1628 GNUNET_CRYPTO_hash (access_token, strlen (access_token), &hc);
1629 ticketbuf = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
1630 *ticketbuf = *ticket;
1631 GNUNET_CONTAINER_multihashmap_put (
1632 OIDC_access_token_map,
1635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1639 * Responds to token url-encoded POST request
1641 * @param con_handle the connection handle
1642 * @param url the url
1643 * @param cls the RequestHandle
1646 token_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1650 struct RequestHandle *handle = cls;
1651 const struct EgoEntry *ego_entry;
1652 struct GNUNET_TIME_Relative expiration_time;
1653 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl;
1654 struct GNUNET_RECLAIM_Ticket ticket;
1655 struct GNUNET_CRYPTO_EcdsaPublicKey cid;
1656 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1657 struct GNUNET_HashCode cache_key;
1658 struct MHD_Response *resp;
1661 char *json_response;
1668 * Check Authorization
1670 if (GNUNET_SYSERR == check_authorization (handle, &cid))
1672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1673 "OIDC authorization for token endpoint failed\n");
1674 GNUNET_SCHEDULER_add_now (&do_error, handle);
1682 // TODO Do not allow multiple equal parameter names
1683 // REQUIRED grant_type
1684 GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY,
1685 strlen (OIDC_GRANT_TYPE_KEY),
1687 grant_type = get_url_parameter_copy (handle, OIDC_GRANT_TYPE_KEY);
1688 if (NULL == grant_type)
1690 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1691 handle->edesc = GNUNET_strdup ("missing parameter grant_type");
1692 handle->response_code = MHD_HTTP_BAD_REQUEST;
1693 GNUNET_SCHEDULER_add_now (&do_error, handle);
1697 // Check parameter grant_type == "authorization_code"
1698 if (0 != strcmp (OIDC_GRANT_TYPE_VALUE, grant_type))
1700 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_UNSUPPORTED_GRANT_TYPE);
1701 handle->response_code = MHD_HTTP_BAD_REQUEST;
1702 GNUNET_SCHEDULER_add_now (&do_error, handle);
1707 code = get_url_parameter_copy (handle, OIDC_CODE_KEY);
1710 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1711 handle->edesc = GNUNET_strdup ("missing parameter code");
1712 handle->response_code = MHD_HTTP_BAD_REQUEST;
1713 GNUNET_SCHEDULER_add_now (&do_error, handle);
1716 ego_entry = find_ego (handle, &cid);
1717 if (NULL == ego_entry)
1719 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1720 handle->edesc = GNUNET_strdup ("Unknown client");
1721 handle->response_code = MHD_HTTP_BAD_REQUEST;
1722 GNUNET_SCHEDULER_add_now (&do_error, handle);
1724 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1726 if (GNUNET_OK != OIDC_parse_authz_code (privkey, code, &ticket, &cl, &nonce))
1728 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1729 handle->edesc = GNUNET_strdup ("invalid code");
1730 handle->response_code = MHD_HTTP_BAD_REQUEST;
1731 GNUNET_SCHEDULER_add_now (&do_error, handle);
1736 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1737 "reclaim-rest-plugin",
1741 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_SERVER_ERROR);
1742 handle->edesc = GNUNET_strdup ("gnunet configuration failed");
1743 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1744 GNUNET_SCHEDULER_add_now (&do_error, handle);
1749 // TODO OPTIONAL acr,amr,azp
1750 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
1751 "reclaim-rest-plugin",
1755 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_REQUEST);
1756 handle->edesc = GNUNET_strdup ("No signing secret configured!");
1757 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1758 GNUNET_SCHEDULER_add_now (&do_error, handle);
1761 id_token = OIDC_id_token_new (&ticket.audience,
1765 (NULL != nonce) ? nonce : NULL,
1767 access_token = OIDC_access_token_new ();
1768 OIDC_build_token_response (access_token,
1773 persist_access_token (handle, access_token, &ticket);
1774 resp = GNUNET_REST_create_response (json_response);
1775 MHD_add_response_header (resp, "Cache-Control", "no-store");
1776 MHD_add_response_header (resp, "Pragma", "no-cache");
1777 MHD_add_response_header (resp, "Content-Type", "application/json");
1778 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
1779 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (cl);
1780 GNUNET_free (access_token);
1781 GNUNET_free (json_response);
1782 GNUNET_free (id_token);
1783 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
1787 * Collects claims and stores them in handle
1790 consume_ticket (void *cls,
1791 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
1792 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
1794 struct RequestHandle *handle = cls;
1798 if (NULL == identity)
1800 GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle);
1803 tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
1806 value = json_string (tmp_value);
1807 json_object_set_new (handle->oidc->response, attr->name, value);
1808 GNUNET_free (tmp_value);
1812 * Responds to userinfo GET and url-encoded POST request
1814 * @param con_handle the connection handle
1815 * @param url the url
1816 * @param cls the RequestHandle
1819 userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle,
1823 // TODO expiration time
1824 struct RequestHandle *handle = cls;
1825 char delimiter[] = " ";
1826 struct GNUNET_HashCode cache_key;
1827 char *authorization;
1828 char *authorization_type;
1829 char *authorization_access_token;
1830 struct GNUNET_RECLAIM_Ticket *ticket;
1831 const struct EgoEntry *ego_entry;
1832 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
1834 GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY,
1835 strlen (OIDC_AUTHORIZATION_HEADER_KEY),
1837 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle
1841 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1842 handle->edesc = GNUNET_strdup ("No Access Token");
1843 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1844 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1848 GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map,
1851 // split header in "Bearer" and access_token
1852 authorization = GNUNET_strdup (authorization);
1853 authorization_type = strtok (authorization, delimiter);
1854 if (0 != strcmp ("Bearer", authorization_type))
1856 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1857 handle->edesc = GNUNET_strdup ("No Access Token");
1858 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1859 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1860 GNUNET_free (authorization);
1863 authorization_access_token = strtok (NULL, delimiter);
1864 if (NULL == authorization_access_token)
1866 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1867 handle->edesc = GNUNET_strdup ("Access token missing");
1868 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1869 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1870 GNUNET_free (authorization);
1874 GNUNET_CRYPTO_hash (authorization_access_token,
1875 strlen (authorization_access_token),
1878 GNUNET_CONTAINER_multihashmap_contains (OIDC_access_token_map,
1881 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1882 handle->edesc = GNUNET_strdup ("The access token expired");
1883 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1884 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1885 GNUNET_free (authorization);
1889 GNUNET_CONTAINER_multihashmap_get (OIDC_access_token_map, &cache_key);
1890 GNUNET_assert (NULL != ticket);
1891 ego_entry = find_ego (handle, &ticket->audience);
1892 if (NULL == ego_entry)
1894 handle->emsg = GNUNET_strdup (OIDC_ERROR_KEY_INVALID_TOKEN);
1895 handle->edesc = GNUNET_strdup ("The access token expired");
1896 handle->response_code = MHD_HTTP_UNAUTHORIZED;
1897 GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle);
1898 GNUNET_free (authorization);
1902 handle->idp = GNUNET_RECLAIM_connect (cfg);
1903 handle->oidc->response = json_object ();
1904 json_object_set_new (handle->oidc->response,
1906 json_string (ego_entry->keystring));
1907 privkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
1908 handle->idp_op = GNUNET_RECLAIM_ticket_consume (handle->idp,
1913 GNUNET_free (authorization);
1918 * Handle rest request
1920 * @param handle the request handle
1923 init_cont (struct RequestHandle *handle)
1925 struct GNUNET_REST_RequestHandlerError err;
1926 static const struct GNUNET_REST_RequestHandler handlers[] =
1927 {{MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint},
1928 {MHD_HTTP_METHOD_POST,
1929 GNUNET_REST_API_NS_AUTHORIZE,
1930 &authorize_endpoint}, // url-encoded
1931 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont},
1932 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint},
1933 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint},
1934 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint},
1935 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, &options_cont},
1936 GNUNET_REST_HANDLER_END};
1939 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
1941 handle->response_code = err.error_code;
1942 GNUNET_SCHEDULER_add_now (&do_error, handle);
1947 * If listing is enabled, prints information about the egos.
1949 * This function is initially called for all egos and then again
1950 * whenever a ego's identifier changes or if it is deleted. At the
1951 * end of the initial pass over all egos, the function is once called
1952 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
1953 * be invoked in the future or that there was an error.
1955 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
1956 * this function is only called ONCE, and 'NULL' being passed in
1957 * 'ego' does indicate an error (i.e. name is taken or no default
1958 * value is known). If 'ego' is non-NULL and if '*ctx'
1959 * is set in those callbacks, the value WILL be passed to a subsequent
1960 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
1961 * that one was not NULL).
1963 * When an identity is renamed, this function is called with the
1964 * (known) ego but the NEW identifier.
1966 * When an identity is deleted, this function is called with the
1967 * (known) ego and "NULL" for the 'identifier'. In this case,
1968 * the 'ego' is henceforth invalid (and the 'ctx' should also be
1971 * @param cls closure
1972 * @param ego ego handle
1973 * @param ctx context for application to store data for this ego
1974 * (during the lifetime of this process, initially NULL)
1975 * @param identifier identifier assigned by the user for this ego,
1976 * NULL if the user just deleted the ego and it
1977 * must thus no longer be used
1980 list_ego (void *cls,
1981 struct GNUNET_IDENTITY_Ego *ego,
1983 const char *identifier)
1985 struct RequestHandle *handle = cls;
1986 struct EgoEntry *ego_entry;
1987 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
1989 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
1991 handle->state = ID_REST_STATE_POST_INIT;
1995 if (ID_REST_STATE_INIT == handle->state)
1997 ego_entry = GNUNET_new (struct EgoEntry);
1998 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
1999 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2000 ego_entry->ego = ego;
2001 ego_entry->identifier = GNUNET_strdup (identifier);
2002 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2007 /* Ego renamed or added */
2008 if (identifier != NULL)
2010 for (ego_entry = handle->ego_head; NULL != ego_entry;
2011 ego_entry = ego_entry->next)
2013 if (ego_entry->ego == ego)
2016 GNUNET_free (ego_entry->identifier);
2017 ego_entry->identifier = GNUNET_strdup (identifier);
2021 if (NULL == ego_entry)
2024 ego_entry = GNUNET_new (struct EgoEntry);
2025 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
2026 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
2027 ego_entry->ego = ego;
2028 ego_entry->identifier = GNUNET_strdup (identifier);
2029 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
2037 for (ego_entry = handle->ego_head; NULL != ego_entry;
2038 ego_entry = ego_entry->next)
2040 if (ego_entry->ego == ego)
2043 if (NULL != ego_entry)
2044 GNUNET_CONTAINER_DLL_remove (handle->ego_head,
2051 rest_identity_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
2052 GNUNET_REST_ResultProcessor proc,
2055 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
2056 handle->oidc = GNUNET_new (struct OIDC_Variables);
2057 if (NULL == OIDC_cookie_jar_map)
2058 OIDC_cookie_jar_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2059 if (NULL == OIDC_access_token_map)
2060 OIDC_access_token_map =
2061 GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2062 handle->response_code = 0;
2063 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
2064 handle->proc_cls = proc_cls;
2065 handle->proc = proc;
2066 handle->state = ID_REST_STATE_INIT;
2067 handle->rest_handle = rest_handle;
2069 handle->url = GNUNET_strdup (rest_handle->url);
2070 if (handle->url[strlen (handle->url) - 1] == '/')
2071 handle->url[strlen (handle->url) - 1] = '\0';
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
2073 handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle);
2074 handle->gns_handle = GNUNET_GNS_connect (cfg);
2075 handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg);
2076 handle->timeout_task =
2077 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
2082 * Entry point for the plugin.
2084 * @param cls Config info
2085 * @return NULL on error, otherwise the plugin context
2088 libgnunet_plugin_rest_openid_connect_init (void *cls)
2090 static struct Plugin plugin;
2091 struct GNUNET_REST_Plugin *api;
2094 if (NULL != plugin.cfg)
2095 return NULL; /* can only initialize once! */
2096 memset (&plugin, 0, sizeof (struct Plugin));
2098 api = GNUNET_new (struct GNUNET_REST_Plugin);
2100 api->name = GNUNET_REST_API_NS_OIDC;
2101 api->process_request = &rest_identity_process_request;
2102 GNUNET_asprintf (&allow_methods,
2103 "%s, %s, %s, %s, %s",
2104 MHD_HTTP_METHOD_GET,
2105 MHD_HTTP_METHOD_POST,
2106 MHD_HTTP_METHOD_PUT,
2107 MHD_HTTP_METHOD_DELETE,
2108 MHD_HTTP_METHOD_OPTIONS);
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 _ ("OpenID Connect REST API initialized\n"));
2117 * Exit point from the plugin.
2119 * @param cls the plugin context (as returned by "init")
2120 * @return always NULL
2123 libgnunet_plugin_rest_openid_connect_done (void *cls)
2125 struct GNUNET_REST_Plugin *api = cls;
2126 struct Plugin *plugin = api->cls;
2129 struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it;
2132 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_cookie_jar_map);
2133 while (GNUNET_YES ==
2134 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2135 GNUNET_free_non_null (value);
2136 GNUNET_CONTAINER_multihashmap_destroy (OIDC_cookie_jar_map);
2139 GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_access_token_map);
2140 while (GNUNET_YES ==
2141 GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value))
2142 GNUNET_free_non_null (value);
2143 GNUNET_CONTAINER_multihashmap_destroy (OIDC_access_token_map);
2144 GNUNET_CONTAINER_multihashmap_iterator_destroy (hashmap_it);
2145 GNUNET_free_non_null (allow_methods);
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 "OpenID Connect REST plugin is finished\n");
2152 /* end of plugin_rest_openid_connect.c */