LCOV - code coverage report
Current view: top level - lib - merchant_api_patch-private-otp-devices-DEVICE_ID.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 104 0
Test Date: 2026-04-12 12:58:13 Functions: 0.0 % 5 0

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2022-2026 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify
       6              :   it under the terms of the GNU Lesser General Public License as
       7              :   published by the Free Software Foundation; either version 2.1,
       8              :   or (at your option) any later version.
       9              : 
      10              :   TALER is distributed in the hope that it will be useful,
      11              :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13              :   GNU Lesser General Public License for more details.
      14              : 
      15              :   You should have received a copy of the GNU Lesser General
      16              :   Public License along with TALER; see the file COPYING.LGPL.
      17              :   If not, see <http://www.gnu.org/licenses/>
      18              : */
      19              : /**
      20              :  * @file merchant_api_patch-private-otp-devices-DEVICE_ID-new.c
      21              :  * @brief Implementation of the PATCH /private/otp-devices/$DEVICE_ID request
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "taler/platform.h"
      25              : #include <curl/curl.h>
      26              : #include <jansson.h>
      27              : #include <microhttpd.h> /* just for HTTP status codes */
      28              : #include <gnunet/gnunet_util_lib.h>
      29              : #include <gnunet/gnunet_curl_lib.h>
      30              : #include <taler/merchant/patch-private-otp-devices-DEVICE_ID.h>
      31              : #include "merchant_api_curl_defaults.h"
      32              : #include "merchant_api_common.h"
      33              : #include <taler/taler_json_lib.h>
      34              : #include <taler/taler_curl_lib.h>
      35              : 
      36              : 
      37              : /**
      38              :  * Handle for a PATCH /private/otp-devices/$DEVICE_ID operation.
      39              :  */
      40              : struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle
      41              : {
      42              :   /**
      43              :    * Base URL of the merchant backend.
      44              :    */
      45              :   char *base_url;
      46              : 
      47              :   /**
      48              :    * The full URL for this request.
      49              :    */
      50              :   char *url;
      51              : 
      52              :   /**
      53              :    * Handle for the request.
      54              :    */
      55              :   struct GNUNET_CURL_Job *job;
      56              : 
      57              :   /**
      58              :    * Function to call with the result.
      59              :    */
      60              :   TALER_MERCHANT_PatchPrivateOtpDeviceCallback cb;
      61              : 
      62              :   /**
      63              :    * Closure for @a cb.
      64              :    */
      65              :   TALER_MERCHANT_PATCH_PRIVATE_OTP_DEVICE_RESULT_CLOSURE *cb_cls;
      66              : 
      67              :   /**
      68              :    * Reference to the execution context.
      69              :    */
      70              :   struct GNUNET_CURL_Context *ctx;
      71              : 
      72              :   /**
      73              :    * Minor context that holds body and headers.
      74              :    */
      75              :   struct TALER_CURL_PostContext post_ctx;
      76              : 
      77              :   /**
      78              :    * OTP device identifier.
      79              :    */
      80              :   char *otp_device_id;
      81              : 
      82              :   /**
      83              :    * Human-readable description.
      84              :    */
      85              :   char *otp_device_description;
      86              : 
      87              :   /**
      88              :    * Base32-encoded OTP secret key, or NULL to keep unchanged.
      89              :    */
      90              :   char *otp_key;
      91              : 
      92              :   /**
      93              :    * OTP algorithm.
      94              :    */
      95              :   enum TALER_MerchantConfirmationAlgorithm mca;
      96              : 
      97              :   /**
      98              :    * Counter value (for HOTP).
      99              :    */
     100              :   uint64_t otp_ctr;
     101              : 
     102              :   /**
     103              :    * True if @e otp_ctr was explicitly set via options.
     104              :    */
     105              :   bool have_otp_ctr;
     106              : };
     107              : 
     108              : 
     109              : /**
     110              :  * Function called when we're done processing the
     111              :  * HTTP PATCH /private/otp-devices/$DEVICE_ID request.
     112              :  *
     113              :  * @param cls the `struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle`
     114              :  * @param response_code HTTP response code, 0 on error
     115              :  * @param response response body, NULL if not in JSON
     116              :  */
     117              : static void
     118            0 : handle_patch_otp_device_finished (void *cls,
     119              :                                   long response_code,
     120              :                                   const void *response)
     121              : {
     122            0 :   struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *odh = cls;
     123            0 :   const json_t *json = response;
     124            0 :   struct TALER_MERCHANT_PatchPrivateOtpDeviceResponse odr = {
     125            0 :     .hr.http_status = (unsigned int) response_code,
     126              :     .hr.reply = json
     127              :   };
     128              : 
     129            0 :   odh->job = NULL;
     130            0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     131              :               "PATCH /private/otp-devices/$DEVICE_ID completed with response code %u\n",
     132              :               (unsigned int) response_code);
     133            0 :   switch (response_code)
     134              :   {
     135            0 :   case 0:
     136            0 :     odr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     137            0 :     break;
     138            0 :   case MHD_HTTP_NO_CONTENT:
     139            0 :     break;
     140            0 :   case MHD_HTTP_BAD_REQUEST:
     141            0 :     odr.hr.ec = TALER_JSON_get_error_code (json);
     142            0 :     odr.hr.hint = TALER_JSON_get_error_hint (json);
     143            0 :     GNUNET_break_op (0);
     144            0 :     break;
     145            0 :   case MHD_HTTP_UNAUTHORIZED:
     146            0 :     odr.hr.ec = TALER_JSON_get_error_code (json);
     147            0 :     odr.hr.hint = TALER_JSON_get_error_hint (json);
     148            0 :     break;
     149            0 :   case MHD_HTTP_FORBIDDEN:
     150            0 :     odr.hr.ec = TALER_JSON_get_error_code (json);
     151            0 :     odr.hr.hint = TALER_JSON_get_error_hint (json);
     152            0 :     break;
     153            0 :   case MHD_HTTP_NOT_FOUND:
     154            0 :     odr.hr.ec = TALER_JSON_get_error_code (json);
     155            0 :     odr.hr.hint = TALER_JSON_get_error_hint (json);
     156            0 :     break;
     157            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     158            0 :     odr.hr.ec = TALER_JSON_get_error_code (json);
     159            0 :     odr.hr.hint = TALER_JSON_get_error_hint (json);
     160            0 :     break;
     161            0 :   default:
     162            0 :     TALER_MERCHANT_parse_error_details_ (json,
     163              :                                          response_code,
     164              :                                          &odr.hr);
     165            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     166              :                 "Unexpected response code %u/%d\n",
     167              :                 (unsigned int) response_code,
     168              :                 (int) odr.hr.ec);
     169            0 :     GNUNET_break_op (0);
     170            0 :     break;
     171              :   }
     172            0 :   odh->cb (odh->cb_cls,
     173              :            &odr);
     174            0 :   TALER_MERCHANT_patch_private_otp_device_cancel (odh);
     175            0 : }
     176              : 
     177              : 
     178              : struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *
     179            0 : TALER_MERCHANT_patch_private_otp_device_create (
     180              :   struct GNUNET_CURL_Context *ctx,
     181              :   const char *url,
     182              :   const char *otp_device_id,
     183              :   const char *otp_device_description,
     184              :   const char *otp_key,
     185              :   enum TALER_MerchantConfirmationAlgorithm mca)
     186              : {
     187              :   struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *odh;
     188              : 
     189            0 :   odh = GNUNET_new (struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle);
     190            0 :   odh->ctx = ctx;
     191            0 :   odh->base_url = GNUNET_strdup (url);
     192            0 :   odh->otp_device_id = GNUNET_strdup (otp_device_id);
     193            0 :   odh->otp_device_description = GNUNET_strdup (otp_device_description);
     194            0 :   if (NULL != otp_key)
     195            0 :     odh->otp_key = GNUNET_strdup (otp_key);
     196            0 :   odh->mca = mca;
     197            0 :   return odh;
     198              : }
     199              : 
     200              : 
     201              : enum GNUNET_GenericReturnValue
     202            0 : TALER_MERCHANT_patch_private_otp_device_set_options_ (
     203              :   struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *odh,
     204              :   unsigned int num_options,
     205              :   const struct TALER_MERCHANT_PatchPrivateOtpDeviceOptionValue *options)
     206              : {
     207            0 :   for (unsigned int i = 0; i < num_options; i++)
     208              :   {
     209            0 :     switch (options[i].option)
     210              :     {
     211            0 :     case TALER_MERCHANT_PATCH_PRIVATE_OTP_DEVICE_OPTION_END:
     212            0 :       return GNUNET_OK;
     213            0 :     case TALER_MERCHANT_PATCH_PRIVATE_OTP_DEVICE_OPTION_OTP_CTR:
     214            0 :       odh->otp_ctr = options[i].details.otp_ctr;
     215            0 :       odh->have_otp_ctr = true;
     216            0 :       break;
     217            0 :     default:
     218            0 :       GNUNET_break (0);
     219            0 :       return GNUNET_SYSERR;
     220              :     }
     221              :   }
     222            0 :   return GNUNET_OK;
     223              : }
     224              : 
     225              : 
     226              : enum TALER_ErrorCode
     227            0 : TALER_MERCHANT_patch_private_otp_device_start (
     228              :   struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *odh,
     229              :   TALER_MERCHANT_PatchPrivateOtpDeviceCallback cb,
     230              :   TALER_MERCHANT_PATCH_PRIVATE_OTP_DEVICE_RESULT_CLOSURE *cb_cls)
     231              : {
     232              :   json_t *req_obj;
     233              :   CURL *eh;
     234              : 
     235            0 :   odh->cb = cb;
     236            0 :   odh->cb_cls = cb_cls;
     237              :   {
     238              :     char *path;
     239              : 
     240            0 :     GNUNET_asprintf (&path,
     241              :                      "private/otp-devices/%s",
     242              :                      odh->otp_device_id);
     243            0 :     odh->url = TALER_url_join (odh->base_url,
     244              :                                path,
     245              :                                NULL);
     246            0 :     GNUNET_free (path);
     247              :   }
     248            0 :   if (NULL == odh->url)
     249            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     250            0 :   req_obj = GNUNET_JSON_PACK (
     251              :     GNUNET_JSON_pack_string ("otp_device_description",
     252              :                              odh->otp_device_description),
     253              :     GNUNET_JSON_pack_uint64 ("otp_algorithm",
     254              :                              (uint32_t) odh->mca),
     255              :     GNUNET_JSON_pack_allow_null (
     256              :       GNUNET_JSON_pack_string ("otp_key",
     257              :                                odh->otp_key)));
     258            0 :   if (odh->have_otp_ctr)
     259              :   {
     260            0 :     json_object_set_new (req_obj,
     261              :                          "otp_ctr",
     262            0 :                          json_integer ((json_int_t) odh->otp_ctr));
     263              :   }
     264            0 :   eh = TALER_MERCHANT_curl_easy_get_ (odh->url);
     265            0 :   if ( (NULL == eh) ||
     266              :        (GNUNET_OK !=
     267            0 :         TALER_curl_easy_post (&odh->post_ctx,
     268              :                               eh,
     269              :                               req_obj)) )
     270              :   {
     271            0 :     GNUNET_break (0);
     272            0 :     json_decref (req_obj);
     273            0 :     if (NULL != eh)
     274            0 :       curl_easy_cleanup (eh);
     275            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     276              :   }
     277            0 :   json_decref (req_obj);
     278            0 :   GNUNET_assert (CURLE_OK ==
     279              :                  curl_easy_setopt (eh,
     280              :                                    CURLOPT_CUSTOMREQUEST,
     281              :                                    MHD_HTTP_METHOD_PATCH));
     282            0 :   odh->job = GNUNET_CURL_job_add2 (odh->ctx,
     283              :                                    eh,
     284            0 :                                    odh->post_ctx.headers,
     285              :                                    &handle_patch_otp_device_finished,
     286              :                                    odh);
     287            0 :   if (NULL == odh->job)
     288            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     289            0 :   return TALER_EC_NONE;
     290              : }
     291              : 
     292              : 
     293              : void
     294            0 : TALER_MERCHANT_patch_private_otp_device_cancel (
     295              :   struct TALER_MERCHANT_PatchPrivateOtpDeviceHandle *odh)
     296              : {
     297            0 :   if (NULL != odh->job)
     298              :   {
     299            0 :     GNUNET_CURL_job_cancel (odh->job);
     300            0 :     odh->job = NULL;
     301              :   }
     302            0 :   TALER_curl_easy_post_finished (&odh->post_ctx);
     303            0 :   GNUNET_free (odh->otp_device_id);
     304            0 :   GNUNET_free (odh->otp_device_description);
     305            0 :   GNUNET_free (odh->otp_key);
     306            0 :   GNUNET_free (odh->url);
     307            0 :   GNUNET_free (odh->base_url);
     308            0 :   GNUNET_free (odh);
     309            0 : }
     310              : 
     311              : 
     312              : /* end of merchant_api_patch-private-otp-devices-DEVICE_ID-new.c */
        

Generated by: LCOV version 2.0-1