JWT Parsing API reclaim_attestation
authorMarkus Voggenreiter <Markus.Voggenreiter@tum.de>
Sat, 7 Dec 2019 17:37:43 +0000 (18:37 +0100)
committerMarkus Voggenreiter <Markus.Voggenreiter@tum.de>
Sat, 7 Dec 2019 17:37:43 +0000 (18:37 +0100)
src/reclaim/json_reclaim.c
src/reclaim/plugin_rest_reclaim.c

index 775ab58d62a1ccc78948754903e88721f9a3be71..a464a9088c37ae49afb7d91931e8850d76409869 100644 (file)
@@ -322,7 +322,7 @@ parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
                                                    (void **) &data,
                                                    &data_size)))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attestation value invalid!\n");
     return GNUNET_SYSERR;
   }
   attr = GNUNET_RECLAIM_ATTESTATION_claim_new (name_str, type, data, data_size);
index a495c5e8524a6eca9c02a362db63d1da13db996a..dcda75b657ece93323bb4d36a6a517219364e16d 100644 (file)
@@ -534,6 +534,69 @@ add_attestation_ref_cont (struct GNUNET_REST_RequestHandle *con_handle,
   GNUNET_JSON_parse_free (attrspec);
 }
 
+static void
+parse_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle,
+                        const char *url,
+                        void *cls)
+{
+  struct RequestHandle *handle = cls;
+
+  char term_data[handle->rest_handle->data_size + 1];
+  json_t *data_json;
+  json_error_t err;
+  int unpack_state;
+  struct MHD_Response *resp;
+  char *val_str = NULL;
+  const char *type_str = NULL;
+  term_data[handle->rest_handle->data_size] = '\0';
+  GNUNET_memcpy (term_data,
+                 handle->rest_handle->data,
+                 handle->rest_handle->data_size);
+  data_json = json_loads (term_data, JSON_DECODE_ANY, &err);
+  GNUNET_assert (NULL != data_json);
+  if (! json_is_object (data_json))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Error json is not array nor object!\n");
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  unpack_state = json_unpack (data_json,
+                              "{s:s, s:s!}",
+                              "value",
+                              &val_str,
+                              "type",
+                              &type_str);
+  if ((0 != unpack_state) || (NULL == val_str) || (NULL == type_str))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Error json object has a wrong format!\n");
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  if (0 == strcmp (type_str, "JWT"))
+  {
+  // The value is a JWT
+  char *decoded_jwt;
+  char delim[] = ".";
+  char *jwt_body = strtok (val_str, delim);
+  jwt_body = strtok (NULL, delim);
+  GNUNET_STRINGS_base64_decode (jwt_body, strlen (jwt_body),
+                                (void **) &decoded_jwt);
+  resp = GNUNET_REST_create_response (decoded_jwt);
+  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
+  GNUNET_free (decoded_jwt);
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Error requested parsing type not supported!\n");
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  cleanup_handle (handle);
+  json_decref (data_json);
+}
 
 static void
 add_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle,
@@ -554,6 +617,19 @@ add_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle,
       return;
     }
   }
+  /* Check for substring "parse" */
+  if (strlen (GNUNET_REST_API_NS_RECLAIM_ATTESTATION_REFERENCE) < strlen (
+      handle->url))
+      {
+        if ( strncmp ("parse", (handle->url + strlen (
+                          GNUNET_REST_API_NS_RECLAIM_ATTESTATION_REFERENCE)
+                        + 1), strlen (
+            "parse")) == 0)
+        {
+          parse_attestation_cont (con_handle,url,cls);
+          return;
+        }
+      }
   const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity_priv;
   const char *identity;
   struct EgoEntry *ego_entry;