27#include "microhttpd.h"
35#include "gnunet_signatures.h"
41#define GNUNET_REST_API_NS_PABC "/pabc"
46#define GNUNET_REST_API_NS_PABC_CR "/pabc/cr"
146 if (NULL !=
handle->resp_object)
147 json_decref (
handle->resp_object);
148 if (NULL !=
handle->timeout_task)
170 struct MHD_Response *resp;
174 if (0 ==
handle->response_code)
179 MHD_add_response_header (resp,
"Content-Type",
"application/json");
196 handle->timeout_task = NULL;
206 struct MHD_Response *resp;
208 result_str = json_dumps (
handle->resp_object, 0);
211 MHD_add_response_header (resp,
"Access-Control-Allow-Methods",
allow_methods);
218static enum pabc_status
220 const struct pabc_public_parameters *pp,
221 struct pabc_user_context *usr_ctx,
222 const char *id_token)
224 json_t *payload_json;
226 json_error_t json_err;
228 const char *jwt_body;
232 const char *pabc_key;
237 jwt_body = strtok (jwt_string, delim);
238 jwt_body = strtok (NULL, delim);
240 (
void **) &decoded_jwt);
242 payload_json = json_loads (decoded_jwt, JSON_DECODE_ANY, &json_err);
245 json_object_foreach (payload_json,
key,
value)
248 if (0 == strcmp (
"iss",
key))
250 if (0 == strcmp (
"sub",
key))
251 pabc_key =
"subject";
252 if (0 == strcmp (
"jti",
key))
254 if (0 == strcmp (
"exp",
key))
255 pabc_key =
"expiration";
256 if (0 == strcmp (
"iat",
key))
258 if (0 == strcmp (
"nbf",
key))
260 if (0 == strcmp (
"aud",
key))
263 if (json_is_string (
value))
266 tmp_val = json_dumps (
value, JSON_ENCODE_ANY);
270 "Unable to encode JSON value for `%s'\n",
key);
274 "Setting `%s' to `%s'\n",
key, tmp_val);
275 status = pabc_set_attribute_value_by_name (
ctx, pp, usr_ctx,
282 "Failed to set attribute `%s'.\n",
key);
292 struct pabc_public_parameters *pp,
293 struct pabc_user_context **usr_ctx)
295 if (PABC_OK != pabc_new_user_context (
ctx, pp, usr_ctx))
298 if (PABC_OK != pabc_populate_user_context (
ctx, *usr_ctx))
300 pabc_free_user_context (
ctx, pp, usr_ctx);
313 char term_data[
handle->rest_handle->data_size + 1];
318 json_t *idtoken_json;
320 json_t *identity_json;
322 struct pabc_public_parameters *pp = NULL;
323 struct pabc_context *
ctx = NULL;
324 struct pabc_user_context *usr_ctx = NULL;
325 struct pabc_credential_request *cr = NULL;
326 struct pabc_nonce *nonce = NULL;
331 "Credential request...\n");
333 if (0 >=
handle->rest_handle->data_size)
339 term_data[
handle->rest_handle->data_size] =
'\0';
341 handle->rest_handle->data,
342 handle->rest_handle->data_size);
343 data_json = json_loads (term_data, JSON_DECODE_ANY, &err);
344 if (NULL == data_json)
347 "Unable to parse %s\n", term_data);
351 if (! json_is_object (data_json))
354 "Unable to parse %s\n", term_data);
355 json_decref (data_json);
360 nonce_json = json_object_get (data_json,
"nonce");
361 if (NULL == nonce_json)
364 "Unable to parse nonce\n");
365 json_decref (data_json);
369 iss_json = json_object_get (data_json,
"issuer");
370 if (NULL == iss_json)
373 "Unable to parse issuer\n");
374 json_decref (data_json);
378 identity_json = json_object_get (data_json,
"identity");
379 if (NULL == identity_json)
382 "Unable to parse identity\n");
383 json_decref (data_json);
387 idtoken_json = json_object_get (data_json,
"id_token");
388 if (NULL == idtoken_json)
391 "Unable to parse id_token\n");
392 json_decref (data_json);
396 pp_json = json_object_get (data_json,
"public_params");
400 "Unable to parse public parameters\n");
401 json_decref (data_json);
406 PABC_ASSERT (pabc_new_ctx (&
ctx));
407 char *pp_str = json_dumps (pp_json, JSON_ENCODE_ANY);
408 status = pabc_decode_and_new_public_parameters (
ctx,
412 GNUNET_assert (PABC_OK == pabc_cred_get_ppid_from_pp (pp_str, &ppid));
417 "Failed to read public parameters: %s\n",
419 json_decref (data_json);
429 "Failed to write public parameters.\n");
430 json_decref (data_json);
435 json_string_value (iss_json),
442 pabc_free_public_parameters (
ctx, &pp);
443 json_decref (data_json);
448 json_string_value (iss_json),
456 json_string_value (idtoken_json));
460 pabc_free_user_context (
ctx, pp, &usr_ctx);
461 pabc_free_public_parameters (
ctx, &pp);
462 json_decref (data_json);
473 pabc_free_user_context (
ctx, pp, &usr_ctx);
474 pabc_free_public_parameters (
ctx, &pp);
475 json_decref (data_json);
479 char *nonce_str = json_dumps (nonce_json, JSON_ENCODE_ANY);
480 status = pabc_decode_nonce (
ctx, nonce, nonce_str);
484 pabc_free_nonce (
ctx, &nonce);
485 pabc_free_user_context (
ctx, pp, &usr_ctx);
486 pabc_free_public_parameters (
ctx, &pp);
487 json_decref (data_json);
493 status = pabc_new_credential_request (
ctx, pp, &cr);
497 pabc_free_nonce (
ctx, &nonce);
498 pabc_free_user_context (
ctx, pp, &usr_ctx);
499 pabc_free_public_parameters (
ctx, &pp);
500 json_decref (data_json);
505 status = pabc_gen_credential_request (
ctx, pp, usr_ctx, nonce, cr);
509 pabc_free_nonce (
ctx, &nonce);
510 pabc_free_credential_request (
ctx, pp, &cr);
511 pabc_free_user_context (
ctx, pp, &usr_ctx);
512 pabc_free_public_parameters (
ctx, &pp);
513 json_decref (data_json);
517 handle->resp_object = json_object ();
521 ppid, &response_str));
525 pabc_free_nonce (
ctx, &nonce);
526 pabc_free_credential_request (
ctx, pp, &cr);
527 pabc_free_user_context (
ctx, pp, &usr_ctx);
528 pabc_free_public_parameters (
ctx, &pp);
529 json_decref (data_json);
533 json_decref (
handle->resp_object);
534 handle->resp_object = json_loads (response_str, JSON_DECODE_ANY, &err);
539 pabc_free_nonce (
ctx, &nonce);
540 pabc_free_credential_request (
ctx, pp, &cr);
541 pabc_free_user_context (
ctx, pp, &usr_ctx);
542 pabc_free_public_parameters (
ctx, &pp);
544 json_decref (data_json);
560 struct MHD_Response *resp;
565 MHD_add_response_header (resp,
"Access-Control-Allow-Methods",
allow_methods);
580 {MHD_HTTP_METHOD_POST,
586 handle->response_code = 0;
588 handle->proc_cls = proc_cls;
590 handle->rest_handle = rest_handle;
634 MHD_HTTP_METHOD_POST,
635 MHD_HTTP_METHOD_OPTIONS);
637 _ (
"Identity Provider REST API initialized\n"));
662 "PABC REST plugin is finished\n");
struct GNUNET_MQ_MessageHandlers handlers[]
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_FS_Handle * ctx
static char * value
Value of the record to add/remove.
static int status
The program status; 0 for success.
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Identity attribute definitions.
reclaim service; implements identity and personal data sharing for GNUnet
API for helper library to parse/create REST.
GNUnet service REST plugin header.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_REST_handle_request(struct GNUNET_REST_RequestHandle *conn, const struct GNUNET_REST_RequestHandler *handlers, struct GNUNET_REST_RequestHandlerError *err, void *cls)
void(* GNUNET_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
#define GNUNET_REST_HANDLER_END
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
size_t GNUNET_STRINGS_base64url_decode(const char *data, size_t len, void **out)
Decode from Base64url.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
@ MHD_HTTP_BAD_REQUEST
Bad Request [RFC7231, Section 6.5.1].
@ MHD_HTTP_OK
OK [RFC7231, Section 6.3.1].
enum GNUNET_GenericReturnValue PABC_write_public_parameters(char const *const pp_name, struct pabc_public_parameters *const pp)
enum GNUNET_GenericReturnValue PABC_write_usr_ctx(char const *const usr_name, char const *const pp_name, struct pabc_context const *const ctx, struct pabc_public_parameters const *const pp, struct pabc_user_context *const usr_ctx)
enum GNUNET_GenericReturnValue PABC_read_usr_ctx(char const *const usr_name, char const *const pp_name, struct pabc_context const *const ctx, struct pabc_public_parameters const *const pp, struct pabc_user_context **usr_ctx)
void * libgnunet_plugin_rest_reclaim_done(void *cls)
Exit point from the plugin.
#define GNUNET_REST_API_NS_PABC
REST root namespace.
#define GNUNET_REST_API_NS_PABC_CR
Credential request endpoint.
static void do_timeout(void *cls)
Task run on timeout, sends error message.
static void return_response(void *cls)
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration handle.
static void cr_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
static struct RequestHandle * requests_head
DLL.
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Respond to OPTIONS request.
static enum GNUNET_GenericReturnValue setup_new_user_context(struct pabc_context *ctx, struct pabc_public_parameters *pp, struct pabc_user_context **usr_ctx)
static char * allow_methods
HTTP methods allows for this plugin.
void * libgnunet_plugin_rest_pabc_init(void *cls)
Entry point for the plugin.
static struct RequestHandle * requests_tail
DLL.
static enum pabc_status set_attributes_from_idtoken(const struct pabc_context *ctx, const struct pabc_public_parameters *pp, struct pabc_user_context *usr_ctx, const char *id_token)
static enum GNUNET_GenericReturnValue rest_identity_process_request(struct GNUNET_REST_RequestHandle *rest_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
static void do_error(void *cls)
Task run on error, sends error message.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
void * cls
Closure for all of the callbacks.
struct returned by the initialization function of the plugin
void * cls
The closure of the plugin.
const char * name
Plugin name.
const char * url
The url as string.
void(* proc)(struct GNUNET_REST_RequestHandle *handle, const char *url, void *cls)
Namespace to handle.
Entry in list of pending tasks.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
char * emsg
Error response message.
struct RequestHandle * prev
DLL.
int response_code
HTTP response code.
struct GNUNET_SCHEDULER_Task * timeout_task
ID of a task associated with the resolution process.
void * proc_cls
The closure of the result processor.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
struct RequestHandle * next
DLL.
json_t * resp_object
Response object.
struct GNUNET_REST_RequestHandle * rest_handle
Handle to rest request.
struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).