merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

merchant_api_post-private-otp-devices.c (8654B)


      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_post-private-otp-devices-new.c
     21  * @brief Implementation of the POST /private/otp-devices 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/taler-merchant/post-private-otp-devices.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 POST /private/otp-devices operation.
     39  */
     40 struct TALER_MERCHANT_PostPrivateOtpDevicesHandle
     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_PostPrivateOtpDevicesCallback cb;
     61 
     62   /**
     63    * Closure for @a cb.
     64    */
     65   TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_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).
     89    */
     90   char *otp_key;
     91 
     92   /**
     93    * OTP algorithm to use.
     94    */
     95   enum TALER_MerchantConfirmationAlgorithm otp_algorithm;
     96 
     97   /**
     98    * Initial 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 POST /private/otp-devices request.
    112  *
    113  * @param cls the `struct TALER_MERCHANT_PostPrivateOtpDevicesHandle`
    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 handle_post_otp_devices_finished (void *cls,
    119                                   long response_code,
    120                                   const void *response)
    121 {
    122   struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh = cls;
    123   const json_t *json = response;
    124   struct TALER_MERCHANT_PostPrivateOtpDevicesResponse odr = {
    125     .hr.http_status = (unsigned int) response_code,
    126     .hr.reply = json
    127   };
    128 
    129   ppoh->job = NULL;
    130   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    131               "POST /private/otp-devices completed with response code %u\n",
    132               (unsigned int) response_code);
    133   switch (response_code)
    134   {
    135   case 0:
    136     odr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    137     break;
    138   case MHD_HTTP_NO_CONTENT:
    139     break;
    140   case MHD_HTTP_BAD_REQUEST:
    141     odr.hr.ec = TALER_JSON_get_error_code (json);
    142     odr.hr.hint = TALER_JSON_get_error_hint (json);
    143     break;
    144   case MHD_HTTP_UNAUTHORIZED:
    145     odr.hr.ec = TALER_JSON_get_error_code (json);
    146     odr.hr.hint = TALER_JSON_get_error_hint (json);
    147     break;
    148   case MHD_HTTP_FORBIDDEN:
    149     odr.hr.ec = TALER_JSON_get_error_code (json);
    150     odr.hr.hint = TALER_JSON_get_error_hint (json);
    151     break;
    152   case MHD_HTTP_NOT_FOUND:
    153     odr.hr.ec = TALER_JSON_get_error_code (json);
    154     odr.hr.hint = TALER_JSON_get_error_hint (json);
    155     break;
    156   case MHD_HTTP_CONFLICT:
    157     odr.hr.ec = TALER_JSON_get_error_code (json);
    158     odr.hr.hint = TALER_JSON_get_error_hint (json);
    159     break;
    160   case MHD_HTTP_INTERNAL_SERVER_ERROR:
    161     odr.hr.ec = TALER_JSON_get_error_code (json);
    162     odr.hr.hint = TALER_JSON_get_error_hint (json);
    163     break;
    164   default:
    165     TALER_MERCHANT_parse_error_details_ (json,
    166                                          response_code,
    167                                          &odr.hr);
    168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    169                 "Unexpected response code %u/%d\n",
    170                 (unsigned int) response_code,
    171                 (int) odr.hr.ec);
    172     GNUNET_break_op (0);
    173     break;
    174   }
    175   ppoh->cb (ppoh->cb_cls,
    176             &odr);
    177   TALER_MERCHANT_post_private_otp_devices_cancel (ppoh);
    178 }
    179 
    180 
    181 struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *
    182 TALER_MERCHANT_post_private_otp_devices_create (
    183   struct GNUNET_CURL_Context *ctx,
    184   const char *url,
    185   const char *otp_device_id,
    186   const char *otp_device_description,
    187   const char *otp_key,
    188   enum TALER_MerchantConfirmationAlgorithm otp_algorithm)
    189 {
    190   struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh;
    191 
    192   ppoh = GNUNET_new (struct TALER_MERCHANT_PostPrivateOtpDevicesHandle);
    193   ppoh->ctx = ctx;
    194   ppoh->base_url = GNUNET_strdup (url);
    195   ppoh->otp_device_id = GNUNET_strdup (otp_device_id);
    196   ppoh->otp_device_description = GNUNET_strdup (otp_device_description);
    197   if (NULL != otp_key)
    198     ppoh->otp_key = GNUNET_strdup (otp_key);
    199   ppoh->otp_algorithm = otp_algorithm;
    200   return ppoh;
    201 }
    202 
    203 
    204 enum GNUNET_GenericReturnValue
    205 TALER_MERCHANT_post_private_otp_devices_set_options_ (
    206   struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh,
    207   unsigned int num_options,
    208   const struct TALER_MERCHANT_PostPrivateOtpDevicesOptionValue *options)
    209 {
    210   for (unsigned int i = 0; i < num_options; i++)
    211   {
    212     switch (options[i].option)
    213     {
    214     case TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_OPTION_END:
    215       return GNUNET_OK;
    216     case TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_OPTION_OTP_CTR:
    217       ppoh->otp_ctr = options[i].details.otp_ctr;
    218       ppoh->have_otp_ctr = true;
    219       break;
    220     default:
    221       GNUNET_break (0);
    222       return GNUNET_SYSERR;
    223     }
    224   }
    225   return GNUNET_OK;
    226 }
    227 
    228 
    229 enum TALER_ErrorCode
    230 TALER_MERCHANT_post_private_otp_devices_start (
    231   struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh,
    232   TALER_MERCHANT_PostPrivateOtpDevicesCallback cb,
    233   TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_RESULT_CLOSURE *cb_cls)
    234 {
    235   json_t *req_obj;
    236   CURL *eh;
    237 
    238   ppoh->cb = cb;
    239   ppoh->cb_cls = cb_cls;
    240   ppoh->url = TALER_url_join (ppoh->base_url,
    241                               "private/otp-devices",
    242                               NULL);
    243   if (NULL == ppoh->url)
    244     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    245   req_obj = GNUNET_JSON_PACK (
    246     GNUNET_JSON_pack_string ("otp_device_id",
    247                              ppoh->otp_device_id),
    248     GNUNET_JSON_pack_string ("otp_device_description",
    249                              ppoh->otp_device_description),
    250     GNUNET_JSON_pack_uint64 ("otp_algorithm",
    251                              (uint32_t) ppoh->otp_algorithm),
    252     GNUNET_JSON_pack_allow_null (
    253       GNUNET_JSON_pack_string ("otp_key",
    254                                ppoh->otp_key)),
    255     GNUNET_JSON_pack_uint64 ("otp_ctr",
    256                              ppoh->otp_ctr));
    257   eh = TALER_MERCHANT_curl_easy_get_ (ppoh->url);
    258   if ( (NULL == eh) ||
    259        (GNUNET_OK !=
    260         TALER_curl_easy_post (&ppoh->post_ctx,
    261                               eh,
    262                               req_obj)) )
    263   {
    264     GNUNET_break (0);
    265     json_decref (req_obj);
    266     if (NULL != eh)
    267       curl_easy_cleanup (eh);
    268     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    269   }
    270   json_decref (req_obj);
    271   ppoh->job = GNUNET_CURL_job_add2 (ppoh->ctx,
    272                                     eh,
    273                                     ppoh->post_ctx.headers,
    274                                     &handle_post_otp_devices_finished,
    275                                     ppoh);
    276   if (NULL == ppoh->job)
    277     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    278   return TALER_EC_NONE;
    279 }
    280 
    281 
    282 void
    283 TALER_MERCHANT_post_private_otp_devices_cancel (
    284   struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh)
    285 {
    286   if (NULL != ppoh->job)
    287   {
    288     GNUNET_CURL_job_cancel (ppoh->job);
    289     ppoh->job = NULL;
    290   }
    291   TALER_curl_easy_post_finished (&ppoh->post_ctx);
    292   GNUNET_free (ppoh->otp_device_id);
    293   GNUNET_free (ppoh->otp_device_description);
    294   GNUNET_free (ppoh->otp_key);
    295   GNUNET_free (ppoh->url);
    296   GNUNET_free (ppoh->base_url);
    297   GNUNET_free (ppoh);
    298 }
    299 
    300 
    301 /* end of merchant_api_post-private-otp-devices-new.c */